diff --git a/404.html b/404.html index 7c6ec7dd1..aeec86f73 100644 --- a/404.html +++ b/404.html @@ -134,4 +134,4 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/404/index.html b/404/index.html index 0ada43f72..14e55d7cb 100644 --- a/404/index.html +++ b/404/index.html @@ -134,4 +134,4 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/all-exercises/index.html b/all-exercises/index.html index 4e9fbf516..08ff5496b 100644 --- a/all-exercises/index.html +++ b/all-exercises/index.html @@ -136,4 +136,4 @@ } } }) -
\ No newline at end of file +
\ No newline at end of file diff --git a/credits/index.html b/credits/index.html index e70d3aa5e..612f2a4f1 100644 --- a/credits/index.html +++ b/credits/index.html @@ -134,4 +134,4 @@ } } }) -

Kiitokset ja materiaalista

Kurssin on tehnyt Helsingin yliopiston Agile Education Research -tutkimusryhmä.

Kurssimateriaali

Kurssimateriaalin ja tehtävien tekijät ovat Erkki Kaila, Antti Laaksonen ja Matti Luukkainen. Muutama kurssin tehtävistä on Arto Hellaksen (né Vihavainen) käsialaa.

Kurssin materiaali on lisensoitu Creative Commons BY-NC-SA 4.0 -lisenssillä, joten voit käyttää ja levittää sitä vapaasti, kunhan alkuperäisten tekijöiden nimiä ei poisteta. Jos teet muutoksia materiaaliin ja haluat levittää muunneltua versiota, se täytyy lisensoida samalla lisenssillä. Materiaalien käyttö kaupalliseen tarkoitukseen on ilman erillistä lupaa kielletty.

Kurssilla käytössä oleva teknologia

Kurssisivuston ovat tehneet Henrik Nygren ja Antti Leinonen. Helsingin yliopiston Agile Education Research -tutkimusryhmä on luonut kurssilla käytetyn ohjelmointitehtävien palautusympäristön (Test My Code) ja sen liitännäiset ohjelmointiympäristöihin, kurssimateriaalissa olevan kyselyjärjestelmän ja muut toiminnot.

\ No newline at end of file +

Kiitokset ja materiaalista

Kurssin on tehnyt Helsingin yliopiston Agile Education Research -tutkimusryhmä.

Kurssimateriaali

Kurssimateriaalin ja tehtävien tekijät ovat Erkki Kaila, Antti Laaksonen ja Matti Luukkainen. Muutama kurssin tehtävistä on Arto Hellaksen (né Vihavainen) käsialaa.

Kurssin materiaali on lisensoitu Creative Commons BY-NC-SA 4.0 -lisenssillä, joten voit käyttää ja levittää sitä vapaasti, kunhan alkuperäisten tekijöiden nimiä ei poisteta. Jos teet muutoksia materiaaliin ja haluat levittää muunneltua versiota, se täytyy lisensoida samalla lisenssillä. Materiaalien käyttö kaupalliseen tarkoitukseen on ilman erillistä lupaa kielletty.

Kurssilla käytössä oleva teknologia

Kurssisivuston ovat tehneet Henrik Nygren ja Antti Leinonen. Helsingin yliopiston Agile Education Research -tutkimusryhmä on luonut kurssilla käytetyn ohjelmointitehtävien palautusympäristön (Test My Code) ja sen liitännäiset ohjelmointiympäristöihin, kurssimateriaalissa olevan kyselyjärjestelmän ja muut toiminnot.

\ No newline at end of file diff --git a/error_messages/index.html b/error_messages/index.html index dd1767bc4..d02e69d75 100644 --- a/error_messages/index.html +++ b/error_messages/index.html @@ -169,4 +169,4 @@

print(my_age//2) # error: the variable my_age has not been converted into an integer

TypeError: cannot concatenate 'str' and 'int' objects on line [line number]

-

See above.

\ No newline at end of file +

See above.

\ No newline at end of file diff --git a/exam-adv/index.html b/exam-adv/index.html index a35cae153..d3634aa36 100644 --- a/exam-adv/index.html +++ b/exam-adv/index.html @@ -189,4 +189,4 @@
  • Your programming task submissions will be checked with a plagiarism detector. Any cheating detected leads to a failed exam at minimum, but may also lead to expulsion from the course.
  • The programming tasks in the exam will be similar to the programming tasks in the course material. The best way to prepare for the exam is to revisit the course material.
  • Your course results will be emailed to you after the exam, along with instructions for registering your course credits.
  • -
    \ No newline at end of file +
    \ No newline at end of file diff --git a/exam-intro/index.html b/exam-intro/index.html index 6734cc9bd..a52a04a25 100644 --- a/exam-intro/index.html +++ b/exam-intro/index.html @@ -191,4 +191,4 @@
  • Your programming task submissions will be checked with a plagiarism detector. Any cheating detected leads to a failed exam at minimum, but may also lead to expulsion from the course.
  • The programming tasks in the exam will be similar to the programming tasks in the course material. The best way to prepare for the exam is to revisit the course material.
  • Your course results will be emailed to you after the exam, along with instructions for registering your course credits.
  • -
    \ No newline at end of file +
    \ No newline at end of file diff --git a/exam/index.html b/exam/index.html index 989ce1c60..4872311f3 100644 --- a/exam/index.html +++ b/exam/index.html @@ -189,4 +189,4 @@
  • Your programming task submissions will be checked with a plagiarism detector. Any cheating detected leads to a failed exam at minimum, but may also lead to expulsion from the course.
  • The programming tasks in the exam will be similar to the programming tasks in the course material. The best way to prepare for the exam is to revisit the course material.
  • Your course results will be emailed to you after the exam, along with instructions for registering your course credits.
  • -
    \ No newline at end of file +
    \ No newline at end of file diff --git a/faq/index.html b/faq/index.html index 28810d26d..ba76f9886 100644 --- a/faq/index.html +++ b/faq/index.html @@ -170,4 +170,4 @@

    I'm currently receiving unemployment benefits in Finland. Can I still take this course?

    -

    Please take this up with your employment services office (TE-toimisto). Unfortunately, not all employment services offices treat this course in the same way. Some offices have withdrawn benefits for simply participating in the course, even if the participant is not aiming for a right to study or official study credits. On the other hand, some offices have even recommended this course without withdrawing benefits.

    \ No newline at end of file +

    Please take this up with your employment services office (TE-toimisto). Unfortunately, not all employment services offices treat this course in the same way. Some offices have withdrawn benefits for simply participating in the course, even if the participant is not aiming for a right to study or official study credits. On the other hand, some offices have even recommended this course without withdrawing benefits.

    \ No newline at end of file diff --git a/grading-and-exams/index.html b/grading-and-exams/index.html index 2fd370fb7..85a12257c 100644 --- a/grading-and-exams/index.html +++ b/grading-and-exams/index.html @@ -184,4 +184,4 @@ Fail -

    The final grade is calculated based on the number of exercise points received during the course (50 % of final grade) and points received in the exam (50 % of final grade). Your exercise points percentage equals ([all received exercise points] / [number of exercise points available on course]) * 100. We are working on including this percentage in the visualisation in the bottom right corner of each page.

    For example, if your total points percentage for the exercises for the whole course is 42 %, and you receive 79 % of the points in the exam, your final points percentage will be (42 % + 79 %) / 2 = 60.5 %, which equals grade 2 (Satisfactory).

    To pass the course you must have received at least 25% of the exercise points for each part on the course, regardless of the above calculation.

    Exam points cutoff

    To pass the course you must receive at least 50% of the total points available in the exam.

    \ No newline at end of file +

    The final grade is calculated based on the number of exercise points received during the course (50 % of final grade) and points received in the exam (50 % of final grade). Your exercise points percentage equals ([all received exercise points] / [number of exercise points available on course]) * 100. We are working on including this percentage in the visualisation in the bottom right corner of each page.

    For example, if your total points percentage for the exercises for the whole course is 42 %, and you receive 79 % of the points in the exam, your final points percentage will be (42 % + 79 %) / 2 = 60.5 %, which equals grade 2 (Satisfactory).

    To pass the course you must have received at least 25% of the exercise points for each part on the course, regardless of the above calculation.

    Exam points cutoff

    To pass the course you must receive at least 50% of the total points available in the exam.

    \ No newline at end of file diff --git a/index.html b/index.html index 98edc6361..9f7d9228a 100644 --- a/index.html +++ b/index.html @@ -146,7 +146,7 @@ } })

    About this course

    The course starts on January 15th 2024.

    This is the course material page for the Introduction to Programming course (BSCS1001, 5 ECTS) and the Advanced Course in Programming (BSCS1002, 5 ECTS) from the Department of Computer Science at the University of Helsinki.

    To pass either of the two courses you are expected to complete programming exercises and take part in an exam. Please see the Grading and exams page for more details.

    The Introduction to Programming course consists of parts 1 through 7 in the material, and the Advanced Course in Programming consists of parts 8 through 14.

    NB: It is no longer possible to directly apply for a right to study at the University of Helsinki through these courses. If you are interested in studying at the University of Helsinki, please have a look at the admissions website.

    Support channels on the course

    The official course support channels are available through workshop guidance on campus and Discord. The course channels in Discord are available through this link.

    Please see the Support and assistance page for more details.

    Use Discord primarily for communication. You can be in contact with the course teachers by e-mail ohjelmoinnin-mooc@helsinki.fi.

    Timetable

    The course will be open until the end of the year 2024.

    Lectures

    Live lectures are organized on Thursdays, starting on September 5th. -You can follow the lecture live via Zoom starting at 14.15.

    The lecture recordings are updated in the table below after the lecture. Until then, you can find last year's corresponding lecture in the table.

    Please note, that exam dates and similar information in the older recordings may not concern this year's course.

    +You can follow the lecture live via Zoom starting at 10.15.

    The lecture recordings are updated in the table below after the lecture. Until then, you can find last year's corresponding lecture in the table.

    Please note, that exam dates and similar information in the older recordings may not concern this year's course.

    @@ -215,4 +215,4 @@ -
    PartSlides
    \ No newline at end of file +
    \ No newline at end of file diff --git a/page-data/all-exercises/page-data.json b/page-data/all-exercises/page-data.json index 5e369da4a..ae19e3665 100644 --- a/page-data/all-exercises/page-data.json +++ b/page-data/all-exercises/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-info-template-js", "path": "/all-exercises", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-all-sections","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    ","frontmatter":{"path":"/all-exercises","title":"All exercises"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/all-exercises.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08.","overview":null,"hidden":null},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6.","overview":null,"hidden":null},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09.","overview":null,"hidden":null},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide","overview":true,"hidden":true},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary","overview":null,"hidden":true},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started","overview":null,"hidden":false},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user","overview":null,"hidden":false},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables","overview":null,"hidden":false},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations","overview":null,"hidden":false},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements","overview":null,"hidden":false},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies","overview":null,"hidden":false},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers","overview":null,"hidden":false},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques","overview":null,"hidden":false},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application","overview":null,"hidden":false},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions","overview":null,"hidden":false},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion","overview":null,"hidden":false},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions","overview":null,"hidden":false},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples","overview":null,"hidden":false},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators","overview":null,"hidden":false},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments","overview":null,"hidden":false},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming","overview":null,"hidden":false},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions","overview":null,"hidden":false},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame","overview":null,"hidden":false},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation","overview":null,"hidden":false},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events","overview":null,"hidden":false},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques","overview":null,"hidden":false},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game","overview":null,"hidden":false},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology","overview":null,"hidden":null},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals","overview":null,"hidden":false},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions","overview":null,"hidden":false},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops","overview":null,"hidden":false},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings","overview":null,"hidden":false},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops","overview":null,"hidden":false},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions","overview":null,"hidden":false},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions","overview":null,"hidden":false},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool","overview":null,"hidden":false},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions","overview":null,"hidden":false},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists","overview":null,"hidden":false},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration","overview":null,"hidden":false},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting","overview":null,"hidden":false},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists","overview":null,"hidden":false},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References","overview":null,"hidden":false},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists","overview":null,"hidden":false},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary","overview":null,"hidden":false},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple","overview":null,"hidden":false},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files","overview":null,"hidden":false},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files","overview":null,"hidden":false},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors","overview":null,"hidden":false},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables","overview":null,"hidden":false},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules","overview":null,"hidden":false},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness","overview":null,"hidden":false},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates","overview":null,"hidden":false},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing","overview":null,"hidden":false},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules","overview":null,"hidden":false},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features","overview":null,"hidden":false},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods","overview":null,"hidden":false},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects","overview":null,"hidden":false},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes","overview":null,"hidden":false},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods","overview":null,"hidden":false},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes","overview":null,"hidden":false},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation","overview":null,"hidden":false},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes","overview":null,"hidden":false},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods","overview":null,"hidden":false},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes","overview":null,"hidden":false},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes","overview":null,"hidden":false},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references","overview":null,"hidden":false},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-all-sections","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    ","frontmatter":{"path":"/all-exercises","title":"All exercises"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/all-exercises.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08.","overview":null,"hidden":null},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6.","overview":null,"hidden":null},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09.","overview":null,"hidden":null},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide","overview":true,"hidden":true},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary","overview":null,"hidden":true},"moocfiExercises":[]}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started","overview":null,"hidden":false},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables","overview":null,"hidden":false},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations","overview":null,"hidden":false},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements","overview":null,"hidden":false},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies","overview":null,"hidden":false},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user","overview":null,"hidden":false},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers","overview":null,"hidden":false},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques","overview":null,"hidden":false},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions","overview":null,"hidden":false},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions","overview":null,"hidden":false},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples","overview":null,"hidden":false},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments","overview":null,"hidden":false},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators","overview":null,"hidden":false},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions","overview":null,"hidden":false},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming","overview":null,"hidden":false},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame","overview":null,"hidden":false},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion","overview":null,"hidden":false},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation","overview":null,"hidden":false},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events","overview":null,"hidden":false},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques","overview":null,"hidden":false},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game","overview":null,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game","overview":null,"hidden":false},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology","overview":null,"hidden":null},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals","overview":null,"hidden":false},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions","overview":null,"hidden":false},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops","overview":null,"hidden":false},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings","overview":null,"hidden":false},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions","overview":null,"hidden":false},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops","overview":null,"hidden":false},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions","overview":null,"hidden":false},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool","overview":null,"hidden":false},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration","overview":null,"hidden":false},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions","overview":null,"hidden":false},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists","overview":null,"hidden":false},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting","overview":null,"hidden":false},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists","overview":null,"hidden":false},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists","overview":null,"hidden":false},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References","overview":null,"hidden":false},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary","overview":null,"hidden":false},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple","overview":null,"hidden":false},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files","overview":null,"hidden":false},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files","overview":null,"hidden":false},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables","overview":null,"hidden":false},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors","overview":null,"hidden":false},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules","overview":null,"hidden":false},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness","overview":null,"hidden":false},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates","overview":null,"hidden":false},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing","overview":null,"hidden":false},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules","overview":null,"hidden":false},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features","overview":null,"hidden":false},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods","overview":null,"hidden":false},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects","overview":null,"hidden":false},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes","overview":null,"hidden":false},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods","overview":null,"hidden":false},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes","overview":null,"hidden":false},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes","overview":null,"hidden":false},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references","overview":null,"hidden":false},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods","overview":null,"hidden":false},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation","overview":null,"hidden":false},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes","overview":null,"hidden":false},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes","overview":null,"hidden":false},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9","overview":true,"hidden":false},"moocfiExercises":[]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application","overview":null,"hidden":false},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/index/page-data.json b/page-data/index/page-data.json index 4f46a98e9..aeeac9a6f 100644 --- a/page-data/index/page-data.json +++ b/page-data/index/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-info-page-template-js", "path": "/", - "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"The course starts on January 15th 2024."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is the course material page for the Introduction to Programming course (BSCS1001, 5 ECTS) and the Advanced Course in Programming (BSCS1002, 5 ECTS) from the Department of Computer Science at the University of Helsinki."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To pass either of the two courses you are expected to complete programming exercises and take part in an exam. Please see the "},{"type":"element","tagName":"a","properties":{"href":"/grading-and-exams"},"children":[{"type":"text","value":"Grading and exams"}]},{"type":"text","value":" page for more details."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The Introduction to Programming course consists of parts 1 through 7 in the material, and the Advanced Course in Programming consists of parts 8 through 14."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" It is "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"no longer possible"}]},{"type":"text","value":" to directly apply for a right to study at the University of Helsinki through these courses. If you are interested in studying at the University of Helsinki, please have a look at the "},{"type":"element","tagName":"a","properties":{"href":"https://www.helsinki.fi/en/admissions-and-education","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"admissions website"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"support-channels-on-the-course","style":"position:relative;"},"children":[{"type":"text","value":"Support channels on the course"},{"type":"element","tagName":"a","properties":{"href":"#support-channels-on-the-course","ariaLabel":"support channels on the course 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 official course support channels are available through workshop guidance on campus and Discord. The course channels in Discord are available through "},{"type":"element","tagName":"a","properties":{"href":"https://study.cs.helsinki.fi/discord/join/ohjelmoinnin_mooc","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"this link"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please see the "},{"type":"element","tagName":"a","properties":{"href":"/support-and-assistance"},"children":[{"type":"text","value":"Support and assistance"}]},{"type":"text","value":" page for more details."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Use Discord primarily for communication. You can be in contact with the course teachers by e-mail "},{"type":"element","tagName":"a","properties":{"href":"mailto:ohjelmoinnin-mooc@helsinki.fi","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"ohjelmoinnin-mooc@helsinki.fi"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"timetable","style":"position:relative;"},"children":[{"type":"text","value":"Timetable"},{"type":"element","tagName":"a","properties":{"href":"#timetable","ariaLabel":"timetable 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 course will be open until the end of the year 2024."}]},{"type":"element","tagName":"h2","properties":{"id":"lectures","style":"position:relative;"},"children":[{"type":"text","value":"Lectures"},{"type":"element","tagName":"a","properties":{"href":"#lectures","ariaLabel":"lectures 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":"Live lectures are organized on Thursdays, starting on September 5th.\nYou can follow the lecture live via "},{"type":"element","tagName":"a","properties":{"href":"https://helsinki.zoom.us/j/63688020089?pwd=vKhxH2OMcOzPqcteFZHVqUiVCfdmOz.1","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Zoom"}]},{"type":"text","value":" starting at 14.15."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The lecture recordings are updated in the table below after the lecture. Until then, you can find last year's corresponding lecture in the table."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please note, that exam dates and similar information in the older recordings may not concern this year's course."}]},{"type":"element","tagName":"table","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"thead","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Recording"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Slides"}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 1 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/0UrIgF5qKFM"},"children":[{"type":"text","value":"Recording 07.09.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1PIT9gEO5qFJQ6TS8fGs9j_qdeVlYc1sh5xy7VYOOCN8/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 2 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/xjBP3awP-I8"},"children":[{"type":"text","value":"Recording 14.09.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1zKxONZbjNRkaULUW_T2geSq7RO003ipNvHR3ASPKPhc/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 3 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/G0zdNWCm8h8"},"children":[{"type":"text","value":"Recording 21.09.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1OWiZt29hFynu_50B1FmzbzOYC7-z8iyx4mQ3TJTPjs0/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 4 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/U7MFzDabDqg"},"children":[{"type":"text","value":"Recording 28.09.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1xm5bRqeMhFj2S-iDnDj2f0qzbJfXWjqQ2uX_ZAItWzo/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 5 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/OclzsDLgckQ"},"children":[{"type":"text","value":"Recording 05.10.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1PBVezc4Bm2Be9cRBtVComzJlLYVURC_5DUEFG_jtb8o/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 6 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/UjWu85v73YI"},"children":[{"type":"text","value":"Recording 12.10.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1kVjKar8gVwIyFerqMmgg6y1RRGlWHVC2Xh0oKH_hjJI/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 7 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/lMPptaf_EpI"},"children":[{"type":"text","value":"Recording 19.10.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1Q6vO88F5fNjDpVqmyzrS8vcrxyNgZ84s67MOuM8UPa4/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 8 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/B6tm0FAy9wE"},"children":[{"type":"text","value":"Recording 02.11.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1gBegxAXVWdm47AebP5gIGfFerwP1K0vyLRzX4GJtLjY/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 9 (2022)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/r6vV5SXpmWs"},"children":[{"type":"text","value":"Recording"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 10 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/C3jfc3Np4nA"},"children":[{"type":"text","value":"Recording 16.11.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1ntybDeCoPykmTz0R8rjHAU03F8pGr1VZHQzj4UGId7g/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 11 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/LI9LITLkF30"},"children":[{"type":"text","value":"Recording 23.11.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1qWc6ilCxCYqsjkk5dwKpGdHLMnA588BEyakJOrfZw9c/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 12 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/ZcfqkDfJG_Y"},"children":[{"type":"text","value":"Recording 30.11.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1zxc6lYSWK50wy_8Y2s1FKgCAOe9Nz1-0gS6tba2idg0/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"}]}]},"html":"

    The course starts on January 15th 2024.

    This is the course material page for the Introduction to Programming course (BSCS1001, 5 ECTS) and the Advanced Course in Programming (BSCS1002, 5 ECTS) from the Department of Computer Science at the University of Helsinki.

    To pass either of the two courses you are expected to complete programming exercises and take part in an exam. Please see the Grading and exams page for more details.

    The Introduction to Programming course consists of parts 1 through 7 in the material, and the Advanced Course in Programming consists of parts 8 through 14.

    NB: It is no longer possible to directly apply for a right to study at the University of Helsinki through these courses. If you are interested in studying at the University of Helsinki, please have a look at the admissions website.

    Support channels on the course

    The official course support channels are available through workshop guidance on campus and Discord. The course channels in Discord are available through this link.

    Please see the Support and assistance page for more details.

    Use Discord primarily for communication. You can be in contact with the course teachers by e-mail ohjelmoinnin-mooc@helsinki.fi.

    Timetable

    The course will be open until the end of the year 2024.

    Lectures

    Live lectures are organized on Thursdays, starting on September 5th.\nYou can follow the lecture live via Zoom starting at 14.15.

    The lecture recordings are updated in the table below after the lecture. Until then, you can find last year's corresponding lecture in the table.

    Please note, that exam dates and similar information in the older recordings may not concern this year's course.

    \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 \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 \n \n \n
    PartRecordingSlides
    Part 1 (2023)Recording 07.09.2023Slides
    Part 2 (2023)Recording 14.09.2023Slides
    Part 3 (2023)Recording 21.09.2023Slides
    Part 4 (2023)Recording 28.09.2023Slides
    Part 5 (2023)Recording 05.10.2023Slides
    Part 6 (2023)Recording 12.10.2023Slides
    Part 7 (2023)Recording 19.10.2023Slides
    Part 8 (2023)Recording 02.11.2023Slides
    Part 9 (2022)Recording
    Part 10 (2023)Recording 16.11.2023Slides
    Part 11 (2023)Recording 23.11.2023Slides
    Part 12 (2023)Recording 30.11.2023Slides
    ","frontmatter":{"path":"/","title":"About this course","banner":true},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/index.md"}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"The course starts on January 15th 2024."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is the course material page for the Introduction to Programming course (BSCS1001, 5 ECTS) and the Advanced Course in Programming (BSCS1002, 5 ECTS) from the Department of Computer Science at the University of Helsinki."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To pass either of the two courses you are expected to complete programming exercises and take part in an exam. Please see the "},{"type":"element","tagName":"a","properties":{"href":"/grading-and-exams"},"children":[{"type":"text","value":"Grading and exams"}]},{"type":"text","value":" page for more details."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The Introduction to Programming course consists of parts 1 through 7 in the material, and the Advanced Course in Programming consists of parts 8 through 14."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" It is "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"no longer possible"}]},{"type":"text","value":" to directly apply for a right to study at the University of Helsinki through these courses. If you are interested in studying at the University of Helsinki, please have a look at the "},{"type":"element","tagName":"a","properties":{"href":"https://www.helsinki.fi/en/admissions-and-education","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"admissions website"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"support-channels-on-the-course","style":"position:relative;"},"children":[{"type":"text","value":"Support channels on the course"},{"type":"element","tagName":"a","properties":{"href":"#support-channels-on-the-course","ariaLabel":"support channels on the course 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 official course support channels are available through workshop guidance on campus and Discord. The course channels in Discord are available through "},{"type":"element","tagName":"a","properties":{"href":"https://study.cs.helsinki.fi/discord/join/ohjelmoinnin_mooc","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"this link"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please see the "},{"type":"element","tagName":"a","properties":{"href":"/support-and-assistance"},"children":[{"type":"text","value":"Support and assistance"}]},{"type":"text","value":" page for more details."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Use Discord primarily for communication. You can be in contact with the course teachers by e-mail "},{"type":"element","tagName":"a","properties":{"href":"mailto:ohjelmoinnin-mooc@helsinki.fi","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"ohjelmoinnin-mooc@helsinki.fi"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"timetable","style":"position:relative;"},"children":[{"type":"text","value":"Timetable"},{"type":"element","tagName":"a","properties":{"href":"#timetable","ariaLabel":"timetable 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 course will be open until the end of the year 2024."}]},{"type":"element","tagName":"h2","properties":{"id":"lectures","style":"position:relative;"},"children":[{"type":"text","value":"Lectures"},{"type":"element","tagName":"a","properties":{"href":"#lectures","ariaLabel":"lectures 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":"Live lectures are organized on Thursdays, starting on September 5th.\nYou can follow the lecture live via "},{"type":"element","tagName":"a","properties":{"href":"https://helsinki.zoom.us/j/63688020089?pwd=vKhxH2OMcOzPqcteFZHVqUiVCfdmOz.1","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Zoom"}]},{"type":"text","value":" starting at 10.15."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The lecture recordings are updated in the table below after the lecture. Until then, you can find last year's corresponding lecture in the table."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please note, that exam dates and similar information in the older recordings may not concern this year's course."}]},{"type":"element","tagName":"table","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"thead","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Recording"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Slides"}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 1 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/0UrIgF5qKFM"},"children":[{"type":"text","value":"Recording 07.09.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1PIT9gEO5qFJQ6TS8fGs9j_qdeVlYc1sh5xy7VYOOCN8/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 2 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/xjBP3awP-I8"},"children":[{"type":"text","value":"Recording 14.09.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1zKxONZbjNRkaULUW_T2geSq7RO003ipNvHR3ASPKPhc/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 3 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/G0zdNWCm8h8"},"children":[{"type":"text","value":"Recording 21.09.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1OWiZt29hFynu_50B1FmzbzOYC7-z8iyx4mQ3TJTPjs0/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 4 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/U7MFzDabDqg"},"children":[{"type":"text","value":"Recording 28.09.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1xm5bRqeMhFj2S-iDnDj2f0qzbJfXWjqQ2uX_ZAItWzo/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 5 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/OclzsDLgckQ"},"children":[{"type":"text","value":"Recording 05.10.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1PBVezc4Bm2Be9cRBtVComzJlLYVURC_5DUEFG_jtb8o/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 6 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/UjWu85v73YI"},"children":[{"type":"text","value":"Recording 12.10.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1kVjKar8gVwIyFerqMmgg6y1RRGlWHVC2Xh0oKH_hjJI/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 7 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/lMPptaf_EpI"},"children":[{"type":"text","value":"Recording 19.10.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1Q6vO88F5fNjDpVqmyzrS8vcrxyNgZ84s67MOuM8UPa4/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 8 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/B6tm0FAy9wE"},"children":[{"type":"text","value":"Recording 02.11.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1gBegxAXVWdm47AebP5gIGfFerwP1K0vyLRzX4GJtLjY/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 9 (2022)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/r6vV5SXpmWs"},"children":[{"type":"text","value":"Recording"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 10 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/C3jfc3Np4nA"},"children":[{"type":"text","value":"Recording 16.11.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1ntybDeCoPykmTz0R8rjHAU03F8pGr1VZHQzj4UGId7g/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 11 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/LI9LITLkF30"},"children":[{"type":"text","value":"Recording 23.11.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1qWc6ilCxCYqsjkk5dwKpGdHLMnA588BEyakJOrfZw9c/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Part 12 (2023)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://youtu.be/ZcfqkDfJG_Y"},"children":[{"type":"text","value":"Recording 30.11.2023"}]}]},{"type":"text","value":"\n "},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.google.com/presentation/d/1zxc6lYSWK50wy_8Y2s1FKgCAOe9Nz1-0gS6tba2idg0/edit?usp=sharing"},"children":[{"type":"text","value":"Slides"}]}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n "}]},{"type":"text","value":"\n"}]}]},"html":"

    The course starts on January 15th 2024.

    This is the course material page for the Introduction to Programming course (BSCS1001, 5 ECTS) and the Advanced Course in Programming (BSCS1002, 5 ECTS) from the Department of Computer Science at the University of Helsinki.

    To pass either of the two courses you are expected to complete programming exercises and take part in an exam. Please see the Grading and exams page for more details.

    The Introduction to Programming course consists of parts 1 through 7 in the material, and the Advanced Course in Programming consists of parts 8 through 14.

    NB: It is no longer possible to directly apply for a right to study at the University of Helsinki through these courses. If you are interested in studying at the University of Helsinki, please have a look at the admissions website.

    Support channels on the course

    The official course support channels are available through workshop guidance on campus and Discord. The course channels in Discord are available through this link.

    Please see the Support and assistance page for more details.

    Use Discord primarily for communication. You can be in contact with the course teachers by e-mail ohjelmoinnin-mooc@helsinki.fi.

    Timetable

    The course will be open until the end of the year 2024.

    Lectures

    Live lectures are organized on Thursdays, starting on September 5th.\nYou can follow the lecture live via Zoom starting at 10.15.

    The lecture recordings are updated in the table below after the lecture. Until then, you can find last year's corresponding lecture in the table.

    Please note, that exam dates and similar information in the older recordings may not concern this year's course.

    \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 \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 \n \n \n
    PartRecordingSlides
    Part 1 (2023)Recording 07.09.2023Slides
    Part 2 (2023)Recording 14.09.2023Slides
    Part 3 (2023)Recording 21.09.2023Slides
    Part 4 (2023)Recording 28.09.2023Slides
    Part 5 (2023)Recording 05.10.2023Slides
    Part 6 (2023)Recording 12.10.2023Slides
    Part 7 (2023)Recording 19.10.2023Slides
    Part 8 (2023)Recording 02.11.2023Slides
    Part 9 (2022)Recording
    Part 10 (2023)Recording 16.11.2023Slides
    Part 11 (2023)Recording 23.11.2023Slides
    Part 12 (2023)Recording 30.11.2023Slides
    ","frontmatter":{"path":"/","title":"About this course","banner":true},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/index.md"}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-1/1-getting-started/page-data.json b/page-data/part-1/1-getting-started/page-data.json index 794bbc1a7..305729ef1 100644 --- a/page-data/part-1/1-getting-started/page-data.json +++ b/page-data/part-1/1-getting-started/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-1/1-getting-started", - "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 have written and executed your first Python program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to use the print command"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use programming for arithmetic operations"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Computer programs consist of "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"commands"}]},{"type":"text","value":", each command instructing the computer to take some action. A computer executes these commands one by one. Among other things, commands can be used to perform calculations, compare things in the computer's memory, cause changes in how the program functions, relay messages or ask for information from the program's user."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's begin programming by getting familiar with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command, which "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"prints"}]},{"type":"text","value":" text. In this context, printing essentially means that the program will show some text on the screen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program will print the line \"Hi there!\":"}]},{"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":"\"Hi there!\""}]},{"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 run, it produces this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hi there!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program will not work unless the code is written exactly as it is above. For example, trying to run the print command without the quotation marks, like so"}]},{"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":"text","value":"Hi there!"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"will not print out the message, but instead causes an error:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"File \""},{"type":"element","tagName":"stdin","properties":{},"children":[{"type":"text","value":"\", line 1\n print(Hi there!)\n ^\nSyntaxError: invalid syntax\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In summary, if you want to print text, the text must all be encased in quotation marks or Python will not interpret it correctly."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Emoticon","tmcname":"part01-01_emoticon","height":"300px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which prints out an emoticon: :-)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"a-program-of-multiple-commands","style":"position:relative;"},"children":[{"type":"text","value":"A program of multiple commands"},{"type":"element","tagName":"a","properties":{"href":"#a-program-of-multiple-commands","ariaLabel":"a program of multiple commands 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":"Multiple commands written in succession will be executed in order from first to last.\nFor example this program"}]},{"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":"\"Welcome to Introduction to Programming!\""}]},{"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":"\"First we will practice using the print command.\""}]},{"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":"\"This program prints three lines of text on the screen.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"prints the following lines on the screen:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Welcome to Introduction to Programming!\nFirst we will practice using the print command.\nThis program prints three lines of text on the screen."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fix the code: Seven Brothers","tmcname":"part01-02_seven_brothers"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"\"Seitsemän veljestä\" is one of the first novels ever written in Finnish. It is the story of seven orphaned brothers learning to make their way in the world ("},{"type":"element","tagName":"a","properties":{"href":"https://en.wikipedia.org/wiki/Seitsem%C3%A4n_veljest%C3%A4","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"read more on Wikipedia"}]},{"type":"text","value":")."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program is supposed to print out the names of the brothers in alphabetical order, but it's not working quite right yet. Please fix the program so that the names are printed in the correct order."}]},{"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":"\"Simeoni\""}]},{"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":"\"Juhani\""}]},{"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":"\"Eero\""}]},{"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":"\"Lauri\""}]},{"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":"\"Aapo\""}]},{"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":"\"Tuomas\""}]},{"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":"\"Timo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Row, Row, Row Your Boat","tmcname":"part01-03_row_your_boat"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which prints out the following lines exactly as they are written here, punctuation and all:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Row, row, row your boat,\nGently down the stream.\nMerrily, merrily, merrily, merrily,\nLife is but a dream."}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"arithmetic-operations","style":"position:relative;"},"children":[{"type":"text","value":"Arithmetic operations"},{"type":"element","tagName":"a","properties":{"href":"#arithmetic-operations","ariaLabel":"arithmetic operations 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":"You can also put arithmetic operations inside a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command. Running it will then print out the result of the operation. For example, the following program"}]},{"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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"5"}]},{"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","number"]},"children":[{"type":"text","value":"3"}]},{"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":"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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"2"}]},{"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":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"prints out these lines:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"7\n9\n22"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice the lack of quotation marks around the arithmetic operations above. Quotation marks are used to signify "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"strings"}]},{"type":"text","value":". In the context of programming, strings are sequences of characters. They can consist of letters, numbers and any other types of characters, such as punctuation. Strings aren't just words as we commonly understand them, but instead a single string can be as long as multiple complete sentences.\nStrings are usually printed out exactly as they are written. Thus, the following two commands produce two quite different results:"}]},{"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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"2"}]},{"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":["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":"\"2 + 2 * 10\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program prints out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"22\n2 + 2 * 10"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With the second line of code, Python does not calculate the result of the operation, but instead prints out the operation itself, as a string.\nSo, strings are printed out just as they are written, without any reference to their contents."}]},{"type":"element","tagName":"h2","properties":{"id":"commenting","style":"position:relative;"},"children":[{"type":"text","value":"Commenting"},{"type":"element","tagName":"a","properties":{"href":"#commenting","ariaLabel":"commenting 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":"Any line beginning with the pound sign "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"#"}]},{"type":"text","value":", also known as a hash or a number sign, is a comment. This means that any text on that line following the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"#"}]},{"type":"text","value":" symbol will not in any way affect how the program functions. Python will simply ignore it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Comments are used for explaining how a program works, to both the programmer themselves, and others reading the program code. In this program a comment explains the calculation performed in the code:"}]},{"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":"\"Hours in a year:\""}]},{"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":"# there are 365 days in a year and 24 hours in each day"}]},{"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","number"]},"children":[{"type":"text","value":"365"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"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 run, the comment will not be visible to the user:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hours in a year:\n8760"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Short comments can also be added to the end of a line:"}]},{"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":"\"Hours in a year:\""}]},{"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","number"]},"children":[{"type":"text","value":"365"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 365 days, 24 hours in each day"}]}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Minutes in a year","tmcname":"part01-04_minutes_in_a_year"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which prints out the number of minutes in a year. Use Python code to perform the calculation, as in the previous code example."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Print some code","tmcname":"part01-05_print_code"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Thus far, you have probably used double quotation marks "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\""}]},{"type":"text","value":" to print out strings. In addition to the double quotation marks, Python also accepts single quotation marks "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"'"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This comes in handy if you ever want to print out the actual quotation marks themselves:"}]},{"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":"\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":"'\"Come right back!\", shouted the police officer.'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"\"Come right back!\", shouted the police officer."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"print(\"Hello there!\")"}]}]}]},{"type":"comment","value":"\n\nA quiz to review the contents of this section:\n\n\n\n"}]},"html":"

    After this section

    Computer programs consist of commands, each command instructing the computer to take some action. A computer executes these commands one by one. Among other things, commands can be used to perform calculations, compare things in the computer's memory, cause changes in how the program functions, relay messages or ask for information from the program's user.

    Let's begin programming by getting familiar with the print command, which prints text. In this context, printing essentially means that the program will show some text on the screen.

    The following program will print the line \"Hi there!\":

    print(\"Hi there!\")

    When the program is run, it produces this:

    Hi there!

    The program will not work unless the code is written exactly as it is above. For example, trying to run the print command without the quotation marks, like so

    print(Hi there!)

    will not print out the message, but instead causes an error:

    \nFile \"\", line 1\n  print(Hi there!)\n                   ^\nSyntaxError: invalid syntax\n

    In summary, if you want to print text, the text must all be encased in quotation marks or Python will not interpret it correctly.

    Please write a program which prints out an emoticon: :-)

    A program of multiple commands

    Multiple commands written in succession will be executed in order from first to last.\nFor example this program

    print(\"Welcome to Introduction to Programming!\")\nprint(\"First we will practice using the print command.\")\nprint(\"This program prints three lines of text on the screen.\")

    prints the following lines on the screen:

    Welcome to Introduction to Programming!\nFirst we will practice using the print command.\nThis program prints three lines of text on the screen.

    \"Seitsemän veljestä\" is one of the first novels ever written in Finnish. It is the story of seven orphaned brothers learning to make their way in the world (read more on Wikipedia).

    This program is supposed to print out the names of the brothers in alphabetical order, but it's not working quite right yet. Please fix the program so that the names are printed in the correct order.

    print(\"Simeoni\")\nprint(\"Juhani\")\nprint(\"Eero\")\nprint(\"Lauri\")\nprint(\"Aapo\")\nprint(\"Tuomas\")\nprint(\"Timo\")

    Please write a program which prints out the following lines exactly as they are written here, punctuation and all:

    Row, row, row your boat,\nGently down the stream.\nMerrily, merrily, merrily, merrily,\nLife is but a dream.

    Arithmetic operations

    You can also put arithmetic operations inside a print command. Running it will then print out the result of the operation. For example, the following program

    print(2 + 5)\nprint(3 * 3)\nprint(2 + 2 * 10)

    prints out these lines:

    7\n9\n22

    Notice the lack of quotation marks around the arithmetic operations above. Quotation marks are used to signify strings. In the context of programming, strings are sequences of characters. They can consist of letters, numbers and any other types of characters, such as punctuation. Strings aren't just words as we commonly understand them, but instead a single string can be as long as multiple complete sentences.\nStrings are usually printed out exactly as they are written. Thus, the following two commands produce two quite different results:

    print(2 + 2 * 10)\nprint(\"2 + 2 * 10\")

    This program prints out:

    22\n2 + 2 * 10

    With the second line of code, Python does not calculate the result of the operation, but instead prints out the operation itself, as a string.\nSo, strings are printed out just as they are written, without any reference to their contents.

    Commenting

    Any line beginning with the pound sign #, also known as a hash or a number sign, is a comment. This means that any text on that line following the # symbol will not in any way affect how the program functions. Python will simply ignore it.

    Comments are used for explaining how a program works, to both the programmer themselves, and others reading the program code. In this program a comment explains the calculation performed in the code:

    print(\"Hours in a year:\")\n# there are 365 days in a year and 24 hours in each day\nprint(365*24)

    When the program is run, the comment will not be visible to the user:

    Hours in a year:\n8760

    Short comments can also be added to the end of a line:

    print(\"Hours in a year:\")\nprint(365*24) # 365 days, 24 hours in each day

    Please write a program which prints out the number of minutes in a year. Use Python code to perform the calculation, as in the previous code example.

    Thus far, you have probably used double quotation marks \" to print out strings. In addition to the double quotation marks, Python also accepts single quotation marks '.

    This comes in handy if you ever want to print out the actual quotation marks themselves:

    \nprint('\"Come right back!\", shouted the police officer.')\n

    \"Come right back!\", shouted the police officer.

    Please write a program which prints out the following:

    print(\"Hello there!\")

    ","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-1/1-getting-started.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 have written and executed your first Python program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to use the print command"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use programming for arithmetic operations"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Computer programs consist of "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"commands"}]},{"type":"text","value":", each command instructing the computer to take some action. A computer executes these commands one by one. Among other things, commands can be used to perform calculations, compare things in the computer's memory, cause changes in how the program functions, relay messages or ask for information from the program's user."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's begin programming by getting familiar with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command, which "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"prints"}]},{"type":"text","value":" text. In this context, printing essentially means that the program will show some text on the screen."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program will print the line \"Hi there!\":"}]},{"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":"\"Hi there!\""}]},{"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 run, it produces this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hi there!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program will not work unless the code is written exactly as it is above. For example, trying to run the print command without the quotation marks, like so"}]},{"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":"text","value":"Hi there!"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"will not print out the message, but instead causes an error:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"File \""},{"type":"element","tagName":"stdin","properties":{},"children":[{"type":"text","value":"\", line 1\n print(Hi there!)\n ^\nSyntaxError: invalid syntax\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In summary, if you want to print text, the text must all be encased in quotation marks or Python will not interpret it correctly."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Emoticon","tmcname":"part01-01_emoticon","height":"300px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which prints out an emoticon: :-)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"a-program-of-multiple-commands","style":"position:relative;"},"children":[{"type":"text","value":"A program of multiple commands"},{"type":"element","tagName":"a","properties":{"href":"#a-program-of-multiple-commands","ariaLabel":"a program of multiple commands 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":"Multiple commands written in succession will be executed in order from first to last.\nFor example this program"}]},{"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":"\"Welcome to Introduction to Programming!\""}]},{"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":"\"First we will practice using the print command.\""}]},{"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":"\"This program prints three lines of text on the screen.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"prints the following lines on the screen:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Welcome to Introduction to Programming!\nFirst we will practice using the print command.\nThis program prints three lines of text on the screen."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fix the code: Seven Brothers","tmcname":"part01-02_seven_brothers"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"\"Seitsemän veljestä\" is one of the first novels ever written in Finnish. It is the story of seven orphaned brothers learning to make their way in the world ("},{"type":"element","tagName":"a","properties":{"href":"https://en.wikipedia.org/wiki/Seitsem%C3%A4n_veljest%C3%A4","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"read more on Wikipedia"}]},{"type":"text","value":")."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program is supposed to print out the names of the brothers in alphabetical order, but it's not working quite right yet. Please fix the program so that the names are printed in the correct order."}]},{"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":"\"Simeoni\""}]},{"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":"\"Juhani\""}]},{"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":"\"Eero\""}]},{"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":"\"Lauri\""}]},{"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":"\"Aapo\""}]},{"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":"\"Tuomas\""}]},{"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":"\"Timo\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Row, Row, Row Your Boat","tmcname":"part01-03_row_your_boat"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which prints out the following lines exactly as they are written here, punctuation and all:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Row, row, row your boat,\nGently down the stream.\nMerrily, merrily, merrily, merrily,\nLife is but a dream."}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"arithmetic-operations","style":"position:relative;"},"children":[{"type":"text","value":"Arithmetic operations"},{"type":"element","tagName":"a","properties":{"href":"#arithmetic-operations","ariaLabel":"arithmetic operations 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":"You can also put arithmetic operations inside a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command. Running it will then print out the result of the operation. For example, the following program"}]},{"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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"5"}]},{"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","number"]},"children":[{"type":"text","value":"3"}]},{"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":"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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"2"}]},{"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":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"prints out these lines:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"7\n9\n22"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice the lack of quotation marks around the arithmetic operations above. Quotation marks are used to signify "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"strings"}]},{"type":"text","value":". In the context of programming, strings are sequences of characters. They can consist of letters, numbers and any other types of characters, such as punctuation. Strings aren't just words as we commonly understand them, but instead a single string can be as long as multiple complete sentences.\nStrings are usually printed out exactly as they are written. Thus, the following two commands produce two quite different results:"}]},{"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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"2"}]},{"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":["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":"\"2 + 2 * 10\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program prints out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"22\n2 + 2 * 10"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With the second line of code, Python does not calculate the result of the operation, but instead prints out the operation itself, as a string.\nSo, strings are printed out just as they are written, without any reference to their contents."}]},{"type":"element","tagName":"h2","properties":{"id":"commenting","style":"position:relative;"},"children":[{"type":"text","value":"Commenting"},{"type":"element","tagName":"a","properties":{"href":"#commenting","ariaLabel":"commenting 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":"Any line beginning with the pound sign "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"#"}]},{"type":"text","value":", also known as a hash or a number sign, is a comment. This means that any text on that line following the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"#"}]},{"type":"text","value":" symbol will not in any way affect how the program functions. Python will simply ignore it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Comments are used for explaining how a program works, to both the programmer themselves, and others reading the program code. In this program a comment explains the calculation performed in the code:"}]},{"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":"\"Hours in a year:\""}]},{"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":"# there are 365 days in a year and 24 hours in each day"}]},{"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","number"]},"children":[{"type":"text","value":"365"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"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 run, the comment will not be visible to the user:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hours in a year:\n8760"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Short comments can also be added to the end of a line:"}]},{"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":"\"Hours in a year:\""}]},{"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","number"]},"children":[{"type":"text","value":"365"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 365 days, 24 hours in each day"}]}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Minutes in a year","tmcname":"part01-04_minutes_in_a_year"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which prints out the number of minutes in a year. Use Python code to perform the calculation, as in the previous code example."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Print some code","tmcname":"part01-05_print_code"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Thus far, you have probably used double quotation marks "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\""}]},{"type":"text","value":" to print out strings. In addition to the double quotation marks, Python also accepts single quotation marks "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"'"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This comes in handy if you ever want to print out the actual quotation marks themselves:"}]},{"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":"\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":"'\"Come right back!\", shouted the police officer.'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"\"Come right back!\", shouted the police officer."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"print(\"Hello there!\")"}]}]}]},{"type":"comment","value":"\n\nA quiz to review the contents of this section:\n\n\n\n"}]},"html":"

    After this section

    Computer programs consist of commands, each command instructing the computer to take some action. A computer executes these commands one by one. Among other things, commands can be used to perform calculations, compare things in the computer's memory, cause changes in how the program functions, relay messages or ask for information from the program's user.

    Let's begin programming by getting familiar with the print command, which prints text. In this context, printing essentially means that the program will show some text on the screen.

    The following program will print the line \"Hi there!\":

    print(\"Hi there!\")

    When the program is run, it produces this:

    Hi there!

    The program will not work unless the code is written exactly as it is above. For example, trying to run the print command without the quotation marks, like so

    print(Hi there!)

    will not print out the message, but instead causes an error:

    \nFile \"\", line 1\n  print(Hi there!)\n                   ^\nSyntaxError: invalid syntax\n

    In summary, if you want to print text, the text must all be encased in quotation marks or Python will not interpret it correctly.

    Please write a program which prints out an emoticon: :-)

    A program of multiple commands

    Multiple commands written in succession will be executed in order from first to last.\nFor example this program

    print(\"Welcome to Introduction to Programming!\")\nprint(\"First we will practice using the print command.\")\nprint(\"This program prints three lines of text on the screen.\")

    prints the following lines on the screen:

    Welcome to Introduction to Programming!\nFirst we will practice using the print command.\nThis program prints three lines of text on the screen.

    \"Seitsemän veljestä\" is one of the first novels ever written in Finnish. It is the story of seven orphaned brothers learning to make their way in the world (read more on Wikipedia).

    This program is supposed to print out the names of the brothers in alphabetical order, but it's not working quite right yet. Please fix the program so that the names are printed in the correct order.

    print(\"Simeoni\")\nprint(\"Juhani\")\nprint(\"Eero\")\nprint(\"Lauri\")\nprint(\"Aapo\")\nprint(\"Tuomas\")\nprint(\"Timo\")

    Please write a program which prints out the following lines exactly as they are written here, punctuation and all:

    Row, row, row your boat,\nGently down the stream.\nMerrily, merrily, merrily, merrily,\nLife is but a dream.

    Arithmetic operations

    You can also put arithmetic operations inside a print command. Running it will then print out the result of the operation. For example, the following program

    print(2 + 5)\nprint(3 * 3)\nprint(2 + 2 * 10)

    prints out these lines:

    7\n9\n22

    Notice the lack of quotation marks around the arithmetic operations above. Quotation marks are used to signify strings. In the context of programming, strings are sequences of characters. They can consist of letters, numbers and any other types of characters, such as punctuation. Strings aren't just words as we commonly understand them, but instead a single string can be as long as multiple complete sentences.\nStrings are usually printed out exactly as they are written. Thus, the following two commands produce two quite different results:

    print(2 + 2 * 10)\nprint(\"2 + 2 * 10\")

    This program prints out:

    22\n2 + 2 * 10

    With the second line of code, Python does not calculate the result of the operation, but instead prints out the operation itself, as a string.\nSo, strings are printed out just as they are written, without any reference to their contents.

    Commenting

    Any line beginning with the pound sign #, also known as a hash or a number sign, is a comment. This means that any text on that line following the # symbol will not in any way affect how the program functions. Python will simply ignore it.

    Comments are used for explaining how a program works, to both the programmer themselves, and others reading the program code. In this program a comment explains the calculation performed in the code:

    print(\"Hours in a year:\")\n# there are 365 days in a year and 24 hours in each day\nprint(365*24)

    When the program is run, the comment will not be visible to the user:

    Hours in a year:\n8760

    Short comments can also be added to the end of a line:

    print(\"Hours in a year:\")\nprint(365*24) # 365 days, 24 hours in each day

    Please write a program which prints out the number of minutes in a year. Use Python code to perform the calculation, as in the previous code example.

    Thus far, you have probably used double quotation marks \" to print out strings. In addition to the double quotation marks, Python also accepts single quotation marks '.

    This comes in handy if you ever want to print out the actual quotation marks themselves:

    \nprint('\"Come right back!\", shouted the police officer.')\n

    \"Come right back!\", shouted the police officer.

    Please write a program which prints out the following:

    print(\"Hello there!\")

    ","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-1/1-getting-started.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-1/2-information-from-the-user/page-data.json b/page-data/part-1/2-information-from-the-user/page-data.json index f88ad1947..4092fb3f2 100644 --- a/page-data/part-1/2-information-from-the-user/page-data.json +++ b/page-data/part-1/2-information-from-the-user/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-1/2-information-from-the-user", - "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 know how to write a program which uses input from the user"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to use variables to store input and print it out"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to combine strings"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Input"}]},{"type":"text","value":" refers to any information a user gives to the program. Specifically, the Python command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" reads in a line of input typed in by the user. It may also be used to display a message to the user, to prompt for specific input."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program reads in the name of the user with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" command. It then prints it out with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command:"}]},{"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":"name "},{"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":"\"What is your name? \""}]},{"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":"\"Hi there, \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The execution of this program could look like this (input from the user in red):"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your name? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Paul Python"}]},{"type":"text","value":"\nHi there, Paul Python"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What this program prints out is partially dependent on input from the user. That means the execution of the program could also look like this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your name? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Paula Programmer"}]},{"type":"text","value":"\nHi there, Paula Programmer"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The word "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" in this program is a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"variable"}]},{"type":"text","value":". In the context of programming, a variable is a location for storing some "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"value"}]},{"type":"text","value":", such as a string or a number. This value can be used later, and it can also be changed."}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Naming variables"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In principle, variables can be named quite freely, within certain limits specified in the Python language."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is a common international programming practice to name variables in English, but you may come across code where variables are named in other languages, such as the native language of the programmer. The name of the variable has no direct effect on its content, so the name, in that sense, does not matter. However, it can often be helpful in understanding how code functions if variables are named logically and in English."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Name twice","tmcname":"part01-06_name_twice"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for the user's name and then prints it twice, on two consecutive lines."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the how the program should function:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your name? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Paul"}]},{"type":"text","value":"\nPaul\nPaul"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"referencing-a-variable","style":"position:relative;"},"children":[{"type":"text","value":"Referencing a variable"},{"type":"element","tagName":"a","properties":{"href":"#referencing-a-variable","ariaLabel":"referencing a variable 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":"A single variable can be referred to many times in a program:"}]},{"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":"name "},{"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":"\"What is your name? \""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hi, \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"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":"\"!\""}]},{"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 "},{"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":"\" is quite a nice name.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the user gives the name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Paul Python"}]},{"type":"text","value":", this program prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your name? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Paul Python"}]},{"type":"text","value":"\nHi, Paul Python!\nPaul Python is quite a nice name."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a closer look at the way the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command is used above. Within the brackets of the command there is both text in quotation marks as well as variable names which refer to input from the user. These have been combined with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" operator, which "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"concatenates"}]},{"type":"text","value":" two strings into a single string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strings and variables can be combined quite freely:"}]},{"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":"name "},{"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":"\"What is your name? \""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hi \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"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":"\"! Let me make sure: your name is \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"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":"\"?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the user gives the name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Ellen Example"}]},{"type":"text","value":" this prints out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your name? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Ellen Example"}]},{"type":"text","value":"\nHi Ellen Example! Let me make sure: your name is Ellen Example?"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Name and exclamation marks","tmcname":"part01-07_name_and_exclamation_marks"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for the user's name and then prints it out twice on a single line so that there is an exclamation mark at the beginning of the line, another between the two names and a third one at the end of the line."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should function as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your name? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Paul"}]},{"type":"text","value":"\n!Paul!Paul!"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"more-than-one-input","style":"position:relative;"},"children":[{"type":"text","value":"More than one input"},{"type":"element","tagName":"a","properties":{"href":"#more-than-one-input","ariaLabel":"more than one input 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":"A program can ask for more than one input. Notice how below each "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" command stores the received value in a different 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":"name "},{"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":"\"What is your name? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nemail "},{"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":"\"What is your email address? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnickname "},{"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":"\"What is your nickname? \""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Let's make sure we got this right\""}]},{"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":"\"Your name: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" name"},{"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":"\"Your email address: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" email"},{"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":"\"Your nickname: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" nickname"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program could print out this, for example:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your name? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Frances Fictitious"}]},{"type":"text","value":"\nWhat is your email address? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"mailto:frances99@example.com","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"frances99@example.com"}]}]},{"type":"text","value":"\nWhat is your nickname? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Fran"}]},{"type":"text","value":"\nLet's make sure we got this right\nYour name: Frances Fictitious\nYour email address: "},{"type":"element","tagName":"a","properties":{"href":"mailto:frances99@example.com","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"frances99@example.com"}]},{"type":"text","value":"\nYour nickname: Fran"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the same variable is used to store more than one input, each new value will replace the previous one. For example:"}]},{"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":"address "},{"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":"\"What is your address? \""}]},{"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":"\"So you live at address \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" address"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\naddress "},{"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 new address: \""}]},{"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":"\"Your address is now \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" address"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example execution of the program:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your address? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Python Path 101, Flat 3D"}]},{"type":"text","value":"\nSo you live at address Python Path 101, Flat 3D\nPlease type in a new address: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"New Road 999"}]},{"type":"text","value":"\nYour address is now New Road 999"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This means that if the same variable is used to store two inputs in succession, there is no way to access the first input value after it has been replaced by the second:"}]},{"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":"address "},{"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":"\"What is your address? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\naddress "},{"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 new address: \""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Your address is now \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" address"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how the program's output might look like:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your address? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Python Path 10"}]},{"type":"text","value":"\nPlease type in a new address: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Programmer's Walk 23"}]},{"type":"text","value":"\nYour address is now Programmer's Walk 23"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Name and address","tmcname":"part01-08_name_and_address"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for the user's name and address. The program should also print out the given information, as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Given name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Steve"}]},{"type":"text","value":"\nFamily name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Sanders"}]},{"type":"text","value":"\nStreet address: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"91 Station Road"}]},{"type":"text","value":"\nCity and postal code: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"London EC05 6AW"}]},{"type":"text","value":"\nSteve Sanders\n91 Station Road\nLondon EC05 6AW"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fix the code: Utterances","tmcname":"part01-09_utterances"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here is a program which should ask for three utterances and print them out, like so:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The 1st part: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hickory"}]},{"type":"text","value":"\nThe 2nd part: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"dickory"}]},{"type":"text","value":"\nThe 3rd part: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"dock"}]},{"type":"text","value":"\nhickory-dickory-dock!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"However, there is something wrong with the code below. Please fix it."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Story","tmcname":"part01-10_story"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which prints out the following story. The user gives a name and a year, which should be inserted into the printout."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Mary"}]},{"type":"text","value":"\nPlease type in a year: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1572"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Mary is a valiant knight, born in the year 1572. One morning Mary woke up to an awful racket: a dragon was approaching the village. Only Mary could save the village's residents."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The story should change according to the input given by the user."}]}]},{"type":"comment","value":"\n\nA quiz to review the contents of this section:\n\n\n\n"}]},"html":"

    After this section

    Input refers to any information a user gives to the program. Specifically, the Python command input reads in a line of input typed in by the user. It may also be used to display a message to the user, to prompt for specific input.

    The following program reads in the name of the user with the input command. It then prints it out with the print command:

    name = input(\"What is your name? \")\nprint(\"Hi there, \" + name)

    The execution of this program could look like this (input from the user in red):

    What is your name? Paul Python\nHi there, Paul Python

    What this program prints out is partially dependent on input from the user. That means the execution of the program could also look like this:

    What is your name? Paula Programmer\nHi there, Paula Programmer

    The word name in this program is a variable. In the context of programming, a variable is a location for storing some value, such as a string or a number. This value can be used later, and it can also be changed.

    In principle, variables can be named quite freely, within certain limits specified in the Python language.

    It is a common international programming practice to name variables in English, but you may come across code where variables are named in other languages, such as the native language of the programmer. The name of the variable has no direct effect on its content, so the name, in that sense, does not matter. However, it can often be helpful in understanding how code functions if variables are named logically and in English.

    Please write a program which asks for the user's name and then prints it twice, on two consecutive lines.

    An example of the how the program should function:

    What is your name? Paul\nPaul\nPaul

    Referencing a variable

    A single variable can be referred to many times in a program:

    name = input(\"What is your name? \")\n\nprint(\"Hi, \" + name + \"!\")\nprint(name + \" is quite a nice name.\")

    If the user gives the name Paul Python, this program prints out the following:

    What is your name? Paul Python\nHi, Paul Python!\nPaul Python is quite a nice name.

    Let's have a closer look at the way the print command is used above. Within the brackets of the command there is both text in quotation marks as well as variable names which refer to input from the user. These have been combined with a + operator, which concatenates two strings into a single string.

    Strings and variables can be combined quite freely:

    name = input(\"What is your name? \")\n\nprint(\"Hi \" + name + \"! Let me make sure: your name is \" + name + \"?\")

    If the user gives the name Ellen Example this prints out

    What is your name? Ellen Example\nHi Ellen Example! Let me make sure: your name is Ellen Example?

    Please write a program which asks for the user's name and then prints it out twice on a single line so that there is an exclamation mark at the beginning of the line, another between the two names and a third one at the end of the line.

    The program should function as follows:

    What is your name? Paul\n!Paul!Paul!

    More than one input

    A program can ask for more than one input. Notice how below each input command stores the received value in a different variable.

    name = input(\"What is your name? \")\nemail = input(\"What is your email address? \")\nnickname = input(\"What is your nickname? \")\n\nprint(\"Let's make sure we got this right\")\nprint(\"Your name: \" + name)\nprint(\"Your email address: \" + email)\nprint(\"Your nickname: \" + nickname)

    The program could print out this, for example:

    What is your name? Frances Fictitious\nWhat is your email address? frances99@example.com\nWhat is your nickname? Fran\nLet's make sure we got this right\nYour name: Frances Fictitious\nYour email address: frances99@example.com\nYour nickname: Fran

    If the same variable is used to store more than one input, each new value will replace the previous one. For example:

    address = input(\"What is your address? \")\nprint(\"So you live at address \" + address)\n\naddress = input(\"Please type in a new address: \")\nprint(\"Your address is now \" + address)

    An example execution of the program:

    What is your address? Python Path 101, Flat 3D\nSo you live at address Python Path 101, Flat 3D\nPlease type in a new address: New Road 999\nYour address is now New Road 999

    This means that if the same variable is used to store two inputs in succession, there is no way to access the first input value after it has been replaced by the second:

    address = input(\"What is your address? \")\naddress = input(\"Please type in a new address: \")\n\nprint(\"Your address is now \" + address)

    An example of how the program's output might look like:

    What is your address? Python Path 10\nPlease type in a new address: Programmer's Walk 23\nYour address is now Programmer's Walk 23

    Please write a program which asks for the user's name and address. The program should also print out the given information, as follows:

    Given name: Steve\nFamily name: Sanders\nStreet address: 91 Station Road\nCity and postal code: London EC05 6AW\nSteve Sanders\n91 Station Road\nLondon EC05 6AW

    Here is a program which should ask for three utterances and print them out, like so:

    The 1st part: hickory\nThe 2nd part: dickory\nThe 3rd part: dock\nhickory-dickory-dock!

    However, there is something wrong with the code below. Please fix it.

    Please write a program which prints out the following story. The user gives a name and a year, which should be inserted into the printout.

    Please type in a name: Mary\nPlease type in a year: 1572

    Mary is a valiant knight, born in the year 1572. One morning Mary woke up to an awful racket: a dragon was approaching the village. Only Mary could save the village's residents.

    The story should change according to the input given by the user.

    ","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-1/2-information-from-the-user.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know how to write a program which uses input from the user"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to use variables to store input and print it out"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to combine strings"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Input"}]},{"type":"text","value":" refers to any information a user gives to the program. Specifically, the Python command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" reads in a line of input typed in by the user. It may also be used to display a message to the user, to prompt for specific input."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program reads in the name of the user with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" command. It then prints it out with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command:"}]},{"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":"name "},{"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":"\"What is your name? \""}]},{"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":"\"Hi there, \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The execution of this program could look like this (input from the user in red):"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your name? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Paul Python"}]},{"type":"text","value":"\nHi there, Paul Python"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What this program prints out is partially dependent on input from the user. That means the execution of the program could also look like this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your name? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Paula Programmer"}]},{"type":"text","value":"\nHi there, Paula Programmer"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The word "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" in this program is a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"variable"}]},{"type":"text","value":". In the context of programming, a variable is a location for storing some "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"value"}]},{"type":"text","value":", such as a string or a number. This value can be used later, and it can also be changed."}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Naming variables"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In principle, variables can be named quite freely, within certain limits specified in the Python language."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is a common international programming practice to name variables in English, but you may come across code where variables are named in other languages, such as the native language of the programmer. The name of the variable has no direct effect on its content, so the name, in that sense, does not matter. However, it can often be helpful in understanding how code functions if variables are named logically and in English."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Name twice","tmcname":"part01-06_name_twice"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for the user's name and then prints it twice, on two consecutive lines."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the how the program should function:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your name? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Paul"}]},{"type":"text","value":"\nPaul\nPaul"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"referencing-a-variable","style":"position:relative;"},"children":[{"type":"text","value":"Referencing a variable"},{"type":"element","tagName":"a","properties":{"href":"#referencing-a-variable","ariaLabel":"referencing a variable 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":"A single variable can be referred to many times in a program:"}]},{"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":"name "},{"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":"\"What is your name? \""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hi, \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"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":"\"!\""}]},{"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 "},{"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":"\" is quite a nice name.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the user gives the name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Paul Python"}]},{"type":"text","value":", this program prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your name? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Paul Python"}]},{"type":"text","value":"\nHi, Paul Python!\nPaul Python is quite a nice name."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a closer look at the way the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command is used above. Within the brackets of the command there is both text in quotation marks as well as variable names which refer to input from the user. These have been combined with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" operator, which "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"concatenates"}]},{"type":"text","value":" two strings into a single string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Strings and variables can be combined quite freely:"}]},{"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":"name "},{"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":"\"What is your name? \""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hi \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"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":"\"! Let me make sure: your name is \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"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":"\"?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the user gives the name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Ellen Example"}]},{"type":"text","value":" this prints out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your name? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Ellen Example"}]},{"type":"text","value":"\nHi Ellen Example! Let me make sure: your name is Ellen Example?"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Name and exclamation marks","tmcname":"part01-07_name_and_exclamation_marks"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for the user's name and then prints it out twice on a single line so that there is an exclamation mark at the beginning of the line, another between the two names and a third one at the end of the line."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should function as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your name? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Paul"}]},{"type":"text","value":"\n!Paul!Paul!"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"more-than-one-input","style":"position:relative;"},"children":[{"type":"text","value":"More than one input"},{"type":"element","tagName":"a","properties":{"href":"#more-than-one-input","ariaLabel":"more than one input 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":"A program can ask for more than one input. Notice how below each "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" command stores the received value in a different 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":"name "},{"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":"\"What is your name? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nemail "},{"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":"\"What is your email address? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnickname "},{"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":"\"What is your nickname? \""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Let's make sure we got this right\""}]},{"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":"\"Your name: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" name"},{"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":"\"Your email address: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" email"},{"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":"\"Your nickname: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" nickname"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program could print out this, for example:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your name? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Frances Fictitious"}]},{"type":"text","value":"\nWhat is your email address? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"mailto:frances99@example.com","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"frances99@example.com"}]}]},{"type":"text","value":"\nWhat is your nickname? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Fran"}]},{"type":"text","value":"\nLet's make sure we got this right\nYour name: Frances Fictitious\nYour email address: "},{"type":"element","tagName":"a","properties":{"href":"mailto:frances99@example.com","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"frances99@example.com"}]},{"type":"text","value":"\nYour nickname: Fran"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the same variable is used to store more than one input, each new value will replace the previous one. For example:"}]},{"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":"address "},{"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":"\"What is your address? \""}]},{"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":"\"So you live at address \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" address"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\naddress "},{"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 new address: \""}]},{"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":"\"Your address is now \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" address"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example execution of the program:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your address? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Python Path 101, Flat 3D"}]},{"type":"text","value":"\nSo you live at address Python Path 101, Flat 3D\nPlease type in a new address: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"New Road 999"}]},{"type":"text","value":"\nYour address is now New Road 999"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This means that if the same variable is used to store two inputs in succession, there is no way to access the first input value after it has been replaced by the second:"}]},{"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":"address "},{"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":"\"What is your address? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\naddress "},{"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 new address: \""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Your address is now \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" address"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how the program's output might look like:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your address? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Python Path 10"}]},{"type":"text","value":"\nPlease type in a new address: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Programmer's Walk 23"}]},{"type":"text","value":"\nYour address is now Programmer's Walk 23"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Name and address","tmcname":"part01-08_name_and_address"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for the user's name and address. The program should also print out the given information, as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Given name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Steve"}]},{"type":"text","value":"\nFamily name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Sanders"}]},{"type":"text","value":"\nStreet address: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"91 Station Road"}]},{"type":"text","value":"\nCity and postal code: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"London EC05 6AW"}]},{"type":"text","value":"\nSteve Sanders\n91 Station Road\nLondon EC05 6AW"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fix the code: Utterances","tmcname":"part01-09_utterances"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here is a program which should ask for three utterances and print them out, like so:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The 1st part: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hickory"}]},{"type":"text","value":"\nThe 2nd part: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"dickory"}]},{"type":"text","value":"\nThe 3rd part: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"dock"}]},{"type":"text","value":"\nhickory-dickory-dock!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"However, there is something wrong with the code below. Please fix it."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Story","tmcname":"part01-10_story"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which prints out the following story. The user gives a name and a year, which should be inserted into the printout."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Mary"}]},{"type":"text","value":"\nPlease type in a year: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1572"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Mary is a valiant knight, born in the year 1572. One morning Mary woke up to an awful racket: a dragon was approaching the village. Only Mary could save the village's residents."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The story should change according to the input given by the user."}]}]},{"type":"comment","value":"\n\nA quiz to review the contents of this section:\n\n\n\n"}]},"html":"

    After this section

    Input refers to any information a user gives to the program. Specifically, the Python command input reads in a line of input typed in by the user. It may also be used to display a message to the user, to prompt for specific input.

    The following program reads in the name of the user with the input command. It then prints it out with the print command:

    name = input(\"What is your name? \")\nprint(\"Hi there, \" + name)

    The execution of this program could look like this (input from the user in red):

    What is your name? Paul Python\nHi there, Paul Python

    What this program prints out is partially dependent on input from the user. That means the execution of the program could also look like this:

    What is your name? Paula Programmer\nHi there, Paula Programmer

    The word name in this program is a variable. In the context of programming, a variable is a location for storing some value, such as a string or a number. This value can be used later, and it can also be changed.

    In principle, variables can be named quite freely, within certain limits specified in the Python language.

    It is a common international programming practice to name variables in English, but you may come across code where variables are named in other languages, such as the native language of the programmer. The name of the variable has no direct effect on its content, so the name, in that sense, does not matter. However, it can often be helpful in understanding how code functions if variables are named logically and in English.

    Please write a program which asks for the user's name and then prints it twice, on two consecutive lines.

    An example of the how the program should function:

    What is your name? Paul\nPaul\nPaul

    Referencing a variable

    A single variable can be referred to many times in a program:

    name = input(\"What is your name? \")\n\nprint(\"Hi, \" + name + \"!\")\nprint(name + \" is quite a nice name.\")

    If the user gives the name Paul Python, this program prints out the following:

    What is your name? Paul Python\nHi, Paul Python!\nPaul Python is quite a nice name.

    Let's have a closer look at the way the print command is used above. Within the brackets of the command there is both text in quotation marks as well as variable names which refer to input from the user. These have been combined with a + operator, which concatenates two strings into a single string.

    Strings and variables can be combined quite freely:

    name = input(\"What is your name? \")\n\nprint(\"Hi \" + name + \"! Let me make sure: your name is \" + name + \"?\")

    If the user gives the name Ellen Example this prints out

    What is your name? Ellen Example\nHi Ellen Example! Let me make sure: your name is Ellen Example?

    Please write a program which asks for the user's name and then prints it out twice on a single line so that there is an exclamation mark at the beginning of the line, another between the two names and a third one at the end of the line.

    The program should function as follows:

    What is your name? Paul\n!Paul!Paul!

    More than one input

    A program can ask for more than one input. Notice how below each input command stores the received value in a different variable.

    name = input(\"What is your name? \")\nemail = input(\"What is your email address? \")\nnickname = input(\"What is your nickname? \")\n\nprint(\"Let's make sure we got this right\")\nprint(\"Your name: \" + name)\nprint(\"Your email address: \" + email)\nprint(\"Your nickname: \" + nickname)

    The program could print out this, for example:

    What is your name? Frances Fictitious\nWhat is your email address? frances99@example.com\nWhat is your nickname? Fran\nLet's make sure we got this right\nYour name: Frances Fictitious\nYour email address: frances99@example.com\nYour nickname: Fran

    If the same variable is used to store more than one input, each new value will replace the previous one. For example:

    address = input(\"What is your address? \")\nprint(\"So you live at address \" + address)\n\naddress = input(\"Please type in a new address: \")\nprint(\"Your address is now \" + address)

    An example execution of the program:

    What is your address? Python Path 101, Flat 3D\nSo you live at address Python Path 101, Flat 3D\nPlease type in a new address: New Road 999\nYour address is now New Road 999

    This means that if the same variable is used to store two inputs in succession, there is no way to access the first input value after it has been replaced by the second:

    address = input(\"What is your address? \")\naddress = input(\"Please type in a new address: \")\n\nprint(\"Your address is now \" + address)

    An example of how the program's output might look like:

    What is your address? Python Path 10\nPlease type in a new address: Programmer's Walk 23\nYour address is now Programmer's Walk 23

    Please write a program which asks for the user's name and address. The program should also print out the given information, as follows:

    Given name: Steve\nFamily name: Sanders\nStreet address: 91 Station Road\nCity and postal code: London EC05 6AW\nSteve Sanders\n91 Station Road\nLondon EC05 6AW

    Here is a program which should ask for three utterances and print them out, like so:

    The 1st part: hickory\nThe 2nd part: dickory\nThe 3rd part: dock\nhickory-dickory-dock!

    However, there is something wrong with the code below. Please fix it.

    Please write a program which prints out the following story. The user gives a name and a year, which should be inserted into the printout.

    Please type in a name: Mary\nPlease type in a year: 1572

    Mary is a valiant knight, born in the year 1572. One morning Mary woke up to an awful racket: a dragon was approaching the village. Only Mary could save the village's residents.

    The story should change according to the input given by the user.

    ","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-1/2-information-from-the-user.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-1/3-more-about-variables/page-data.json b/page-data/part-1/3-more-about-variables/page-data.json index 786eaca45..7b2a852e1 100644 --- a/page-data/part-1/3-more-about-variables/page-data.json +++ b/page-data/part-1/3-more-about-variables/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-1/3-more-about-variables", - "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 able to use variables in different contexts"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what kind of data can be stored in variables"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will understand the difference between strings, integers and floating point numbers"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please fill in this questionnaire before getting started with this section. You will get one exercise point for answering."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variables are needed for various purposes in programming. You can use variables to store any information that will be needed later in the program's execution."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In Python programming variables are created like so:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"variable_name = ..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"..."}]},{"type":"text","value":" means the value stored in the variable."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, when you used the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" command to read a string from the user, you stored the string in a variable and then used the variable later in your program:"}]},{"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":"name "},{"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":"\"What is your name? \""}]},{"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":"\"Hi, \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" name"},{"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":"What is your name? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Ghosty"}]},{"type":"text","value":"\nHi, Ghosty"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The value stored in a variable can also be defined using other 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":"text","value":"given_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":"\"Paul\""}]},{"type":"text","value":"\nfamily_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":"\"Python\""}]},{"type":"text","value":"\n\nname "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" given_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":"\" \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" family_name\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":"name"},{"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 Python"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here the values stored in the three variables are not obtained from user input. They remain the same every time the program is executed. This is called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"hard-coding"}]},{"type":"text","value":" data into the program."}]},{"type":"element","tagName":"h2","properties":{"id":"changing-the-value-of-a-variable","style":"position:relative;"},"children":[{"type":"text","value":"Changing the value of a variable"},{"type":"element","tagName":"a","properties":{"href":"#changing-the-value-of-a-variable","ariaLabel":"changing the value of a variable 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":"As implied by the name "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"variable"}]},{"type":"text","value":", the value stored in a variable can change. In the previous section we noticed that the new value replaces the old one."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"During the execution of the following program, the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"word"}]},{"type":"text","value":" will have three different 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":"word "},{"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 word: \""}]},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nword "},{"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":"\"And another word: \""}]},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nword "},{"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":"\"third\""}]},{"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":"word"},{"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 word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"first"}]},{"type":"text","value":"\nfirst\nAnd another word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"second"}]},{"type":"text","value":"\nsecond\nthird"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The value stored in the variable changes each time the variable is assigned a new value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The new value of a variable can be derived from its old value. In the following example the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"word"}]},{"type":"text","value":" is first assigned a value based on user input. Then it is assigned a new value, which is the old value with three exclamation marks added to the end."}]},{"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":"word "},{"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 word: \""}]},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nword "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" word "},{"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":"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":"word"},{"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 word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"test"}]},{"type":"text","value":"\ntest\ntest!!!"}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Choosing a good name for a variable"},"children":[{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is often useful to name variables according to what they are used for. For example, if the variable contains a word, the name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"word"}]},{"type":"text","value":" is a better choice than, say, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There is no set limit to the length of a variable name in Python, but there are some other limitations. A variable name should begin with a letter, and it can only contain letters, numbers and underscores _."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lowercase and uppercase letters are different characters. The variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Name"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NAME"}]},{"type":"text","value":" are all different variables. While this rule has a few exceptions, we will ignore those for now."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is a common programming practice in Python to use only lowercase characters in variable names. If the variable name consists of multiple words, use an underscore between the words. While this rule also has a few exceptions, we will ignore those for now."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"integers","style":"position:relative;"},"children":[{"type":"text","value":"Integers"},{"type":"element","tagName":"a","properties":{"href":"#integers","ariaLabel":"integers 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":"Thus far, we have only stored strings in variables, but there are also many other types of information we will want to store and access later. Let's have a look at integers first. Integers are numbers that do not have a decimal or fractional part, such as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-15"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program creates the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"age"}]},{"type":"text","value":", which contains an integer value."}]},{"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":"age "},{"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":"24"}]},{"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":"age"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program prints out just this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"24"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice the lack of quotation marks here. In fact, if we were to add quotation marks around the number, this would mean our variable would no longer be an integer, but a string instead. A string can contain numbers, but it is processed differently."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, why does it matter that variables have a type, when the following program still prints out the same thing twice?"}]},{"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":"number1 "},{"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":"100"}]},{"type":"text","value":"\nnumber2 "},{"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":"\"100\""}]},{"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":"number1"},{"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":"number2"},{"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":"100\n100"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variable types matter because different operations affect different types of variables in different ways. Let's have a look at an example:"}]},{"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":"number1 "},{"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":"100"}]},{"type":"text","value":"\nnumber2 "},{"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":"\"100\""}]},{"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":"number1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number1"},{"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":"number2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"200\n100100"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For integer values the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" operator means addition, but for string values it means concatenation, or \"stringing together\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Not all operators are available for all types of variables. While numbers can be divided using the division operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":", attempting to divide a string by a number causes an error:"}]},{"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","string"]},"children":[{"type":"text","value":"\"100\""}]},{"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","operator"]},"children":[{"type":"text","value":"/"}]},{"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":"sample-output","properties":{},"children":[{"type":"text","value":"\nTypeError: unsupported operand type(s) for /: 'str' and 'int'\n"}]},{"type":"element","tagName":"h2","properties":{"id":"combining-values-when-printing","style":"position:relative;"},"children":[{"type":"text","value":"Combining values when printing"},{"type":"element","tagName":"a","properties":{"href":"#combining-values-when-printing","ariaLabel":"combining values when printing 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":"Similarly, the following program will not work, because "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"The result is \""}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"result"}]},{"type":"text","value":" are of two different types:"}]},{"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":"result "},{"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":"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":"25"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the following line produces an error"}]},{"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 result is \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" result"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program does not print out anything, but instead throws an error:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TypeError: unsupported operand type(s) for +: 'str' and 'int'"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here, Python tells us that combining two different types of values will not work just like that. In this case, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"The result is \""}]},{"type":"text","value":" is of type string, while the value stored in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"result"}]},{"type":"text","value":" is of type integer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we do want to print out a string and an integer in a single command, the integer can be cast as a string with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":" function, and the two strings can then be combined normally. For example, this would 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":"result "},{"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":"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":"25"}]},{"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 result is \""}]},{"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":"str"}]},{"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":"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":"The result is 250"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command also has built-in functionalities that support combining different types of values. The simplest way is to add a comma between the values. All the values will be printed out regardless of their type:"}]},{"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":"result "},{"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":"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":"25"}]},{"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 result is\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The result is 250"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice that there is an automatically added whitespace character between the values separated by a comma here."}]},{"type":"element","tagName":"h2","properties":{"id":"printing-with-f-strings","style":"position:relative;"},"children":[{"type":"text","value":"Printing with f-strings"},{"type":"element","tagName":"a","properties":{"href":"#printing-with-f-strings","ariaLabel":"printing with f 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":"What if we want to have more flexibility and control over what we print out? So called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"f-strings"}]},{"type":"text","value":" are another way of formatting printouts in Python. The syntax can initially look a bit confusing, but in the end f-strings are often the simplest way of formatting text."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With f-strings the previous example would 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":"text","value":"result "},{"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":"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":"25"}]},{"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\"The result 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":"result"},{"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":"p","properties":{},"children":[{"type":"text","value":"Let's break this apart. In the very beginning of the string we are printing out there is the character "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"f"}]},{"type":"text","value":". This tells Python that what follows is an f-string. Within the string, enclosed in curly brackets, is the variable name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"result"}]},{"type":"text","value":". The value it contains becomes a part of the printed string. The printout is exactly the same as in the previous examples:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The result is 250"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A single f-string can contain multiple variables. For example this code"}]},{"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":"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":"\"Mark\""}]},{"type":"text","value":"\nage "},{"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":"37"}]},{"type":"text","value":"\ncity "},{"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":"\"Palo Alto\""}]},{"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\"Hi "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", you are "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"age"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" years old. You live 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":"city"},{"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":"p","properties":{},"children":[{"type":"text","value":"prints out this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hi Mark, you are 37 years old. You live in Palo Alto."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is difficult to create a printout exactly like this using the comma notation in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command. For example, this program"}]},{"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":"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":"\"Mark\""}]},{"type":"text","value":"\nage "},{"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":"37"}]},{"type":"text","value":"\ncity "},{"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":"\"Palo Alto\""}]},{"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":"\"Hi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"\", you are\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" age"},{"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":"\"years old. You live in\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" city"},{"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":"\".\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hi Mark , you are 37 years old. You live in Palo Alto ."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice the automatically inserted whitespace between each comma-separated part of the printout. Preventing "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" from adding the extra spaces is technically possible, but not worth the trouble given that we can instead use f-strings."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In its simplicity the comma notation of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command can often be useful, but it does sometimes cause more trouble than it's worth. F-strings are usually a more reliable option. In part 4 you will learn more about the handy features of f-strings when it comes to formatting printouts."}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"F-strings and Python versions"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you are using an older version of Python, f-strings may not work. They were introduced in Python version 3.6. Later on during the course you will install Python on your own computer. Unfortunately, the more modern versions of Python are not always available for older operating systems. If that is the case with your computer, when there are exercises requiring the use of f-strings, you can always try them out in the in-browser exercise templates in these early parts of this course."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Extra space","tmcname":"part01-10b_extra_space","height":"400px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your friend is working on an app for jobseekers. She sends you this bit of code:"}]},{"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":"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":"\"Tim Tester\""}]},{"type":"text","value":"\nage "},{"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":"20"}]},{"type":"text","value":"\nskill1 "},{"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\""}]},{"type":"text","value":"\nlevel1 "},{"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":"\"beginner\""}]},{"type":"text","value":"\nskill2 "},{"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":"\"java\""}]},{"type":"text","value":"\nlevel2 "},{"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":"\"veteran\""}]},{"type":"text","value":"\nskill3 "},{"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":"\"programming\""}]},{"type":"text","value":"\nlevel3 "},{"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":"\"semiprofessional\""}]},{"type":"text","value":"\nlower "},{"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":"2000"}]},{"type":"text","value":"\nupper "},{"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":"3000"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"my name is \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"\" , I am \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" age"},{"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":"\"years old\""}]},{"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":"\"my skills 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":"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":"\"- \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" skill1"},{"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":"\" (\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" level1"},{"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":"\")\""}]},{"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":"\"- \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" skill2"},{"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":"\" (\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" level2"},{"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":"\")\""}]},{"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":"\"- \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" skill3"},{"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":"\" (\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" level3"},{"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":"\" )\""}]},{"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":"\"I am looking for a job with a salary of\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"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":"\"-\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"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":"\"euros per month\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should print out "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"exactly"}]},{"type":"text","value":" the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"my name is Tim Tester, I am 20 years old\n\nmy skills are\n - python (beginner)\n - java (veteran)\n - programming (semiprofessional)\n\nI am looking for a job with a salary of 2000-3000 euros per month\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The code works almost correctly, but not quite. This exercise has very strict tests, which check the output for every single bit of whitespace."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please fix the code so that the printout looks right. Notice especially how the comma notation in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command automatically inserts a space around the different comma-separated parts."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The easiest way to transform the code so that it meets requirements is to use f-strings."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hint: you can print an empty line by adding an empty "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command, or by adding the newline character "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\\n"}]},{"type":"text","value":" into your string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Do remember to be extra careful when formatting printouts also in the future on this course. Some of the exercises have tests that require your output to be exactly as specified in the examples given. For example, please use actual whitespace characters in your code, instead of ASCII character codes for whitespace, or some such."}]}]},{"type":"element","tagName":"h2","properties":{"id":"floating-point-numbers","style":"position:relative;"},"children":[{"type":"text","value":"Floating point numbers"},{"type":"element","tagName":"a","properties":{"href":"#floating-point-numbers","ariaLabel":"floating point numbers 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":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Floating point number"}]},{"type":"text","value":" or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"float"}]},{"type":"text","value":" is a term you will come across often in programming. It refers to numbers with a decimal point. They can be used much in the same way as integer values."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program calculates the mean of three floating point numbers:"}]},{"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":"number1 "},{"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":"2.5"}]},{"type":"text","value":"\nnumber2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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.25"}]},{"type":"text","value":"\nnumber3 "},{"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.62"}]},{"type":"text","value":"\n\nmean "},{"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":"text","value":"number1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number3"},{"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":"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\"Mean: "}]},{"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":"\""}]}]},{"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":"Mean: 1.6233333333333333"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Arithmetics","tmcname":"part01-11_arithmetics"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program already contains two integer variables, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":":"}]},{"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":"x "},{"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":"27"}]},{"type":"text","value":"\ny "},{"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":"15"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please complete the program so that it also prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"27 + 15 = 42\n27 - 15 = 12\n27 * 15 = 405\n27 / 15 = 1.8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should work correctly even if the values of the variables are changed. That is, if the first two lines are replaced with 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":"text","value":"x "},{"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":"4"}]},{"type":"text","value":"\ny "},{"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":"9"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"the program should print out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4 + 9 = 13\n4 - 9 = -5\n4 * 9 = 36\n4 / 9 = 0.4444444444444444"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fix the code: Print a single line","tmcname":"part01-12_print_a_single_line"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command usually prints out a line of its own, complete with a change of line at the end. However, if the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command is given an additional argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"end = \"\""}]},{"type":"text","value":", it will not print a line change."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"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":"\"Hi \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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"},{"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":"\"there!\""}]},{"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":"Hi there!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please fix this program so that the entire calculation, complete with result, is printed out on a single line. Do not change the number of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" commands used."}]},{"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":"\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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"\" + \""}]},{"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","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":"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"},{"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","number"]},"children":[{"type":"text","value":"4"}]},{"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":"\" = \""}]},{"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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"8"}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]}]},{"type":"comment","value":"\n\nA quiz to review the contents of this section:\n\n\n\n"}]},"html":"

    After this section

    Please fill in this questionnaire before getting started with this section. You will get one exercise point for answering.

    Variables are needed for various purposes in programming. You can use variables to store any information that will be needed later in the program's execution.

    In Python programming variables are created like so:

    variable_name = ...

    Here ... means the value stored in the variable.

    For example, when you used the input command to read a string from the user, you stored the string in a variable and then used the variable later in your program:

    name = input(\"What is your name? \")\nprint(\"Hi, \" + name)

    What is your name? Ghosty\nHi, Ghosty

    The value stored in a variable can also be defined using other variables:

    given_name = \"Paul\"\nfamily_name = \"Python\"\n\nname = given_name + \" \" + family_name\n\nprint(name)

    Paul Python

    Here the values stored in the three variables are not obtained from user input. They remain the same every time the program is executed. This is called hard-coding data into the program.

    Changing the value of a variable

    As implied by the name variable, the value stored in a variable can change. In the previous section we noticed that the new value replaces the old one.

    During the execution of the following program, the variable word will have three different values:

    word = input(\"Please type in a word: \")\nprint(word)\n\nword = input(\"And another word: \")\nprint(word)\n\nword = \"third\"\nprint(word)

    Please type in a word: first\nfirst\nAnd another word: second\nsecond\nthird

    The value stored in the variable changes each time the variable is assigned a new value.

    The new value of a variable can be derived from its old value. In the following example the variable word is first assigned a value based on user input. Then it is assigned a new value, which is the old value with three exclamation marks added to the end.

    word = input(\"Please type in a word: \")\nprint(word)\n\nword = word + \"!!!\"\nprint(word)

    Please type in a word: test\ntest\ntest!!!

    Integers

    Thus far, we have only stored strings in variables, but there are also many other types of information we will want to store and access later. Let's have a look at integers first. Integers are numbers that do not have a decimal or fractional part, such as -15, 0 and 1.

    The following program creates the variable age, which contains an integer value.

    age = 24\nprint(age)

    The program prints out just this:

    24

    Notice the lack of quotation marks here. In fact, if we were to add quotation marks around the number, this would mean our variable would no longer be an integer, but a string instead. A string can contain numbers, but it is processed differently.

    So, why does it matter that variables have a type, when the following program still prints out the same thing twice?

    number1 = 100\nnumber2 = \"100\"\n\nprint(number1)\nprint(number2)

    100\n100

    Variable types matter because different operations affect different types of variables in different ways. Let's have a look at an example:

    number1 = 100\nnumber2 = \"100\"\n\nprint(number1 + number1)\nprint(number2 + number2)

    This prints out the following:

    200\n100100

    For integer values the + operator means addition, but for string values it means concatenation, or \"stringing together\".

    Not all operators are available for all types of variables. While numbers can be divided using the division operator /, attempting to divide a string by a number causes an error:

    number = \"100\"\nprint(number / 2)
    \nTypeError: unsupported operand type(s) for /: 'str' and 'int'\n

    Combining values when printing

    Similarly, the following program will not work, because \"The result is \" and result are of two different types:

    result = 10 * 25\n# the following line produces an error\nprint(\"The result is \" + result)

    The program does not print out anything, but instead throws an error:

    TypeError: unsupported operand type(s) for +: 'str' and 'int'

    Here, Python tells us that combining two different types of values will not work just like that. In this case, \"The result is \" is of type string, while the value stored in result is of type integer.

    If we do want to print out a string and an integer in a single command, the integer can be cast as a string with the str function, and the two strings can then be combined normally. For example, this would work:

    result = 10 * 25\nprint(\"The result is \" + str(result))

    The result is 250

    The print command also has built-in functionalities that support combining different types of values. The simplest way is to add a comma between the values. All the values will be printed out regardless of their type:

    result = 10 * 25\nprint(\"The result is\", result)

    The result is 250

    Notice that there is an automatically added whitespace character between the values separated by a comma here.

    Printing with f-strings

    What if we want to have more flexibility and control over what we print out? So called f-strings are another way of formatting printouts in Python. The syntax can initially look a bit confusing, but in the end f-strings are often the simplest way of formatting text.

    With f-strings the previous example would look like this:

    result = 10 * 25\nprint(f\"The result is {result}\")

    Let's break this apart. In the very beginning of the string we are printing out there is the character f. This tells Python that what follows is an f-string. Within the string, enclosed in curly brackets, is the variable name result. The value it contains becomes a part of the printed string. The printout is exactly the same as in the previous examples:

    The result is 250

    A single f-string can contain multiple variables. For example this code

    name = \"Mark\"\nage = 37\ncity = \"Palo Alto\"\nprint(f\"Hi {name}, you are {age} years old. You live in {city}.\")

    prints out this:

    Hi Mark, you are 37 years old. You live in Palo Alto.

    It is difficult to create a printout exactly like this using the comma notation in the print command. For example, this program

    name = \"Mark\"\nage = 37\ncity = \"Palo Alto\"\nprint(\"Hi\", name, \", you are\", age, \"years old. You live in\", city, \".\")

    prints out the following:

    Hi Mark , you are 37 years old. You live in Palo Alto .

    Notice the automatically inserted whitespace between each comma-separated part of the printout. Preventing print from adding the extra spaces is technically possible, but not worth the trouble given that we can instead use f-strings.

    In its simplicity the comma notation of the print command can often be useful, but it does sometimes cause more trouble than it's worth. F-strings are usually a more reliable option. In part 4 you will learn more about the handy features of f-strings when it comes to formatting printouts.

    If you are using an older version of Python, f-strings may not work. They were introduced in Python version 3.6. Later on during the course you will install Python on your own computer. Unfortunately, the more modern versions of Python are not always available for older operating systems. If that is the case with your computer, when there are exercises requiring the use of f-strings, you can always try them out in the in-browser exercise templates in these early parts of this course.

    Your friend is working on an app for jobseekers. She sends you this bit of code:

    name = \"Tim Tester\"\nage = 20\nskill1 = \"python\"\nlevel1 = \"beginner\"\nskill2 = \"java\"\nlevel2 = \"veteran\"\nskill3 = \"programming\"\nlevel3 = \"semiprofessional\"\nlower = 2000\nupper = 3000\n\nprint(\"my name is \", name, \" , I am \", age, \"years old\")\nprint(\"my skills are\")\nprint(\"- \", skill1, \" (\", level1, \")\")\nprint(\"- \", skill2, \" (\", level2, \")\")\nprint(\"- \", skill3, \" (\", level3, \" )\")\nprint(\"I am looking for a job with a salary of\", lower, \"-\", upper, \"euros per month\")

    The program should print out exactly the following:

    \nmy name is Tim Tester, I am 20 years old\n\nmy skills are\n - python (beginner)\n - java (veteran)\n - programming (semiprofessional)\n\nI am looking for a job with a salary of 2000-3000 euros per month\n

    The code works almost correctly, but not quite. This exercise has very strict tests, which check the output for every single bit of whitespace.

    Please fix the code so that the printout looks right. Notice especially how the comma notation in the print command automatically inserts a space around the different comma-separated parts.

    The easiest way to transform the code so that it meets requirements is to use f-strings.

    Hint: you can print an empty line by adding an empty print command, or by adding the newline character \\n into your string.

    Do remember to be extra careful when formatting printouts also in the future on this course. Some of the exercises have tests that require your output to be exactly as specified in the examples given. For example, please use actual whitespace characters in your code, instead of ASCII character codes for whitespace, or some such.

    Floating point numbers

    Floating point number or float is a term you will come across often in programming. It refers to numbers with a decimal point. They can be used much in the same way as integer values.

    This program calculates the mean of three floating point numbers:

    number1 = 2.5\nnumber2 = -1.25\nnumber3 = 3.62\n\nmean = (number1 + number2 + number3) / 3\nprint(f\"Mean: {mean}\")

    Mean: 1.6233333333333333

    This program already contains two integer variables, x and y:

    x = 27\ny = 15

    Please complete the program so that it also prints out the following:

    27 + 15 = 42\n27 - 15 = 12\n27 * 15 = 405\n27 / 15 = 1.8

    The program should work correctly even if the values of the variables are changed. That is, if the first two lines are replaced with this

    x = 4\ny = 9

    the program should print out the following:

    4 + 9 = 13\n4 - 9 = -5\n4 * 9 = 36\n4 / 9 = 0.4444444444444444

    Each print command usually prints out a line of its own, complete with a change of line at the end. However, if the print command is given an additional argument end = \"\", it will not print a line change.

    For example:

    print(\"Hi \", end=\"\")\nprint(\"there!\")

    Hi there!

    Please fix this program so that the entire calculation, complete with result, is printed out on a single line. Do not change the number of print commands used.

    \nprint(5)\nprint(\" + \")\nprint(8)\nprint(\" - \")\nprint(4)\nprint(\" = \")\nprint(5 + 8 - 4)
    ","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-1/3-more-about-variables.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 able to use variables in different contexts"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what kind of data can be stored in variables"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will understand the difference between strings, integers and floating point numbers"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please fill in this questionnaire before getting started with this section. You will get one exercise point for answering."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d"},"children":[]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variables are needed for various purposes in programming. You can use variables to store any information that will be needed later in the program's execution."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In Python programming variables are created like so:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"variable_name = ..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"..."}]},{"type":"text","value":" means the value stored in the variable."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, when you used the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" command to read a string from the user, you stored the string in a variable and then used the variable later in your program:"}]},{"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":"name "},{"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":"\"What is your name? \""}]},{"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":"\"Hi, \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" name"},{"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":"What is your name? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Ghosty"}]},{"type":"text","value":"\nHi, Ghosty"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The value stored in a variable can also be defined using other 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":"text","value":"given_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":"\"Paul\""}]},{"type":"text","value":"\nfamily_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":"\"Python\""}]},{"type":"text","value":"\n\nname "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" given_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":"\" \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" family_name\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":"name"},{"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 Python"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here the values stored in the three variables are not obtained from user input. They remain the same every time the program is executed. This is called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"hard-coding"}]},{"type":"text","value":" data into the program."}]},{"type":"element","tagName":"h2","properties":{"id":"changing-the-value-of-a-variable","style":"position:relative;"},"children":[{"type":"text","value":"Changing the value of a variable"},{"type":"element","tagName":"a","properties":{"href":"#changing-the-value-of-a-variable","ariaLabel":"changing the value of a variable 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":"As implied by the name "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"variable"}]},{"type":"text","value":", the value stored in a variable can change. In the previous section we noticed that the new value replaces the old one."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"During the execution of the following program, the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"word"}]},{"type":"text","value":" will have three different 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":"word "},{"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 word: \""}]},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nword "},{"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":"\"And another word: \""}]},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nword "},{"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":"\"third\""}]},{"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":"word"},{"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 word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"first"}]},{"type":"text","value":"\nfirst\nAnd another word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"second"}]},{"type":"text","value":"\nsecond\nthird"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The value stored in the variable changes each time the variable is assigned a new value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The new value of a variable can be derived from its old value. In the following example the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"word"}]},{"type":"text","value":" is first assigned a value based on user input. Then it is assigned a new value, which is the old value with three exclamation marks added to the end."}]},{"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":"word "},{"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 word: \""}]},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nword "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" word "},{"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":"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":"word"},{"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 word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"test"}]},{"type":"text","value":"\ntest\ntest!!!"}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Choosing a good name for a variable"},"children":[{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is often useful to name variables according to what they are used for. For example, if the variable contains a word, the name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"word"}]},{"type":"text","value":" is a better choice than, say, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There is no set limit to the length of a variable name in Python, but there are some other limitations. A variable name should begin with a letter, and it can only contain letters, numbers and underscores _."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lowercase and uppercase letters are different characters. The variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Name"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NAME"}]},{"type":"text","value":" are all different variables. While this rule has a few exceptions, we will ignore those for now."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is a common programming practice in Python to use only lowercase characters in variable names. If the variable name consists of multiple words, use an underscore between the words. While this rule also has a few exceptions, we will ignore those for now."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"integers","style":"position:relative;"},"children":[{"type":"text","value":"Integers"},{"type":"element","tagName":"a","properties":{"href":"#integers","ariaLabel":"integers 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":"Thus far, we have only stored strings in variables, but there are also many other types of information we will want to store and access later. Let's have a look at integers first. Integers are numbers that do not have a decimal or fractional part, such as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-15"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program creates the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"age"}]},{"type":"text","value":", which contains an integer value."}]},{"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":"age "},{"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":"24"}]},{"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":"age"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program prints out just this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"24"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice the lack of quotation marks here. In fact, if we were to add quotation marks around the number, this would mean our variable would no longer be an integer, but a string instead. A string can contain numbers, but it is processed differently."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, why does it matter that variables have a type, when the following program still prints out the same thing twice?"}]},{"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":"number1 "},{"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":"100"}]},{"type":"text","value":"\nnumber2 "},{"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":"\"100\""}]},{"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":"number1"},{"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":"number2"},{"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":"100\n100"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Variable types matter because different operations affect different types of variables in different ways. Let's have a look at an example:"}]},{"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":"number1 "},{"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":"100"}]},{"type":"text","value":"\nnumber2 "},{"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":"\"100\""}]},{"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":"number1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number1"},{"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":"number2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"200\n100100"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For integer values the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" operator means addition, but for string values it means concatenation, or \"stringing together\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Not all operators are available for all types of variables. While numbers can be divided using the division operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":", attempting to divide a string by a number causes an error:"}]},{"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","string"]},"children":[{"type":"text","value":"\"100\""}]},{"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","operator"]},"children":[{"type":"text","value":"/"}]},{"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":"sample-output","properties":{},"children":[{"type":"text","value":"\nTypeError: unsupported operand type(s) for /: 'str' and 'int'\n"}]},{"type":"element","tagName":"h2","properties":{"id":"combining-values-when-printing","style":"position:relative;"},"children":[{"type":"text","value":"Combining values when printing"},{"type":"element","tagName":"a","properties":{"href":"#combining-values-when-printing","ariaLabel":"combining values when printing 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":"Similarly, the following program will not work, because "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"The result is \""}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"result"}]},{"type":"text","value":" are of two different types:"}]},{"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":"result "},{"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":"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":"25"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the following line produces an error"}]},{"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 result is \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" result"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program does not print out anything, but instead throws an error:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TypeError: unsupported operand type(s) for +: 'str' and 'int'"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here, Python tells us that combining two different types of values will not work just like that. In this case, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"The result is \""}]},{"type":"text","value":" is of type string, while the value stored in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"result"}]},{"type":"text","value":" is of type integer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we do want to print out a string and an integer in a single command, the integer can be cast as a string with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":" function, and the two strings can then be combined normally. For example, this would 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":"result "},{"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":"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":"25"}]},{"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 result is \""}]},{"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":"str"}]},{"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":"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":"The result is 250"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command also has built-in functionalities that support combining different types of values. The simplest way is to add a comma between the values. All the values will be printed out regardless of their type:"}]},{"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":"result "},{"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":"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":"25"}]},{"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 result is\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The result is 250"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice that there is an automatically added whitespace character between the values separated by a comma here."}]},{"type":"element","tagName":"h2","properties":{"id":"printing-with-f-strings","style":"position:relative;"},"children":[{"type":"text","value":"Printing with f-strings"},{"type":"element","tagName":"a","properties":{"href":"#printing-with-f-strings","ariaLabel":"printing with f 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":"What if we want to have more flexibility and control over what we print out? So called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"f-strings"}]},{"type":"text","value":" are another way of formatting printouts in Python. The syntax can initially look a bit confusing, but in the end f-strings are often the simplest way of formatting text."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With f-strings the previous example would 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":"text","value":"result "},{"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":"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":"25"}]},{"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\"The result 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":"result"},{"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":"p","properties":{},"children":[{"type":"text","value":"Let's break this apart. In the very beginning of the string we are printing out there is the character "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"f"}]},{"type":"text","value":". This tells Python that what follows is an f-string. Within the string, enclosed in curly brackets, is the variable name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"result"}]},{"type":"text","value":". The value it contains becomes a part of the printed string. The printout is exactly the same as in the previous examples:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The result is 250"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A single f-string can contain multiple variables. For example this code"}]},{"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":"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":"\"Mark\""}]},{"type":"text","value":"\nage "},{"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":"37"}]},{"type":"text","value":"\ncity "},{"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":"\"Palo Alto\""}]},{"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\"Hi "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", you are "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"age"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" years old. You live 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":"city"},{"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":"p","properties":{},"children":[{"type":"text","value":"prints out this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hi Mark, you are 37 years old. You live in Palo Alto."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is difficult to create a printout exactly like this using the comma notation in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command. For example, this program"}]},{"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":"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":"\"Mark\""}]},{"type":"text","value":"\nage "},{"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":"37"}]},{"type":"text","value":"\ncity "},{"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":"\"Palo Alto\""}]},{"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":"\"Hi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"\", you are\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" age"},{"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":"\"years old. You live in\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" city"},{"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":"\".\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hi Mark , you are 37 years old. You live in Palo Alto ."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice the automatically inserted whitespace between each comma-separated part of the printout. Preventing "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" from adding the extra spaces is technically possible, but not worth the trouble given that we can instead use f-strings."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In its simplicity the comma notation of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command can often be useful, but it does sometimes cause more trouble than it's worth. F-strings are usually a more reliable option. In part 4 you will learn more about the handy features of f-strings when it comes to formatting printouts."}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"F-strings and Python versions"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you are using an older version of Python, f-strings may not work. They were introduced in Python version 3.6. Later on during the course you will install Python on your own computer. Unfortunately, the more modern versions of Python are not always available for older operating systems. If that is the case with your computer, when there are exercises requiring the use of f-strings, you can always try them out in the in-browser exercise templates in these early parts of this course."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Extra space","tmcname":"part01-10b_extra_space","height":"400px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your friend is working on an app for jobseekers. She sends you this bit of code:"}]},{"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":"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":"\"Tim Tester\""}]},{"type":"text","value":"\nage "},{"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":"20"}]},{"type":"text","value":"\nskill1 "},{"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\""}]},{"type":"text","value":"\nlevel1 "},{"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":"\"beginner\""}]},{"type":"text","value":"\nskill2 "},{"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":"\"java\""}]},{"type":"text","value":"\nlevel2 "},{"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":"\"veteran\""}]},{"type":"text","value":"\nskill3 "},{"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":"\"programming\""}]},{"type":"text","value":"\nlevel3 "},{"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":"\"semiprofessional\""}]},{"type":"text","value":"\nlower "},{"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":"2000"}]},{"type":"text","value":"\nupper "},{"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":"3000"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"my name is \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"\" , I am \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" age"},{"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":"\"years old\""}]},{"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":"\"my skills 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":"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":"\"- \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" skill1"},{"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":"\" (\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" level1"},{"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":"\")\""}]},{"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":"\"- \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" skill2"},{"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":"\" (\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" level2"},{"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":"\")\""}]},{"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":"\"- \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" skill3"},{"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":"\" (\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" level3"},{"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":"\" )\""}]},{"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":"\"I am looking for a job with a salary of\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"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":"\"-\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"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":"\"euros per month\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should print out "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"exactly"}]},{"type":"text","value":" the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"my name is Tim Tester, I am 20 years old\n\nmy skills are\n - python (beginner)\n - java (veteran)\n - programming (semiprofessional)\n\nI am looking for a job with a salary of 2000-3000 euros per month\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The code works almost correctly, but not quite. This exercise has very strict tests, which check the output for every single bit of whitespace."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please fix the code so that the printout looks right. Notice especially how the comma notation in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command automatically inserts a space around the different comma-separated parts."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The easiest way to transform the code so that it meets requirements is to use f-strings."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hint: you can print an empty line by adding an empty "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command, or by adding the newline character "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\\n"}]},{"type":"text","value":" into your string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Do remember to be extra careful when formatting printouts also in the future on this course. Some of the exercises have tests that require your output to be exactly as specified in the examples given. For example, please use actual whitespace characters in your code, instead of ASCII character codes for whitespace, or some such."}]}]},{"type":"element","tagName":"h2","properties":{"id":"floating-point-numbers","style":"position:relative;"},"children":[{"type":"text","value":"Floating point numbers"},{"type":"element","tagName":"a","properties":{"href":"#floating-point-numbers","ariaLabel":"floating point numbers 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":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Floating point number"}]},{"type":"text","value":" or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"float"}]},{"type":"text","value":" is a term you will come across often in programming. It refers to numbers with a decimal point. They can be used much in the same way as integer values."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program calculates the mean of three floating point numbers:"}]},{"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":"number1 "},{"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":"2.5"}]},{"type":"text","value":"\nnumber2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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.25"}]},{"type":"text","value":"\nnumber3 "},{"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.62"}]},{"type":"text","value":"\n\nmean "},{"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":"text","value":"number1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number3"},{"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":"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\"Mean: "}]},{"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":"\""}]}]},{"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":"Mean: 1.6233333333333333"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Arithmetics","tmcname":"part01-11_arithmetics"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program already contains two integer variables, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":":"}]},{"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":"x "},{"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":"27"}]},{"type":"text","value":"\ny "},{"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":"15"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please complete the program so that it also prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"27 + 15 = 42\n27 - 15 = 12\n27 * 15 = 405\n27 / 15 = 1.8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should work correctly even if the values of the variables are changed. That is, if the first two lines are replaced with 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":"text","value":"x "},{"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":"4"}]},{"type":"text","value":"\ny "},{"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":"9"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"the program should print out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4 + 9 = 13\n4 - 9 = -5\n4 * 9 = 36\n4 / 9 = 0.4444444444444444"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fix the code: Print a single line","tmcname":"part01-12_print_a_single_line"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command usually prints out a line of its own, complete with a change of line at the end. However, if the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command is given an additional argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"end = \"\""}]},{"type":"text","value":", it will not print a line change."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"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":"\"Hi \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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"},{"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":"\"there!\""}]},{"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":"Hi there!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please fix this program so that the entire calculation, complete with result, is printed out on a single line. Do not change the number of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" commands used."}]},{"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":"\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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"\" + \""}]},{"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","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":"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"},{"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","number"]},"children":[{"type":"text","value":"4"}]},{"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":"\" = \""}]},{"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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"8"}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]}]},{"type":"comment","value":"\n\nA quiz to review the contents of this section:\n\n\n\n"}]},"html":"

    After this section

    Please fill in this questionnaire before getting started with this section. You will get one exercise point for answering.

    Variables are needed for various purposes in programming. You can use variables to store any information that will be needed later in the program's execution.

    In Python programming variables are created like so:

    variable_name = ...

    Here ... means the value stored in the variable.

    For example, when you used the input command to read a string from the user, you stored the string in a variable and then used the variable later in your program:

    name = input(\"What is your name? \")\nprint(\"Hi, \" + name)

    What is your name? Ghosty\nHi, Ghosty

    The value stored in a variable can also be defined using other variables:

    given_name = \"Paul\"\nfamily_name = \"Python\"\n\nname = given_name + \" \" + family_name\n\nprint(name)

    Paul Python

    Here the values stored in the three variables are not obtained from user input. They remain the same every time the program is executed. This is called hard-coding data into the program.

    Changing the value of a variable

    As implied by the name variable, the value stored in a variable can change. In the previous section we noticed that the new value replaces the old one.

    During the execution of the following program, the variable word will have three different values:

    word = input(\"Please type in a word: \")\nprint(word)\n\nword = input(\"And another word: \")\nprint(word)\n\nword = \"third\"\nprint(word)

    Please type in a word: first\nfirst\nAnd another word: second\nsecond\nthird

    The value stored in the variable changes each time the variable is assigned a new value.

    The new value of a variable can be derived from its old value. In the following example the variable word is first assigned a value based on user input. Then it is assigned a new value, which is the old value with three exclamation marks added to the end.

    word = input(\"Please type in a word: \")\nprint(word)\n\nword = word + \"!!!\"\nprint(word)

    Please type in a word: test\ntest\ntest!!!

    Integers

    Thus far, we have only stored strings in variables, but there are also many other types of information we will want to store and access later. Let's have a look at integers first. Integers are numbers that do not have a decimal or fractional part, such as -15, 0 and 1.

    The following program creates the variable age, which contains an integer value.

    age = 24\nprint(age)

    The program prints out just this:

    24

    Notice the lack of quotation marks here. In fact, if we were to add quotation marks around the number, this would mean our variable would no longer be an integer, but a string instead. A string can contain numbers, but it is processed differently.

    So, why does it matter that variables have a type, when the following program still prints out the same thing twice?

    number1 = 100\nnumber2 = \"100\"\n\nprint(number1)\nprint(number2)

    100\n100

    Variable types matter because different operations affect different types of variables in different ways. Let's have a look at an example:

    number1 = 100\nnumber2 = \"100\"\n\nprint(number1 + number1)\nprint(number2 + number2)

    This prints out the following:

    200\n100100

    For integer values the + operator means addition, but for string values it means concatenation, or \"stringing together\".

    Not all operators are available for all types of variables. While numbers can be divided using the division operator /, attempting to divide a string by a number causes an error:

    number = \"100\"\nprint(number / 2)
    \nTypeError: unsupported operand type(s) for /: 'str' and 'int'\n

    Combining values when printing

    Similarly, the following program will not work, because \"The result is \" and result are of two different types:

    result = 10 * 25\n# the following line produces an error\nprint(\"The result is \" + result)

    The program does not print out anything, but instead throws an error:

    TypeError: unsupported operand type(s) for +: 'str' and 'int'

    Here, Python tells us that combining two different types of values will not work just like that. In this case, \"The result is \" is of type string, while the value stored in result is of type integer.

    If we do want to print out a string and an integer in a single command, the integer can be cast as a string with the str function, and the two strings can then be combined normally. For example, this would work:

    result = 10 * 25\nprint(\"The result is \" + str(result))

    The result is 250

    The print command also has built-in functionalities that support combining different types of values. The simplest way is to add a comma between the values. All the values will be printed out regardless of their type:

    result = 10 * 25\nprint(\"The result is\", result)

    The result is 250

    Notice that there is an automatically added whitespace character between the values separated by a comma here.

    Printing with f-strings

    What if we want to have more flexibility and control over what we print out? So called f-strings are another way of formatting printouts in Python. The syntax can initially look a bit confusing, but in the end f-strings are often the simplest way of formatting text.

    With f-strings the previous example would look like this:

    result = 10 * 25\nprint(f\"The result is {result}\")

    Let's break this apart. In the very beginning of the string we are printing out there is the character f. This tells Python that what follows is an f-string. Within the string, enclosed in curly brackets, is the variable name result. The value it contains becomes a part of the printed string. The printout is exactly the same as in the previous examples:

    The result is 250

    A single f-string can contain multiple variables. For example this code

    name = \"Mark\"\nage = 37\ncity = \"Palo Alto\"\nprint(f\"Hi {name}, you are {age} years old. You live in {city}.\")

    prints out this:

    Hi Mark, you are 37 years old. You live in Palo Alto.

    It is difficult to create a printout exactly like this using the comma notation in the print command. For example, this program

    name = \"Mark\"\nage = 37\ncity = \"Palo Alto\"\nprint(\"Hi\", name, \", you are\", age, \"years old. You live in\", city, \".\")

    prints out the following:

    Hi Mark , you are 37 years old. You live in Palo Alto .

    Notice the automatically inserted whitespace between each comma-separated part of the printout. Preventing print from adding the extra spaces is technically possible, but not worth the trouble given that we can instead use f-strings.

    In its simplicity the comma notation of the print command can often be useful, but it does sometimes cause more trouble than it's worth. F-strings are usually a more reliable option. In part 4 you will learn more about the handy features of f-strings when it comes to formatting printouts.

    If you are using an older version of Python, f-strings may not work. They were introduced in Python version 3.6. Later on during the course you will install Python on your own computer. Unfortunately, the more modern versions of Python are not always available for older operating systems. If that is the case with your computer, when there are exercises requiring the use of f-strings, you can always try them out in the in-browser exercise templates in these early parts of this course.

    Your friend is working on an app for jobseekers. She sends you this bit of code:

    name = \"Tim Tester\"\nage = 20\nskill1 = \"python\"\nlevel1 = \"beginner\"\nskill2 = \"java\"\nlevel2 = \"veteran\"\nskill3 = \"programming\"\nlevel3 = \"semiprofessional\"\nlower = 2000\nupper = 3000\n\nprint(\"my name is \", name, \" , I am \", age, \"years old\")\nprint(\"my skills are\")\nprint(\"- \", skill1, \" (\", level1, \")\")\nprint(\"- \", skill2, \" (\", level2, \")\")\nprint(\"- \", skill3, \" (\", level3, \" )\")\nprint(\"I am looking for a job with a salary of\", lower, \"-\", upper, \"euros per month\")

    The program should print out exactly the following:

    \nmy name is Tim Tester, I am 20 years old\n\nmy skills are\n - python (beginner)\n - java (veteran)\n - programming (semiprofessional)\n\nI am looking for a job with a salary of 2000-3000 euros per month\n

    The code works almost correctly, but not quite. This exercise has very strict tests, which check the output for every single bit of whitespace.

    Please fix the code so that the printout looks right. Notice especially how the comma notation in the print command automatically inserts a space around the different comma-separated parts.

    The easiest way to transform the code so that it meets requirements is to use f-strings.

    Hint: you can print an empty line by adding an empty print command, or by adding the newline character \\n into your string.

    Do remember to be extra careful when formatting printouts also in the future on this course. Some of the exercises have tests that require your output to be exactly as specified in the examples given. For example, please use actual whitespace characters in your code, instead of ASCII character codes for whitespace, or some such.

    Floating point numbers

    Floating point number or float is a term you will come across often in programming. It refers to numbers with a decimal point. They can be used much in the same way as integer values.

    This program calculates the mean of three floating point numbers:

    number1 = 2.5\nnumber2 = -1.25\nnumber3 = 3.62\n\nmean = (number1 + number2 + number3) / 3\nprint(f\"Mean: {mean}\")

    Mean: 1.6233333333333333

    This program already contains two integer variables, x and y:

    x = 27\ny = 15

    Please complete the program so that it also prints out the following:

    27 + 15 = 42\n27 - 15 = 12\n27 * 15 = 405\n27 / 15 = 1.8

    The program should work correctly even if the values of the variables are changed. That is, if the first two lines are replaced with this

    x = 4\ny = 9

    the program should print out the following:

    4 + 9 = 13\n4 - 9 = -5\n4 * 9 = 36\n4 / 9 = 0.4444444444444444

    Each print command usually prints out a line of its own, complete with a change of line at the end. However, if the print command is given an additional argument end = \"\", it will not print a line change.

    For example:

    print(\"Hi \", end=\"\")\nprint(\"there!\")

    Hi there!

    Please fix this program so that the entire calculation, complete with result, is printed out on a single line. Do not change the number of print commands used.

    \nprint(5)\nprint(\" + \")\nprint(8)\nprint(\" - \")\nprint(4)\nprint(\" = \")\nprint(5 + 8 - 4)
    ","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-1/3-more-about-variables.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-1/4-arithmetic-operations/page-data.json b/page-data/part-1/4-arithmetic-operations/page-data.json index ce37a2ed9..82374644e 100644 --- a/page-data/part-1/4-arithmetic-operations/page-data.json +++ b/page-data/part-1/4-arithmetic-operations/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-1/4-arithmetic-operations", - "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 able to use variables in various arithmetic operations"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to deal with numbers in user input"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to cast values into other fundamental data types"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the previous sections you've seen examples with basic arithmetics. In the following table you can see the most common arithmetic operators in Python, with examples:"}]},{"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\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":"Operator"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Purpose"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Example"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Result"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Addition"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 + 4"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"6"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Subtraction"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"10 - 2.5"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"7.5"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Multiplication"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-2 * 123"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-246"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"/"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Division (floating point result)"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"9 / 2"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"4.5"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"//"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Division (integer result)"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"9 // 2"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"4"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Modulo"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"9 % 2"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"**"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Exponentiation"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 ** 3"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"8"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The order of operations is familiar from mathematics: first calculate the exponents, then multiplication and division, and finally addition and subtraction. The order can be changed with parentheses."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example this bit of code"}]},{"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","number"]},"children":[{"type":"text","value":"2"}]},{"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":" "},{"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":"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"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":"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"prints out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"11\n15"}]}]},{"type":"element","tagName":"h2","properties":{"id":"operands-operators-and-data-types","style":"position:relative;"},"children":[{"type":"text","value":"Operands, operators and data types"},{"type":"element","tagName":"a","properties":{"href":"#operands-operators-and-data-types","ariaLabel":"operands operators and data types 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":"A calculation usually consists of "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"operands"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"operators"}]},{"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: 431px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/f717f6a68e7b1bd3b3fb9cbe2898da5b/9cb4e/1_4_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: 62.60869565217391%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB1UlEQVQ4y2VTia6iQBDk/39tY57R9eApXoCAHHILD5jaVI/jstlJKjP2dHdVTaMFAEpBVt8DYQgUBfB8AnmuYc5mj2OgaXSNqTXLmgdfLw0Tc13A9wHPA4IAGEd9N01AWf7NM/XcrTnDz49WMAxabV1rUA2JGOMdY237rzLTx1JKCSMTXi+FNFVIEoUogpyvV40k0VbjWOe07ST5JGI9+xBiuSwV7nclTY0Cx1GoKqDrtEI2zTIlLoxa3gWBJvi8oW3vhYFWx7HD19cCwzDKANq2lt/AKI24zmcHTVNhsfiFokiEjM0Z930f1vl8Ev985L7vsF7/xjRpxq7rsVqtMQyDqOE6HA6IogeWyyWKIv9M+3Q6IY5jWJfLxTwrxnGUAjZQ71f2PA9Zlr2nO8n+/W1Ljh6GjlFdFEWwjsejHIIgRBiG2O12cF33k7BarbDdbhGGkcTu9zv2+73kMJ91zKOwqqr0UKiM7ERZlkiSRJDnOWzbFpKiKMQSwbPJJ1hvHFnzb6lpGtxuN2FiM7M/n09pQjAWBIHEtGU1+xaV/g5NkOpYZBbtbjYbscjz+P6r8P0ej8enyRzWnKWuaziOI0Ogtev1Km9FcDhswju6SNP0P4W0/Ae4n+ixOVvxjQAAAABJRU5ErkJggg=='); 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":["/static/f717f6a68e7b1bd3b3fb9cbe2898da5b/a0b58/1_4_1.webp 230w","/static/f717f6a68e7b1bd3b3fb9cbe2898da5b/01668/1_4_1.webp 431w"],"sizes":"(max-width: 431px) 100vw, 431px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/f717f6a68e7b1bd3b3fb9cbe2898da5b/81c8e/1_4_1.png 230w","/static/f717f6a68e7b1bd3b3fb9cbe2898da5b/9cb4e/1_4_1.png 431w"],"sizes":"(max-width: 431px) 100vw, 431px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/f717f6a68e7b1bd3b3fb9cbe2898da5b/9cb4e/1_4_1.png","alt":"1 4 1","title":"1 4 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":"p","properties":{},"children":[{"type":"text","value":"The data type of an operand usually determines the data type of the result: if two integers are added together, the result will also be an integer. If a floating point number is subtracted from another floating point number, the result is a floating point number. In fact, if a single one of the operands in an expression is a floating point number, the result will also be a floating point number, regardless of the other operands."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Division "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" is an exception to this rule. Its result is a floating point number, even if the operands are integers. For example "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1 / 5"}]},{"type":"text","value":" will result in the floating point number "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0.2"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Example:"}]},{"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":"height "},{"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":"172.5"}]},{"type":"text","value":"\nweight "},{"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":"68.55"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the Body Mass Index, or BMI, is calculated by dividing body mass with the square of height"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# height is converted into metres in the formula"}]},{"type":"text","value":"\nbmi "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" weight "},{"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":"text","value":"height "},{"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":"100"}]},{"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":"2"}]},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"The BMI 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":"bmi"},{"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":"p","properties":{},"children":[{"type":"text","value":"This program prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The BMI is 23.037177063642087"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice Python also has an integer division operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"//"}]},{"type":"text","value":". If the operands are integers, it will produce an integer. The result is rounded down to the nearest integer. For example this program"}]},{"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":"x "},{"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":"\ny "},{"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":"2"}]},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"/ operator "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"y"},{"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"},{"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\"// operator "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"//"}]},{"type":"text","value":"y"},{"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":"p","properties":{},"children":[{"type":"text","value":"prints out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"/ operator 1.5\n// operator 1"}]}]},{"type":"element","tagName":"h2","properties":{"id":"numbers-as-input","style":"position:relative;"},"children":[{"type":"text","value":"Numbers as input"},{"type":"element","tagName":"a","properties":{"href":"#numbers-as-input","ariaLabel":"numbers as input 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 have already used the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" command to read in strings from the user. The same function can be used to read in numbers, but the string produced by the function must then be converted to a numeric data type in the program code. In the previous section we cast integers as strings with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":" function. The same basic principle applies here, but the name of the casting function will be different."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A string can be converted into an integer with the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":". The following program asks the user for their year of birth and stores it in the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input_str"}]},{"type":"text","value":". The program then creates another variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"year"}]},{"type":"text","value":", which contains the year converted into an integer. After this the calculation "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2021-year"}]},{"type":"text","value":" is possible, using the user-supplied value."}]},{"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":"input_str "},{"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":"\"Which year were you born? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nyear "},{"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":"text","value":"input_str"},{"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\"Your age at the end of the year 2021: "}]},{"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","number"]},"children":[{"type":"text","value":"2021"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" 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":"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":"Which year were you born? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1995"}]},{"type":"text","value":"\nYour age at the end of the year 2021: 26"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Usually you do not need to create two separate variables (like "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input_str"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"year"}]},{"type":"text","value":" above) to read a number value from the user. Instead, reading the input with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" function and converting it with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":" function can be achieved in one go:"}]},{"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":"year "},{"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"]},"children":[{"type":"text","value":"\"Which year were you born? \""}]},{"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\"Your age at the end of the year 2021: "}]},{"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","number"]},"children":[{"type":"text","value":"2021"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" 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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Similarly, a string can be converted into a floating point number with the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]},{"type":"text","value":". This programs asks the user for their height and weight, and uses these to calculate their BMI:"}]},{"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":"height "},{"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":"float"}]},{"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"]},"children":[{"type":"text","value":"\"What is your height? \""}]},{"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":"\nweight "},{"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":"float"}]},{"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"]},"children":[{"type":"text","value":"\"What is your weight? \""}]},{"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\nheight "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" height "},{"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":"100"}]},{"type":"text","value":"\nbmi "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" weight "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" height "},{"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":"2"}]},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"The BMI 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":"bmi"},{"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":"p","properties":{},"children":[{"type":"text","value":"An example printout from the program:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your height? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"163"}]},{"type":"text","value":"\nWhat is your weight? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"74.45"}]},{"type":"text","value":"\nThe BMI is 28.02137829801649"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Times five","tmcname":"part01-13_times_five"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a number. The program then prints out the number multiplied by five."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should function as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n3 times 5 is 15"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Name and age","tmcname":"part01-14_name_and_age"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for their name and year of birth. The program then prints out a message as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your name? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Frances Fictitious"}]},{"type":"text","value":"\nWhich year were you born? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1990"}]},{"type":"text","value":"\nHi Frances Fictitious, you will be 31 years old at the end of the year 2021"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"using-variables","style":"position:relative;"},"children":[{"type":"text","value":"Using variables"},{"type":"element","tagName":"a","properties":{"href":"#using-variables","ariaLabel":"using variables 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":"Let's have a look at a program which calculates the sum of three numbers given by the user:"}]},{"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":"number1 "},{"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"]},"children":[{"type":"text","value":"\"First number: \""}]},{"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":"\nnumber2 "},{"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"]},"children":[{"type":"text","value":"\"Second number: \""}]},{"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":"\nnumber3 "},{"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"]},"children":[{"type":"text","value":"\"Third number: \""}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" number1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number3\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\"The sum of the numbers: "}]},{"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":"sum"}]},{"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":"p","properties":{},"children":[{"type":"text","value":"An example execution of the program:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nSecond number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"21"}]},{"type":"text","value":"\nThird number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nThe sum of the numbers: 33"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program uses four different variables, but two would easily suffice in this case:"}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"0"}]},{"type":"text","value":"\n\nnumber "},{"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"]},"children":[{"type":"text","value":"\"First number: \""}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number\n\nnumber "},{"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"]},"children":[{"type":"text","value":"\"Second number: \""}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number\n\nnumber "},{"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"]},"children":[{"type":"text","value":"\"Third number: \""}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number\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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"The sum of the numbers: "}]},{"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":"sum"}]},{"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":"p","properties":{},"children":[{"type":"text","value":"Now all inputs from the user are read into the one and the same variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":". The value of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"increased"}]},{"type":"text","value":" by the value of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" each time the user inputs a new number."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's take a closer look at this command:"}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here, the value of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" and the value of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" are added together, and the result is stored back in the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":". For example, if before the command the value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" is 3 and the value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" is 2, after the command is executed, the value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" is 5."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Increasing the value of a variable is a very common operation. As such, there is a commonly used shorthand notation which achieves the same result as the explicit summing up above:"}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" number"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This allows us to write the above program a little more concisely:"}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"0"}]},{"type":"text","value":"\n\nnumber "},{"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"]},"children":[{"type":"text","value":"\"First number: \""}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" number\n\nnumber "},{"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"]},"children":[{"type":"text","value":"\"Second number: \""}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" number\n\nnumber "},{"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"]},"children":[{"type":"text","value":"\"Third number: \""}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" number\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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"The sum of the numbers: "}]},{"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":"sum"}]},{"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":"p","properties":{},"children":[{"type":"text","value":"In fact, we don't necessarily need the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" at all. The inputs from the user can also be processed 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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"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"]},"children":[{"type":"text","value":"\"First number: \""}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"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"]},"children":[{"type":"text","value":"\"Second number: \""}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"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"]},"children":[{"type":"text","value":"\"Third number: \""}]},{"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":"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\"The sum of the numbers: "}]},{"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":"sum"}]},{"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":"p","properties":{},"children":[{"type":"text","value":"Of course, it will depend on the context how many variables are needed. If it is required to remember each value the user inputs, it will not be possible to \"reuse\" the same variable to read different values from the user. Consider the following:"}]},{"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":"number1 "},{"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"]},"children":[{"type":"text","value":"\"First number: \""}]},{"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":"\nnumber2 "},{"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"]},"children":[{"type":"text","value":"\"Second number: \""}]},{"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":"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":"number1"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"number2"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"number1"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"number2"},{"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":"First number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nSecond number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n2 + 3 = 5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"On the other hand, the above program does not have a named variable for storing the sum of the two values."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"\"Reusing\" a variable only makes sense when there is a need to temporarily store things of a similar type and purpose, for example when summing numbers."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"data"}]},{"type":"text","value":" is used to first store the name of the user, and then their age. This is not at all sensible."}]},{"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":"data "},{"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":"\"What is your name? \""}]},{"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":"\"Hi \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" data "},{"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":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ndata "},{"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"]},"children":[{"type":"text","value":"\"What is your age? \""}]},{"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":"# program continues..."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A better idea is to use separate variables, with "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"descriptive"}]},{"type":"text","value":" names:"}]},{"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":"name "},{"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":"\"What is your name? \""}]},{"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":"\"Hi \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"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":"\"!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nage "},{"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"]},"children":[{"type":"text","value":"\"What is your age? \""}]},{"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":"# program continues..."}]}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Seconds in a day","tmcname":"part01-15_seconds_in_a_day"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a number of days. The program then prints out the number of seconds in the amount of days given."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should function as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many days? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nSeconds in that many days: 86400"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Another example:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many days? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nSeconds in that many days: 604800"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fix the code: Product","tmcname":"part01-16_product"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program asks the user for three numbers. The program then prints out their product, that is, the numbers multiplied by each other. There is, however, something wrong with the program - it doesn't work quite right, as you can see if you run it. Please fix it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the expected execution of the program:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in the first number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nPlease type in the second number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nPlease type in the third number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nThe product is 30"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Sum and product","tmcname":"part01-17_sum_and_product"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for two numbers. The program will then print out the sum and the product of the two numbers."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should function as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Number 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nNumber 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nThe sum of the numbers: 10\nThe product of the numbers: 21"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Sum and mean","tmcname":"part01-18_sum_and_mean"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for four numbers. The program then prints out the sum and the mean of the numbers."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should function as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Number 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nNumber 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nNumber 3: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nNumber 4: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nThe sum of the numbers is 16 and the mean is 4.0"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Food expenditure","tmcname":"part01-19_food_expenditure"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which estimates a user's typical food expenditure."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program asks the user how many times a week they eat at the student cafeteria. Then it asks for the price of a typical student lunch, and for money spent on groceries during the week."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Based on this information the program calculates the user's typical food expenditure both weekly and daily."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should function as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many times a week do you eat at the student cafeteria? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nThe price of a typical student lunch? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2.5"}]},{"type":"text","value":"\nHow much money do you spend on groceries in a week? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"28.5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Average food expenditure:\nDaily: 5.5 euros\nWeekly: 38.5 euros"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Students in groups","tmcname":"part01-20_students_in_groups"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for the number of students on a course and the desired group size. The program will then print out the number of groups formed from the students on the course. If the division is not even, one of the groups may have fewer members than specified."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you can't get your code working as expected, it is absolutely okay to move on and come back to this exercise later. The topic of the next section is "},{"type":"element","tagName":"a","properties":{"href":"/part-1/5-conditional-statements"},"children":[{"type":"text","value":"conditional statements"}]},{"type":"text","value":". This exercise can also be solved using a conditional construction."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many students on the course? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\nDesired group size? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nNumber of groups formed: 2"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many students on the course? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"11"}]},{"type":"text","value":"\nDesired group size? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nNumber of groups formed: 4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hint: the integer division operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"//"}]},{"type":"text","value":" could come in handy here."}]}]},{"type":"comment","value":"\n\nA quiz to review the contents of this section:\n\n\n\n"}]},"html":"

    After this section

    In the previous sections you've seen examples with basic arithmetics. In the following table you can see the most common arithmetic operators in Python, with examples:

    \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\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
    OperatorPurposeExampleResult
    +Addition2 + 46
    -Subtraction10 - 2.57.5
    *Multiplication-2 * 123-246
    /Division (floating point result)9 / 24.5
    //Division (integer result)9 // 24
    %Modulo9 % 21
    **Exponentiation2 ** 38

    The order of operations is familiar from mathematics: first calculate the exponents, then multiplication and division, and finally addition and subtraction. The order can be changed with parentheses.

    For example this bit of code

    print(2 + 3 * 3)\nprint((2 + 3) * 3)

    prints out

    11\n15

    Operands, operators and data types

    A calculation usually consists of operands and operators:

    \n \n \n \n \n \n \"1\n \n \n

    The data type of an operand usually determines the data type of the result: if two integers are added together, the result will also be an integer. If a floating point number is subtracted from another floating point number, the result is a floating point number. In fact, if a single one of the operands in an expression is a floating point number, the result will also be a floating point number, regardless of the other operands.

    Division / is an exception to this rule. Its result is a floating point number, even if the operands are integers. For example 1 / 5 will result in the floating point number 0.2.

    Example:

    height = 172.5\nweight = 68.55\n\n# the Body Mass Index, or BMI, is calculated by dividing body mass with the square of height\n# height is converted into metres in the formula\nbmi = weight / (height / 100) ** 2\n\nprint(f\"The BMI is {bmi}\")

    This program prints out the following:

    The BMI is 23.037177063642087

    Notice Python also has an integer division operator //. If the operands are integers, it will produce an integer. The result is rounded down to the nearest integer. For example this program

    x = 3\ny = 2\n\nprint(f\"/ operator {x/y}\")\nprint(f\"// operator {x//y}\")

    prints out

    / operator 1.5\n// operator 1

    Numbers as input

    We have already used the input command to read in strings from the user. The same function can be used to read in numbers, but the string produced by the function must then be converted to a numeric data type in the program code. In the previous section we cast integers as strings with the str function. The same basic principle applies here, but the name of the casting function will be different.

    A string can be converted into an integer with the function int. The following program asks the user for their year of birth and stores it in the variable input_str. The program then creates another variable year, which contains the year converted into an integer. After this the calculation 2021-year is possible, using the user-supplied value.

    input_str = input(\"Which year were you born? \")\nyear = int(input_str)\nprint(f\"Your age at the end of the year 2021: {2021 - year}\" )

    Which year were you born? 1995\nYour age at the end of the year 2021: 26

    Usually you do not need to create two separate variables (like input_str and year above) to read a number value from the user. Instead, reading the input with the input function and converting it with the int function can be achieved in one go:

    year = int(input(\"Which year were you born? \"))\nprint(f\"Your age at the end of the year 2021: {2021 - year}\" )

    Similarly, a string can be converted into a floating point number with the function float. This programs asks the user for their height and weight, and uses these to calculate their BMI:

    height = float(input(\"What is your height? \"))\nweight = float(input(\"What is your weight? \"))\n\nheight = height / 100\nbmi = weight / height ** 2\n\nprint(f\"The BMI is {bmi}\")

    An example printout from the program:

    What is your height? 163\nWhat is your weight? 74.45\nThe BMI is 28.02137829801649

    Please write a program which asks the user for a number. The program then prints out the number multiplied by five.

    The program should function as follows:

    Please type in a number: 3\n3 times 5 is 15

    Please write a program which asks the user for their name and year of birth. The program then prints out a message as follows:

    What is your name? Frances Fictitious\nWhich year were you born? 1990\nHi Frances Fictitious, you will be 31 years old at the end of the year 2021

    Using variables

    Let's have a look at a program which calculates the sum of three numbers given by the user:

    number1 = int(input(\"First number: \"))\nnumber2 = int(input(\"Second number: \"))\nnumber3 = int(input(\"Third number: \"))\n\nsum = number1 + number2 + number3\nprint(f\"The sum of the numbers: {sum}\")

    An example execution of the program:

    First number: 5\nSecond number: 21\nThird number: 7\nThe sum of the numbers: 33

    The program uses four different variables, but two would easily suffice in this case:

    sum = 0\n\nnumber = int(input(\"First number: \"))\nsum = sum + number\n\nnumber = int(input(\"Second number: \"))\nsum = sum + number\n\nnumber = int(input(\"Third number: \"))\nsum = sum + number\n\nprint(f\"The sum of the numbers: {sum}\")

    Now all inputs from the user are read into the one and the same variable number. The value of the variable sum is increased by the value of the variable number each time the user inputs a new number.

    Let's take a closer look at this command:

    sum = sum + number

    Here, the value of the variable sum and the value of the variable number are added together, and the result is stored back in the variable sum. For example, if before the command the value of sum is 3 and the value of number is 2, after the command is executed, the value of sum is 5.

    Increasing the value of a variable is a very common operation. As such, there is a commonly used shorthand notation which achieves the same result as the explicit summing up above:

    sum += number

    This allows us to write the above program a little more concisely:

    sum = 0\n\nnumber = int(input(\"First number: \"))\nsum += number\n\nnumber = int(input(\"Second number: \"))\nsum += number\n\nnumber = int(input(\"Third number: \"))\nsum += number\n\nprint(f\"The sum of the numbers: {sum}\")

    In fact, we don't necessarily need the variable number at all. The inputs from the user can also be processed like this:

    sum = 0\n\nsum += int(input(\"First number: \"))\nsum += int(input(\"Second number: \"))\nsum += int(input(\"Third number: \"))\n\nprint(f\"The sum of the numbers: {sum}\")

    Of course, it will depend on the context how many variables are needed. If it is required to remember each value the user inputs, it will not be possible to \"reuse\" the same variable to read different values from the user. Consider the following:

    number1 = int(input(\"First number: \"))\nnumber2 = int(input(\"Second number: \"))\n\nprint(f\"{number1} + {number2} = {number1+number2}\")

    First number: 2\nSecond number: 3\n2 + 3 = 5

    On the other hand, the above program does not have a named variable for storing the sum of the two values.

    \"Reusing\" a variable only makes sense when there is a need to temporarily store things of a similar type and purpose, for example when summing numbers.

    In the following example the variable data is used to first store the name of the user, and then their age. This is not at all sensible.

    data = input(\"What is your name? \")\nprint(\"Hi \" + data + \"!\")\n\ndata = int(input(\"What is your age? \"))\n# program continues...

    A better idea is to use separate variables, with descriptive names:

    name = input(\"What is your name? \")\nprint(\"Hi \" + name + \"!\")\n\nage = int(input(\"What is your age? \"))\n# program continues...

    Please write a program which asks the user for a number of days. The program then prints out the number of seconds in the amount of days given.

    The program should function as follows:

    How many days? 1\nSeconds in that many days: 86400

    Another example:

    How many days? 7\nSeconds in that many days: 604800

    This program asks the user for three numbers. The program then prints out their product, that is, the numbers multiplied by each other. There is, however, something wrong with the program - it doesn't work quite right, as you can see if you run it. Please fix it.

    An example of the expected execution of the program:

    Please type in the first number: 2\nPlease type in the second number: 3\nPlease type in the third number: 5\nThe product is 30

    Please write a program which asks the user for two numbers. The program will then print out the sum and the product of the two numbers.

    The program should function as follows:

    Number 1: 3\nNumber 2: 7\nThe sum of the numbers: 10\nThe product of the numbers: 21

    Please write a program which asks the user for four numbers. The program then prints out the sum and the mean of the numbers.

    The program should function as follows:

    Number 1: 2\nNumber 2: 1\nNumber 3: 6\nNumber 4: 7\nThe sum of the numbers is 16 and the mean is 4.0

    Please write a program which estimates a user's typical food expenditure.

    The program asks the user how many times a week they eat at the student cafeteria. Then it asks for the price of a typical student lunch, and for money spent on groceries during the week.

    Based on this information the program calculates the user's typical food expenditure both weekly and daily.

    The program should function as follows:

    How many times a week do you eat at the student cafeteria? 4\nThe price of a typical student lunch? 2.5\nHow much money do you spend on groceries in a week? 28.5

    Average food expenditure:\nDaily: 5.5 euros\nWeekly: 38.5 euros

    Please write a program which asks for the number of students on a course and the desired group size. The program will then print out the number of groups formed from the students on the course. If the division is not even, one of the groups may have fewer members than specified.

    If you can't get your code working as expected, it is absolutely okay to move on and come back to this exercise later. The topic of the next section is conditional statements. This exercise can also be solved using a conditional construction.

    How many students on the course? 8\nDesired group size? 4\nNumber of groups formed: 2

    How many students on the course? 11\nDesired group size? 3\nNumber of groups formed: 4

    Hint: the integer division operator // could come in handy here.

    ","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-1/4-arithmetic-operations.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 able to use variables in various arithmetic operations"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to deal with numbers in user input"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to cast values into other fundamental data types"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the previous sections you've seen examples with basic arithmetics. In the following table you can see the most common arithmetic operators in Python, with examples:"}]},{"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\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":"Operator"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Purpose"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Example"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Result"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Addition"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 + 4"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"6"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Subtraction"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"10 - 2.5"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"7.5"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Multiplication"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-2 * 123"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-246"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"/"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Division (floating point result)"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"9 / 2"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"4.5"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"//"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Division (integer result)"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"9 // 2"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"4"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Modulo"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"9 % 2"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"**"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Exponentiation"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 ** 3"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"8"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The order of operations is familiar from mathematics: first calculate the exponents, then multiplication and division, and finally addition and subtraction. The order can be changed with parentheses."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example this bit of code"}]},{"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","number"]},"children":[{"type":"text","value":"2"}]},{"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":" "},{"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":"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"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":"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"prints out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"11\n15"}]}]},{"type":"element","tagName":"h2","properties":{"id":"operands-operators-and-data-types","style":"position:relative;"},"children":[{"type":"text","value":"Operands, operators and data types"},{"type":"element","tagName":"a","properties":{"href":"#operands-operators-and-data-types","ariaLabel":"operands operators and data types 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":"A calculation usually consists of "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"operands"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"operators"}]},{"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: 431px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/f717f6a68e7b1bd3b3fb9cbe2898da5b/9cb4e/1_4_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: 62.60869565217391%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB1UlEQVQ4y2VTia6iQBDk/39tY57R9eApXoCAHHILD5jaVI/jstlJKjP2dHdVTaMFAEpBVt8DYQgUBfB8AnmuYc5mj2OgaXSNqTXLmgdfLw0Tc13A9wHPA4IAGEd9N01AWf7NM/XcrTnDz49WMAxabV1rUA2JGOMdY237rzLTx1JKCSMTXi+FNFVIEoUogpyvV40k0VbjWOe07ST5JGI9+xBiuSwV7nclTY0Cx1GoKqDrtEI2zTIlLoxa3gWBJvi8oW3vhYFWx7HD19cCwzDKANq2lt/AKI24zmcHTVNhsfiFokiEjM0Z930f1vl8Ev985L7vsF7/xjRpxq7rsVqtMQyDqOE6HA6IogeWyyWKIv9M+3Q6IY5jWJfLxTwrxnGUAjZQ71f2PA9Zlr2nO8n+/W1Ljh6GjlFdFEWwjsejHIIgRBiG2O12cF33k7BarbDdbhGGkcTu9zv2+73kMJ91zKOwqqr0UKiM7ERZlkiSRJDnOWzbFpKiKMQSwbPJJ1hvHFnzb6lpGtxuN2FiM7M/n09pQjAWBIHEtGU1+xaV/g5NkOpYZBbtbjYbscjz+P6r8P0ej8enyRzWnKWuaziOI0Ogtev1Km9FcDhswju6SNP0P4W0/Ae4n+ixOVvxjQAAAABJRU5ErkJggg=='); 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":["/static/f717f6a68e7b1bd3b3fb9cbe2898da5b/a0b58/1_4_1.webp 230w","/static/f717f6a68e7b1bd3b3fb9cbe2898da5b/01668/1_4_1.webp 431w"],"sizes":"(max-width: 431px) 100vw, 431px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/f717f6a68e7b1bd3b3fb9cbe2898da5b/81c8e/1_4_1.png 230w","/static/f717f6a68e7b1bd3b3fb9cbe2898da5b/9cb4e/1_4_1.png 431w"],"sizes":"(max-width: 431px) 100vw, 431px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/f717f6a68e7b1bd3b3fb9cbe2898da5b/9cb4e/1_4_1.png","alt":"1 4 1","title":"1 4 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":"p","properties":{},"children":[{"type":"text","value":"The data type of an operand usually determines the data type of the result: if two integers are added together, the result will also be an integer. If a floating point number is subtracted from another floating point number, the result is a floating point number. In fact, if a single one of the operands in an expression is a floating point number, the result will also be a floating point number, regardless of the other operands."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Division "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" is an exception to this rule. Its result is a floating point number, even if the operands are integers. For example "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1 / 5"}]},{"type":"text","value":" will result in the floating point number "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0.2"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Example:"}]},{"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":"height "},{"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":"172.5"}]},{"type":"text","value":"\nweight "},{"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":"68.55"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the Body Mass Index, or BMI, is calculated by dividing body mass with the square of height"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# height is converted into metres in the formula"}]},{"type":"text","value":"\nbmi "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" weight "},{"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":"text","value":"height "},{"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":"100"}]},{"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":"2"}]},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"The BMI 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":"bmi"},{"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":"p","properties":{},"children":[{"type":"text","value":"This program prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The BMI is 23.037177063642087"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice Python also has an integer division operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"//"}]},{"type":"text","value":". If the operands are integers, it will produce an integer. The result is rounded down to the nearest integer. For example this program"}]},{"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":"x "},{"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":"\ny "},{"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":"2"}]},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"/ operator "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":"y"},{"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"},{"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\"// operator "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"//"}]},{"type":"text","value":"y"},{"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":"p","properties":{},"children":[{"type":"text","value":"prints out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"/ operator 1.5\n// operator 1"}]}]},{"type":"element","tagName":"h2","properties":{"id":"numbers-as-input","style":"position:relative;"},"children":[{"type":"text","value":"Numbers as input"},{"type":"element","tagName":"a","properties":{"href":"#numbers-as-input","ariaLabel":"numbers as input 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 have already used the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" command to read in strings from the user. The same function can be used to read in numbers, but the string produced by the function must then be converted to a numeric data type in the program code. In the previous section we cast integers as strings with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":" function. The same basic principle applies here, but the name of the casting function will be different."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A string can be converted into an integer with the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":". The following program asks the user for their year of birth and stores it in the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input_str"}]},{"type":"text","value":". The program then creates another variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"year"}]},{"type":"text","value":", which contains the year converted into an integer. After this the calculation "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2021-year"}]},{"type":"text","value":" is possible, using the user-supplied value."}]},{"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":"input_str "},{"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":"\"Which year were you born? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nyear "},{"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":"text","value":"input_str"},{"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\"Your age at the end of the year 2021: "}]},{"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","number"]},"children":[{"type":"text","value":"2021"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" 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":"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":"Which year were you born? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1995"}]},{"type":"text","value":"\nYour age at the end of the year 2021: 26"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Usually you do not need to create two separate variables (like "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input_str"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"year"}]},{"type":"text","value":" above) to read a number value from the user. Instead, reading the input with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" function and converting it with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":" function can be achieved in one go:"}]},{"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":"year "},{"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"]},"children":[{"type":"text","value":"\"Which year were you born? \""}]},{"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\"Your age at the end of the year 2021: "}]},{"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","number"]},"children":[{"type":"text","value":"2021"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" 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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Similarly, a string can be converted into a floating point number with the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]},{"type":"text","value":". This programs asks the user for their height and weight, and uses these to calculate their BMI:"}]},{"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":"height "},{"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":"float"}]},{"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"]},"children":[{"type":"text","value":"\"What is your height? \""}]},{"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":"\nweight "},{"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":"float"}]},{"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"]},"children":[{"type":"text","value":"\"What is your weight? \""}]},{"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\nheight "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" height "},{"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":"100"}]},{"type":"text","value":"\nbmi "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" weight "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" height "},{"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":"2"}]},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"The BMI 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":"bmi"},{"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":"p","properties":{},"children":[{"type":"text","value":"An example printout from the program:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your height? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"163"}]},{"type":"text","value":"\nWhat is your weight? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"74.45"}]},{"type":"text","value":"\nThe BMI is 28.02137829801649"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Times five","tmcname":"part01-13_times_five"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a number. The program then prints out the number multiplied by five."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should function as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n3 times 5 is 15"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Name and age","tmcname":"part01-14_name_and_age"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for their name and year of birth. The program then prints out a message as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your name? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Frances Fictitious"}]},{"type":"text","value":"\nWhich year were you born? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1990"}]},{"type":"text","value":"\nHi Frances Fictitious, you will be 31 years old at the end of the year 2021"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"using-variables","style":"position:relative;"},"children":[{"type":"text","value":"Using variables"},{"type":"element","tagName":"a","properties":{"href":"#using-variables","ariaLabel":"using variables 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":"Let's have a look at a program which calculates the sum of three numbers given by the user:"}]},{"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":"number1 "},{"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"]},"children":[{"type":"text","value":"\"First number: \""}]},{"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":"\nnumber2 "},{"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"]},"children":[{"type":"text","value":"\"Second number: \""}]},{"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":"\nnumber3 "},{"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"]},"children":[{"type":"text","value":"\"Third number: \""}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" number1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number3\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\"The sum of the numbers: "}]},{"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":"sum"}]},{"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":"p","properties":{},"children":[{"type":"text","value":"An example execution of the program:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nSecond number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"21"}]},{"type":"text","value":"\nThird number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nThe sum of the numbers: 33"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program uses four different variables, but two would easily suffice in this case:"}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"0"}]},{"type":"text","value":"\n\nnumber "},{"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"]},"children":[{"type":"text","value":"\"First number: \""}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number\n\nnumber "},{"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"]},"children":[{"type":"text","value":"\"Second number: \""}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number\n\nnumber "},{"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"]},"children":[{"type":"text","value":"\"Third number: \""}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number\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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"The sum of the numbers: "}]},{"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":"sum"}]},{"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":"p","properties":{},"children":[{"type":"text","value":"Now all inputs from the user are read into the one and the same variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":". The value of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"increased"}]},{"type":"text","value":" by the value of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" each time the user inputs a new number."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's take a closer look at this command:"}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here, the value of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" and the value of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" are added together, and the result is stored back in the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":". For example, if before the command the value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" is 3 and the value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" is 2, after the command is executed, the value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" is 5."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Increasing the value of a variable is a very common operation. As such, there is a commonly used shorthand notation which achieves the same result as the explicit summing up above:"}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" number"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This allows us to write the above program a little more concisely:"}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"0"}]},{"type":"text","value":"\n\nnumber "},{"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"]},"children":[{"type":"text","value":"\"First number: \""}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" number\n\nnumber "},{"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"]},"children":[{"type":"text","value":"\"Second number: \""}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" number\n\nnumber "},{"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"]},"children":[{"type":"text","value":"\"Third number: \""}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" number\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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"The sum of the numbers: "}]},{"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":"sum"}]},{"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":"p","properties":{},"children":[{"type":"text","value":"In fact, we don't necessarily need the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" at all. The inputs from the user can also be processed 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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"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"]},"children":[{"type":"text","value":"\"First number: \""}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"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"]},"children":[{"type":"text","value":"\"Second number: \""}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"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"]},"children":[{"type":"text","value":"\"Third number: \""}]},{"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":"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\"The sum of the numbers: "}]},{"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":"sum"}]},{"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":"p","properties":{},"children":[{"type":"text","value":"Of course, it will depend on the context how many variables are needed. If it is required to remember each value the user inputs, it will not be possible to \"reuse\" the same variable to read different values from the user. Consider the following:"}]},{"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":"number1 "},{"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"]},"children":[{"type":"text","value":"\"First number: \""}]},{"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":"\nnumber2 "},{"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"]},"children":[{"type":"text","value":"\"Second number: \""}]},{"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":"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":"number1"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"number2"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"number1"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"number2"},{"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":"First number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nSecond number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n2 + 3 = 5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"On the other hand, the above program does not have a named variable for storing the sum of the two values."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"\"Reusing\" a variable only makes sense when there is a need to temporarily store things of a similar type and purpose, for example when summing numbers."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"data"}]},{"type":"text","value":" is used to first store the name of the user, and then their age. This is not at all sensible."}]},{"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":"data "},{"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":"\"What is your name? \""}]},{"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":"\"Hi \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" data "},{"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":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ndata "},{"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"]},"children":[{"type":"text","value":"\"What is your age? \""}]},{"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":"# program continues..."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A better idea is to use separate variables, with "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"descriptive"}]},{"type":"text","value":" names:"}]},{"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":"name "},{"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":"\"What is your name? \""}]},{"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":"\"Hi \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"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":"\"!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nage "},{"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"]},"children":[{"type":"text","value":"\"What is your age? \""}]},{"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":"# program continues..."}]}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Seconds in a day","tmcname":"part01-15_seconds_in_a_day"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a number of days. The program then prints out the number of seconds in the amount of days given."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should function as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many days? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nSeconds in that many days: 86400"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Another example:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many days? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nSeconds in that many days: 604800"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fix the code: Product","tmcname":"part01-16_product"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program asks the user for three numbers. The program then prints out their product, that is, the numbers multiplied by each other. There is, however, something wrong with the program - it doesn't work quite right, as you can see if you run it. Please fix it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the expected execution of the program:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in the first number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nPlease type in the second number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nPlease type in the third number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nThe product is 30"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Sum and product","tmcname":"part01-17_sum_and_product"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for two numbers. The program will then print out the sum and the product of the two numbers."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should function as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Number 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nNumber 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nThe sum of the numbers: 10\nThe product of the numbers: 21"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Sum and mean","tmcname":"part01-18_sum_and_mean"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for four numbers. The program then prints out the sum and the mean of the numbers."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should function as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Number 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nNumber 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nNumber 3: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nNumber 4: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nThe sum of the numbers is 16 and the mean is 4.0"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Food expenditure","tmcname":"part01-19_food_expenditure"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which estimates a user's typical food expenditure."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program asks the user how many times a week they eat at the student cafeteria. Then it asks for the price of a typical student lunch, and for money spent on groceries during the week."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Based on this information the program calculates the user's typical food expenditure both weekly and daily."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should function as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many times a week do you eat at the student cafeteria? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nThe price of a typical student lunch? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2.5"}]},{"type":"text","value":"\nHow much money do you spend on groceries in a week? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"28.5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Average food expenditure:\nDaily: 5.5 euros\nWeekly: 38.5 euros"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Students in groups","tmcname":"part01-20_students_in_groups"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for the number of students on a course and the desired group size. The program will then print out the number of groups formed from the students on the course. If the division is not even, one of the groups may have fewer members than specified."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you can't get your code working as expected, it is absolutely okay to move on and come back to this exercise later. The topic of the next section is "},{"type":"element","tagName":"a","properties":{"href":"/part-1/5-conditional-statements"},"children":[{"type":"text","value":"conditional statements"}]},{"type":"text","value":". This exercise can also be solved using a conditional construction."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many students on the course? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\nDesired group size? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nNumber of groups formed: 2"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many students on the course? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"11"}]},{"type":"text","value":"\nDesired group size? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nNumber of groups formed: 4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hint: the integer division operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"//"}]},{"type":"text","value":" could come in handy here."}]}]},{"type":"comment","value":"\n\nA quiz to review the contents of this section:\n\n\n\n"}]},"html":"

    After this section

    In the previous sections you've seen examples with basic arithmetics. In the following table you can see the most common arithmetic operators in Python, with examples:

    \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\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
    OperatorPurposeExampleResult
    +Addition2 + 46
    -Subtraction10 - 2.57.5
    *Multiplication-2 * 123-246
    /Division (floating point result)9 / 24.5
    //Division (integer result)9 // 24
    %Modulo9 % 21
    **Exponentiation2 ** 38

    The order of operations is familiar from mathematics: first calculate the exponents, then multiplication and division, and finally addition and subtraction. The order can be changed with parentheses.

    For example this bit of code

    print(2 + 3 * 3)\nprint((2 + 3) * 3)

    prints out

    11\n15

    Operands, operators and data types

    A calculation usually consists of operands and operators:

    \n \n \n \n \n \n \"1\n \n \n

    The data type of an operand usually determines the data type of the result: if two integers are added together, the result will also be an integer. If a floating point number is subtracted from another floating point number, the result is a floating point number. In fact, if a single one of the operands in an expression is a floating point number, the result will also be a floating point number, regardless of the other operands.

    Division / is an exception to this rule. Its result is a floating point number, even if the operands are integers. For example 1 / 5 will result in the floating point number 0.2.

    Example:

    height = 172.5\nweight = 68.55\n\n# the Body Mass Index, or BMI, is calculated by dividing body mass with the square of height\n# height is converted into metres in the formula\nbmi = weight / (height / 100) ** 2\n\nprint(f\"The BMI is {bmi}\")

    This program prints out the following:

    The BMI is 23.037177063642087

    Notice Python also has an integer division operator //. If the operands are integers, it will produce an integer. The result is rounded down to the nearest integer. For example this program

    x = 3\ny = 2\n\nprint(f\"/ operator {x/y}\")\nprint(f\"// operator {x//y}\")

    prints out

    / operator 1.5\n// operator 1

    Numbers as input

    We have already used the input command to read in strings from the user. The same function can be used to read in numbers, but the string produced by the function must then be converted to a numeric data type in the program code. In the previous section we cast integers as strings with the str function. The same basic principle applies here, but the name of the casting function will be different.

    A string can be converted into an integer with the function int. The following program asks the user for their year of birth and stores it in the variable input_str. The program then creates another variable year, which contains the year converted into an integer. After this the calculation 2021-year is possible, using the user-supplied value.

    input_str = input(\"Which year were you born? \")\nyear = int(input_str)\nprint(f\"Your age at the end of the year 2021: {2021 - year}\" )

    Which year were you born? 1995\nYour age at the end of the year 2021: 26

    Usually you do not need to create two separate variables (like input_str and year above) to read a number value from the user. Instead, reading the input with the input function and converting it with the int function can be achieved in one go:

    year = int(input(\"Which year were you born? \"))\nprint(f\"Your age at the end of the year 2021: {2021 - year}\" )

    Similarly, a string can be converted into a floating point number with the function float. This programs asks the user for their height and weight, and uses these to calculate their BMI:

    height = float(input(\"What is your height? \"))\nweight = float(input(\"What is your weight? \"))\n\nheight = height / 100\nbmi = weight / height ** 2\n\nprint(f\"The BMI is {bmi}\")

    An example printout from the program:

    What is your height? 163\nWhat is your weight? 74.45\nThe BMI is 28.02137829801649

    Please write a program which asks the user for a number. The program then prints out the number multiplied by five.

    The program should function as follows:

    Please type in a number: 3\n3 times 5 is 15

    Please write a program which asks the user for their name and year of birth. The program then prints out a message as follows:

    What is your name? Frances Fictitious\nWhich year were you born? 1990\nHi Frances Fictitious, you will be 31 years old at the end of the year 2021

    Using variables

    Let's have a look at a program which calculates the sum of three numbers given by the user:

    number1 = int(input(\"First number: \"))\nnumber2 = int(input(\"Second number: \"))\nnumber3 = int(input(\"Third number: \"))\n\nsum = number1 + number2 + number3\nprint(f\"The sum of the numbers: {sum}\")

    An example execution of the program:

    First number: 5\nSecond number: 21\nThird number: 7\nThe sum of the numbers: 33

    The program uses four different variables, but two would easily suffice in this case:

    sum = 0\n\nnumber = int(input(\"First number: \"))\nsum = sum + number\n\nnumber = int(input(\"Second number: \"))\nsum = sum + number\n\nnumber = int(input(\"Third number: \"))\nsum = sum + number\n\nprint(f\"The sum of the numbers: {sum}\")

    Now all inputs from the user are read into the one and the same variable number. The value of the variable sum is increased by the value of the variable number each time the user inputs a new number.

    Let's take a closer look at this command:

    sum = sum + number

    Here, the value of the variable sum and the value of the variable number are added together, and the result is stored back in the variable sum. For example, if before the command the value of sum is 3 and the value of number is 2, after the command is executed, the value of sum is 5.

    Increasing the value of a variable is a very common operation. As such, there is a commonly used shorthand notation which achieves the same result as the explicit summing up above:

    sum += number

    This allows us to write the above program a little more concisely:

    sum = 0\n\nnumber = int(input(\"First number: \"))\nsum += number\n\nnumber = int(input(\"Second number: \"))\nsum += number\n\nnumber = int(input(\"Third number: \"))\nsum += number\n\nprint(f\"The sum of the numbers: {sum}\")

    In fact, we don't necessarily need the variable number at all. The inputs from the user can also be processed like this:

    sum = 0\n\nsum += int(input(\"First number: \"))\nsum += int(input(\"Second number: \"))\nsum += int(input(\"Third number: \"))\n\nprint(f\"The sum of the numbers: {sum}\")

    Of course, it will depend on the context how many variables are needed. If it is required to remember each value the user inputs, it will not be possible to \"reuse\" the same variable to read different values from the user. Consider the following:

    number1 = int(input(\"First number: \"))\nnumber2 = int(input(\"Second number: \"))\n\nprint(f\"{number1} + {number2} = {number1+number2}\")

    First number: 2\nSecond number: 3\n2 + 3 = 5

    On the other hand, the above program does not have a named variable for storing the sum of the two values.

    \"Reusing\" a variable only makes sense when there is a need to temporarily store things of a similar type and purpose, for example when summing numbers.

    In the following example the variable data is used to first store the name of the user, and then their age. This is not at all sensible.

    data = input(\"What is your name? \")\nprint(\"Hi \" + data + \"!\")\n\ndata = int(input(\"What is your age? \"))\n# program continues...

    A better idea is to use separate variables, with descriptive names:

    name = input(\"What is your name? \")\nprint(\"Hi \" + name + \"!\")\n\nage = int(input(\"What is your age? \"))\n# program continues...

    Please write a program which asks the user for a number of days. The program then prints out the number of seconds in the amount of days given.

    The program should function as follows:

    How many days? 1\nSeconds in that many days: 86400

    Another example:

    How many days? 7\nSeconds in that many days: 604800

    This program asks the user for three numbers. The program then prints out their product, that is, the numbers multiplied by each other. There is, however, something wrong with the program - it doesn't work quite right, as you can see if you run it. Please fix it.

    An example of the expected execution of the program:

    Please type in the first number: 2\nPlease type in the second number: 3\nPlease type in the third number: 5\nThe product is 30

    Please write a program which asks the user for two numbers. The program will then print out the sum and the product of the two numbers.

    The program should function as follows:

    Number 1: 3\nNumber 2: 7\nThe sum of the numbers: 10\nThe product of the numbers: 21

    Please write a program which asks the user for four numbers. The program then prints out the sum and the mean of the numbers.

    The program should function as follows:

    Number 1: 2\nNumber 2: 1\nNumber 3: 6\nNumber 4: 7\nThe sum of the numbers is 16 and the mean is 4.0

    Please write a program which estimates a user's typical food expenditure.

    The program asks the user how many times a week they eat at the student cafeteria. Then it asks for the price of a typical student lunch, and for money spent on groceries during the week.

    Based on this information the program calculates the user's typical food expenditure both weekly and daily.

    The program should function as follows:

    How many times a week do you eat at the student cafeteria? 4\nThe price of a typical student lunch? 2.5\nHow much money do you spend on groceries in a week? 28.5

    Average food expenditure:\nDaily: 5.5 euros\nWeekly: 38.5 euros

    Please write a program which asks for the number of students on a course and the desired group size. The program will then print out the number of groups formed from the students on the course. If the division is not even, one of the groups may have fewer members than specified.

    If you can't get your code working as expected, it is absolutely okay to move on and come back to this exercise later. The topic of the next section is conditional statements. This exercise can also be solved using a conditional construction.

    How many students on the course? 8\nDesired group size? 4\nNumber of groups formed: 2

    How many students on the course? 11\nDesired group size? 3\nNumber of groups formed: 4

    Hint: the integer division operator // could come in handy here.

    ","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-1/4-arithmetic-operations.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-1/5-conditional-statements/page-data.json b/page-data/part-1/5-conditional-statements/page-data.json index 00135e96f..fc3cb9f56 100644 --- a/page-data/part-1/5-conditional-statements/page-data.json +++ b/page-data/part-1/5-conditional-statements/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-1/5-conditional-statements", - "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 able to use a simple conditional statement in programming"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what a Boolean value is"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to express conditionals with comparison operators"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Thus far, every program we have written has been executed line by line in order. Instead of executing every line of code every single time a program is run, it is often useful to create sections of the program which are are only executed in certain situations."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, the following code checks whether the user is of age:"}]},{"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":"age "},{"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"]},"children":[{"type":"text","value":"\"How old are you? \""}]},{"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":"if"}]},{"type":"text","value":" age "},{"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":"17"}]},{"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":"\"You are of age!\""}]},{"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":"\"Here's a copy of GTA6 for you.\""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Next customer, please!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the user is over the age of 17, the execution of the program should look like this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How old are you? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"18"}]},{"type":"text","value":"\nYou are of age!\nHere's a copy of GTA6 for you.\nNext customer, please!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the user is 17 or under, only this is printed out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How old are you? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"16"}]},{"type":"text","value":"\nNext customer, please!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"These examples show us that the value given as input affects which parts of the program are executed. The program contains a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"conditional statement"}]},{"type":"text","value":" with a block of code which is executed only if the condition in the statement is true."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 483px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 126.52173913043478%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAZCAYAAAAxFw7TAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB/klEQVQ4y6WV564CIRCF9/0fSv9pYo29d2Pvvc/NR4JhkdVNLglBF+ZwzswBPAnRXq+XGg+Hg9zvd983u3muQEbX78ViIbfb7eN7IKC9sw56Pp/q/2AwkP1+75szNw5kCIvpdPqxezKZlG63+0HA7E7A6/Uqs9nsnTekNptNabVaqjcaDVmv13I+nz8l2ztoQAIAqtfrst1uJZfLSSqVkkQiocBXq5XUajUZDocyn89lNBqpdZ6rUkhmkQZfLpdSrVZVDtvttpJN8OPxeJM4nU6KgMfHy+WipDECBgiTduv3+2qdK03IZ2OPXQuFgsTjcUmn00oa9JmkUWGdjl6vJ+Px2FnlNyDSYrGYZLNZyWQyUqlUVH40QzOoVCrJZDL5DohEvHU8HlUetGxT8mazUTmFPVVGgWlwH6CrKORQF4XGb9MiBLK5E9BlG5uhBoAhOafCNiDKSNVPY2uGBAQxpHc6HVUDJyA3iikZRhQD21BlzGzfOkhWPgxzOZjM8K1r3lkU+8oyc4rkaDQqxWJRBesbKFSV7Q2QgnWQjr2QTZ7/BUj+OCkknxFw8658n+WwgNwwnKZ8Pi/lclkikYgqFG6gw5pzHgqQAEAJoFNl00Zfn4AgX3I8kQ3wbrfzFSXwxv4Gqv1o28peE4qhZkPu9FvjAvsJaJsXyTwN397lP2jZmkRqsNQNAAAAAElFTkSuQmCC'); 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":["/static/416cb381134e2a6b8d72caecc6260d5a/a0b58/1_5_1.webp 230w","/static/416cb381134e2a6b8d72caecc6260d5a/bc10c/1_5_1.webp 460w","/static/416cb381134e2a6b8d72caecc6260d5a/30f41/1_5_1.webp 483w"],"sizes":"(max-width: 483px) 100vw, 483px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/416cb381134e2a6b8d72caecc6260d5a/81c8e/1_5_1.png 230w","/static/416cb381134e2a6b8d72caecc6260d5a/08a84/1_5_1.png 460w","/static/416cb381134e2a6b8d72caecc6260d5a/77a9e/1_5_1.png 483w"],"sizes":"(max-width: 483px) 100vw, 483px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/416cb381134e2a6b8d72caecc6260d5a/77a9e/1_5_1.png","alt":"1 5 1","title":"1 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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In a conditional statement the keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" is followed by a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"condition"}]},{"type":"text","value":", such as a comparison of two values. The code block following this header line is only executed if the condition is true."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice the colon character following the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" header. In the following code there is no colon:"}]},{"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":"age "},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# no colon at the end of the following line"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" age "},{"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":"17"}]},{"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":"\"You are of age.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Upon execution this causes an error:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"File \"program.py\", line 3\n if age > 17\n ^\nSyntaxError: invalid syntax\n"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"h2","properties":{"id":"comparison-operators","style":"position:relative;"},"children":[{"type":"text","value":"Comparison operators"},{"type":"element","tagName":"a","properties":{"href":"#comparison-operators","ariaLabel":"comparison operators 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":"Very typically conditions consist of comparing two values. Here is a table with the most common comparison operators used in Python:"}]},{"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\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":"Operator"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Purpose"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Example"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Equal to"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a == b"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!="}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Not equal to"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a != b"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Greater than"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a > b"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">="}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Greater than or equal to"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a >= b"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Less than"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a < b"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<="}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Less than or equal to"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a <= b"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at a program which prints out different things based on whether the number the user inputs is negative, positive, or equal to zero:"}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is negative.\""}]},{"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":"if"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is positive.\""}]},{"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":"if"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is zero.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Examples of how the program functions with three different inputs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"15"}]},{"type":"text","value":"\nThe number is positive."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-18"}]},{"type":"text","value":"\nThe number is negative."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nThe number is zero."}]}]},{"type":"element","tagName":"h2","properties":{"id":"indentation","style":"position:relative;"},"children":[{"type":"text","value":"Indentation"},{"type":"element","tagName":"a","properties":{"href":"#indentation","ariaLabel":"indentation 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":"Python recognises that a block of code is part of a conditional statement if each line of code in the block is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"indented"}]},{"type":"text","value":" the same. That is, there should be a bit of whitespace at the beginning of every line of code within the code block. Each line should have the same amount of whitespace."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"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":"password "},{"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 password: \""}]},{"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":"if"}]},{"type":"text","value":" password "},{"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":"\"kittycat\""}]},{"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":"\"You knew the password!\""}]},{"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":"\"You must be either the intended user...\""}]},{"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":"\"...or quite an accomplished hacker.\""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The program has finished its execution. Thanks and bye!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can use the Tab key, short for "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"tabulator"}]},{"type":"text","value":" key, to insert a set amount of whitespace."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 537px; margin-bottom: 1rem;"},"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,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAEQklEQVQ4y4WT/TPbBxzHv39D7zZH57FDqdZQWykroYmQB/KAREhEHj0kCEHiKb6SSIR4SDwuw5W5YaImqnO7royutM1Zx7pq1TZ3G03cVrfK3Oqzy37aL7v99H7d+z6/vD+fzxsBgCMA2AeAg3/pf/H+/80iuy92v9VrW2eMBqN1espqNrQYFtr07WPWz2YGdGrdoqnTNDIzPWNBG9DFwb6PhsbHxofUKvXisGXYMj46PoI2oF+MjowODluGxzSodh65ab0JQX7BEBMVC0XC4mM3X4mMgWJhMfifDQBsIu5UWCACzzNeQCVRT3MZefDOW97AoDNPs6jZ4O3hA9xc7ht8cioE+gYBYn9of8rKYt2vlStXOg0dFj5HYFdW1y626dtv8Ni8DbSheUGLaj/hsPI39NrWebQB/ZTN5Ng7DB02RZVyKj+X+9DUaZorl8hmBFzhN8jdO3d/T8WTnQQi1alv61yiZ2Q5CViCs03fvsygMZw4TIqjRa1bFhaIHEkfJjubVepVaZH0l8Q4zG9oA/qoXCLbxsQnHTXWqTZKxJInyOSUFQLPeALeLwia8/mvos6Hg593IMgkssOIC5Hg7xUARYIiJyYOA95v+wCPzT8kppD+iZ3P4v6Rdj3txNfTD3IyWa9JeLIL2ZqadjZisNvl2azn1matzYBL2+bgST+bewdmuSzuJj0987nRYJwT8cTf0cj0HYOubb5YVGKnEKk/6jX6LyWF0hUKkbqnRbV3Kkorl5AHHw8dMNm8nRwmZzeDnvPEbDCuGcnUPTol64eB3gGbQq7Yyc3O3TYajLYauWKHnk5/ptPobU31TdsMGvOFBtUuqupUG1nU7J8aa1VLyESXGd6PvgqRoRHg5+EDUmkF5IRHQ5iXP5SVVjhxiTi4FBwOIp7Y4b6kOx43t8CRQaDAu96BwMnhHOEwKW98PHyBQWOeIF/1DRxdT6M4qemZh0QSzanXtj6TRccckvFkR19P/yo+OfUgJQn/sruje5VGph9gE3AOg67tXk4max8Tn3So0+gf5THZO0nxya9UdapNZGdick9LyVyr5InstTzRujGVZB9X1vcr65qsbEbeA3WTZs69NzaTvdZU3zTX3to+z80rWFdW1dq6O0zzfI5gTV5etWDqNH8u4onvIVbrLBAuXQbBB3GA9fSFi2cDQFxUusLKYv0aFnQRCDiiS1ggcrk5DUtwFQqKXO7HT7iaeFwilvzp9mMux/5VLpEdX4u5BsiCbeEoNjbhJQ5PdrwXccUZHhb1Wt3YfINKoq2HnAs9EXCFmzwO//sg32AXj8PfFBWIt84HhLgYNOaWpFC6GXIu9DiDQNmWSWWPIy5EHiFbm1tPy4rL7lfLqh4b9e23uo1dt+Tl8ts6tW7S3NWzUFOpWG1R62Z7Tb1zNZU1y9rmltv9Pf3TCrli2d1vy4Blqra67ut6ZcPiYN/gxN+K6mUiaF+/igAAAABJRU5ErkJggg=='); 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":["/static/43a34a2e4b0bc023009954d6f7809f7c/a0b58/1_5_keyboard.webp 230w","/static/43a34a2e4b0bc023009954d6f7809f7c/bc10c/1_5_keyboard.webp 460w","/static/43a34a2e4b0bc023009954d6f7809f7c/27c24/1_5_keyboard.webp 537w"],"sizes":"(max-width: 537px) 100vw, 537px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/43a34a2e4b0bc023009954d6f7809f7c/81c8e/1_5_keyboard.png 230w","/static/43a34a2e4b0bc023009954d6f7809f7c/08a84/1_5_keyboard.png 460w","/static/43a34a2e4b0bc023009954d6f7809f7c/b1cde/1_5_keyboard.png 537w"],"sizes":"(max-width: 537px) 100vw, 537px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/43a34a2e4b0bc023009954d6f7809f7c/b1cde/1_5_keyboard.png","alt":"1 5 keyboard","title":"1 5 keyboard","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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Many text editors will automatically indent the following line when the Enter key is pressed after a colon character. When you want to end an indented code block you can use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Backspace"}]},{"type":"text","value":" key to return to the beginning of the line."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 293px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 96.08695652173913%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAATCAYAAACQjC21AAAACXBIWXMAAAsTAAALEwEAmpwYAAAEtElEQVQ4y3WU6U+UVxSHSdMvbbWNSgXZh2FmGAYYdnBBERA6SsdWZYdgK4ggOyrKElBR6oYoaFllHdZhUQGRalkTo8QUqMFGwCVubfzWf+Bp3kuaNDF9k5O89+Te555zfvcck7DQMAb6Boj4PoLw78LxcvWmsb6JI5m5hG4PZkdAEGnJqRia29gVFELUnnB2BocyYOwnISoGfYiOwC3bKC4oRvpMtvj401B3Aw9nT6R/maWciovlREUkopBvRuMYQHTkQa5cq0ahCcLHT4dK7U9DUxtbg+Nx89mJQrGJtEO5K8DYyDgmJ6bQ63YTFvqtgN7sG6Awv4ygwDj8A6PIzjrJUGUVP3jq+EkfT5kumqe19ZRFprHPJZhtXjrKymtWgE8X/mDm0QzTU9NMjE/y6uUrJN+D6Wnuj4/x8pe7fGhqZLamlme3Bng6Mc785ARPJqdYvHuH2YZaxvOO8WHw9gqw6EQRjvZqtE5uOFgrMHYZ2Ru2BzuZI3q5mnMBwXQbOtF6+KFx8Uat0qJWuqJSOKNUadG4+iCXqenJy4eZGUwK8gpE/by1PqjlTrQ2txETEYuvmy/Zji7sCgmjtbWdiN37iI+ME+IFbNqOm9oNT40HXs6eKK3kGIZGYGoSk8zDWdhZyFDaqbBYZ0l9TT36b/Qigngza6I2bqOhowcbCxlySzlyCxkOlnIcrFZMYavCzMyWxoYmGPsVk672LqLDY8hOz+ZQYgpzs3OcO3OO/QlJnEjNYProcRa6ukmMjONAYgoH07JJTMkgMTWT5NRM0pNSSdHpeVRQBO/eYXJnaIT3799zs/8mfcY+UcOHDx6yvLiEob2L/p4+jCcKeVFTx9+Dg7xu7+BDXz9/Go0sNzYxejSP3sxs/lpcWhHFTeNFd2c3fh4bUdqq+PrL9STE7ud65c8orBVCsNVfmQl444XLaNdZoHP1Zq/fVrwsZKjtHDG3lIs6C2DA5u30dveiC96Jj7uvAKQmpVJXXYevu5+4SLbBDmNXD9U19ezYsYvomARCQsLQOLnj4eKF0kZB+79AF5Urbc1tQmnp2Ziu/prYiDguX6zAxtwWlcyRLz5dRfONZiorKvnsk89FJhoHZ7RqN9w1Hjj8F1h9rZo3r99QefkqZ0+d5VTxaYZuDbHwZIHTJaVCoNKSUpYWl1heWqbiYgUpiSloFP8DvFJ+lUvny7l04SrLS8/FoJCiO3/2PG0tBl48f0FlRZXYU3ryDI9nHjM6Morc2kHAPgIqbJTYWzmwfo09NddrOfhjMl5ab8zXbGCT12aRqnRYSn3dKlNyMnJFBpLPTeP+MVAq+kqXqDG0GIiLihebXB21oksMre3odXoCtwSJgyWFJQzfHhaNII06yaTLOto6VoDSQorSfK0FTTeahdqWplbYmtvh77uVlsYWnJUuODlohGDHcvLoN/azdpUpMgt7ZJb2wi86RQJ2GjrpMHSK6CRx7o3eE6pL6+HBYd6+eSvS6WzvFPD5ud9ZfLaINFSkmp4uLqXweBHzc/MrwKorVWJcZaVlk3k4k+QDhxkducfI8Ahph9KF/0jWUWF5uXkk7k9ioHeAsftjpKekk38sn5yMHGZ/mxXAfwDrwXYWIBU21wAAAABJRU5ErkJggg=='); 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":["/static/7f8b7a68350e9455732bc0462f21ccff/a0b58/1_5_keyboard2.webp 230w","/static/7f8b7a68350e9455732bc0462f21ccff/2b735/1_5_keyboard2.webp 293w"],"sizes":"(max-width: 293px) 100vw, 293px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7f8b7a68350e9455732bc0462f21ccff/81c8e/1_5_keyboard2.png 230w","/static/7f8b7a68350e9455732bc0462f21ccff/3cf3e/1_5_keyboard2.png 293w"],"sizes":"(max-width: 293px) 100vw, 293px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/7f8b7a68350e9455732bc0462f21ccff/3cf3e/1_5_keyboard2.png","alt":"1 5 keyboard2","title":"1 5 keyboard2","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":"small","properties":{},"children":[{"type":"element","tagName":"center","properties":{},"children":[{"type":"text","value":"\nThe source of the keyboard pictures:\n "},{"type":"element","tagName":"a","properties":{"href":"https://pixabay.com/users/Clker-Free-Vector-Images-3736/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=311803"},"children":[{"type":"text","value":"Clker-Free-Vector-Images"}]},{"type":"text","value":" from "},{"type":"element","tagName":"a","properties":{"href":"https://pixabay.com/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=311803"},"children":[{"type":"text","value":"Pixabay"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Orwell","tmcname":"part01-21_orwell"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for an integer number. The program should print out \"Orwell\" if the number is exactly 1984, and otherwise do nothing."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2020"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1984"}]},{"type":"text","value":"\nOrwell"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Absolute value","tmcname":"part01-22_absolute_value"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for an integer number. If the number is less than zero, the program should print out the number multiplied by -1. Otherwise the program prints out the number as is. Please have a look at the examples of expected behaviour below."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-7"}]},{"type":"text","value":"\nThe absolute value of this number is 7"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nThe absolute value of this number is 1"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-99"}]},{"type":"text","value":"\nThe absolute value of this number is 99"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Soup or no soup","tmcname":"part01-23_soup_or_no_soup"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for the user's name. If the name is anything but \"Jerry\", the program then asks for the number of portions and prints out the total cost. The price of a single portion is 5.90."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Two examples of the program's execution:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please tell me your name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Kramer"}]},{"type":"text","value":"\nHow many portions of soup? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nThe total cost is 11.8\nNext please!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please tell me your name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Jerry"}]},{"type":"text","value":"\nNext please!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Order of magnitude","tmcname":"part01-24_order_of_magnitude"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for an integer number. The program should then print out the magnitude of the number according to the following examples."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"950"}]},{"type":"text","value":"\nThis number is smaller than 1000\nThank you!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"59"}]},{"type":"text","value":"\nThis number is smaller than 1000\nThis number is smaller than 100\nThank you!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nThis number is smaller than 1000\nThis number is smaller than 100\nThis number is smaller than 10\nThank you!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1123"}]},{"type":"text","value":"\nThank you!"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"boolean-values-and-boolean-expressions","style":"position:relative;"},"children":[{"type":"text","value":"Boolean values and Boolean expressions"},{"type":"element","tagName":"a","properties":{"href":"#boolean-values-and-boolean-expressions","ariaLabel":"boolean values and boolean expressions 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":"Any condition used in a conditional statement will result in a truth value, that is, either true or false. For example, the condition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a < 5"}]},{"type":"text","value":" is true if "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" is less than 5, and false if "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" is equal to or greater than 5."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"These types of values are often called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Boolean"}]},{"type":"text","value":" values, named after the English mathematician George Boole. In Python they are handled by the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]},{"type":"text","value":" data type. Variables of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]},{"type":"text","value":" can only have two values: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Any bit of code that results in a Boolean value is called a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Boolean expression"}]},{"type":"text","value":". For example, the condition in a conditional statement is always a Boolean expression, and the words "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"condition"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Boolean expression"}]},{"type":"text","value":" can often be used interchangeably."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The result of a Boolean expression can be stored in a variable just like the result of any numerical calculation:"}]},{"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":"a "},{"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":"\ncondition "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" a "},{"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":"5"}]},{"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":"condition"},{"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":"if"}]},{"type":"text","value":" condition"},{"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":"\"a is less than 5\""}]},{"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\na is less than 5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The Python keywords "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" can also be used directly. In the following example the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command is executed every time, because the value of the condition is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":":"}]},{"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":"condition "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" condition"},{"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":"\"This is printed every time.\""}]},{"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":"This is printed every time."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A program like this is not very useful, but later on during the course you will see examples of Boolean variables coming in very handy."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Calculator","tmcname":"part01-25_calculator"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for two numbers and an operation. If the operation is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"add"}]},{"type":"text","value":", "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"multiply"}]},{"type":"text","value":" or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"subtract"}]},{"type":"text","value":", the program should calculate and print out the result of the operation with the given numbers. If the user types in anything else, the program should print out nothing."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Number 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":"17"}]},{"type":"text","value":"\nOperation: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"add"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"10 + 17 = 27"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Number 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nNumber 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nOperation: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"multiply"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4 * 6 = 24"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Number 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nNumber 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nOperation: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"subtract"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4 - 6 = -2"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Temperatures","tmcname":"part01-26_temperatures"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a temperature in degrees Fahrenheit, and then prints out the same in degrees Celsius. If the converted temperature falls below zero degrees Celsius, the program should also print out \"Brr! It's cold in here!\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The formula for converting degrees Fahrenheit to degrees Celsius can be found easily by any search engine of your choice."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Two examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a temperature (F): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"101"}]},{"type":"text","value":"\n101 degrees Fahrenheit equals 38.333333333333336 degrees Celsius"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a temperature (F): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"21"}]},{"type":"text","value":"\n21 degrees Fahrenheit equals -6.111111111111111 degrees Celsius\nBrr! It's cold in here!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Daily wages","tmcname":"part01-27_daily_wages"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for the hourly wage, hours worked, and the day of the week. The program should then print out the daily wages, which equal hourly wage multiplied by hours worked, except on Sundays when the hourly wage is doubled."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hourly wage: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8.5"}]},{"type":"text","value":"\nHours worked: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nDay of the week: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Monday"}]},{"type":"text","value":"\nDaily wages: 25.5 euros"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hourly wage: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"12.5"}]},{"type":"text","value":"\nHours worked: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nDay of the week: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Sunday"}]},{"type":"text","value":"\nDaily wages: 250.0 euros"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Loyalty bonus","tmcname":"part01-28_loyalty_bonus"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program calculates the end of year bonus a customer receives on their loyalty card. The bonus is calculated with the following formula:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If there are less than a hundred points on the card, the bonus is 10 %"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"In any other case the bonus is 15 %"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should work like this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many points are on your card? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"55"}]},{"type":"text","value":"\nYour bonus is 10 %\nYou now have 60.5 points"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"But there is a problem with the program, so with some inputs it doesn't work quite right:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many points are on your card? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"95"}]},{"type":"text","value":"\nYour bonus is 10 %\nYour bonus is 15 %\nYou now have 120.175 points"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please fix the program so that there is always either a 10 % or a 15 % bonus, but never both."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"What to wear tomorrow","tmcname":"part01-29_what_to_wear_tomorrow"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for tomorrow's weather forecast and then suggests weather-appropriate clothing."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The suggestion should change if the temperature (measured in degrees Celsius) is over 20, 10 or 5 degrees, and also if there is rain on the radar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is the weather forecast for tomorrow?\nTemperature: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"21"}]},{"type":"text","value":"\nWill it rain (yes/no): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"no"}]},{"type":"text","value":"\nWear jeans and a T-shirt"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is the weather forecast for tomorrow?\nTemperature: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"11"}]},{"type":"text","value":"\nWill it rain (yes/no): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"no"}]},{"type":"text","value":"\nWear jeans and a T-shirt\nI recommend a jumper as well"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is the weather forecast for tomorrow?\nTemperature: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nWill it rain (yes/no): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"no"}]},{"type":"text","value":"\nWear jeans and a T-shirt\nI recommend a jumper as well\nTake a jacket with you"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is the weather forecast for tomorrow?\nTemperature: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nWill it rain (yes/no): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"yes"}]},{"type":"text","value":"\nWear jeans and a T-shirt\nI recommend a jumper as well\nTake a jacket with you\nMake it a warm coat, actually\nI think gloves are in order\nDon't forget your umbrella!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Solving a quadratic equation","tmcname":"part01-30_quadratic_formula"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the Python "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" module there is the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":", which calculates the square root of a number. You can use it like so:"}]},{"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":"from"}]},{"type":"text","value":" math "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" sqrt\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":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"This should print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We will return to the concept of a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"module"}]},{"type":"text","value":" and the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement later. For now, it is sufficient to understand that the line "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"from math import sqrt"}]},{"type":"text","value":" allows us to use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":" function in our program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program for solving a quadratic equation of the form ax²+bx+c. The program asks for values a, b and c. It should then use the quadratic formula to solve the equation. The quadratic formula expressed with the Python "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":" function is as follows:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"x = (-b ± sqrt(b²-4ac))/(2a)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can assume the equation will always have two real roots, so the above formula will always work."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Value of a: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nValue of b: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nValue of c: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The roots are 2.0 and -4.0"}]}]}]},{"type":"comment","value":"\n\nA quiz to review the contents of this section:\n\n\n\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this week's materials. The questionnaire is worth one exercise point."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f"},"children":[]}]}]},"html":"

    After this section

    Thus far, every program we have written has been executed line by line in order. Instead of executing every line of code every single time a program is run, it is often useful to create sections of the program which are are only executed in certain situations.

    For example, the following code checks whether the user is of age:

    age = int(input(\"How old are you? \"))\n\nif age > 17:\n    print(\"You are of age!\")\n    print(\"Here's a copy of GTA6 for you.\")\n\nprint(\"Next customer, please!\")

    When the user is over the age of 17, the execution of the program should look like this:

    How old are you? 18\nYou are of age!\nHere's a copy of GTA6 for you.\nNext customer, please!

    If the user is 17 or under, only this is printed out:

    How old are you? 16\nNext customer, please!

    These examples show us that the value given as input affects which parts of the program are executed. The program contains a conditional statement with a block of code which is executed only if the condition in the statement is true.

    \n \n \n \n \n \"1\n \n

    In a conditional statement the keyword if is followed by a condition, such as a comparison of two values. The code block following this header line is only executed if the condition is true.

    Notice the colon character following the if header. In the following code there is no colon:

    age = 10\n\n# no colon at the end of the following line\nif age > 17\n    print(\"You are of age.\")

    Upon execution this causes an error:

    \n
    \nFile \"program.py\", line 3\n  if age > 17\n            ^\nSyntaxError: invalid syntax\n
    \n

    Comparison operators

    Very typically conditions consist of comparing two values. Here is a table with the most common comparison operators used in Python:

    \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\n\n\n\n\n\n\n
    OperatorPurposeExample
    ==Equal toa == b
    !=Not equal toa != b
    >Greater thana > b
    >=Greater than or equal toa >= b
    <Less thana < b
    <=Less than or equal toa <= b

    Let's have a look at a program which prints out different things based on whether the number the user inputs is negative, positive, or equal to zero:

    number = int(input(\"Please type in a number: \"))\n\nif number < 0:\n    print(\"The number is negative.\")\n\nif number > 0:\n    print(\"The number is positive.\")\n\nif number == 0:\n    print(\"The number is zero.\")

    Examples of how the program functions with three different inputs:

    Please type in a number: 15\nThe number is positive.

    Please type in a number: -18\nThe number is negative.

    Please type in a number: 0\nThe number is zero.

    Indentation

    Python recognises that a block of code is part of a conditional statement if each line of code in the block is indented the same. That is, there should be a bit of whitespace at the beginning of every line of code within the code block. Each line should have the same amount of whitespace.

    For example:

    password = input(\"Please type in a password: \")\n\nif password == \"kittycat\":\n    print(\"You knew the password!\")\n    print(\"You must be either the intended user...\")\n    print(\"...or quite an accomplished hacker.\")\n\nprint(\"The program has finished its execution. Thanks and bye!\")

    You can use the Tab key, short for tabulator key, to insert a set amount of whitespace.

    \n \n \n \n \n \"1\n \n

    Many text editors will automatically indent the following line when the Enter key is pressed after a colon character. When you want to end an indented code block you can use the Backspace key to return to the beginning of the line.

    \n \n \n \n \n \"1\n \n \n
    \nThe source of the keyboard pictures:\n Clker-Free-Vector-Images from Pixabay\n

    Please write a program which asks the user for an integer number. The program should print out \"Orwell\" if the number is exactly 1984, and otherwise do nothing.

    Please type in a number: 2020

    Please type in a number: 1984\nOrwell

    Please write a program which asks the user for an integer number. If the number is less than zero, the program should print out the number multiplied by -1. Otherwise the program prints out the number as is. Please have a look at the examples of expected behaviour below.

    Please type in a number: -7\nThe absolute value of this number is 7

    Please type in a number: 1\nThe absolute value of this number is 1

    Please type in a number: -99\nThe absolute value of this number is 99

    Please write a program which asks for the user's name. If the name is anything but \"Jerry\", the program then asks for the number of portions and prints out the total cost. The price of a single portion is 5.90.

    Two examples of the program's execution:

    Please tell me your name: Kramer\nHow many portions of soup? 2\nThe total cost is 11.8\nNext please!

    Please tell me your name: Jerry\nNext please!

    Please write a program which asks the user for an integer number. The program should then print out the magnitude of the number according to the following examples.

    Please type in a number: 950\nThis number is smaller than 1000\nThank you!

    Please type in a number: 59\nThis number is smaller than 1000\nThis number is smaller than 100\nThank you!

    Please type in a number: 2\nThis number is smaller than 1000\nThis number is smaller than 100\nThis number is smaller than 10\nThank you!

    Please type in a number: 1123\nThank you!

    Boolean values and Boolean expressions

    Any condition used in a conditional statement will result in a truth value, that is, either true or false. For example, the condition a < 5 is true if a is less than 5, and false if a is equal to or greater than 5.

    These types of values are often called Boolean values, named after the English mathematician George Boole. In Python they are handled by the bool data type. Variables of type bool can only have two values: True or False.

    Any bit of code that results in a Boolean value is called a Boolean expression. For example, the condition in a conditional statement is always a Boolean expression, and the words condition and Boolean expression can often be used interchangeably.

    The result of a Boolean expression can be stored in a variable just like the result of any numerical calculation:

    a = 3\ncondition = a < 5\nprint(condition)\nif condition:\n    print(\"a is less than 5\")

    True\na is less than 5

    The Python keywords True and False can also be used directly. In the following example the print command is executed every time, because the value of the condition is True:

    condition = True\nif condition:\n    print(\"This is printed every time.\")

    This is printed every time.

    A program like this is not very useful, but later on during the course you will see examples of Boolean variables coming in very handy.

    Please write a program which asks the user for two numbers and an operation. If the operation is add, multiply or subtract, the program should calculate and print out the result of the operation with the given numbers. If the user types in anything else, the program should print out nothing.

    Some examples of expected behaviour:

    Number 1: 10\nNumber 2: 17\nOperation: add

    10 + 17 = 27

    Number 1: 4\nNumber 2: 6\nOperation: multiply

    4 * 6 = 24

    Number 1: 4\nNumber 2: 6\nOperation: subtract

    4 - 6 = -2

    Please write a program which asks the user for a temperature in degrees Fahrenheit, and then prints out the same in degrees Celsius. If the converted temperature falls below zero degrees Celsius, the program should also print out \"Brr! It's cold in here!\".

    The formula for converting degrees Fahrenheit to degrees Celsius can be found easily by any search engine of your choice.

    Two examples of expected behaviour:

    Please type in a temperature (F): 101\n101 degrees Fahrenheit equals 38.333333333333336 degrees Celsius

    Please type in a temperature (F): 21\n21 degrees Fahrenheit equals -6.111111111111111 degrees Celsius\nBrr! It's cold in here!

    Please write a program which asks for the hourly wage, hours worked, and the day of the week. The program should then print out the daily wages, which equal hourly wage multiplied by hours worked, except on Sundays when the hourly wage is doubled.

    Hourly wage: 8.5\nHours worked: 3\nDay of the week: Monday\nDaily wages: 25.5 euros

    Hourly wage: 12.5\nHours worked: 10\nDay of the week: Sunday\nDaily wages: 250.0 euros

    This program calculates the end of year bonus a customer receives on their loyalty card. The bonus is calculated with the following formula:

    The program should work like this:

    How many points are on your card? 55\nYour bonus is 10 %\nYou now have 60.5 points

    But there is a problem with the program, so with some inputs it doesn't work quite right:

    How many points are on your card? 95\nYour bonus is 10 %\nYour bonus is 15 %\nYou now have 120.175 points

    Please fix the program so that there is always either a 10 % or a 15 % bonus, but never both.

    Please write a program which asks for tomorrow's weather forecast and then suggests weather-appropriate clothing.

    The suggestion should change if the temperature (measured in degrees Celsius) is over 20, 10 or 5 degrees, and also if there is rain on the radar.

    Some examples of expected behaviour:

    What is the weather forecast for tomorrow?\nTemperature: 21\nWill it rain (yes/no): no\nWear jeans and a T-shirt

    What is the weather forecast for tomorrow?\nTemperature: 11\nWill it rain (yes/no): no\nWear jeans and a T-shirt\nI recommend a jumper as well

    What is the weather forecast for tomorrow?\nTemperature: 7\nWill it rain (yes/no): no\nWear jeans and a T-shirt\nI recommend a jumper as well\nTake a jacket with you

    What is the weather forecast for tomorrow?\nTemperature: 3\nWill it rain (yes/no): yes\nWear jeans and a T-shirt\nI recommend a jumper as well\nTake a jacket with you\nMake it a warm coat, actually\nI think gloves are in order\nDon't forget your umbrella!

    In the Python math module there is the function sqrt, which calculates the square root of a number. You can use it like so:

    from math import sqrt\n\nprint(sqrt(9))

    This should print out

    3.0

    We will return to the concept of a module and the import statement later. For now, it is sufficient to understand that the line from math import sqrt allows us to use the sqrt function in our program.

    Please write a program for solving a quadratic equation of the form ax²+bx+c. The program asks for values a, b and c. It should then use the quadratic formula to solve the equation. The quadratic formula expressed with the Python sqrt function is as follows:

    x = (-b ± sqrt(b²-4ac))/(2a).

    You can assume the equation will always have two real roots, so the above formula will always work.

    An example of expected behaviour:

    Value of a: 1\nValue of b: 2\nValue of c: -8

    The roots are 2.0 and -4.0

    Please respond to a quick questionnaire on this week's materials. The questionnaire is worth one exercise point.

    ","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-1/5-conditional-statements.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 able to use a simple conditional statement in programming"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what a Boolean value is"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to express conditionals with comparison operators"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Thus far, every program we have written has been executed line by line in order. Instead of executing every line of code every single time a program is run, it is often useful to create sections of the program which are are only executed in certain situations."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, the following code checks whether the user is of age:"}]},{"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":"age "},{"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"]},"children":[{"type":"text","value":"\"How old are you? \""}]},{"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":"if"}]},{"type":"text","value":" age "},{"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":"17"}]},{"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":"\"You are of age!\""}]},{"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":"\"Here's a copy of GTA6 for you.\""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Next customer, please!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the user is over the age of 17, the execution of the program should look like this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How old are you? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"18"}]},{"type":"text","value":"\nYou are of age!\nHere's a copy of GTA6 for you.\nNext customer, please!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the user is 17 or under, only this is printed out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How old are you? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"16"}]},{"type":"text","value":"\nNext customer, please!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"These examples show us that the value given as input affects which parts of the program are executed. The program contains a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"conditional statement"}]},{"type":"text","value":" with a block of code which is executed only if the condition in the statement is true."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 483px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 126.52173913043478%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAZCAYAAAAxFw7TAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB/klEQVQ4y6WV564CIRCF9/0fSv9pYo29d2Pvvc/NR4JhkdVNLglBF+ZwzswBPAnRXq+XGg+Hg9zvd983u3muQEbX78ViIbfb7eN7IKC9sw56Pp/q/2AwkP1+75szNw5kCIvpdPqxezKZlG63+0HA7E7A6/Uqs9nsnTekNptNabVaqjcaDVmv13I+nz8l2ztoQAIAqtfrst1uJZfLSSqVkkQiocBXq5XUajUZDocyn89lNBqpdZ6rUkhmkQZfLpdSrVZVDtvttpJN8OPxeJM4nU6KgMfHy+WipDECBgiTduv3+2qdK03IZ2OPXQuFgsTjcUmn00oa9JmkUWGdjl6vJ+Px2FnlNyDSYrGYZLNZyWQyUqlUVH40QzOoVCrJZDL5DohEvHU8HlUetGxT8mazUTmFPVVGgWlwH6CrKORQF4XGb9MiBLK5E9BlG5uhBoAhOafCNiDKSNVPY2uGBAQxpHc6HVUDJyA3iikZRhQD21BlzGzfOkhWPgxzOZjM8K1r3lkU+8oyc4rkaDQqxWJRBesbKFSV7Q2QgnWQjr2QTZ7/BUj+OCkknxFw8658n+WwgNwwnKZ8Pi/lclkikYgqFG6gw5pzHgqQAEAJoFNl00Zfn4AgX3I8kQ3wbrfzFSXwxv4Gqv1o28peE4qhZkPu9FvjAvsJaJsXyTwN397lP2jZmkRqsNQNAAAAAElFTkSuQmCC'); 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":["/static/416cb381134e2a6b8d72caecc6260d5a/a0b58/1_5_1.webp 230w","/static/416cb381134e2a6b8d72caecc6260d5a/bc10c/1_5_1.webp 460w","/static/416cb381134e2a6b8d72caecc6260d5a/30f41/1_5_1.webp 483w"],"sizes":"(max-width: 483px) 100vw, 483px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/416cb381134e2a6b8d72caecc6260d5a/81c8e/1_5_1.png 230w","/static/416cb381134e2a6b8d72caecc6260d5a/08a84/1_5_1.png 460w","/static/416cb381134e2a6b8d72caecc6260d5a/77a9e/1_5_1.png 483w"],"sizes":"(max-width: 483px) 100vw, 483px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/416cb381134e2a6b8d72caecc6260d5a/77a9e/1_5_1.png","alt":"1 5 1","title":"1 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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In a conditional statement the keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" is followed by a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"condition"}]},{"type":"text","value":", such as a comparison of two values. The code block following this header line is only executed if the condition is true."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice the colon character following the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" header. In the following code there is no colon:"}]},{"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":"age "},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# no colon at the end of the following line"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" age "},{"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":"17"}]},{"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":"\"You are of age.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Upon execution this causes an error:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"File \"program.py\", line 3\n if age > 17\n ^\nSyntaxError: invalid syntax\n"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"h2","properties":{"id":"comparison-operators","style":"position:relative;"},"children":[{"type":"text","value":"Comparison operators"},{"type":"element","tagName":"a","properties":{"href":"#comparison-operators","ariaLabel":"comparison operators 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":"Very typically conditions consist of comparing two values. Here is a table with the most common comparison operators used in Python:"}]},{"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\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":"Operator"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Purpose"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Example"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Equal to"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a == b"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!="}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Not equal to"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a != b"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Greater than"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a > b"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">="}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Greater than or equal to"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a >= b"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Less than"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a < b"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<="}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Less than or equal to"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a <= b"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at a program which prints out different things based on whether the number the user inputs is negative, positive, or equal to zero:"}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is negative.\""}]},{"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":"if"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is positive.\""}]},{"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":"if"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is zero.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Examples of how the program functions with three different inputs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"15"}]},{"type":"text","value":"\nThe number is positive."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-18"}]},{"type":"text","value":"\nThe number is negative."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nThe number is zero."}]}]},{"type":"element","tagName":"h2","properties":{"id":"indentation","style":"position:relative;"},"children":[{"type":"text","value":"Indentation"},{"type":"element","tagName":"a","properties":{"href":"#indentation","ariaLabel":"indentation 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":"Python recognises that a block of code is part of a conditional statement if each line of code in the block is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"indented"}]},{"type":"text","value":" the same. That is, there should be a bit of whitespace at the beginning of every line of code within the code block. Each line should have the same amount of whitespace."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"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":"password "},{"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 password: \""}]},{"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":"if"}]},{"type":"text","value":" password "},{"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":"\"kittycat\""}]},{"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":"\"You knew the password!\""}]},{"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":"\"You must be either the intended user...\""}]},{"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":"\"...or quite an accomplished hacker.\""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The program has finished its execution. Thanks and bye!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can use the Tab key, short for "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"tabulator"}]},{"type":"text","value":" key, to insert a set amount of whitespace."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 537px; margin-bottom: 1rem;"},"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,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAEQklEQVQ4y4WT/TPbBxzHv39D7zZH57FDqdZQWykroYmQB/KAREhEHj0kCEHiKb6SSIR4SDwuw5W5YaImqnO7royutM1Zx7pq1TZ3G03cVrfK3Oqzy37aL7v99H7d+z6/vD+fzxsBgCMA2AeAg3/pf/H+/80iuy92v9VrW2eMBqN1espqNrQYFtr07WPWz2YGdGrdoqnTNDIzPWNBG9DFwb6PhsbHxofUKvXisGXYMj46PoI2oF+MjowODluGxzSodh65ab0JQX7BEBMVC0XC4mM3X4mMgWJhMfifDQBsIu5UWCACzzNeQCVRT3MZefDOW97AoDNPs6jZ4O3hA9xc7ht8cioE+gYBYn9of8rKYt2vlStXOg0dFj5HYFdW1y626dtv8Ni8DbSheUGLaj/hsPI39NrWebQB/ZTN5Ng7DB02RZVyKj+X+9DUaZorl8hmBFzhN8jdO3d/T8WTnQQi1alv61yiZ2Q5CViCs03fvsygMZw4TIqjRa1bFhaIHEkfJjubVepVaZH0l8Q4zG9oA/qoXCLbxsQnHTXWqTZKxJInyOSUFQLPeALeLwia8/mvos6Hg593IMgkssOIC5Hg7xUARYIiJyYOA95v+wCPzT8kppD+iZ3P4v6Rdj3txNfTD3IyWa9JeLIL2ZqadjZisNvl2azn1matzYBL2+bgST+bewdmuSzuJj0987nRYJwT8cTf0cj0HYOubb5YVGKnEKk/6jX6LyWF0hUKkbqnRbV3Kkorl5AHHw8dMNm8nRwmZzeDnvPEbDCuGcnUPTol64eB3gGbQq7Yyc3O3TYajLYauWKHnk5/ptPobU31TdsMGvOFBtUuqupUG1nU7J8aa1VLyESXGd6PvgqRoRHg5+EDUmkF5IRHQ5iXP5SVVjhxiTi4FBwOIp7Y4b6kOx43t8CRQaDAu96BwMnhHOEwKW98PHyBQWOeIF/1DRxdT6M4qemZh0QSzanXtj6TRccckvFkR19P/yo+OfUgJQn/sruje5VGph9gE3AOg67tXk4max8Tn3So0+gf5THZO0nxya9UdapNZGdick9LyVyr5InstTzRujGVZB9X1vcr65qsbEbeA3WTZs69NzaTvdZU3zTX3to+z80rWFdW1dq6O0zzfI5gTV5etWDqNH8u4onvIVbrLBAuXQbBB3GA9fSFi2cDQFxUusLKYv0aFnQRCDiiS1ggcrk5DUtwFQqKXO7HT7iaeFwilvzp9mMux/5VLpEdX4u5BsiCbeEoNjbhJQ5PdrwXccUZHhb1Wt3YfINKoq2HnAs9EXCFmzwO//sg32AXj8PfFBWIt84HhLgYNOaWpFC6GXIu9DiDQNmWSWWPIy5EHiFbm1tPy4rL7lfLqh4b9e23uo1dt+Tl8ts6tW7S3NWzUFOpWG1R62Z7Tb1zNZU1y9rmltv9Pf3TCrli2d1vy4Blqra67ut6ZcPiYN/gxN+K6mUiaF+/igAAAABJRU5ErkJggg=='); 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":["/static/43a34a2e4b0bc023009954d6f7809f7c/a0b58/1_5_keyboard.webp 230w","/static/43a34a2e4b0bc023009954d6f7809f7c/bc10c/1_5_keyboard.webp 460w","/static/43a34a2e4b0bc023009954d6f7809f7c/27c24/1_5_keyboard.webp 537w"],"sizes":"(max-width: 537px) 100vw, 537px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/43a34a2e4b0bc023009954d6f7809f7c/81c8e/1_5_keyboard.png 230w","/static/43a34a2e4b0bc023009954d6f7809f7c/08a84/1_5_keyboard.png 460w","/static/43a34a2e4b0bc023009954d6f7809f7c/b1cde/1_5_keyboard.png 537w"],"sizes":"(max-width: 537px) 100vw, 537px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/43a34a2e4b0bc023009954d6f7809f7c/b1cde/1_5_keyboard.png","alt":"1 5 keyboard","title":"1 5 keyboard","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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Many text editors will automatically indent the following line when the Enter key is pressed after a colon character. When you want to end an indented code block you can use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Backspace"}]},{"type":"text","value":" key to return to the beginning of the line."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 293px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-background-image"],"style":"padding-bottom: 96.08695652173913%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAATCAYAAACQjC21AAAACXBIWXMAAAsTAAALEwEAmpwYAAAEtElEQVQ4y3WU6U+UVxSHSdMvbbWNSgXZh2FmGAYYdnBBERA6SsdWZYdgK4ggOyrKElBR6oYoaFllHdZhUQGRalkTo8QUqMFGwCVubfzWf+Bp3kuaNDF9k5O89+Te555zfvcck7DQMAb6Boj4PoLw78LxcvWmsb6JI5m5hG4PZkdAEGnJqRia29gVFELUnnB2BocyYOwnISoGfYiOwC3bKC4oRvpMtvj401B3Aw9nT6R/maWciovlREUkopBvRuMYQHTkQa5cq0ahCcLHT4dK7U9DUxtbg+Nx89mJQrGJtEO5K8DYyDgmJ6bQ63YTFvqtgN7sG6Awv4ygwDj8A6PIzjrJUGUVP3jq+EkfT5kumqe19ZRFprHPJZhtXjrKymtWgE8X/mDm0QzTU9NMjE/y6uUrJN+D6Wnuj4/x8pe7fGhqZLamlme3Bng6Mc785ARPJqdYvHuH2YZaxvOO8WHw9gqw6EQRjvZqtE5uOFgrMHYZ2Ru2BzuZI3q5mnMBwXQbOtF6+KFx8Uat0qJWuqJSOKNUadG4+iCXqenJy4eZGUwK8gpE/by1PqjlTrQ2txETEYuvmy/Zji7sCgmjtbWdiN37iI+ME+IFbNqOm9oNT40HXs6eKK3kGIZGYGoSk8zDWdhZyFDaqbBYZ0l9TT36b/Qigngza6I2bqOhowcbCxlySzlyCxkOlnIcrFZMYavCzMyWxoYmGPsVk672LqLDY8hOz+ZQYgpzs3OcO3OO/QlJnEjNYProcRa6ukmMjONAYgoH07JJTMkgMTWT5NRM0pNSSdHpeVRQBO/eYXJnaIT3799zs/8mfcY+UcOHDx6yvLiEob2L/p4+jCcKeVFTx9+Dg7xu7+BDXz9/Go0sNzYxejSP3sxs/lpcWhHFTeNFd2c3fh4bUdqq+PrL9STE7ud65c8orBVCsNVfmQl444XLaNdZoHP1Zq/fVrwsZKjtHDG3lIs6C2DA5u30dveiC96Jj7uvAKQmpVJXXYevu5+4SLbBDmNXD9U19ezYsYvomARCQsLQOLnj4eKF0kZB+79AF5Urbc1tQmnp2Ziu/prYiDguX6zAxtwWlcyRLz5dRfONZiorKvnsk89FJhoHZ7RqN9w1Hjj8F1h9rZo3r99QefkqZ0+d5VTxaYZuDbHwZIHTJaVCoNKSUpYWl1heWqbiYgUpiSloFP8DvFJ+lUvny7l04SrLS8/FoJCiO3/2PG0tBl48f0FlRZXYU3ryDI9nHjM6Morc2kHAPgIqbJTYWzmwfo09NddrOfhjMl5ab8zXbGCT12aRqnRYSn3dKlNyMnJFBpLPTeP+MVAq+kqXqDG0GIiLihebXB21oksMre3odXoCtwSJgyWFJQzfHhaNII06yaTLOto6VoDSQorSfK0FTTeahdqWplbYmtvh77uVlsYWnJUuODlohGDHcvLoN/azdpUpMgt7ZJb2wi86RQJ2GjrpMHSK6CRx7o3eE6pL6+HBYd6+eSvS6WzvFPD5ud9ZfLaINFSkmp4uLqXweBHzc/MrwKorVWJcZaVlk3k4k+QDhxkducfI8Ahph9KF/0jWUWF5uXkk7k9ioHeAsftjpKekk38sn5yMHGZ/mxXAfwDrwXYWIBU21wAAAABJRU5ErkJggg=='); 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":["/static/7f8b7a68350e9455732bc0462f21ccff/a0b58/1_5_keyboard2.webp 230w","/static/7f8b7a68350e9455732bc0462f21ccff/2b735/1_5_keyboard2.webp 293w"],"sizes":"(max-width: 293px) 100vw, 293px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7f8b7a68350e9455732bc0462f21ccff/81c8e/1_5_keyboard2.png 230w","/static/7f8b7a68350e9455732bc0462f21ccff/3cf3e/1_5_keyboard2.png 293w"],"sizes":"(max-width: 293px) 100vw, 293px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/7f8b7a68350e9455732bc0462f21ccff/3cf3e/1_5_keyboard2.png","alt":"1 5 keyboard2","title":"1 5 keyboard2","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":"small","properties":{},"children":[{"type":"element","tagName":"center","properties":{},"children":[{"type":"text","value":"\nThe source of the keyboard pictures:\n "},{"type":"element","tagName":"a","properties":{"href":"https://pixabay.com/users/Clker-Free-Vector-Images-3736/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=311803"},"children":[{"type":"text","value":"Clker-Free-Vector-Images"}]},{"type":"text","value":" from "},{"type":"element","tagName":"a","properties":{"href":"https://pixabay.com/?utm_source=link-attribution&utm_medium=referral&utm_campaign=image&utm_content=311803"},"children":[{"type":"text","value":"Pixabay"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Orwell","tmcname":"part01-21_orwell"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for an integer number. The program should print out \"Orwell\" if the number is exactly 1984, and otherwise do nothing."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2020"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1984"}]},{"type":"text","value":"\nOrwell"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Absolute value","tmcname":"part01-22_absolute_value"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for an integer number. If the number is less than zero, the program should print out the number multiplied by -1. Otherwise the program prints out the number as is. Please have a look at the examples of expected behaviour below."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-7"}]},{"type":"text","value":"\nThe absolute value of this number is 7"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nThe absolute value of this number is 1"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-99"}]},{"type":"text","value":"\nThe absolute value of this number is 99"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Soup or no soup","tmcname":"part01-23_soup_or_no_soup"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for the user's name. If the name is anything but \"Jerry\", the program then asks for the number of portions and prints out the total cost. The price of a single portion is 5.90."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Two examples of the program's execution:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please tell me your name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Kramer"}]},{"type":"text","value":"\nHow many portions of soup? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nThe total cost is 11.8\nNext please!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please tell me your name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Jerry"}]},{"type":"text","value":"\nNext please!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Order of magnitude","tmcname":"part01-24_order_of_magnitude"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for an integer number. The program should then print out the magnitude of the number according to the following examples."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"950"}]},{"type":"text","value":"\nThis number is smaller than 1000\nThank you!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"59"}]},{"type":"text","value":"\nThis number is smaller than 1000\nThis number is smaller than 100\nThank you!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nThis number is smaller than 1000\nThis number is smaller than 100\nThis number is smaller than 10\nThank you!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1123"}]},{"type":"text","value":"\nThank you!"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"boolean-values-and-boolean-expressions","style":"position:relative;"},"children":[{"type":"text","value":"Boolean values and Boolean expressions"},{"type":"element","tagName":"a","properties":{"href":"#boolean-values-and-boolean-expressions","ariaLabel":"boolean values and boolean expressions 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":"Any condition used in a conditional statement will result in a truth value, that is, either true or false. For example, the condition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a < 5"}]},{"type":"text","value":" is true if "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" is less than 5, and false if "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" is equal to or greater than 5."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"These types of values are often called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Boolean"}]},{"type":"text","value":" values, named after the English mathematician George Boole. In Python they are handled by the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]},{"type":"text","value":" data type. Variables of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]},{"type":"text","value":" can only have two values: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Any bit of code that results in a Boolean value is called a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Boolean expression"}]},{"type":"text","value":". For example, the condition in a conditional statement is always a Boolean expression, and the words "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"condition"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Boolean expression"}]},{"type":"text","value":" can often be used interchangeably."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The result of a Boolean expression can be stored in a variable just like the result of any numerical calculation:"}]},{"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":"a "},{"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":"\ncondition "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" a "},{"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":"5"}]},{"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":"condition"},{"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":"if"}]},{"type":"text","value":" condition"},{"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":"\"a is less than 5\""}]},{"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\na is less than 5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The Python keywords "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" can also be used directly. In the following example the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command is executed every time, because the value of the condition is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":":"}]},{"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":"condition "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" condition"},{"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":"\"This is printed every time.\""}]},{"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":"This is printed every time."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A program like this is not very useful, but later on during the course you will see examples of Boolean variables coming in very handy."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Calculator","tmcname":"part01-25_calculator"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for two numbers and an operation. If the operation is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"add"}]},{"type":"text","value":", "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"multiply"}]},{"type":"text","value":" or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"subtract"}]},{"type":"text","value":", the program should calculate and print out the result of the operation with the given numbers. If the user types in anything else, the program should print out nothing."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Number 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":"17"}]},{"type":"text","value":"\nOperation: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"add"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"10 + 17 = 27"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Number 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nNumber 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nOperation: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"multiply"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4 * 6 = 24"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Number 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nNumber 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nOperation: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"subtract"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4 - 6 = -2"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Temperatures","tmcname":"part01-26_temperatures"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a temperature in degrees Fahrenheit, and then prints out the same in degrees Celsius. If the converted temperature falls below zero degrees Celsius, the program should also print out \"Brr! It's cold in here!\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The formula for converting degrees Fahrenheit to degrees Celsius can be found easily by any search engine of your choice."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Two examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a temperature (F): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"101"}]},{"type":"text","value":"\n101 degrees Fahrenheit equals 38.333333333333336 degrees Celsius"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a temperature (F): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"21"}]},{"type":"text","value":"\n21 degrees Fahrenheit equals -6.111111111111111 degrees Celsius\nBrr! It's cold in here!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Daily wages","tmcname":"part01-27_daily_wages"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for the hourly wage, hours worked, and the day of the week. The program should then print out the daily wages, which equal hourly wage multiplied by hours worked, except on Sundays when the hourly wage is doubled."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hourly wage: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8.5"}]},{"type":"text","value":"\nHours worked: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nDay of the week: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Monday"}]},{"type":"text","value":"\nDaily wages: 25.5 euros"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hourly wage: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"12.5"}]},{"type":"text","value":"\nHours worked: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nDay of the week: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Sunday"}]},{"type":"text","value":"\nDaily wages: 250.0 euros"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Loyalty bonus","tmcname":"part01-28_loyalty_bonus"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program calculates the end of year bonus a customer receives on their loyalty card. The bonus is calculated with the following formula:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If there are less than a hundred points on the card, the bonus is 10 %"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"In any other case the bonus is 15 %"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should work like this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many points are on your card? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"55"}]},{"type":"text","value":"\nYour bonus is 10 %\nYou now have 60.5 points"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"But there is a problem with the program, so with some inputs it doesn't work quite right:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many points are on your card? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"95"}]},{"type":"text","value":"\nYour bonus is 10 %\nYour bonus is 15 %\nYou now have 120.175 points"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please fix the program so that there is always either a 10 % or a 15 % bonus, but never both."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"What to wear tomorrow","tmcname":"part01-29_what_to_wear_tomorrow"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for tomorrow's weather forecast and then suggests weather-appropriate clothing."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The suggestion should change if the temperature (measured in degrees Celsius) is over 20, 10 or 5 degrees, and also if there is rain on the radar."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is the weather forecast for tomorrow?\nTemperature: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"21"}]},{"type":"text","value":"\nWill it rain (yes/no): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"no"}]},{"type":"text","value":"\nWear jeans and a T-shirt"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is the weather forecast for tomorrow?\nTemperature: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"11"}]},{"type":"text","value":"\nWill it rain (yes/no): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"no"}]},{"type":"text","value":"\nWear jeans and a T-shirt\nI recommend a jumper as well"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is the weather forecast for tomorrow?\nTemperature: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nWill it rain (yes/no): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"no"}]},{"type":"text","value":"\nWear jeans and a T-shirt\nI recommend a jumper as well\nTake a jacket with you"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is the weather forecast for tomorrow?\nTemperature: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nWill it rain (yes/no): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"yes"}]},{"type":"text","value":"\nWear jeans and a T-shirt\nI recommend a jumper as well\nTake a jacket with you\nMake it a warm coat, actually\nI think gloves are in order\nDon't forget your umbrella!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Solving a quadratic equation","tmcname":"part01-30_quadratic_formula"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the Python "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" module there is the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":", which calculates the square root of a number. You can use it like so:"}]},{"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":"from"}]},{"type":"text","value":" math "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" sqrt\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":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"This should print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We will return to the concept of a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"module"}]},{"type":"text","value":" and the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement later. For now, it is sufficient to understand that the line "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"from math import sqrt"}]},{"type":"text","value":" allows us to use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":" function in our program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program for solving a quadratic equation of the form ax²+bx+c. The program asks for values a, b and c. It should then use the quadratic formula to solve the equation. The quadratic formula expressed with the Python "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":" function is as follows:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"x = (-b ± sqrt(b²-4ac))/(2a)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can assume the equation will always have two real roots, so the above formula will always work."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Value of a: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nValue of b: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nValue of c: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The roots are 2.0 and -4.0"}]}]}]},{"type":"comment","value":"\n\nA quiz to review the contents of this section:\n\n\n\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this week's materials. The questionnaire is worth one exercise point."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f"},"children":[]}]}]},"html":"

    After this section

    Thus far, every program we have written has been executed line by line in order. Instead of executing every line of code every single time a program is run, it is often useful to create sections of the program which are are only executed in certain situations.

    For example, the following code checks whether the user is of age:

    age = int(input(\"How old are you? \"))\n\nif age > 17:\n    print(\"You are of age!\")\n    print(\"Here's a copy of GTA6 for you.\")\n\nprint(\"Next customer, please!\")

    When the user is over the age of 17, the execution of the program should look like this:

    How old are you? 18\nYou are of age!\nHere's a copy of GTA6 for you.\nNext customer, please!

    If the user is 17 or under, only this is printed out:

    How old are you? 16\nNext customer, please!

    These examples show us that the value given as input affects which parts of the program are executed. The program contains a conditional statement with a block of code which is executed only if the condition in the statement is true.

    \n \n \n \n \n \"1\n \n

    In a conditional statement the keyword if is followed by a condition, such as a comparison of two values. The code block following this header line is only executed if the condition is true.

    Notice the colon character following the if header. In the following code there is no colon:

    age = 10\n\n# no colon at the end of the following line\nif age > 17\n    print(\"You are of age.\")

    Upon execution this causes an error:

    \n
    \nFile \"program.py\", line 3\n  if age > 17\n            ^\nSyntaxError: invalid syntax\n
    \n

    Comparison operators

    Very typically conditions consist of comparing two values. Here is a table with the most common comparison operators used in Python:

    \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\n\n\n\n\n\n\n
    OperatorPurposeExample
    ==Equal toa == b
    !=Not equal toa != b
    >Greater thana > b
    >=Greater than or equal toa >= b
    <Less thana < b
    <=Less than or equal toa <= b

    Let's have a look at a program which prints out different things based on whether the number the user inputs is negative, positive, or equal to zero:

    number = int(input(\"Please type in a number: \"))\n\nif number < 0:\n    print(\"The number is negative.\")\n\nif number > 0:\n    print(\"The number is positive.\")\n\nif number == 0:\n    print(\"The number is zero.\")

    Examples of how the program functions with three different inputs:

    Please type in a number: 15\nThe number is positive.

    Please type in a number: -18\nThe number is negative.

    Please type in a number: 0\nThe number is zero.

    Indentation

    Python recognises that a block of code is part of a conditional statement if each line of code in the block is indented the same. That is, there should be a bit of whitespace at the beginning of every line of code within the code block. Each line should have the same amount of whitespace.

    For example:

    password = input(\"Please type in a password: \")\n\nif password == \"kittycat\":\n    print(\"You knew the password!\")\n    print(\"You must be either the intended user...\")\n    print(\"...or quite an accomplished hacker.\")\n\nprint(\"The program has finished its execution. Thanks and bye!\")

    You can use the Tab key, short for tabulator key, to insert a set amount of whitespace.

    \n \n \n \n \n \"1\n \n

    Many text editors will automatically indent the following line when the Enter key is pressed after a colon character. When you want to end an indented code block you can use the Backspace key to return to the beginning of the line.

    \n \n \n \n \n \"1\n \n \n
    \nThe source of the keyboard pictures:\n Clker-Free-Vector-Images from Pixabay\n

    Please write a program which asks the user for an integer number. The program should print out \"Orwell\" if the number is exactly 1984, and otherwise do nothing.

    Please type in a number: 2020

    Please type in a number: 1984\nOrwell

    Please write a program which asks the user for an integer number. If the number is less than zero, the program should print out the number multiplied by -1. Otherwise the program prints out the number as is. Please have a look at the examples of expected behaviour below.

    Please type in a number: -7\nThe absolute value of this number is 7

    Please type in a number: 1\nThe absolute value of this number is 1

    Please type in a number: -99\nThe absolute value of this number is 99

    Please write a program which asks for the user's name. If the name is anything but \"Jerry\", the program then asks for the number of portions and prints out the total cost. The price of a single portion is 5.90.

    Two examples of the program's execution:

    Please tell me your name: Kramer\nHow many portions of soup? 2\nThe total cost is 11.8\nNext please!

    Please tell me your name: Jerry\nNext please!

    Please write a program which asks the user for an integer number. The program should then print out the magnitude of the number according to the following examples.

    Please type in a number: 950\nThis number is smaller than 1000\nThank you!

    Please type in a number: 59\nThis number is smaller than 1000\nThis number is smaller than 100\nThank you!

    Please type in a number: 2\nThis number is smaller than 1000\nThis number is smaller than 100\nThis number is smaller than 10\nThank you!

    Please type in a number: 1123\nThank you!

    Boolean values and Boolean expressions

    Any condition used in a conditional statement will result in a truth value, that is, either true or false. For example, the condition a < 5 is true if a is less than 5, and false if a is equal to or greater than 5.

    These types of values are often called Boolean values, named after the English mathematician George Boole. In Python they are handled by the bool data type. Variables of type bool can only have two values: True or False.

    Any bit of code that results in a Boolean value is called a Boolean expression. For example, the condition in a conditional statement is always a Boolean expression, and the words condition and Boolean expression can often be used interchangeably.

    The result of a Boolean expression can be stored in a variable just like the result of any numerical calculation:

    a = 3\ncondition = a < 5\nprint(condition)\nif condition:\n    print(\"a is less than 5\")

    True\na is less than 5

    The Python keywords True and False can also be used directly. In the following example the print command is executed every time, because the value of the condition is True:

    condition = True\nif condition:\n    print(\"This is printed every time.\")

    This is printed every time.

    A program like this is not very useful, but later on during the course you will see examples of Boolean variables coming in very handy.

    Please write a program which asks the user for two numbers and an operation. If the operation is add, multiply or subtract, the program should calculate and print out the result of the operation with the given numbers. If the user types in anything else, the program should print out nothing.

    Some examples of expected behaviour:

    Number 1: 10\nNumber 2: 17\nOperation: add

    10 + 17 = 27

    Number 1: 4\nNumber 2: 6\nOperation: multiply

    4 * 6 = 24

    Number 1: 4\nNumber 2: 6\nOperation: subtract

    4 - 6 = -2

    Please write a program which asks the user for a temperature in degrees Fahrenheit, and then prints out the same in degrees Celsius. If the converted temperature falls below zero degrees Celsius, the program should also print out \"Brr! It's cold in here!\".

    The formula for converting degrees Fahrenheit to degrees Celsius can be found easily by any search engine of your choice.

    Two examples of expected behaviour:

    Please type in a temperature (F): 101\n101 degrees Fahrenheit equals 38.333333333333336 degrees Celsius

    Please type in a temperature (F): 21\n21 degrees Fahrenheit equals -6.111111111111111 degrees Celsius\nBrr! It's cold in here!

    Please write a program which asks for the hourly wage, hours worked, and the day of the week. The program should then print out the daily wages, which equal hourly wage multiplied by hours worked, except on Sundays when the hourly wage is doubled.

    Hourly wage: 8.5\nHours worked: 3\nDay of the week: Monday\nDaily wages: 25.5 euros

    Hourly wage: 12.5\nHours worked: 10\nDay of the week: Sunday\nDaily wages: 250.0 euros

    This program calculates the end of year bonus a customer receives on their loyalty card. The bonus is calculated with the following formula:

    The program should work like this:

    How many points are on your card? 55\nYour bonus is 10 %\nYou now have 60.5 points

    But there is a problem with the program, so with some inputs it doesn't work quite right:

    How many points are on your card? 95\nYour bonus is 10 %\nYour bonus is 15 %\nYou now have 120.175 points

    Please fix the program so that there is always either a 10 % or a 15 % bonus, but never both.

    Please write a program which asks for tomorrow's weather forecast and then suggests weather-appropriate clothing.

    The suggestion should change if the temperature (measured in degrees Celsius) is over 20, 10 or 5 degrees, and also if there is rain on the radar.

    Some examples of expected behaviour:

    What is the weather forecast for tomorrow?\nTemperature: 21\nWill it rain (yes/no): no\nWear jeans and a T-shirt

    What is the weather forecast for tomorrow?\nTemperature: 11\nWill it rain (yes/no): no\nWear jeans and a T-shirt\nI recommend a jumper as well

    What is the weather forecast for tomorrow?\nTemperature: 7\nWill it rain (yes/no): no\nWear jeans and a T-shirt\nI recommend a jumper as well\nTake a jacket with you

    What is the weather forecast for tomorrow?\nTemperature: 3\nWill it rain (yes/no): yes\nWear jeans and a T-shirt\nI recommend a jumper as well\nTake a jacket with you\nMake it a warm coat, actually\nI think gloves are in order\nDon't forget your umbrella!

    In the Python math module there is the function sqrt, which calculates the square root of a number. You can use it like so:

    from math import sqrt\n\nprint(sqrt(9))

    This should print out

    3.0

    We will return to the concept of a module and the import statement later. For now, it is sufficient to understand that the line from math import sqrt allows us to use the sqrt function in our program.

    Please write a program for solving a quadratic equation of the form ax²+bx+c. The program asks for values a, b and c. It should then use the quadratic formula to solve the equation. The quadratic formula expressed with the Python sqrt function is as follows:

    x = (-b ± sqrt(b²-4ac))/(2a).

    You can assume the equation will always have two real roots, so the above formula will always work.

    An example of expected behaviour:

    Value of a: 1\nValue of b: 2\nValue of c: -8

    The roots are 2.0 and -4.0

    Please respond to a quick questionnaire on this week's materials. The questionnaire is worth one exercise point.

    ","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-1/5-conditional-statements.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-1/page-data.json b/page-data/part-1/page-data.json index 25da50c22..fc2e2d7c5 100644 --- a/page-data/part-1/page-data.json +++ b/page-data/part-1/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/part-1", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-1","title":"Part 1"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-1/index.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-1","title":"Part 1"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-1/index.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-10/1-class-hierarchies/page-data.json b/page-data/part-10/1-class-hierarchies/page-data.json index 617ffb195..6e14d8e8a 100644 --- a/page-data/part-10/1-class-hierarchies/page-data.json +++ b/page-data/part-10/1-class-hierarchies/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-10/1-class-hierarchies", - "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 know what inheritance means in a programming context"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to write classes which inherit other classes"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how inheritance affects the traits in classes"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"special-classes-for-special-purposes","style":"position:relative;"},"children":[{"type":"text","value":"Special classes for special purposes"},{"type":"element","tagName":"a","properties":{"href":"#special-classes-for-special-purposes","ariaLabel":"special classes for special purposes 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":"Sometimes you come across a situation where you have already defined a class, but then realize you need special traits in some, but not all, instances of the class. Then again, sometimes you realize you've defined two very similar classes with only minor differences. As programmers we aim to always repeat ourselves as little as possible, while maintaining clarity and readability. So how can we accommodate for different implementations of intrinsically similar objects?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at two class definitions: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Teacher"}]},{"type":"text","value":". Getter and setter methods have been left out for now, in order to keep the example short."}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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":"id"}]},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" credits"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"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":"id"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" email\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" credits\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Teacher"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" room"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" teaching_years"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" email\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"room "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" room\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"teaching_years "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" teaching_years\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Even in a stripped down example, like the above, we already have quite a bit of repetition: both classes contain attributes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"email"}]},{"type":"text","value":". It would be a good idea to have a single attribute definition, so that a single function would suffice for editing both attributes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, imagine the school's email address changed. All addresses would have to be updated. We "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"could"}]},{"type":"text","value":" write two separate versions of essentially the same 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":"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":"update_email"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"o"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Student"},{"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 o"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" o"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email"},{"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":"\".com\""}]},{"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":"\".edu\""}]},{"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":"update_email2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"o"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Teacher"},{"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 o"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" o"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email"},{"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":"\".com\""}]},{"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":"\".edu\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Writing practically the same thing twice is unnecessary repetition, not to mention it doubles the possibilities for errors. It would be a definite improvement if we could use a single function to work with instances of both classes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Both classes also have attributes which are unique to them. Simply combining "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"all"}]},{"type":"text","value":" attributes in a single class would mean "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"all"}]},{"type":"text","value":" instances of the class would then have unnecessary attributes, just different ones for different instances. That doesn't seem like an ideal situation, either."}]},{"type":"element","tagName":"h2","properties":{"id":"inheritance","style":"position:relative;"},"children":[{"type":"text","value":"Inheritance"},{"type":"element","tagName":"a","properties":{"href":"#inheritance","ariaLabel":"inheritance 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":"Object oriented programming languages usually feature a technique called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inheritance"}]},{"type":"text","value":". A class can "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inherit"}]},{"type":"text","value":" the traits of another class. In addition to these inherited traits a class can also contain traits which are unique to it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Knowing this, it would make sense for the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Teacher"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" classes to have a common base or parent class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":":"}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" email\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The new class contains those traits which are shared by the other two classes. Now "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Teacher"}]},{"type":"text","value":" can "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inherit"}]},{"type":"text","value":" these traits and add their own besides."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The syntax for inheritance simply involves adding the base class name in parentheses on the header line:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" email\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":"update_email_domain"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" new_domain"},{"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":"str"}]},{"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 old_domain "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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":"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email"},{"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":"text","value":"old_domain"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" new_domain"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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":"id"}]},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" credits"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"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":"id"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" email\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" credits\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Teacher"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" room"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" teaching_years"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" email\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"room "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" room\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"teaching_years "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" teaching_years\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Let's test our classes"}]},{"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 saul "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saul Student\""}]},{"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":"\"1234\""}]},{"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":"\"saul@example.com\""}]},{"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n saul"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"update_email_domain"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"example.edu\""}]},{"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":"saul"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n tara "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Teacher"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tara Teacher\""}]},{"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":"\"tara@example.fi\""}]},{"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":"\"A123\""}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n tara"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"update_email_domain"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"example.ex\""}]},{"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":"tara"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Both "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Teacher"}]},{"type":"text","value":" inherit the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" class, so both have the traits defined in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" class, including the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"update_email_domain"}]},{"type":"text","value":". The same method works for instances of both the derived classes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at another example. We have a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bookshelf"}]},{"type":"text","value":" which inherits the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BookContainer"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Book"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" This class models a simple book \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" author"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"author "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" author\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BookContainer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" This class models a container for books \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"books "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_book"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Book"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"books"},{"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":"book"},{"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":"list_books"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"for"}]},{"type":"text","value":" book "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"books"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"author"},{"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\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bookshelf"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"BookContainer"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" This class models a shelf for books \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","builtin"]},"children":[{"type":"text","value":"super"}]},{"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":"__init__"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_book"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" location"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"books"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"insert"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"location"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bookshelf"}]},{"type":"text","value":" contains the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_book"}]},{"type":"text","value":". A method with the same name is defined in the base class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BookContainer"}]},{"type":"text","value":". This is called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"overriding"}]},{"type":"text","value":": if a derived class has a method with the same name as the base class, the derived version overrides the original in instances of the derived class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The idea in the example above is that a new book added to a BookContainer always goes to the top, but with a Bookshelf you can specify the location yourself. The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list_books"}]},{"type":"text","value":" works the same for both classes, as there is no overriding method in the derived class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's try out these classes:"}]},{"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":"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":"# Create some books for testing"}]},{"type":"text","value":"\n b1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Old Man and the Sea\""}]},{"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":"\"Ernest Hemingway\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Silent Spring\""}]},{"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":"\"Rachel Carson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pride and Prejudice\""}]},{"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":"\"Jane Austen\""}]},{"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":"# Create a BookContainer and add the books"}]},{"type":"text","value":"\n container "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BookContainer"},{"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 container"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n container"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n container"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b3"},{"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":"# Create a Bookshelf and add the books (always to the beginning)"}]},{"type":"text","value":"\n shelf "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bookshelf"},{"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 shelf"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b1"},{"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n shelf"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b2"},{"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n shelf"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b3"},{"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Tulostetaan"}]},{"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":"\"Container:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n container"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"list_books"},{"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":"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\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":"\"Shelf:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n shelf"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"list_books"},{"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":" "},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Container:\nOld Man and the Sea (Ernest Hemingway)\nSilent Spring (Rachel Carson)\nPride and Prejudice (Jane Austen)"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Shelf:\nPride and Prejudice (Jane Austen)\nSilent Spring (Rachel Carson)\nOld Man and the Sea (Ernest Hemingway)"}]},{"type":"text","value":" "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, the Bookshelf class also has access to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list_books"}]},{"type":"text","value":" method. Through inheritance the method is a member of all the classes derived from the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BookContainer"}]},{"type":"text","value":" class."}]},{"type":"element","tagName":"h2","properties":{"id":"inheritance-and-scope-of-traits","style":"position:relative;"},"children":[{"type":"text","value":"Inheritance and scope of traits"},{"type":"element","tagName":"a","properties":{"href":"#inheritance-and-scope-of-traits","ariaLabel":"inheritance and scope of traits 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":"A derived class inherits all traits from its base class. Those traits are directly accessible in the derived class, unless they have been defined as private in the base class (with two underscores before the name of the trait)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As the attributes of a Bookshelf are identical to a BookContainer, there was no need to rewrite the constructor of Bookshelf. We simply called the constructor of the base class:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bookshelf"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"BookContainer"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","builtin"]},"children":[{"type":"text","value":"super"}]},{"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":"__init__"},{"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":"p","properties":{},"children":[{"type":"text","value":"Any trait in the base class can be accessed from the derived class with the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"super()"}]},{"type":"text","value":". The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" argument is left out from the method call, as Python adds it automatically."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"But what if the attributes are not identical; can we still use the base class constructor in some way? Let's have a look at a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Thesis"}]},{"type":"text","value":" which inherits the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Book"}]},{"type":"text","value":" class. The derived class "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"can"}]},{"type":"text","value":" still call the constructor from the base class:"}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Book"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" This class models a simple book \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" author"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"author "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" author\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Thesis"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Book"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" This class models a graduate thesis \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" author"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" grade"},{"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","builtin"]},"children":[{"type":"text","value":"super"}]},{"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":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" author"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"grade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" grade\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The constructor in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Thesis"}]},{"type":"text","value":" class calls the constructor in the base class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Book"}]},{"type":"text","value":" with the arguments for "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"author"}]},{"type":"text","value":". Additionally, the constructor in the derived class sets the value for the attribute "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"grade"}]},{"type":"text","value":". This naturally cannot be a part of the base class constructor, as the base class has no such attribute."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The above class can be used 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":"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 thesis "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Thesis"},{"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 and the Universe\""}]},{"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":"\"Peter Pythons\""}]},{"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":"3"}]},{"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":"# Print out the values of the attributes"}]},{"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":"thesis"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"thesis"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"author"},{"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":"thesis"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"grade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python and the Universe\nPeter Pythons\n3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Even if a derived class "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"overrides"}]},{"type":"text","value":" a method in its base class, the derived class can "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"still"}]},{"type":"text","value":" call the overridden method in the base class. In the following example we have a basic "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BonusCard"}]},{"type":"text","value":" and a special "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PlatinumCard"}]},{"type":"text","value":" for especially loyal customers. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"calculate_bonus"}]},{"type":"text","value":" method is overridden in the derived class, but the overriding method calls the base method:"}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Product"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" price"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"price "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" price\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BonusCard"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"products_bought "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_product"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Product"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"products_bought"},{"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":"product"},{"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":"calculate_bonus"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 bonus "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"products_bought"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"price "},{"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.05"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" bonus\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PlatinumCard"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"BonusCard"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","builtin"]},"children":[{"type":"text","value":"super"}]},{"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":"__init__"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"calculate_bonus"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# Call the method in the base class"}]},{"type":"text","value":"\n bonus "},{"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":"super"}]},{"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":"calculate_bonus"},{"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","comment"]},"children":[{"type":"text","value":"# ...and add five percent to the total"}]},{"type":"text","value":"\n bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" bonus "},{"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.05"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" bonus"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, the bonus for a PlatinumCard is calculated by calling the overriden method in the base class, and then adding an extra 5 percent to the base result. An example of how these classes are used:"}]},{"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":"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 card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BonusCard"},{"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 card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bananas\""}]},{"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":"6.50"}]},{"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 card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Satsumas\""}]},{"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":"7.95"}]},{"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 bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"calculate_bonus"},{"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 card2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PlatinumCard"},{"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 card2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bananas\""}]},{"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":"6.50"}]},{"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 card2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Satsumas\""}]},{"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":"7.95"}]},{"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 bonus2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" card2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"calculate_bonus"},{"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":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bonus"},{"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":"bonus2"},{"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.7225\n0.7586250000000001"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Laptop computer","tmcname":"part10-01_laptop_computer"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains a class definition for a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Computer"}]},{"type":"text","value":", which has the attributes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"model"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"speed"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please define a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LaptopComputer"}]},{"type":"text","value":" which "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inherits"}]},{"type":"text","value":" the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Computer"}]},{"type":"text","value":". The constructor of the new class should take a third argument: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"weight"}]},{"type":"text","value":", of type integer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please also include a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method in your class definition. See the example below for the expected format of the string representation printed out."}]},{"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":"laptop "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LaptopComputer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"NoteBook Pro15\""}]},{"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":"1500"}]},{"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":"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":"laptop"},{"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":"NoteBook Pro15, 1500 MHz, 2 kg"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Game Museum","tmcname":"part10-02_game_museum"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains class definitions for a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ComputerGame"}]},{"type":"text","value":" and a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"GameWarehouse"}]},{"type":"text","value":". A GameWarehouse object is used to store ComputerGame objects."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please familiarize yourself with these classes. Then define a new class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"GameMuseum"}]},{"type":"text","value":" which inherits the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"GameWarehouse"}]},{"type":"text","value":" class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The GameMuseum class should "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"override"}]},{"type":"text","value":" the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list_games()"}]},{"type":"text","value":" method, so that it returns a list of only those games which were made before the year 1990."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The new class should also have a constructor which "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"calls the constructor from the parent class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"GameWarehouse"}]}]},{"type":"text","value":". The constructor takes no arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may use the following code to test your implementation:"}]},{"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":"museum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" GameMuseum"},{"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":"\nmuseum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_game"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ComputerGame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pacman\""}]},{"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":"\"Namco\""}]},{"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":"1980"}]},{"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":"\nmuseum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_game"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ComputerGame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"GTA 2\""}]},{"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":"\"Rockstar\""}]},{"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":"1999"}]},{"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":"\nmuseum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_game"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ComputerGame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bubble Bobble\""}]},{"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":"\"Taito\""}]},{"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":"1986"}]},{"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":"for"}]},{"type":"text","value":" game "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" museum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"list_games"},{"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 "},{"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":"game"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"Pacman\nBubble Bobble"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Areas","tmcname":"part10-03_areas"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains a class definition for a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rectangle"}]},{"type":"text","value":". It represents a "},{"type":"element","tagName":"a","properties":{"href":"https://en.wikipedia.org/wiki/Rectangle","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"rectangle shape"}]},{"type":"text","value":". A Rectangle works 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":"text","value":"rectangle "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rectangle"},{"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":" "},{"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":"\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":"rectangle"},{"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":"\"area:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"area"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"rectangle 2x3\narea: 6"}]}]},{"type":"element","tagName":"h2","properties":{"id":"square","style":"position:relative;"},"children":[{"type":"text","value":"Square"},{"type":"element","tagName":"a","properties":{"href":"#square","ariaLabel":"square 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":"Please define a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Square"}]},{"type":"text","value":" which inherits the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rectangle"}]},{"type":"text","value":" class. The sides of a "},{"type":"element","tagName":"a","properties":{"href":"https://en.wikipedia.org/wiki/Square","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"square"}]},{"type":"text","value":" are all of equal length, which makes the square a special case of the rectangle. The new class should not contain any new attributes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A Square object is used as follows:"}]},{"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":"square "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Square"},{"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":"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":"square"},{"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":"\"area:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" square"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"area"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"square 4x4\narea: 16"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Word game","tmcname":"part10-04_word_game"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the class definition for a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"WordGame"}]},{"type":"text","value":". It provides some basic functionality for playing different word-based games:"}]},{"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":"import"}]},{"type":"text","value":" random\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"WordGame"}]},{"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 "},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rounds"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"wins1 "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"wins2 "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rounds "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rounds\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":"round_winner"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player1_word"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player2_word"},{"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":"str"}]},{"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":"# determine a random winner"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" random"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"randint"},{"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":"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\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":"play"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"\"Word game:\""}]},{"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":" 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":"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rounds"},{"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":"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\"round "}]},{"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","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 answer1 "},{"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":"\"player1: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n answer2 "},{"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":"\"player2: \""}]},{"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":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"round_winner"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"answer1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" answer2"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"wins1 "},{"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":"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":"\"player 1 won\""}]},{"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":"elif"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"round_winner"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"answer1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" answer2"},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"wins2 "},{"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":"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":"\"player 2 won\""}]},{"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":"else"}]},{"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":"pass"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# it's a tie"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"game over, wins:\""}]},{"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\"player 1: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"wins1"},{"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 "},{"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\"player 2: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"wins2"},{"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":"p","properties":{},"children":[{"type":"text","value":"The game is played as follows:"}]},{"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":"p "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" WordGame"},{"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":"\np"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"play"},{"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":"Word game:\nround 1\nplayer1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"longword"}]},{"type":"text","value":"\nplayer2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"??"}]},{"type":"text","value":"\nplayer 2 won\nround 2\nplayer1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"i'm the best"}]},{"type":"text","value":"\nplayer2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"wut?"}]},{"type":"text","value":"\nplayer 1 won\nround 3\nplayer1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"who's gonna win"}]},{"type":"text","value":"\nplayer2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"me"}]},{"type":"text","value":"\nplayer 1 won\ngame over, wins:\nplayer 1: 2\nplayer 2: 1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this \"basic\" version of the game the winner is determined randomly. The input from the players has no effect on the result."}]},{"type":"element","tagName":"h2","properties":{"id":"longest-word-wins","style":"position:relative;"},"children":[{"type":"text","value":"Longest word wins"},{"type":"element","tagName":"a","properties":{"href":"#longest-word-wins","ariaLabel":"longest word wins 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":"Please define a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LongestWord"}]},{"type":"text","value":". It is a version of the game where whoever types in the longest word on each round wins."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The new version of the game is implemented by "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inheriting"}]},{"type":"text","value":" the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"WordGame"}]},{"type":"text","value":" class. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"round_winner"}]},{"type":"text","value":" method should also be suitably overridden. The outline of the new class is as follows:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"LongestWord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"WordGame"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rounds"},{"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","builtin"]},"children":[{"type":"text","value":"super"}]},{"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":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rounds"},{"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":"round_winner"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player1_word"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player2_word"},{"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":"str"}]},{"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 code for determining the winner goes here"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how the new game is played:"}]},{"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":"p "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LongestWord"},{"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":"\np"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"play"},{"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":"Word game:\nround 1\nplayer1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"short"}]},{"type":"text","value":"\nplayer2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"longword"}]},{"type":"text","value":"\nplayer 2 won\nround 2\nplayer1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"word"}]},{"type":"text","value":"\nplayer2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"wut?"}]},{"type":"text","value":"\nround 3\nplayer1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"i'm the best"}]},{"type":"text","value":"\nplayer2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"no, me"}]},{"type":"text","value":"\nplayer 1 won\ngame over, wins:\nplayer 1: 1\nplayer 2: 1"}]}]},{"type":"element","tagName":"h2","properties":{"id":"most-vowels-wins","style":"position:relative;"},"children":[{"type":"text","value":"Most vowels wins"},{"type":"element","tagName":"a","properties":{"href":"#most-vowels-wins","ariaLabel":"most vowels wins 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":"Please define another WordGame class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MostVowels"}]},{"type":"text","value":". In this version of the game whoever has squeezed more vowels into their word wins the round."}]},{"type":"element","tagName":"h2","properties":{"id":"rock-paper-scissors","style":"position:relative;"},"children":[{"type":"text","value":"Rock paper scissors"},{"type":"element","tagName":"a","properties":{"href":"#rock-paper-scissors","ariaLabel":"rock paper scissors 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":"Finally, please define a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"RockPaperScissors"}]},{"type":"text","value":" which allows you to play a game of "},{"type":"element","tagName":"a","properties":{"href":"https://en.wikipedia.org/wiki/Rock_paper_scissors","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"rock paper scissors"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The rules of the game are as follows:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"rock beats scissors (the rock can break the scissors but the scissors can't cut the rock)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"paper beats rock (the paper can cover the rock)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"scissors beats paper (the scissors can cut the paper)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the input from either player is invalid, they lose the round. If both players type in something else than "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"rock"}]},{"type":"text","value":", "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"paper"}]},{"type":"text","value":" or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"scissors"}]},{"type":"text","value":", the result is a tie."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how the game is played:"}]},{"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":"p "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RockPaperScissors"},{"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":"text","value":"\np"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"play"},{"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":"Word game:\nround 1\nplayer1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"rock"}]},{"type":"text","value":"\nplayer2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"rock"}]},{"type":"text","value":"\nround 2\nplayer1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"rock"}]},{"type":"text","value":"\nplayer2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"paper"}]},{"type":"text","value":"\nplayer 2 won\nround 3\nplayer1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"scissors"}]},{"type":"text","value":"\nplayer2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"paper"}]},{"type":"text","value":"\nplayer 1 won\nround 4\nplayer1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"paper"}]},{"type":"text","value":"\nplayer2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"dynamite"}]},{"type":"text","value":"\nplayer 1 won\ngame over, wins:\nplayer 1: 2\nplayer 2: 1"}]}]}]}]},"html":"

    After this section

    Special classes for special purposes

    Sometimes you come across a situation where you have already defined a class, but then realize you need special traits in some, but not all, instances of the class. Then again, sometimes you realize you've defined two very similar classes with only minor differences. As programmers we aim to always repeat ourselves as little as possible, while maintaining clarity and readability. So how can we accommodate for different implementations of intrinsically similar objects?

    Let's have a look at two class definitions: Student and Teacher. Getter and setter methods have been left out for now, in order to keep the example short.

    \nclass Student:\n\n    def __init__(self, name: str, id: str, email: str, credits: str):\n        self.name = name\n        self.id = id\n        self.email = email\n        self.credits = credits\n\nclass Teacher:\n\n    def __init__(self, name: str, email: str, room: str, teaching_years: int):\n        self.name = name\n        self.email = email\n        self.room = room\n        self.teaching_years = teaching_years\n

    Even in a stripped down example, like the above, we already have quite a bit of repetition: both classes contain attributes name and email. It would be a good idea to have a single attribute definition, so that a single function would suffice for editing both attributes.

    For example, imagine the school's email address changed. All addresses would have to be updated. We could write two separate versions of essentially the same function:

    \ndef update_email(o: Student):\n    o.email = o.email.replace(\".com\", \".edu\")\n\ndef update_email2(o: Teacher):\n    o.email = o.email.replace(\".com\", \".edu\")\n

    Writing practically the same thing twice is unnecessary repetition, not to mention it doubles the possibilities for errors. It would be a definite improvement if we could use a single function to work with instances of both classes.

    Both classes also have attributes which are unique to them. Simply combining all attributes in a single class would mean all instances of the class would then have unnecessary attributes, just different ones for different instances. That doesn't seem like an ideal situation, either.

    Inheritance

    Object oriented programming languages usually feature a technique called inheritance. A class can inherit the traits of another class. In addition to these inherited traits a class can also contain traits which are unique to it.

    Knowing this, it would make sense for the Teacher and Student classes to have a common base or parent class Person:

    \nclass Person:\n\n   def __init__(self, name: str, email: str):\n       self.name = name\n       self.email = email\n

    The new class contains those traits which are shared by the other two classes. Now Student and Teacher can inherit these traits and add their own besides.

    The syntax for inheritance simply involves adding the base class name in parentheses on the header line:

    class Person:\n\n   def __init__(self, name: str, email: str):\n       self.name = name\n       self.email = email\n\n   def update_email_domain(self, new_domain: str):\n       old_domain = self.email.split(\"@\")[1]\n       self.email = self.email.replace(old_domain, new_domain)\n\n\nclass Student(Person):\n\n   def __init__(self, name: str, id: str, email: str, credits: str):\n       self.name = name\n       self.id = id\n       self.email = email\n       self.credits = credits\n\n\nclass Teacher(Person):\n\n   def __init__(self, name: str, email: str, room: str, teaching_years: int):\n       self.name = name\n       self.email = email\n       self.room = room\n       self.teaching_years = teaching_years\n\n# Let's test our classes\nif __name__ == \"__main__\":\n   saul = Student(\"Saul Student\", \"1234\", \"saul@example.com\", 0)\n   saul.update_email_domain(\"example.edu\")\n   print(saul.email)\n\n   tara = Teacher(\"Tara Teacher\", \"tara@example.fi\", \"A123\", 2)\n   tara.update_email_domain(\"example.ex\")\n   print(tara.email)\n

    Both Student and Teacher inherit the Person class, so both have the traits defined in the Person class, including the method update_email_domain. The same method works for instances of both the derived classes.

    Let's have a look at another example. We have a Bookshelf which inherits the class BookContainer:

    class Book:\n   \"\"\" This class models a simple book \"\"\"\n   def __init__(self, name: str, author: str):\n       self.name = name\n       self.author = author\n\n\nclass BookContainer:\n   \"\"\" This class models a container for books \"\"\"\n\n   def __init__(self):\n       self.books = []\n\n   def add_book(self, book: Book):\n       self.books.append(book)\n\n   def list_books(self):\n       for book in self.books:\n           print(f\"{book.name} ({book.author})\")\n\n\nclass Bookshelf(BookContainer):\n   \"\"\" This class models a shelf for books \"\"\"\n\n   def __init__(self):\n       super().__init__()\n\n   def add_book(self, book: Book, location: int):\n       self.books.insert(location, book)\n

    The class Bookshelf contains the method add_book. A method with the same name is defined in the base class BookContainer. This is called overriding: if a derived class has a method with the same name as the base class, the derived version overrides the original in instances of the derived class.

    The idea in the example above is that a new book added to a BookContainer always goes to the top, but with a Bookshelf you can specify the location yourself. The method list_books works the same for both classes, as there is no overriding method in the derived class.

    Let's try out these classes:

    if __name__ == \"__main__\":\n   # Create some books for testing\n   b1 = Book(\"Old Man and the Sea\", \"Ernest Hemingway\")\n   b2 = Book(\"Silent Spring\", \"Rachel Carson\")\n   b3 = Book(\"Pride and Prejudice\", \"Jane Austen\")\n\n   # Create a BookContainer and add the books\n   container = BookContainer()\n   container.add_book(b1)\n   container.add_book(b2)\n   container.add_book(b3)\n\n   # Create a Bookshelf and add the books (always to the beginning)\n   shelf = Bookshelf()\n   shelf.add_book(b1, 0)\n   shelf.add_book(b2, 0)\n   shelf.add_book(b3, 0)\n\n\n   # Tulostetaan\n   print(\"Container:\")\n   container.list_books()\n\n   print()\n\n   print(\"Shelf:\")\n   shelf.list_books()

    Container:\nOld Man and the Sea (Ernest Hemingway)\nSilent Spring (Rachel Carson)\nPride and Prejudice (Jane Austen)

    Shelf:\nPride and Prejudice (Jane Austen)\nSilent Spring (Rachel Carson)\nOld Man and the Sea (Ernest Hemingway)

    So, the Bookshelf class also has access to the list_books method. Through inheritance the method is a member of all the classes derived from the BookContainer class.

    Inheritance and scope of traits

    A derived class inherits all traits from its base class. Those traits are directly accessible in the derived class, unless they have been defined as private in the base class (with two underscores before the name of the trait).

    As the attributes of a Bookshelf are identical to a BookContainer, there was no need to rewrite the constructor of Bookshelf. We simply called the constructor of the base class:

    class Bookshelf(BookContainer):\n\n   def __init__(self):\n       super().__init__()\n

    Any trait in the base class can be accessed from the derived class with the function super(). The self argument is left out from the method call, as Python adds it automatically.

    But what if the attributes are not identical; can we still use the base class constructor in some way? Let's have a look at a class named Thesis which inherits the Book class. The derived class can still call the constructor from the base class:

    \nclass Book:\n    \"\"\" This class models a simple book \"\"\"\n\n    def __init__(self, name: str, author: str):\n        self.name = name\n        self.author = author\n\n\nclass Thesis(Book):\n    \"\"\" This class models a graduate thesis \"\"\"\n\n    def __init__(self, name: str, author: str, grade: int):\n        super().__init__(name, author)\n        self.grade = grade\n

    The constructor in the Thesis class calls the constructor in the base class Book with the arguments for name and author. Additionally, the constructor in the derived class sets the value for the attribute grade. This naturally cannot be a part of the base class constructor, as the base class has no such attribute.

    The above class can be used like this:

    if __name__ == \"__main__\":\n    thesis = Thesis(\"Python and the Universe\", \"Peter Pythons\", 3)\n\n    # Print out the values of the attributes\n    print(thesis.name)\n    print(thesis.author)\n    print(thesis.grade)\n

    Python and the Universe\nPeter Pythons\n3

    Even if a derived class overrides a method in its base class, the derived class can still call the overridden method in the base class. In the following example we have a basic BonusCard and a special PlatinumCard for especially loyal customers. The calculate_bonus method is overridden in the derived class, but the overriding method calls the base method:

    \nclass Product:\n\n    def __init__(self, name: str, price: float):\n        self.name = name\n        self.price = price\n\nclass BonusCard:\n\n    def __init__(self):\n        self.products_bought = []\n\n    def add_product(self, product: Product):\n        self.products_bought.append(product)\n\n    def calculate_bonus(self):\n        bonus = 0\n        for product in self.products_bought:\n            bonus += product.price * 0.05\n\n        return bonus\n\nclass PlatinumCard(BonusCard):\n\n    def __init__(self):\n        super().__init__()\n\n    def calculate_bonus(self):\n        # Call the method in the base class\n        bonus = super().calculate_bonus()\n\n        # ...and add five percent to the total\n        bonus = bonus * 1.05\n        return bonus

    So, the bonus for a PlatinumCard is calculated by calling the overriden method in the base class, and then adding an extra 5 percent to the base result. An example of how these classes are used:

    if __name__ == \"__main__\":\n    card = BonusCard()\n    card.add_product(Product(\"Bananas\", 6.50))\n    card.add_product(Product(\"Satsumas\", 7.95))\n    bonus = card.calculate_bonus()\n\n    card2 = PlatinumCard()\n    card2.add_product(Product(\"Bananas\", 6.50))\n    card2.add_product(Product(\"Satsumas\", 7.95))\n    bonus2 = card2.calculate_bonus()\n\n    print(bonus)\n    print(bonus2)

    0.7225\n0.7586250000000001

    The exercise template contains a class definition for a Computer, which has the attributes model and speed.

    Please define a class named LaptopComputer which inherits the class Computer. The constructor of the new class should take a third argument: weight, of type integer.

    Please also include a __str__ method in your class definition. See the example below for the expected format of the string representation printed out.

    laptop = LaptopComputer(\"NoteBook Pro15\", 1500, 2)\nprint(laptop)

    NoteBook Pro15, 1500 MHz, 2 kg

    The exercise template contains class definitions for a ComputerGame and a GameWarehouse. A GameWarehouse object is used to store ComputerGame objects.

    Please familiarize yourself with these classes. Then define a new class named GameMuseum which inherits the GameWarehouse class.

    The GameMuseum class should override the list_games() method, so that it returns a list of only those games which were made before the year 1990.

    The new class should also have a constructor which calls the constructor from the parent class GameWarehouse. The constructor takes no arguments.

    You may use the following code to test your implementation:

    museum = GameMuseum()\nmuseum.add_game(ComputerGame(\"Pacman\", \"Namco\", 1980))\nmuseum.add_game(ComputerGame(\"GTA 2\", \"Rockstar\", 1999))\nmuseum.add_game(ComputerGame(\"Bubble Bobble\", \"Taito\", 1986))\nfor game in museum.list_games():\n    print(game.name)

    Pacman\nBubble Bobble

    The exercise template contains a class definition for a Rectangle. It represents a rectangle shape. A Rectangle works like this:

    rectangle = Rectangle(2, 3)\nprint(rectangle)\nprint(\"area:\", rectangle.area())

    rectangle 2x3\narea: 6

    Square

    Please define a class named Square which inherits the Rectangle class. The sides of a square are all of equal length, which makes the square a special case of the rectangle. The new class should not contain any new attributes.

    A Square object is used as follows:

    square = Square(4)\nprint(square)\nprint(\"area:\", square.area())

    square 4x4\narea: 16

    The exercise template contains the class definition for a WordGame. It provides some basic functionality for playing different word-based games:

    import random\n\nclass WordGame():\n    def __init__(self, rounds: int):\n        self.wins1 = 0\n        self.wins2 = 0\n        self.rounds = rounds\n\n    def round_winner(self, player1_word: str, player2_word: str):\n        # determine a random winner\n        return random.randint(1, 2)\n\n    def play(self):\n        print(\"Word game:\")\n        for i in range(1, self.rounds+1):\n            print(f\"round {i}\")\n            answer1 = input(\"player1: \")\n            answer2 = input(\"player2: \")\n\n            if self.round_winner(answer1, answer2) == 1:\n                self.wins1 += 1\n                print(\"player 1 won\")\n            elif self.round_winner(answer1, answer2) == 2:\n                self.wins2 += 1\n                print(\"player 2 won\")\n            else:\n                pass # it's a tie\n\n        print(\"game over, wins:\")\n        print(f\"player 1: {self.wins1}\")\n        print(f\"player 2: {self.wins2}\")

    The game is played as follows:

    p = WordGame(3)\np.play()

    Word game:\nround 1\nplayer1: longword\nplayer2: ??\nplayer 2 won\nround 2\nplayer1: i'm the best\nplayer2: wut?\nplayer 1 won\nround 3\nplayer1: who's gonna win\nplayer2: me\nplayer 1 won\ngame over, wins:\nplayer 1: 2\nplayer 2: 1

    In this \"basic\" version of the game the winner is determined randomly. The input from the players has no effect on the result.

    Longest word wins

    Please define a class named LongestWord. It is a version of the game where whoever types in the longest word on each round wins.

    The new version of the game is implemented by inheriting the WordGame class. The round_winner method should also be suitably overridden. The outline of the new class is as follows:

    class LongestWord(WordGame):\n    def __init__(self, rounds: int):\n        super().__init__(rounds)\n\n    def round_winner(self, player1_word: str, player2_word: str):\n        # your code for determining the winner goes here

    An example of how the new game is played:

    p = LongestWord(3)\np.play()

    Word game:\nround 1\nplayer1: short\nplayer2: longword\nplayer 2 won\nround 2\nplayer1: word\nplayer2: wut?\nround 3\nplayer1: i'm the best\nplayer2: no, me\nplayer 1 won\ngame over, wins:\nplayer 1: 1\nplayer 2: 1

    Most vowels wins

    Please define another WordGame class named MostVowels. In this version of the game whoever has squeezed more vowels into their word wins the round.

    Rock paper scissors

    Finally, please define a class named RockPaperScissors which allows you to play a game of rock paper scissors.

    The rules of the game are as follows:

    If the input from either player is invalid, they lose the round. If both players type in something else than rock, paper or scissors, the result is a tie.

    An example of how the game is played:

    p = RockPaperScissors(4)\np.play()

    Word game:\nround 1\nplayer1: rock\nplayer2: rock\nround 2\nplayer1: rock\nplayer2: paper\nplayer 2 won\nround 3\nplayer1: scissors\nplayer2: paper\nplayer 1 won\nround 4\nplayer1: paper\nplayer2: dynamite\nplayer 1 won\ngame over, wins:\nplayer 1: 2\nplayer 2: 1

    ","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-10/1-class-hierarchies.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know what inheritance means in a programming context"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to write classes which inherit other classes"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how inheritance affects the traits in classes"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"special-classes-for-special-purposes","style":"position:relative;"},"children":[{"type":"text","value":"Special classes for special purposes"},{"type":"element","tagName":"a","properties":{"href":"#special-classes-for-special-purposes","ariaLabel":"special classes for special purposes 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":"Sometimes you come across a situation where you have already defined a class, but then realize you need special traits in some, but not all, instances of the class. Then again, sometimes you realize you've defined two very similar classes with only minor differences. As programmers we aim to always repeat ourselves as little as possible, while maintaining clarity and readability. So how can we accommodate for different implementations of intrinsically similar objects?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at two class definitions: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Teacher"}]},{"type":"text","value":". Getter and setter methods have been left out for now, in order to keep the example short."}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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":"id"}]},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" credits"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"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":"id"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" email\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" credits\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Teacher"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" room"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" teaching_years"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" email\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"room "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" room\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"teaching_years "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" teaching_years\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Even in a stripped down example, like the above, we already have quite a bit of repetition: both classes contain attributes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"email"}]},{"type":"text","value":". It would be a good idea to have a single attribute definition, so that a single function would suffice for editing both attributes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, imagine the school's email address changed. All addresses would have to be updated. We "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"could"}]},{"type":"text","value":" write two separate versions of essentially the same 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":"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":"update_email"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"o"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Student"},{"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 o"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" o"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email"},{"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":"\".com\""}]},{"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":"\".edu\""}]},{"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":"update_email2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"o"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Teacher"},{"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 o"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" o"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email"},{"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":"\".com\""}]},{"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":"\".edu\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Writing practically the same thing twice is unnecessary repetition, not to mention it doubles the possibilities for errors. It would be a definite improvement if we could use a single function to work with instances of both classes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Both classes also have attributes which are unique to them. Simply combining "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"all"}]},{"type":"text","value":" attributes in a single class would mean "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"all"}]},{"type":"text","value":" instances of the class would then have unnecessary attributes, just different ones for different instances. That doesn't seem like an ideal situation, either."}]},{"type":"element","tagName":"h2","properties":{"id":"inheritance","style":"position:relative;"},"children":[{"type":"text","value":"Inheritance"},{"type":"element","tagName":"a","properties":{"href":"#inheritance","ariaLabel":"inheritance 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":"Object oriented programming languages usually feature a technique called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inheritance"}]},{"type":"text","value":". A class can "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inherit"}]},{"type":"text","value":" the traits of another class. In addition to these inherited traits a class can also contain traits which are unique to it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Knowing this, it would make sense for the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Teacher"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" classes to have a common base or parent class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":":"}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" email\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The new class contains those traits which are shared by the other two classes. Now "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Teacher"}]},{"type":"text","value":" can "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inherit"}]},{"type":"text","value":" these traits and add their own besides."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The syntax for inheritance simply involves adding the base class name in parentheses on the header line:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" email\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":"update_email_domain"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" new_domain"},{"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":"str"}]},{"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 old_domain "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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":"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email"},{"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":"text","value":"old_domain"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" new_domain"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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":"id"}]},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" credits"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"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":"id"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" email\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" credits\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Teacher"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" room"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" teaching_years"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" email\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"room "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" room\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"teaching_years "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" teaching_years\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Let's test our classes"}]},{"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 saul "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saul Student\""}]},{"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":"\"1234\""}]},{"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":"\"saul@example.com\""}]},{"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n saul"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"update_email_domain"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"example.edu\""}]},{"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":"saul"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n tara "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Teacher"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tara Teacher\""}]},{"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":"\"tara@example.fi\""}]},{"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":"\"A123\""}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n tara"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"update_email_domain"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"example.ex\""}]},{"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":"tara"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"email"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Both "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Teacher"}]},{"type":"text","value":" inherit the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" class, so both have the traits defined in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" class, including the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"update_email_domain"}]},{"type":"text","value":". The same method works for instances of both the derived classes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at another example. We have a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bookshelf"}]},{"type":"text","value":" which inherits the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BookContainer"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Book"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" This class models a simple book \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" author"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"author "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" author\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BookContainer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" This class models a container for books \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"books "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_book"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Book"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"books"},{"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":"book"},{"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":"list_books"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"for"}]},{"type":"text","value":" book "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"books"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"author"},{"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\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bookshelf"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"BookContainer"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" This class models a shelf for books \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","builtin"]},"children":[{"type":"text","value":"super"}]},{"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":"__init__"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_book"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" location"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"books"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"insert"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"location"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Bookshelf"}]},{"type":"text","value":" contains the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_book"}]},{"type":"text","value":". A method with the same name is defined in the base class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BookContainer"}]},{"type":"text","value":". This is called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"overriding"}]},{"type":"text","value":": if a derived class has a method with the same name as the base class, the derived version overrides the original in instances of the derived class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The idea in the example above is that a new book added to a BookContainer always goes to the top, but with a Bookshelf you can specify the location yourself. The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list_books"}]},{"type":"text","value":" works the same for both classes, as there is no overriding method in the derived class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's try out these classes:"}]},{"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":"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":"# Create some books for testing"}]},{"type":"text","value":"\n b1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Old Man and the Sea\""}]},{"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":"\"Ernest Hemingway\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Silent Spring\""}]},{"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":"\"Rachel Carson\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pride and Prejudice\""}]},{"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":"\"Jane Austen\""}]},{"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":"# Create a BookContainer and add the books"}]},{"type":"text","value":"\n container "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BookContainer"},{"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 container"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n container"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n container"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b3"},{"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":"# Create a Bookshelf and add the books (always to the beginning)"}]},{"type":"text","value":"\n shelf "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bookshelf"},{"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 shelf"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b1"},{"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n shelf"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b2"},{"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n shelf"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b3"},{"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Tulostetaan"}]},{"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":"\"Container:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n container"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"list_books"},{"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":"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\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":"\"Shelf:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n shelf"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"list_books"},{"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":" "},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Container:\nOld Man and the Sea (Ernest Hemingway)\nSilent Spring (Rachel Carson)\nPride and Prejudice (Jane Austen)"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Shelf:\nPride and Prejudice (Jane Austen)\nSilent Spring (Rachel Carson)\nOld Man and the Sea (Ernest Hemingway)"}]},{"type":"text","value":" "}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, the Bookshelf class also has access to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list_books"}]},{"type":"text","value":" method. Through inheritance the method is a member of all the classes derived from the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BookContainer"}]},{"type":"text","value":" class."}]},{"type":"element","tagName":"h2","properties":{"id":"inheritance-and-scope-of-traits","style":"position:relative;"},"children":[{"type":"text","value":"Inheritance and scope of traits"},{"type":"element","tagName":"a","properties":{"href":"#inheritance-and-scope-of-traits","ariaLabel":"inheritance and scope of traits 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":"A derived class inherits all traits from its base class. Those traits are directly accessible in the derived class, unless they have been defined as private in the base class (with two underscores before the name of the trait)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As the attributes of a Bookshelf are identical to a BookContainer, there was no need to rewrite the constructor of Bookshelf. We simply called the constructor of the base class:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bookshelf"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"BookContainer"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","builtin"]},"children":[{"type":"text","value":"super"}]},{"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":"__init__"},{"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":"p","properties":{},"children":[{"type":"text","value":"Any trait in the base class can be accessed from the derived class with the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"super()"}]},{"type":"text","value":". The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" argument is left out from the method call, as Python adds it automatically."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"But what if the attributes are not identical; can we still use the base class constructor in some way? Let's have a look at a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Thesis"}]},{"type":"text","value":" which inherits the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Book"}]},{"type":"text","value":" class. The derived class "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"can"}]},{"type":"text","value":" still call the constructor from the base class:"}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Book"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" This class models a simple book \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" author"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"author "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" author\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Thesis"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Book"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" This class models a graduate thesis \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" author"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" grade"},{"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","builtin"]},"children":[{"type":"text","value":"super"}]},{"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":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" author"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"grade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" grade\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The constructor in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Thesis"}]},{"type":"text","value":" class calls the constructor in the base class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Book"}]},{"type":"text","value":" with the arguments for "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"author"}]},{"type":"text","value":". Additionally, the constructor in the derived class sets the value for the attribute "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"grade"}]},{"type":"text","value":". This naturally cannot be a part of the base class constructor, as the base class has no such attribute."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The above class can be used 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":"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 thesis "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Thesis"},{"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 and the Universe\""}]},{"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":"\"Peter Pythons\""}]},{"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":"3"}]},{"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":"# Print out the values of the attributes"}]},{"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":"thesis"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"thesis"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"author"},{"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":"thesis"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"grade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python and the Universe\nPeter Pythons\n3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Even if a derived class "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"overrides"}]},{"type":"text","value":" a method in its base class, the derived class can "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"still"}]},{"type":"text","value":" call the overridden method in the base class. In the following example we have a basic "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BonusCard"}]},{"type":"text","value":" and a special "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PlatinumCard"}]},{"type":"text","value":" for especially loyal customers. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"calculate_bonus"}]},{"type":"text","value":" method is overridden in the derived class, but the overriding method calls the base method:"}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Product"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" price"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"price "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" price\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BonusCard"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"products_bought "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_product"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Product"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"products_bought"},{"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":"product"},{"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":"calculate_bonus"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 bonus "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"products_bought"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"price "},{"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.05"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" bonus\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PlatinumCard"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"BonusCard"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","builtin"]},"children":[{"type":"text","value":"super"}]},{"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":"__init__"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"calculate_bonus"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# Call the method in the base class"}]},{"type":"text","value":"\n bonus "},{"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":"super"}]},{"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":"calculate_bonus"},{"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","comment"]},"children":[{"type":"text","value":"# ...and add five percent to the total"}]},{"type":"text","value":"\n bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" bonus "},{"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.05"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" bonus"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, the bonus for a PlatinumCard is calculated by calling the overriden method in the base class, and then adding an extra 5 percent to the base result. An example of how these classes are used:"}]},{"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":"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 card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BonusCard"},{"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 card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bananas\""}]},{"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":"6.50"}]},{"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 card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Satsumas\""}]},{"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":"7.95"}]},{"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 bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"calculate_bonus"},{"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 card2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PlatinumCard"},{"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 card2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bananas\""}]},{"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":"6.50"}]},{"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 card2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Satsumas\""}]},{"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":"7.95"}]},{"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 bonus2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" card2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"calculate_bonus"},{"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":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"bonus"},{"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":"bonus2"},{"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.7225\n0.7586250000000001"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Laptop computer","tmcname":"part10-01_laptop_computer"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains a class definition for a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Computer"}]},{"type":"text","value":", which has the attributes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"model"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"speed"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please define a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LaptopComputer"}]},{"type":"text","value":" which "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inherits"}]},{"type":"text","value":" the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Computer"}]},{"type":"text","value":". The constructor of the new class should take a third argument: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"weight"}]},{"type":"text","value":", of type integer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please also include a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method in your class definition. See the example below for the expected format of the string representation printed out."}]},{"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":"laptop "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LaptopComputer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"NoteBook Pro15\""}]},{"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":"1500"}]},{"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":"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":"laptop"},{"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":"NoteBook Pro15, 1500 MHz, 2 kg"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Game Museum","tmcname":"part10-02_game_museum"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains class definitions for a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ComputerGame"}]},{"type":"text","value":" and a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"GameWarehouse"}]},{"type":"text","value":". A GameWarehouse object is used to store ComputerGame objects."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please familiarize yourself with these classes. Then define a new class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"GameMuseum"}]},{"type":"text","value":" which inherits the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"GameWarehouse"}]},{"type":"text","value":" class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The GameMuseum class should "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"override"}]},{"type":"text","value":" the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list_games()"}]},{"type":"text","value":" method, so that it returns a list of only those games which were made before the year 1990."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The new class should also have a constructor which "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"calls the constructor from the parent class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"GameWarehouse"}]}]},{"type":"text","value":". The constructor takes no arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may use the following code to test your implementation:"}]},{"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":"museum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" GameMuseum"},{"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":"\nmuseum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_game"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ComputerGame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pacman\""}]},{"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":"\"Namco\""}]},{"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":"1980"}]},{"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":"\nmuseum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_game"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ComputerGame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"GTA 2\""}]},{"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":"\"Rockstar\""}]},{"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":"1999"}]},{"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":"\nmuseum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_game"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ComputerGame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Bubble Bobble\""}]},{"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":"\"Taito\""}]},{"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":"1986"}]},{"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":"for"}]},{"type":"text","value":" game "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" museum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"list_games"},{"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 "},{"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":"game"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"Pacman\nBubble Bobble"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Areas","tmcname":"part10-03_areas"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains a class definition for a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rectangle"}]},{"type":"text","value":". It represents a "},{"type":"element","tagName":"a","properties":{"href":"https://en.wikipedia.org/wiki/Rectangle","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"rectangle shape"}]},{"type":"text","value":". A Rectangle works 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":"text","value":"rectangle "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rectangle"},{"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":" "},{"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":"\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":"rectangle"},{"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":"\"area:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"area"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"rectangle 2x3\narea: 6"}]}]},{"type":"element","tagName":"h2","properties":{"id":"square","style":"position:relative;"},"children":[{"type":"text","value":"Square"},{"type":"element","tagName":"a","properties":{"href":"#square","ariaLabel":"square 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":"Please define a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Square"}]},{"type":"text","value":" which inherits the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rectangle"}]},{"type":"text","value":" class. The sides of a "},{"type":"element","tagName":"a","properties":{"href":"https://en.wikipedia.org/wiki/Square","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"square"}]},{"type":"text","value":" are all of equal length, which makes the square a special case of the rectangle. The new class should not contain any new attributes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A Square object is used as follows:"}]},{"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":"square "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Square"},{"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":"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":"square"},{"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":"\"area:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" square"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"area"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"square 4x4\narea: 16"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Word game","tmcname":"part10-04_word_game"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the class definition for a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"WordGame"}]},{"type":"text","value":". It provides some basic functionality for playing different word-based games:"}]},{"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":"import"}]},{"type":"text","value":" random\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"WordGame"}]},{"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 "},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rounds"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"wins1 "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"wins2 "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rounds "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rounds\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":"round_winner"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player1_word"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player2_word"},{"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":"str"}]},{"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":"# determine a random winner"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" random"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"randint"},{"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":"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\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":"play"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"\"Word game:\""}]},{"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":" 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":"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rounds"},{"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":"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\"round "}]},{"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","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 answer1 "},{"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":"\"player1: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n answer2 "},{"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":"\"player2: \""}]},{"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":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"round_winner"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"answer1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" answer2"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"wins1 "},{"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":"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":"\"player 1 won\""}]},{"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":"elif"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"round_winner"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"answer1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" answer2"},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"wins2 "},{"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":"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":"\"player 2 won\""}]},{"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":"else"}]},{"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":"pass"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# it's a tie"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"game over, wins:\""}]},{"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\"player 1: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"wins1"},{"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 "},{"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\"player 2: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"wins2"},{"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":"p","properties":{},"children":[{"type":"text","value":"The game is played as follows:"}]},{"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":"p "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" WordGame"},{"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":"\np"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"play"},{"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":"Word game:\nround 1\nplayer1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"longword"}]},{"type":"text","value":"\nplayer2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"??"}]},{"type":"text","value":"\nplayer 2 won\nround 2\nplayer1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"i'm the best"}]},{"type":"text","value":"\nplayer2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"wut?"}]},{"type":"text","value":"\nplayer 1 won\nround 3\nplayer1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"who's gonna win"}]},{"type":"text","value":"\nplayer2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"me"}]},{"type":"text","value":"\nplayer 1 won\ngame over, wins:\nplayer 1: 2\nplayer 2: 1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this \"basic\" version of the game the winner is determined randomly. The input from the players has no effect on the result."}]},{"type":"element","tagName":"h2","properties":{"id":"longest-word-wins","style":"position:relative;"},"children":[{"type":"text","value":"Longest word wins"},{"type":"element","tagName":"a","properties":{"href":"#longest-word-wins","ariaLabel":"longest word wins 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":"Please define a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LongestWord"}]},{"type":"text","value":". It is a version of the game where whoever types in the longest word on each round wins."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The new version of the game is implemented by "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inheriting"}]},{"type":"text","value":" the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"WordGame"}]},{"type":"text","value":" class. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"round_winner"}]},{"type":"text","value":" method should also be suitably overridden. The outline of the new class is as follows:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"LongestWord"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"WordGame"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rounds"},{"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","builtin"]},"children":[{"type":"text","value":"super"}]},{"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":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rounds"},{"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":"round_winner"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player1_word"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player2_word"},{"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":"str"}]},{"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 code for determining the winner goes here"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how the new game is played:"}]},{"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":"p "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LongestWord"},{"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":"\np"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"play"},{"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":"Word game:\nround 1\nplayer1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"short"}]},{"type":"text","value":"\nplayer2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"longword"}]},{"type":"text","value":"\nplayer 2 won\nround 2\nplayer1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"word"}]},{"type":"text","value":"\nplayer2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"wut?"}]},{"type":"text","value":"\nround 3\nplayer1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"i'm the best"}]},{"type":"text","value":"\nplayer2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"no, me"}]},{"type":"text","value":"\nplayer 1 won\ngame over, wins:\nplayer 1: 1\nplayer 2: 1"}]}]},{"type":"element","tagName":"h2","properties":{"id":"most-vowels-wins","style":"position:relative;"},"children":[{"type":"text","value":"Most vowels wins"},{"type":"element","tagName":"a","properties":{"href":"#most-vowels-wins","ariaLabel":"most vowels wins 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":"Please define another WordGame class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MostVowels"}]},{"type":"text","value":". In this version of the game whoever has squeezed more vowels into their word wins the round."}]},{"type":"element","tagName":"h2","properties":{"id":"rock-paper-scissors","style":"position:relative;"},"children":[{"type":"text","value":"Rock paper scissors"},{"type":"element","tagName":"a","properties":{"href":"#rock-paper-scissors","ariaLabel":"rock paper scissors 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":"Finally, please define a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"RockPaperScissors"}]},{"type":"text","value":" which allows you to play a game of "},{"type":"element","tagName":"a","properties":{"href":"https://en.wikipedia.org/wiki/Rock_paper_scissors","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"rock paper scissors"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The rules of the game are as follows:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"rock beats scissors (the rock can break the scissors but the scissors can't cut the rock)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"paper beats rock (the paper can cover the rock)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"scissors beats paper (the scissors can cut the paper)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the input from either player is invalid, they lose the round. If both players type in something else than "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"rock"}]},{"type":"text","value":", "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"paper"}]},{"type":"text","value":" or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"scissors"}]},{"type":"text","value":", the result is a tie."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how the game is played:"}]},{"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":"p "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RockPaperScissors"},{"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":"text","value":"\np"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"play"},{"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":"Word game:\nround 1\nplayer1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"rock"}]},{"type":"text","value":"\nplayer2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"rock"}]},{"type":"text","value":"\nround 2\nplayer1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"rock"}]},{"type":"text","value":"\nplayer2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"paper"}]},{"type":"text","value":"\nplayer 2 won\nround 3\nplayer1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"scissors"}]},{"type":"text","value":"\nplayer2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"paper"}]},{"type":"text","value":"\nplayer 1 won\nround 4\nplayer1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"paper"}]},{"type":"text","value":"\nplayer2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"dynamite"}]},{"type":"text","value":"\nplayer 1 won\ngame over, wins:\nplayer 1: 2\nplayer 2: 1"}]}]}]}]},"html":"

    After this section

    Special classes for special purposes

    Sometimes you come across a situation where you have already defined a class, but then realize you need special traits in some, but not all, instances of the class. Then again, sometimes you realize you've defined two very similar classes with only minor differences. As programmers we aim to always repeat ourselves as little as possible, while maintaining clarity and readability. So how can we accommodate for different implementations of intrinsically similar objects?

    Let's have a look at two class definitions: Student and Teacher. Getter and setter methods have been left out for now, in order to keep the example short.

    \nclass Student:\n\n    def __init__(self, name: str, id: str, email: str, credits: str):\n        self.name = name\n        self.id = id\n        self.email = email\n        self.credits = credits\n\nclass Teacher:\n\n    def __init__(self, name: str, email: str, room: str, teaching_years: int):\n        self.name = name\n        self.email = email\n        self.room = room\n        self.teaching_years = teaching_years\n

    Even in a stripped down example, like the above, we already have quite a bit of repetition: both classes contain attributes name and email. It would be a good idea to have a single attribute definition, so that a single function would suffice for editing both attributes.

    For example, imagine the school's email address changed. All addresses would have to be updated. We could write two separate versions of essentially the same function:

    \ndef update_email(o: Student):\n    o.email = o.email.replace(\".com\", \".edu\")\n\ndef update_email2(o: Teacher):\n    o.email = o.email.replace(\".com\", \".edu\")\n

    Writing practically the same thing twice is unnecessary repetition, not to mention it doubles the possibilities for errors. It would be a definite improvement if we could use a single function to work with instances of both classes.

    Both classes also have attributes which are unique to them. Simply combining all attributes in a single class would mean all instances of the class would then have unnecessary attributes, just different ones for different instances. That doesn't seem like an ideal situation, either.

    Inheritance

    Object oriented programming languages usually feature a technique called inheritance. A class can inherit the traits of another class. In addition to these inherited traits a class can also contain traits which are unique to it.

    Knowing this, it would make sense for the Teacher and Student classes to have a common base or parent class Person:

    \nclass Person:\n\n   def __init__(self, name: str, email: str):\n       self.name = name\n       self.email = email\n

    The new class contains those traits which are shared by the other two classes. Now Student and Teacher can inherit these traits and add their own besides.

    The syntax for inheritance simply involves adding the base class name in parentheses on the header line:

    class Person:\n\n   def __init__(self, name: str, email: str):\n       self.name = name\n       self.email = email\n\n   def update_email_domain(self, new_domain: str):\n       old_domain = self.email.split(\"@\")[1]\n       self.email = self.email.replace(old_domain, new_domain)\n\n\nclass Student(Person):\n\n   def __init__(self, name: str, id: str, email: str, credits: str):\n       self.name = name\n       self.id = id\n       self.email = email\n       self.credits = credits\n\n\nclass Teacher(Person):\n\n   def __init__(self, name: str, email: str, room: str, teaching_years: int):\n       self.name = name\n       self.email = email\n       self.room = room\n       self.teaching_years = teaching_years\n\n# Let's test our classes\nif __name__ == \"__main__\":\n   saul = Student(\"Saul Student\", \"1234\", \"saul@example.com\", 0)\n   saul.update_email_domain(\"example.edu\")\n   print(saul.email)\n\n   tara = Teacher(\"Tara Teacher\", \"tara@example.fi\", \"A123\", 2)\n   tara.update_email_domain(\"example.ex\")\n   print(tara.email)\n

    Both Student and Teacher inherit the Person class, so both have the traits defined in the Person class, including the method update_email_domain. The same method works for instances of both the derived classes.

    Let's have a look at another example. We have a Bookshelf which inherits the class BookContainer:

    class Book:\n   \"\"\" This class models a simple book \"\"\"\n   def __init__(self, name: str, author: str):\n       self.name = name\n       self.author = author\n\n\nclass BookContainer:\n   \"\"\" This class models a container for books \"\"\"\n\n   def __init__(self):\n       self.books = []\n\n   def add_book(self, book: Book):\n       self.books.append(book)\n\n   def list_books(self):\n       for book in self.books:\n           print(f\"{book.name} ({book.author})\")\n\n\nclass Bookshelf(BookContainer):\n   \"\"\" This class models a shelf for books \"\"\"\n\n   def __init__(self):\n       super().__init__()\n\n   def add_book(self, book: Book, location: int):\n       self.books.insert(location, book)\n

    The class Bookshelf contains the method add_book. A method with the same name is defined in the base class BookContainer. This is called overriding: if a derived class has a method with the same name as the base class, the derived version overrides the original in instances of the derived class.

    The idea in the example above is that a new book added to a BookContainer always goes to the top, but with a Bookshelf you can specify the location yourself. The method list_books works the same for both classes, as there is no overriding method in the derived class.

    Let's try out these classes:

    if __name__ == \"__main__\":\n   # Create some books for testing\n   b1 = Book(\"Old Man and the Sea\", \"Ernest Hemingway\")\n   b2 = Book(\"Silent Spring\", \"Rachel Carson\")\n   b3 = Book(\"Pride and Prejudice\", \"Jane Austen\")\n\n   # Create a BookContainer and add the books\n   container = BookContainer()\n   container.add_book(b1)\n   container.add_book(b2)\n   container.add_book(b3)\n\n   # Create a Bookshelf and add the books (always to the beginning)\n   shelf = Bookshelf()\n   shelf.add_book(b1, 0)\n   shelf.add_book(b2, 0)\n   shelf.add_book(b3, 0)\n\n\n   # Tulostetaan\n   print(\"Container:\")\n   container.list_books()\n\n   print()\n\n   print(\"Shelf:\")\n   shelf.list_books()

    Container:\nOld Man and the Sea (Ernest Hemingway)\nSilent Spring (Rachel Carson)\nPride and Prejudice (Jane Austen)

    Shelf:\nPride and Prejudice (Jane Austen)\nSilent Spring (Rachel Carson)\nOld Man and the Sea (Ernest Hemingway)

    So, the Bookshelf class also has access to the list_books method. Through inheritance the method is a member of all the classes derived from the BookContainer class.

    Inheritance and scope of traits

    A derived class inherits all traits from its base class. Those traits are directly accessible in the derived class, unless they have been defined as private in the base class (with two underscores before the name of the trait).

    As the attributes of a Bookshelf are identical to a BookContainer, there was no need to rewrite the constructor of Bookshelf. We simply called the constructor of the base class:

    class Bookshelf(BookContainer):\n\n   def __init__(self):\n       super().__init__()\n

    Any trait in the base class can be accessed from the derived class with the function super(). The self argument is left out from the method call, as Python adds it automatically.

    But what if the attributes are not identical; can we still use the base class constructor in some way? Let's have a look at a class named Thesis which inherits the Book class. The derived class can still call the constructor from the base class:

    \nclass Book:\n    \"\"\" This class models a simple book \"\"\"\n\n    def __init__(self, name: str, author: str):\n        self.name = name\n        self.author = author\n\n\nclass Thesis(Book):\n    \"\"\" This class models a graduate thesis \"\"\"\n\n    def __init__(self, name: str, author: str, grade: int):\n        super().__init__(name, author)\n        self.grade = grade\n

    The constructor in the Thesis class calls the constructor in the base class Book with the arguments for name and author. Additionally, the constructor in the derived class sets the value for the attribute grade. This naturally cannot be a part of the base class constructor, as the base class has no such attribute.

    The above class can be used like this:

    if __name__ == \"__main__\":\n    thesis = Thesis(\"Python and the Universe\", \"Peter Pythons\", 3)\n\n    # Print out the values of the attributes\n    print(thesis.name)\n    print(thesis.author)\n    print(thesis.grade)\n

    Python and the Universe\nPeter Pythons\n3

    Even if a derived class overrides a method in its base class, the derived class can still call the overridden method in the base class. In the following example we have a basic BonusCard and a special PlatinumCard for especially loyal customers. The calculate_bonus method is overridden in the derived class, but the overriding method calls the base method:

    \nclass Product:\n\n    def __init__(self, name: str, price: float):\n        self.name = name\n        self.price = price\n\nclass BonusCard:\n\n    def __init__(self):\n        self.products_bought = []\n\n    def add_product(self, product: Product):\n        self.products_bought.append(product)\n\n    def calculate_bonus(self):\n        bonus = 0\n        for product in self.products_bought:\n            bonus += product.price * 0.05\n\n        return bonus\n\nclass PlatinumCard(BonusCard):\n\n    def __init__(self):\n        super().__init__()\n\n    def calculate_bonus(self):\n        # Call the method in the base class\n        bonus = super().calculate_bonus()\n\n        # ...and add five percent to the total\n        bonus = bonus * 1.05\n        return bonus

    So, the bonus for a PlatinumCard is calculated by calling the overriden method in the base class, and then adding an extra 5 percent to the base result. An example of how these classes are used:

    if __name__ == \"__main__\":\n    card = BonusCard()\n    card.add_product(Product(\"Bananas\", 6.50))\n    card.add_product(Product(\"Satsumas\", 7.95))\n    bonus = card.calculate_bonus()\n\n    card2 = PlatinumCard()\n    card2.add_product(Product(\"Bananas\", 6.50))\n    card2.add_product(Product(\"Satsumas\", 7.95))\n    bonus2 = card2.calculate_bonus()\n\n    print(bonus)\n    print(bonus2)

    0.7225\n0.7586250000000001

    The exercise template contains a class definition for a Computer, which has the attributes model and speed.

    Please define a class named LaptopComputer which inherits the class Computer. The constructor of the new class should take a third argument: weight, of type integer.

    Please also include a __str__ method in your class definition. See the example below for the expected format of the string representation printed out.

    laptop = LaptopComputer(\"NoteBook Pro15\", 1500, 2)\nprint(laptop)

    NoteBook Pro15, 1500 MHz, 2 kg

    The exercise template contains class definitions for a ComputerGame and a GameWarehouse. A GameWarehouse object is used to store ComputerGame objects.

    Please familiarize yourself with these classes. Then define a new class named GameMuseum which inherits the GameWarehouse class.

    The GameMuseum class should override the list_games() method, so that it returns a list of only those games which were made before the year 1990.

    The new class should also have a constructor which calls the constructor from the parent class GameWarehouse. The constructor takes no arguments.

    You may use the following code to test your implementation:

    museum = GameMuseum()\nmuseum.add_game(ComputerGame(\"Pacman\", \"Namco\", 1980))\nmuseum.add_game(ComputerGame(\"GTA 2\", \"Rockstar\", 1999))\nmuseum.add_game(ComputerGame(\"Bubble Bobble\", \"Taito\", 1986))\nfor game in museum.list_games():\n    print(game.name)

    Pacman\nBubble Bobble

    The exercise template contains a class definition for a Rectangle. It represents a rectangle shape. A Rectangle works like this:

    rectangle = Rectangle(2, 3)\nprint(rectangle)\nprint(\"area:\", rectangle.area())

    rectangle 2x3\narea: 6

    Square

    Please define a class named Square which inherits the Rectangle class. The sides of a square are all of equal length, which makes the square a special case of the rectangle. The new class should not contain any new attributes.

    A Square object is used as follows:

    square = Square(4)\nprint(square)\nprint(\"area:\", square.area())

    square 4x4\narea: 16

    The exercise template contains the class definition for a WordGame. It provides some basic functionality for playing different word-based games:

    import random\n\nclass WordGame():\n    def __init__(self, rounds: int):\n        self.wins1 = 0\n        self.wins2 = 0\n        self.rounds = rounds\n\n    def round_winner(self, player1_word: str, player2_word: str):\n        # determine a random winner\n        return random.randint(1, 2)\n\n    def play(self):\n        print(\"Word game:\")\n        for i in range(1, self.rounds+1):\n            print(f\"round {i}\")\n            answer1 = input(\"player1: \")\n            answer2 = input(\"player2: \")\n\n            if self.round_winner(answer1, answer2) == 1:\n                self.wins1 += 1\n                print(\"player 1 won\")\n            elif self.round_winner(answer1, answer2) == 2:\n                self.wins2 += 1\n                print(\"player 2 won\")\n            else:\n                pass # it's a tie\n\n        print(\"game over, wins:\")\n        print(f\"player 1: {self.wins1}\")\n        print(f\"player 2: {self.wins2}\")

    The game is played as follows:

    p = WordGame(3)\np.play()

    Word game:\nround 1\nplayer1: longword\nplayer2: ??\nplayer 2 won\nround 2\nplayer1: i'm the best\nplayer2: wut?\nplayer 1 won\nround 3\nplayer1: who's gonna win\nplayer2: me\nplayer 1 won\ngame over, wins:\nplayer 1: 2\nplayer 2: 1

    In this \"basic\" version of the game the winner is determined randomly. The input from the players has no effect on the result.

    Longest word wins

    Please define a class named LongestWord. It is a version of the game where whoever types in the longest word on each round wins.

    The new version of the game is implemented by inheriting the WordGame class. The round_winner method should also be suitably overridden. The outline of the new class is as follows:

    class LongestWord(WordGame):\n    def __init__(self, rounds: int):\n        super().__init__(rounds)\n\n    def round_winner(self, player1_word: str, player2_word: str):\n        # your code for determining the winner goes here

    An example of how the new game is played:

    p = LongestWord(3)\np.play()

    Word game:\nround 1\nplayer1: short\nplayer2: longword\nplayer 2 won\nround 2\nplayer1: word\nplayer2: wut?\nround 3\nplayer1: i'm the best\nplayer2: no, me\nplayer 1 won\ngame over, wins:\nplayer 1: 1\nplayer 2: 1

    Most vowels wins

    Please define another WordGame class named MostVowels. In this version of the game whoever has squeezed more vowels into their word wins the round.

    Rock paper scissors

    Finally, please define a class named RockPaperScissors which allows you to play a game of rock paper scissors.

    The rules of the game are as follows:

    If the input from either player is invalid, they lose the round. If both players type in something else than rock, paper or scissors, the result is a tie.

    An example of how the game is played:

    p = RockPaperScissors(4)\np.play()

    Word game:\nround 1\nplayer1: rock\nplayer2: rock\nround 2\nplayer1: rock\nplayer2: paper\nplayer 2 won\nround 3\nplayer1: scissors\nplayer2: paper\nplayer 1 won\nround 4\nplayer1: paper\nplayer2: dynamite\nplayer 1 won\ngame over, wins:\nplayer 1: 2\nplayer 2: 1

    ","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-10/1-class-hierarchies.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-10/2-access-modifiers/page-data.json b/page-data/part-10/2-access-modifiers/page-data.json index 57e10b2db..a60f2a006 100644 --- a/page-data/part-10/2-access-modifiers/page-data.json +++ b/page-data/part-10/2-access-modifiers/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-10/2-access-modifiers", - "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 understand the access modifiers private and protected"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how the visibility of traits is determined in Python"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If a trait is defined as private in the base class, it is not directly accessible in any derived classes, as was briefly mentioned in the previous section. Let's take a look at an example. In the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Notebook"}]},{"type":"text","value":" class below the notes are stored in a list, and the list attribute is private:"}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Notebook"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" A Notebook stores notes in string format \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# private attribute"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__notes "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_note"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" note"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__notes"},{"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":"note"},{"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":"retrieve_note"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" index"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__notes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"index"},{"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":"all_notes"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__notes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the integrity of the class is key, making the list attribute "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"notes"}]},{"type":"text","value":" private makes sense. The class provides the client with suitable methods for adding and browsing notes, after all. This approach becomes problematic if we define a new class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NotebookPro"}]},{"type":"text","value":", which inherits the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Notebook"}]},{"type":"text","value":" class. The private list attribute is not accessible to the client, but neither is it accessible to the derived classes. If we try to access it, as in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find_notes"}]},{"type":"text","value":" method below, we get an error:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"NotebookPro"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Notebook"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" A better Notebook with search functionality \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# This is OK, the constructor is public despite the underscores"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"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":"__init__"},{"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","comment"]},"children":[{"type":"text","value":"# This causes an error"}]},{"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":"find_notes"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" search_term"},{"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 found "},{"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":"# the attribute __notes is private"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the derived class can't access it directly"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" note "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__notes"},{"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":"if"}]},{"type":"text","value":" search_term "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" note"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n found"},{"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":"note"},{"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":" found"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"AttributeError: 'NotebookPro' object has no attribute '_NotebookPro__notes'"}]}]},{"type":"element","tagName":"h2","properties":{"id":"protected-traits","style":"position:relative;"},"children":[{"type":"text","value":"Protected traits"},{"type":"element","tagName":"a","properties":{"href":"#protected-traits","ariaLabel":"protected traits 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":"Many object oriented programming languages have a feature, usually a special keyword, for "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"protecting"}]},{"type":"text","value":" traits. This means that a trait should be hidden from the clients of the class, but kept accessible to its subclasses. Python in general abhors keywords, so no such feature is directly available in Python. Instead, there is a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"convention"}]},{"type":"text","value":" of marking protected traits in a certain way."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Remember, a trait can be hidden by prefixing its name with two underscores:"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__notes "},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The agreed convention to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"protect"}]},{"type":"text","value":" a trait is to prefix the name with a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"single"}]},{"type":"text","value":" underscore. Now, this is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"just"}]},{"type":"text","value":" a convention. Nothing prevents a programmer from breaking the convention, but it is considered a bad programming practice."}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_notes "},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Below we have the entire Notebook example, with protected "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"_notes"}]},{"type":"text","value":" instead of private "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__notes"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Notebook"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" A Notebook stores notes in string format \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# protected attribute"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_notes "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_note"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" note"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_notes"},{"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":"note"},{"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":"retrieve_note"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" index"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_notes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"index"},{"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":"all_notes"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_notes"},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"NotebookPro"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Notebook"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" A better Notebook with search functionality \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# This is OK, the constructor is public despite the underscores"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"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":"__init__"},{"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","comment"]},"children":[{"type":"text","value":"# This works, the protected attribute is accessible to the derived class"}]},{"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":"find_notes"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" search_term"},{"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 found "},{"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","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" note "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_notes"},{"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":"if"}]},{"type":"text","value":" search_term "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" note"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n found"},{"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":"note"},{"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":" found\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Below we have a handy table for the visibility of attributes with different access modifiers:"}]},{"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"},{"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":"Access modifier"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Example"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Visible to client"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Visible to derived class"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Public"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.name"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"yes"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"yes"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Protected"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self._name"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"no"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"yes"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Private"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.__name"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"no"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"no"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Access modifiers work the same with all traits. For example, in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" class below we have the protected method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"capitalize_initials"}]},{"type":"text","value":". It can be used from the derived class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Footballer"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_capitalize_initials"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"_capitalize_initials"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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 name_capitalized "},{"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","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 name_capitalized"},{"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":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"capitalize"},{"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\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"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name_capitalized"},{"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":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Footballer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nickname"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" position"},{"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":"str"}]},{"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","builtin"]},"children":[{"type":"text","value":"super"}]},{"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":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"# the method is available as it is protected in the base class"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__nickname "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_capitalize_initials"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nickname"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__position "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" position\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":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 r "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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\"Footballer - name: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", nickname: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__nickname"},{"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 r "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"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\", position: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__position"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" r\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Test the classes"}]},{"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 jp "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Footballer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"peter pythons\""}]},{"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":"\"pyper\""}]},{"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":"\"forward\""}]},{"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":"jp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Footballer - name: Peter Pythons, nickname: Pyper, position: forward"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Supergroup","tmcname":"part10-05_supergroup"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the class definition for a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SuperHero"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please define a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SuperGroup"}]},{"type":"text","value":" which represents a group of superheroes. The class should contain the following members:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Protected"}]},{"type":"text","value":" attributes name (str), location (str) and members (list)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"A constructor which takes the name and location of the group as arguments, in that order"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Getter methods for the name and location attributes"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"A method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_member(hero: SuperHero)"}]},{"type":"text","value":" which adds a new member to the group"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"A method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print_group"}]},{"type":"text","value":" which prints out information about the group and its members, following the format specified below"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the class in action:"}]},{"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":"superperson "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SuperHero"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"SuperPerson\""}]},{"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":"\"Superspeed, superstrength\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ninvisible "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SuperHero"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Invisible Inca\""}]},{"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":"\"Invisibility\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrevengers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SuperGroup"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Revengers\""}]},{"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":"\"Emerald City\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrevengers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_member"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"superperson"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrevengers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_member"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"invisible"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrevengers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_group"},{"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":"Revengers, Emerald City\nMembers:\nSuperPerson, superpowers: Superspeed, superstrength\nInvisible Inca, superpowers: Invisibility"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you need a refresher on getter and setter methods, please have a look at "},{"type":"element","tagName":"a","properties":{"href":"/part-9/3-encapsulation#getters-and-setters"},"children":[{"type":"text","value":"this section in the previous part"}]},{"type":"text","value":" of the material."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Secret magic potion","tmcname":"part10-06_secret_magic_potion"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the class definition for a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MagicPotion"}]},{"type":"text","value":" which allows you to save a recipe for a magic potion. The class definition contains a constructor along with the methods"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_ingredient(ingredient: str, amount: float)"}]},{"type":"text","value":" and"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print_recipe()"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please define a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SecretMagicPotion"}]},{"type":"text","value":" which inherits the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MagicPotion"}]},{"type":"text","value":" class and allows you to also protect the recipe with a password."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The new class should have a constructor which also takes the password string as an argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The class should also contain the following methods:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_ingredient(ingredient: str, amount: float, password: str)"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print_recipe(password: str)"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the password argument given to either of these methods is wrong, the methods should raise a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" exception."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the password is correct, each method should call the relevant method in the parent class. Do not copy and paste anything from the MagicPotion class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how this would 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":"diminuendo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SecretMagicPotion"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Diminuendo maximus\""}]},{"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":"\"hocuspocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndiminuendo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_ingredient"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Toadstool\""}]},{"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":"1.5"}]},{"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":"\"hocuspocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndiminuendo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_ingredient"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Magic sand\""}]},{"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":"3.0"}]},{"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":"\"hocuspocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndiminuendo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_ingredient"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Frogspawn\""}]},{"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":"4.0"}]},{"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":"\"hocuspocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndiminuendo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_recipe"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hocuspocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ndiminuendo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_recipe"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"pocushocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# WRONG password!"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"Diminuendo maximus:\nToadstool 1.5 grams\nMagic sand 3.0 grams\nFrogspawn 4.0 grams\nTraceback (most recent call last):\nFile \"secret_magic_potion.py\", line 98, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nraise ValueError(\"Wrong password!\")\nValueError: Wrong password!"}]}]}]}]}]},"html":"

    After this section

    If a trait is defined as private in the base class, it is not directly accessible in any derived classes, as was briefly mentioned in the previous section. Let's take a look at an example. In the Notebook class below the notes are stored in a list, and the list attribute is private:

    \nclass Notebook:\n    \"\"\" A Notebook stores notes in string format \"\"\"\n\n    def __init__(self):\n        # private attribute\n        self.__notes = []\n\n    def add_note(self, note):\n        self.__notes.append(note)\n\n    def retrieve_note(self, index):\n        return self.__notes[index]\n\n    def all_notes(self):\n        return \",\".join(self.__notes)\n

    If the integrity of the class is key, making the list attribute notes private makes sense. The class provides the client with suitable methods for adding and browsing notes, after all. This approach becomes problematic if we define a new class NotebookPro, which inherits the Notebook class. The private list attribute is not accessible to the client, but neither is it accessible to the derived classes. If we try to access it, as in the find_notes method below, we get an error:

    class NotebookPro(Notebook):\n    \"\"\" A better Notebook with search functionality \"\"\"\n    def __init__(self):\n        # This is OK, the constructor is public despite the underscores\n        super().__init__()\n\n    # This causes an error\n    def find_notes(self, search_term):\n        found = []\n        # the attribute __notes is private\n        # the derived class can't access it directly\n        for note in self.__notes:\n            if search_term in note:\n                found.append(note)\n\n        return found

    AttributeError: 'NotebookPro' object has no attribute '_NotebookPro__notes'

    Protected traits

    Many object oriented programming languages have a feature, usually a special keyword, for protecting traits. This means that a trait should be hidden from the clients of the class, but kept accessible to its subclasses. Python in general abhors keywords, so no such feature is directly available in Python. Instead, there is a convention of marking protected traits in a certain way.

    Remember, a trait can be hidden by prefixing its name with two underscores:

    def __init__(self):\n    self.__notes = []

    The agreed convention to protect a trait is to prefix the name with a single underscore. Now, this is just a convention. Nothing prevents a programmer from breaking the convention, but it is considered a bad programming practice.

    def __init__(self):\n    self._notes = []

    Below we have the entire Notebook example, with protected _notes instead of private __notes:

    class Notebook:\n    \"\"\" A Notebook stores notes in string format \"\"\"\n\n    def __init__(self):\n        # protected attribute\n        self._notes = []\n\n    def add_note(self, note):\n        self._notes.append(note)\n\n    def retrieve_note(self, index):\n        return self._notes[index]\n\n    def all_notes(self):\n        return \",\".join(self._notes)\n\nclass NotebookPro(Notebook):\n    \"\"\" A better Notebook with search functionality \"\"\"\n    def __init__(self):\n        # This is OK, the constructor is public despite the underscores\n        super().__init__()\n\n    # This works, the protected attribute is accessible to the derived class\n    def find_notes(self, search_term):\n        found = []\n        for note in self._notes:\n            if search_term in note:\n                found.append(note)\n\n        return found\n

    Below we have a handy table for the visibility of attributes with different access modifiers:

    \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
    Access modifierExampleVisible to clientVisible to derived class
    Publicself.nameyesyes
    Protectedself._namenoyes
    Privateself.__namenono

    Access modifiers work the same with all traits. For example, in the Person class below we have the protected method capitalize_initials. It can be used from the derived class Footballer:

    class Person:\n    def __init__(self, name: str):\n        self._name = self._capitalize_initials(name)\n\n    def _capitalize_initials(self, name):\n        name_capitalized = []\n        for n in name.split(\" \"):\n            name_capitalized.append(n.capitalize())\n\n        return \" \".join(name_capitalized)\n\n    def __repr__(self):\n        return self.__name\n\nclass Footballer(Person):\n\n    def __init__(self, name: str, nickname: str, position: str):\n        super().__init__(name)\n        # the method is available as it is protected in the base class\n        self.__nickname = self._capitalize_initials(nickname)\n        self.__position = position\n\n    def __repr__(self):\n        r =  f\"Footballer - name: {self._name}, nickname: {self.__nickname}\"\n        r += f\", position: {self.__position}\"\n        return r\n\n# Test the classes\nif __name__ == \"__main__\":\n    jp = Footballer(\"peter pythons\", \"pyper\", \"forward\")\n    print(jp)\n

    Footballer - name: Peter Pythons, nickname: Pyper, position: forward

    The exercise template contains the class definition for a SuperHero.

    Please define a class named SuperGroup which represents a group of superheroes. The class should contain the following members:

    An example of the class in action:

    superperson = SuperHero(\"SuperPerson\", \"Superspeed, superstrength\")\ninvisible = SuperHero(\"Invisible Inca\", \"Invisibility\")\nrevengers = SuperGroup(\"Revengers\", \"Emerald City\")\n\nrevengers.add_member(superperson)\nrevengers.add_member(invisible)\nrevengers.print_group()

    Revengers, Emerald City\nMembers:\nSuperPerson, superpowers: Superspeed, superstrength\nInvisible Inca, superpowers: Invisibility

    If you need a refresher on getter and setter methods, please have a look at this section in the previous part of the material.

    The exercise template contains the class definition for a MagicPotion which allows you to save a recipe for a magic potion. The class definition contains a constructor along with the methods

    Please define a class named SecretMagicPotion which inherits the MagicPotion class and allows you to also protect the recipe with a password.

    The new class should have a constructor which also takes the password string as an argument.

    The class should also contain the following methods:

    If the password argument given to either of these methods is wrong, the methods should raise a ValueError exception.

    If the password is correct, each method should call the relevant method in the parent class. Do not copy and paste anything from the MagicPotion class.

    An example of how this would work:

    diminuendo = SecretMagicPotion(\"Diminuendo maximus\", \"hocuspocus\")\ndiminuendo.add_ingredient(\"Toadstool\", 1.5, \"hocuspocus\")\ndiminuendo.add_ingredient(\"Magic sand\", 3.0, \"hocuspocus\")\ndiminuendo.add_ingredient(\"Frogspawn\", 4.0, \"hocuspocus\")\ndiminuendo.print_recipe(\"hocuspocus\")\n\ndiminuendo.print_recipe(\"pocushocus\") # WRONG password!
    Diminuendo maximus:\nToadstool 1.5 grams\nMagic sand 3.0 grams\nFrogspawn 4.0 grams\nTraceback (most recent call last):\nFile \"secret_magic_potion.py\", line 98, in \nraise ValueError(\"Wrong password!\")\nValueError: Wrong password!
    ","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-10/2-access-modifiers.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 understand the access modifiers private and protected"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how the visibility of traits is determined in Python"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If a trait is defined as private in the base class, it is not directly accessible in any derived classes, as was briefly mentioned in the previous section. Let's take a look at an example. In the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Notebook"}]},{"type":"text","value":" class below the notes are stored in a list, and the list attribute is private:"}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Notebook"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" A Notebook stores notes in string format \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# private attribute"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__notes "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_note"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" note"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__notes"},{"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":"note"},{"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":"retrieve_note"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" index"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__notes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"index"},{"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":"all_notes"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__notes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the integrity of the class is key, making the list attribute "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"notes"}]},{"type":"text","value":" private makes sense. The class provides the client with suitable methods for adding and browsing notes, after all. This approach becomes problematic if we define a new class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NotebookPro"}]},{"type":"text","value":", which inherits the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Notebook"}]},{"type":"text","value":" class. The private list attribute is not accessible to the client, but neither is it accessible to the derived classes. If we try to access it, as in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find_notes"}]},{"type":"text","value":" method below, we get an error:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"NotebookPro"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Notebook"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" A better Notebook with search functionality \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# This is OK, the constructor is public despite the underscores"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"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":"__init__"},{"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","comment"]},"children":[{"type":"text","value":"# This causes an error"}]},{"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":"find_notes"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" search_term"},{"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 found "},{"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":"# the attribute __notes is private"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the derived class can't access it directly"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" note "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__notes"},{"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":"if"}]},{"type":"text","value":" search_term "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" note"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n found"},{"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":"note"},{"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":" found"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"AttributeError: 'NotebookPro' object has no attribute '_NotebookPro__notes'"}]}]},{"type":"element","tagName":"h2","properties":{"id":"protected-traits","style":"position:relative;"},"children":[{"type":"text","value":"Protected traits"},{"type":"element","tagName":"a","properties":{"href":"#protected-traits","ariaLabel":"protected traits 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":"Many object oriented programming languages have a feature, usually a special keyword, for "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"protecting"}]},{"type":"text","value":" traits. This means that a trait should be hidden from the clients of the class, but kept accessible to its subclasses. Python in general abhors keywords, so no such feature is directly available in Python. Instead, there is a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"convention"}]},{"type":"text","value":" of marking protected traits in a certain way."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Remember, a trait can be hidden by prefixing its name with two underscores:"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__notes "},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The agreed convention to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"protect"}]},{"type":"text","value":" a trait is to prefix the name with a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"single"}]},{"type":"text","value":" underscore. Now, this is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"just"}]},{"type":"text","value":" a convention. Nothing prevents a programmer from breaking the convention, but it is considered a bad programming practice."}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_notes "},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Below we have the entire Notebook example, with protected "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"_notes"}]},{"type":"text","value":" instead of private "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__notes"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Notebook"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" A Notebook stores notes in string format \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# protected attribute"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_notes "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_note"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" note"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_notes"},{"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":"note"},{"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":"retrieve_note"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" index"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_notes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"index"},{"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":"all_notes"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_notes"},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"NotebookPro"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Notebook"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" A better Notebook with search functionality \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# This is OK, the constructor is public despite the underscores"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"super"}]},{"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":"__init__"},{"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","comment"]},"children":[{"type":"text","value":"# This works, the protected attribute is accessible to the derived class"}]},{"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":"find_notes"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" search_term"},{"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 found "},{"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","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" note "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_notes"},{"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":"if"}]},{"type":"text","value":" search_term "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" note"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n found"},{"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":"note"},{"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":" found\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Below we have a handy table for the visibility of attributes with different access modifiers:"}]},{"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"},{"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":"Access modifier"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Example"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Visible to client"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Visible to derived class"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Public"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.name"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"yes"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"yes"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Protected"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self._name"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"no"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"yes"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Private"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.__name"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"no"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"no"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Access modifiers work the same with all traits. For example, in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" class below we have the protected method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"capitalize_initials"}]},{"type":"text","value":". It can be used from the derived class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Footballer"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_capitalize_initials"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"_capitalize_initials"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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 name_capitalized "},{"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","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 name_capitalized"},{"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":"n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"capitalize"},{"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\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"]},"children":[{"type":"text","value":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name_capitalized"},{"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":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Footballer"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" nickname"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" position"},{"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":"str"}]},{"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","builtin"]},"children":[{"type":"text","value":"super"}]},{"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":"__init__"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"# the method is available as it is protected in the base class"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__nickname "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_capitalize_initials"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"nickname"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__position "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" position\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":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 r "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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\"Footballer - name: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", nickname: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__nickname"},{"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 r "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"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\", position: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__position"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" r\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Test the classes"}]},{"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 jp "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Footballer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"peter pythons\""}]},{"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":"\"pyper\""}]},{"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":"\"forward\""}]},{"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":"jp"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Footballer - name: Peter Pythons, nickname: Pyper, position: forward"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Supergroup","tmcname":"part10-05_supergroup"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the class definition for a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SuperHero"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please define a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SuperGroup"}]},{"type":"text","value":" which represents a group of superheroes. The class should contain the following members:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Protected"}]},{"type":"text","value":" attributes name (str), location (str) and members (list)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"A constructor which takes the name and location of the group as arguments, in that order"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Getter methods for the name and location attributes"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"A method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_member(hero: SuperHero)"}]},{"type":"text","value":" which adds a new member to the group"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"A method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print_group"}]},{"type":"text","value":" which prints out information about the group and its members, following the format specified below"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the class in action:"}]},{"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":"superperson "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SuperHero"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"SuperPerson\""}]},{"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":"\"Superspeed, superstrength\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ninvisible "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SuperHero"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Invisible Inca\""}]},{"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":"\"Invisibility\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrevengers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SuperGroup"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Revengers\""}]},{"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":"\"Emerald City\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrevengers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_member"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"superperson"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrevengers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_member"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"invisible"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrevengers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_group"},{"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":"Revengers, Emerald City\nMembers:\nSuperPerson, superpowers: Superspeed, superstrength\nInvisible Inca, superpowers: Invisibility"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you need a refresher on getter and setter methods, please have a look at "},{"type":"element","tagName":"a","properties":{"href":"/part-9/3-encapsulation#getters-and-setters"},"children":[{"type":"text","value":"this section in the previous part"}]},{"type":"text","value":" of the material."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Secret magic potion","tmcname":"part10-06_secret_magic_potion"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the class definition for a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MagicPotion"}]},{"type":"text","value":" which allows you to save a recipe for a magic potion. The class definition contains a constructor along with the methods"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_ingredient(ingredient: str, amount: float)"}]},{"type":"text","value":" and"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print_recipe()"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please define a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SecretMagicPotion"}]},{"type":"text","value":" which inherits the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MagicPotion"}]},{"type":"text","value":" class and allows you to also protect the recipe with a password."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The new class should have a constructor which also takes the password string as an argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The class should also contain the following methods:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_ingredient(ingredient: str, amount: float, password: str)"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print_recipe(password: str)"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the password argument given to either of these methods is wrong, the methods should raise a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" exception."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the password is correct, each method should call the relevant method in the parent class. Do not copy and paste anything from the MagicPotion class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how this would 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":"diminuendo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SecretMagicPotion"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Diminuendo maximus\""}]},{"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":"\"hocuspocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndiminuendo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_ingredient"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Toadstool\""}]},{"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":"1.5"}]},{"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":"\"hocuspocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndiminuendo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_ingredient"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Magic sand\""}]},{"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":"3.0"}]},{"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":"\"hocuspocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndiminuendo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_ingredient"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Frogspawn\""}]},{"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":"4.0"}]},{"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":"\"hocuspocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndiminuendo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_recipe"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hocuspocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ndiminuendo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_recipe"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"pocushocus\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# WRONG password!"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"Diminuendo maximus:\nToadstool 1.5 grams\nMagic sand 3.0 grams\nFrogspawn 4.0 grams\nTraceback (most recent call last):\nFile \"secret_magic_potion.py\", line 98, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nraise ValueError(\"Wrong password!\")\nValueError: Wrong password!"}]}]}]}]}]},"html":"

    After this section

    If a trait is defined as private in the base class, it is not directly accessible in any derived classes, as was briefly mentioned in the previous section. Let's take a look at an example. In the Notebook class below the notes are stored in a list, and the list attribute is private:

    \nclass Notebook:\n    \"\"\" A Notebook stores notes in string format \"\"\"\n\n    def __init__(self):\n        # private attribute\n        self.__notes = []\n\n    def add_note(self, note):\n        self.__notes.append(note)\n\n    def retrieve_note(self, index):\n        return self.__notes[index]\n\n    def all_notes(self):\n        return \",\".join(self.__notes)\n

    If the integrity of the class is key, making the list attribute notes private makes sense. The class provides the client with suitable methods for adding and browsing notes, after all. This approach becomes problematic if we define a new class NotebookPro, which inherits the Notebook class. The private list attribute is not accessible to the client, but neither is it accessible to the derived classes. If we try to access it, as in the find_notes method below, we get an error:

    class NotebookPro(Notebook):\n    \"\"\" A better Notebook with search functionality \"\"\"\n    def __init__(self):\n        # This is OK, the constructor is public despite the underscores\n        super().__init__()\n\n    # This causes an error\n    def find_notes(self, search_term):\n        found = []\n        # the attribute __notes is private\n        # the derived class can't access it directly\n        for note in self.__notes:\n            if search_term in note:\n                found.append(note)\n\n        return found

    AttributeError: 'NotebookPro' object has no attribute '_NotebookPro__notes'

    Protected traits

    Many object oriented programming languages have a feature, usually a special keyword, for protecting traits. This means that a trait should be hidden from the clients of the class, but kept accessible to its subclasses. Python in general abhors keywords, so no such feature is directly available in Python. Instead, there is a convention of marking protected traits in a certain way.

    Remember, a trait can be hidden by prefixing its name with two underscores:

    def __init__(self):\n    self.__notes = []

    The agreed convention to protect a trait is to prefix the name with a single underscore. Now, this is just a convention. Nothing prevents a programmer from breaking the convention, but it is considered a bad programming practice.

    def __init__(self):\n    self._notes = []

    Below we have the entire Notebook example, with protected _notes instead of private __notes:

    class Notebook:\n    \"\"\" A Notebook stores notes in string format \"\"\"\n\n    def __init__(self):\n        # protected attribute\n        self._notes = []\n\n    def add_note(self, note):\n        self._notes.append(note)\n\n    def retrieve_note(self, index):\n        return self._notes[index]\n\n    def all_notes(self):\n        return \",\".join(self._notes)\n\nclass NotebookPro(Notebook):\n    \"\"\" A better Notebook with search functionality \"\"\"\n    def __init__(self):\n        # This is OK, the constructor is public despite the underscores\n        super().__init__()\n\n    # This works, the protected attribute is accessible to the derived class\n    def find_notes(self, search_term):\n        found = []\n        for note in self._notes:\n            if search_term in note:\n                found.append(note)\n\n        return found\n

    Below we have a handy table for the visibility of attributes with different access modifiers:

    \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
    Access modifierExampleVisible to clientVisible to derived class
    Publicself.nameyesyes
    Protectedself._namenoyes
    Privateself.__namenono

    Access modifiers work the same with all traits. For example, in the Person class below we have the protected method capitalize_initials. It can be used from the derived class Footballer:

    class Person:\n    def __init__(self, name: str):\n        self._name = self._capitalize_initials(name)\n\n    def _capitalize_initials(self, name):\n        name_capitalized = []\n        for n in name.split(\" \"):\n            name_capitalized.append(n.capitalize())\n\n        return \" \".join(name_capitalized)\n\n    def __repr__(self):\n        return self.__name\n\nclass Footballer(Person):\n\n    def __init__(self, name: str, nickname: str, position: str):\n        super().__init__(name)\n        # the method is available as it is protected in the base class\n        self.__nickname = self._capitalize_initials(nickname)\n        self.__position = position\n\n    def __repr__(self):\n        r =  f\"Footballer - name: {self._name}, nickname: {self.__nickname}\"\n        r += f\", position: {self.__position}\"\n        return r\n\n# Test the classes\nif __name__ == \"__main__\":\n    jp = Footballer(\"peter pythons\", \"pyper\", \"forward\")\n    print(jp)\n

    Footballer - name: Peter Pythons, nickname: Pyper, position: forward

    The exercise template contains the class definition for a SuperHero.

    Please define a class named SuperGroup which represents a group of superheroes. The class should contain the following members:

    An example of the class in action:

    superperson = SuperHero(\"SuperPerson\", \"Superspeed, superstrength\")\ninvisible = SuperHero(\"Invisible Inca\", \"Invisibility\")\nrevengers = SuperGroup(\"Revengers\", \"Emerald City\")\n\nrevengers.add_member(superperson)\nrevengers.add_member(invisible)\nrevengers.print_group()

    Revengers, Emerald City\nMembers:\nSuperPerson, superpowers: Superspeed, superstrength\nInvisible Inca, superpowers: Invisibility

    If you need a refresher on getter and setter methods, please have a look at this section in the previous part of the material.

    The exercise template contains the class definition for a MagicPotion which allows you to save a recipe for a magic potion. The class definition contains a constructor along with the methods

    Please define a class named SecretMagicPotion which inherits the MagicPotion class and allows you to also protect the recipe with a password.

    The new class should have a constructor which also takes the password string as an argument.

    The class should also contain the following methods:

    If the password argument given to either of these methods is wrong, the methods should raise a ValueError exception.

    If the password is correct, each method should call the relevant method in the parent class. Do not copy and paste anything from the MagicPotion class.

    An example of how this would work:

    diminuendo = SecretMagicPotion(\"Diminuendo maximus\", \"hocuspocus\")\ndiminuendo.add_ingredient(\"Toadstool\", 1.5, \"hocuspocus\")\ndiminuendo.add_ingredient(\"Magic sand\", 3.0, \"hocuspocus\")\ndiminuendo.add_ingredient(\"Frogspawn\", 4.0, \"hocuspocus\")\ndiminuendo.print_recipe(\"hocuspocus\")\n\ndiminuendo.print_recipe(\"pocushocus\") # WRONG password!
    Diminuendo maximus:\nToadstool 1.5 grams\nMagic sand 3.0 grams\nFrogspawn 4.0 grams\nTraceback (most recent call last):\nFile \"secret_magic_potion.py\", line 98, in \nraise ValueError(\"Wrong password!\")\nValueError: Wrong password!
    ","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-10/2-access-modifiers.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-10/3-oo-programming-techniques/page-data.json b/page-data/part-10/3-oo-programming-techniques/page-data.json index f2cc92936..e6cd7939a 100644 --- a/page-data/part-10/3-oo-programming-techniques/page-data.json +++ b/page-data/part-10/3-oo-programming-techniques/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-10/3-oo-programming-techniques", - "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 some of the different uses for the variable name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to overload operators in your own classes"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to create an iterable class"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A class can contain a method which returns an object of the very same class. For example, below we have the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Product"}]},{"type":"text","value":", whose method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"product_on_sale"}]},{"type":"text","value":" returns a new Product object with the same name as the original but with a price which is 25 % lower:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Product"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" price"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" price\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" (price "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price"},{"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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"product_on_sale"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 on_sale "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price "},{"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.75"}]},{"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":" on_sale"}]}]}]},{"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":"apple1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Apple\""}]},{"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":"2.99"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napple2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" apple1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"product_on_sale"},{"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":"apple1"},{"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":"apple2"},{"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":"Apple (price 2.99)\nApple (price 2.2425)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's review the purpose of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":": within a class definition it refers to the object itself. Typically it is used to refer to the object's own traits, its attributes and methods. The variable can be used to refer to the entire object as well, for example if the object itself needs to be returned to the client code. In the example below we've added the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cheaper"}]},{"type":"text","value":" to the class definition. It takes another Product as its argument and returns the cheaper of the two:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Product"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" price"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" price\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" (price "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price"},{"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","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"price"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price\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":"cheaper"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" Product"},{"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":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"price"},{"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":" self\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":" Product"}]}]}]},{"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":"apple "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Apple\""}]},{"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":"2.99"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\norange "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Orange\""}]},{"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":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbanana "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Banana\""}]},{"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":"5.25"}]},{"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":"orange"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"cheaper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"apple"},{"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":"orange"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"cheaper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"banana"},{"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":"Apple (2.99)\nOrange (3.95)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"While this works just fine, it is a very specialised case of comparing two objects. It would be better if we could use the Python comparison operators directly on these "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Product"}]},{"type":"text","value":" objects."}]},{"type":"element","tagName":"h2","properties":{"id":"overloading-operators","style":"position:relative;"},"children":[{"type":"text","value":"Overloading operators"},{"type":"element","tagName":"a","properties":{"href":"#overloading-operators","ariaLabel":"overloading operators 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":"Python contains some specially named built-in methods for working with the standard arithmetic and comparison operators. The technique is called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"operator overloading"}]},{"type":"text","value":". If you want to be able to use a certain operator on instances of self-defined classes, you can write a special method which returns the correct result of the operator. We have already used this technique with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method: Python knows to look for a method named like this when a string representation of an object is called for."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's start with the operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" which tells us if the first operand is greater than the second. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Product"}]},{"type":"text","value":" class definition below contains the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__gt__"}]},{"type":"text","value":", which is short for "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"g"}]},{"type":"text","value":"reater "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"t"}]},{"type":"text","value":"han. This specially named method should return the correct result of the comparison. Specifically, it should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" if and only if the current object is greater than the object passed as an argument. The criteria used can be determined by the programmer. By "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"current object"}]},{"type":"text","value":" we mean the object on which the method is called with the dot "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" notation."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Product"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" price"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" price\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" (price "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price"},{"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","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"price"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price\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":"__gt__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" another_product"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"price "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" another_product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"price"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the implementation above, the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__gt__"}]},{"type":"text","value":" returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" if the price of the current product is greater than the price of the product passed as an argument. Otherwise the method returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now the comparison operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" is available for use with objects of type Product:"}]},{"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":"orange "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Orange\""}]},{"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":"2.90"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napple "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Apple\""}]},{"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":"3.95"}]},{"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":"if"}]},{"type":"text","value":" orange "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" apple"},{"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":"\"Orange is greater\""}]},{"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":"else"}]},{"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":"\"Apple is greater\""}]},{"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":"Apple is greater"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As stated above, it is up to the programmer to determine the criteria by which it is decided which is greater and which is lesser. We could, for instance, decide that the order should not be based on price, but be alphabetical by name instead. This would mean that "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"orange"}]},{"type":"text","value":" would now be \"greater than\" "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"apple"}]},{"type":"text","value":", as \"orange\" comes alphabetically last."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Product"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" price"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" price\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" (price "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price"},{"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","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"price"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"name"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name\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":"__gt__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" another_product"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" another_product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"}]}]}]},{"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":"Orange "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Orange\""}]},{"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":"4.90"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nApple "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Apple\""}]},{"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":"3.95"}]},{"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":"if"}]},{"type":"text","value":" Orange "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" Apple"},{"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":"\"Orange is greater\""}]},{"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":"else"}]},{"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":"\"Apple is greater\""}]},{"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":"Orange is greater"}]}]},{"type":"element","tagName":"h2","properties":{"id":"more-operators","style":"position:relative;"},"children":[{"type":"text","value":"More operators"},{"type":"element","tagName":"a","properties":{"href":"#more-operators","ariaLabel":"more operators 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":"Here we have a table containing the standard comparison operators, along with the methods which need to be implemented if we want to make them available for use on our objects:"}]},{"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\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":"Operator"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Traditional meaning"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Name of method"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Less than"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__lt__(self, another)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Greater than"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__gt__(self, another)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Equal to"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__eq__(self, another)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!="}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Not equal to"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__ne__(self, another)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<="}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Less than or equal to"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__le__(self, another)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">="}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Greater than or equal to"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__ge__(self, another)"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can also implement some other operators, including the following arithmetic operators:"}]},{"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\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":"Operator"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Traditional meaning"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Name of method"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Addition"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__add__(self, another)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Subtraction"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__sub__(self, another)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Multiplication"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__mul__(self, another)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"/"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Division (floating point result)"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__truediv__(self, another)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"//"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Division (integer result)"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__floordiv__(self, another)"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"More operators and method names are easily found online. Remember also the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dir"}]},{"type":"text","value":" command for listing the methods available for use on a given object."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is very rarely necessary to implement all the arithmetic and comparison operators in your own classes. For example, division is an operation which rarely makes sense outside numerical objects. What would be the result of dividing a Student object by three, or by another Student object? Nevertheless, some of these operators are often very useful with also your own classes. The selection of methods to implement depends on what makes sense, knowing the properties of your objects."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at a class which models a single note. If we implement the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__add__"}]},{"type":"text","value":" method within our class definition, the addition operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" becomes available on our Note objects:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Note"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" entry_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" entry"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"entry_date "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" entry_date\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"entry "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" entry\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"entry_date"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"entry"},{"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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__add__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" another"},{"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":"# The date of the new note is the current time"}]},{"type":"text","value":"\n new_note "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Note"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"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":" "},{"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 new_note"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"entry "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"entry "},{"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":"\" and \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" another"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"entry\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" new_note"}]}]}]},{"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":"entry1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Note"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2016"}]},{"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":"12"}]},{"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":"17"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Remember to buy presents\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nentry2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Note"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2016"}]},{"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":"12"}]},{"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":"23"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Remember to get a tree\""}]},{"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":"# These notes can be added together with the + operator"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This calls the __add__ method in the Note class"}]},{"type":"text","value":"\nboth "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" entry1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" entry2\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":"both"},{"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":"2020-09-09 14:13:02.163170: Remember to buy presents and Remember to get a tree"}]}]},{"type":"element","tagName":"h2","properties":{"id":"a-string-representation-of-an-object","style":"position:relative;"},"children":[{"type":"text","value":"A string representation of an object"},{"type":"element","tagName":"a","properties":{"href":"#a-string-representation-of-an-object","ariaLabel":"a string representation of an object 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":"You have already implemented quite a few "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" methods in your classes. As you know, the method returns a string representation of the object. Another quite similar method is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":" which returns a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"technical"}]},{"type":"text","value":" representation of the object. The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":" is often implemented so that it returns the program code which can be executed to return an object with "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"identical contents"}]},{"type":"text","value":" to the current object."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"repr"}]},{"type":"text","value":" returns this technical string representation of the object. The technical representation is used also whenever the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method has not been defined for the object. The example below will make this clearer:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" age"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" age\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":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Person("}]},{"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":"repr"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"age"},{"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":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python3"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python3"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python3"]},"children":[{"type":"text","value":"person1 = Person(\"Anna\", 25)\nperson2 = Person(\"Peter\", 99)\nprint(person1)\nprint(person2)"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Person('Anna', 25)\nPerson('Peter', 99)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice how the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":" method itself uses the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"repr"}]},{"type":"text","value":" function to retrieve the technical representation of the string. This is necessary to include the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"'"}]},{"type":"text","value":" characters in the result."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following class has definitions for both "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" age"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" age\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":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Person("}]},{"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":"repr"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"age"},{"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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"age"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" years)\""}]}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python3"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python3"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python3"]},"children":[{"type":"text","value":"Person = Person(\"Anna\", 25)\nprint(Person)\nprint(repr(Person))"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Anna (25 years)\nPerson('Anna', 25)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is worth mentioning that with data structures, such as lists, Python always uses the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":" method for the string representation of the contents. This can sometimes look a bit baffling:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python3"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python3"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python3"]},"children":[{"type":"text","value":"persons = []\npersons.append(Person(\"Anna\", 25))\npersons.append(Person(\"Peter\", 99))\npersons.append(Person(\"Mary\", 55))\nprint(persons)"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[Person('Anna', 25), Person('Peter', 99), Person('Mary', 55)]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Money","tmcname":"part10-07_money"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains an outline for a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Money"}]},{"type":"text","value":". This exercise asks you to implement some additional methods and to fix some small problems in the template."}]},{"type":"element","tagName":"h2","properties":{"id":"fix-the-string-representation","style":"position:relative;"},"children":[{"type":"text","value":"Fix the string representation"},{"type":"element","tagName":"a","properties":{"href":"#fix-the-string-representation","ariaLabel":"fix the string representation 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 "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method in the class definition doesn't work quite right. Given the following two Money objects, the latter is printed out wrong:"}]},{"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":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Money"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Money"},{"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":" "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# two euros and five cents"}]},{"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":"e1"},{"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":"e2"},{"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.10\n2.5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please fix the method so that it prints out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4.10 eur\n2.05 eur"}]}]},{"type":"element","tagName":"h2","properties":{"id":"equal-amounts","style":"position:relative;"},"children":[{"type":"text","value":"Equal amounts"},{"type":"element","tagName":"a","properties":{"href":"#equal-amounts","ariaLabel":"equal amounts 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":"Please define a new method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__eq__(self, another)"}]},{"type":"text","value":" which allows you to use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" comparison operator on Money objects. You can test your implementation with the following code:"}]},{"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":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Money"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Money"},{"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":" "},{"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":"\ne3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Money"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"e1"},{"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":"e2"},{"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":"e3"},{"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":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" e2"},{"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":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" e3"},{"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.10 eur\n2.05 eur\n4.10 eur\nFalse\nTrue"}]}]},{"type":"element","tagName":"h2","properties":{"id":"other-comparison-operators","style":"position:relative;"},"children":[{"type":"text","value":"Other comparison operators"},{"type":"element","tagName":"a","properties":{"href":"#other-comparison-operators","ariaLabel":"other comparison operators 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":"Please implement the appropriate methods for the comparison operators "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":"."}]},{"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":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Money"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Money"},{"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":" "},{"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\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":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":" e2"},{"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":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" e2"},{"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":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" e2"},{"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\nTrue"}]}]},{"type":"element","tagName":"h2","properties":{"id":"addition-and-subtraction","style":"position:relative;"},"children":[{"type":"text","value":"Addition and subtraction"},{"type":"element","tagName":"a","properties":{"href":"#addition-and-subtraction","ariaLabel":"addition and subtraction 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":"Please implement the addition and subtraction operators "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" for Money objects. Both should return a new object of type Money. Neither the object itself nor the object passed as an argument should be changed as a result."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the value of a Money object cannot be negative. If an attempt to subtract would result in a negative result, the method should raise a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" exception."}]},{"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":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Money"},{"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":"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":"\ne2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Money"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ne3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" e2\ne4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" e2\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":"e3"},{"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":"e4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ne5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" e2"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"e1"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"7.00 eur\n1.10 eur\nTraceback (most recent call last):\nFile \"money.py\", line 416, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\ne5 = e2-e1\nFile \"money.py\", line 404, in "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sub"}]},{"type":"text","value":"\nraise ValueError(f\"a negative result is not allowed\")\nValueError: a negative result is not allowed"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-value-must-not-be-directly-accessible","style":"position:relative;"},"children":[{"type":"text","value":"The value must not be directly accessible"},{"type":"element","tagName":"a","properties":{"href":"#the-value-must-not-be-directly-accessible","ariaLabel":"the value must not be directly accessible 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 class still has a small integrity issue. The user can \"cheat\" by accessing the attributes directly and changing the value stored in the Money object:"}]},{"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":"text","value":"e1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"euros "},{"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":"1000"}]},{"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":"e1"},{"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.05 eur\n1000.05 eur"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please "},{"type":"element","tagName":"a","properties":{"href":"/part-9/3-encapsulation#encapsulation"},"children":[{"type":"text","value":"encapsulate"}]},{"type":"text","value":" the implementation of the attributes defined in the class so that the cheat used above is not possible. The class should have no public attributes, and no getter or setter methods for the euros or the cents."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Simple date","tmcname":"part10-08_simple_date"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you are asked to implement the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SimpleDate"}]},{"type":"text","value":" which allows you to handle dates. For simplicity's sake we assume here that "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"each month has 30 days"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Because of this simplification you should not use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":" module from the Python standard library. You will implement similar functionality by yourself instead."}]},{"type":"element","tagName":"h2","properties":{"id":"comparisons","style":"position:relative;"},"children":[{"type":"text","value":"Comparisons"},{"type":"element","tagName":"a","properties":{"href":"#comparisons","ariaLabel":"comparisons 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":"Please implement the outline of the class, along with methods allowing for comparisons with the operators "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":". You can use the following code to test your implementation:"}]},{"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":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpleDate"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpleDate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"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":"12"}]},{"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":"1985"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpleDate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"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":"12"}]},{"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":"1985"}]},{"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":"d1"},{"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":"d2"},{"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":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" d2"},{"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":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":" d2"},{"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":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" d3"},{"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":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" d2"},{"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":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" d2"},{"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.10.2020\n28.12.1985\nFalse\nTrue\nFalse\nFalse\nTrue"}]}]},{"type":"element","tagName":"h2","properties":{"id":"increment","style":"position:relative;"},"children":[{"type":"text","value":"Increment"},{"type":"element","tagName":"a","properties":{"href":"#increment","ariaLabel":"increment 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":"Please implement the addition operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" which allows you to add a given number of days to a SimpleDate object. The operator should return a new SimpleDate object. The original object should not 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":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpleDate"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpleDate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"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":"12"}]},{"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":"1985"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nd3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" d1 "},{"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":"\nd4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" d2 "},{"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":"400"}]},{"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":"d1"},{"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":"d2"},{"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":"d3"},{"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":"d4"},{"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.10.2020\n28.12.1985\n7.10.2020\n8.2.1987"}]}]},{"type":"element","tagName":"h2","properties":{"id":"difference","style":"position:relative;"},"children":[{"type":"text","value":"Difference"},{"type":"element","tagName":"a","properties":{"href":"#difference","ariaLabel":"difference 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":"Please implement the subtraction operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" which allows you to find out the difference in days between two SimpleDate objects. As we assumed each month to have 30 days, a year within the confines of this exercise is 12*30 = 360 days long."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can use the following code to test your program:"}]},{"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":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpleDate"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpleDate"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"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":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpleDate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"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":"12"}]},{"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":"1985"}]},{"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":"d2"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"d1"},{"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":"d1"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"d2"},{"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":"d1"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"d3"},{"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":"28\n28\n12516"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"iterators","style":"position:relative;"},"children":[{"type":"text","value":"Iterators"},{"type":"element","tagName":"a","properties":{"href":"#iterators","ariaLabel":"iterators 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 that the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" statement can be used to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"iterate"}]},{"type":"text","value":" through many different data structures, files and collections of items. A typical use case would be the following 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":"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":"count_positives"}]},{"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":" "},{"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 n "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"if"}]},{"type":"text","value":" item "},{"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 n "},{"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":"return"}]},{"type":"text","value":" n\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function goes through the items in the list one by one, and keeps track of how many of the items were positive."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is possible to make your own classes iterable, too. This is useful when the core purpose of the class involves storing a collection of items. The Bookshelf class from a previous example would be a good candidate, as it would make sense to use a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop to go through the books on the shelf. The same applies to, say, a student register. Being able to iterate through the collection of students could be useful."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To make a class iterable you must implement the iterator methods "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__iter__"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__next__"}]},{"type":"text","value":". We will return to the specifics of these methods after the following example:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Book"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" author"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" page_count"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"author "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" author\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"page_count "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" page_count\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bookshelf"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_books "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_book"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Book"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_books"},{"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":"book"},{"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":"# This is the iterator initialization method"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# The iteration variable(s) should be initialized 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":"__iter__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the method returns a reference to the object itself as "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the iterator is implemented within the same class definition"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This method returns the next item within the object"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If all items have been traversed, the StopIteration event is raised"}]},{"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":"__next__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_books"},{"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":"# Select the current item from the list within the object"}]},{"type":"text","value":"\n book "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_books"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n"},{"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":"# increase the counter (i.e. iteration variable) by one"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"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","comment"]},"children":[{"type":"text","value":"# return the current item"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" book\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"# All books have been traversed"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" StopIteration"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__iter__"}]},{"type":"text","value":" initializes the iteration variable or variables. In this case it suffices to have a simple counter containing the index of the current item in the list. We also need the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__next__"}]},{"type":"text","value":", which returns the next item in the iterator. In the example above the method returns the item at index "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" from the list within the Bookshelf object, and the iterator variable is also incremented."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When all objects have been traversed, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__next__"}]},{"type":"text","value":" method raises the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"StopIteration"}]},{"type":"text","value":" exception. The process is no different from raising any other exceptions, but this exception is automatically handled by Python and its purpose is to signal to the code calling the iterator (e.g. a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop) that the iteration is now over."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Our Bookshelf is now ready for iteration, for example with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop:"}]},{"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":"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 b1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"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 Life of 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":"\"Montague Python\""}]},{"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":"123"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"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 Old Man and the C\""}]},{"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":"\"Ernest Hemingjavay\""}]},{"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":"204"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"A Good Cup of Java\""}]},{"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":"\"Caffee Coder\""}]},{"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":"997"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n shelf "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bookshelf"},{"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 shelf"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n shelf"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n shelf"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b3"},{"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":"# Print the names of all the books"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" book "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" shelf"},{"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":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"The Life of Python\nThe Old Man and the C\nA Good Cup of Java"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"An iterable shopping list","tmcname":"part10-09_iterable_shopping_list"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ShoppingList"}]},{"type":"text","value":" class from the "},{"type":"element","tagName":"a","properties":{"href":"/part-8/2-classes-and-objects#programming-exercise-shopping-list"},"children":[{"type":"text","value":"exercise in part 8"}]},{"type":"text","value":". Please adjust the class so that it is iterable and can thus be used as follows:"}]},{"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":"shopping_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ShoppingList"},{"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":"\nshopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"bananas\""}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nshopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apples\""}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nshopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"pineapple\""}]},{"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":"1"}]},{"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":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" shopping_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","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":"product"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"product"},{"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":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" units\""}]}]},{"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":"bananas: 10 units\napples: 5 units\npineapple: 1 units"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__next__"}]},{"type":"text","value":" method of your iterator should return tuples, where the first item is the name of the product and the second item is the amount."}]}]}]},"html":"

    After this section

    A class can contain a method which returns an object of the very same class. For example, below we have the class Product, whose method product_on_sale returns a new Product object with the same name as the original but with a price which is 25 % lower:

    class Product:\n    def __init__(self, name: str, price: float):\n        self.__name = name\n        self.__price = price\n\n    def __str__(self):\n        return f\"{self.__name} (price {self.__price})\"\n\n    def product_on_sale(self):\n        on_sale = Product(self.__name, self.__price * 0.75)\n        return on_sale
    apple1 = Product(\"Apple\", 2.99)\napple2 = apple1.product_on_sale()\nprint(apple1)\nprint(apple2)

    Apple (price 2.99)\nApple (price 2.2425)

    Let's review the purpose of the variable self: within a class definition it refers to the object itself. Typically it is used to refer to the object's own traits, its attributes and methods. The variable can be used to refer to the entire object as well, for example if the object itself needs to be returned to the client code. In the example below we've added the method cheaper to the class definition. It takes another Product as its argument and returns the cheaper of the two:

    class Product:\n    def __init__(self, name: str, price: float):\n        self.__name = name\n        self.__price = price\n\n    def __str__(self):\n        return f\"{self.__name} (price {self.__price})\"\n\n    @property\n    def price(self):\n        return self.__price\n\n    def cheaper(self, Product):\n        if self.__price < Product.price:\n            return self\n        else:\n            return Product
    apple = Product(\"Apple\", 2.99)\norange = Product(\"Orange\", 3.95)\nbanana = Product(\"Banana\", 5.25)\n\nprint(orange.cheaper(apple))\nprint(orange.cheaper(banana))

    Apple (2.99)\nOrange (3.95)

    While this works just fine, it is a very specialised case of comparing two objects. It would be better if we could use the Python comparison operators directly on these Product objects.

    Overloading operators

    Python contains some specially named built-in methods for working with the standard arithmetic and comparison operators. The technique is called operator overloading. If you want to be able to use a certain operator on instances of self-defined classes, you can write a special method which returns the correct result of the operator. We have already used this technique with the __str__ method: Python knows to look for a method named like this when a string representation of an object is called for.

    Let's start with the operator > which tells us if the first operand is greater than the second. The Product class definition below contains the method __gt__, which is short for greater than. This specially named method should return the correct result of the comparison. Specifically, it should return True if and only if the current object is greater than the object passed as an argument. The criteria used can be determined by the programmer. By current object we mean the object on which the method is called with the dot . notation.

    class Product:\n    def __init__(self, name: str, price: float):\n        self.__name = name\n        self.__price = price\n\n    def __str__(self):\n        return f\"{self.__name} (price {self.__price})\"\n\n    @property\n    def price(self):\n        return self.__price\n\n    def __gt__(self, another_product):\n        return self.price > another_product.price

    In the implementation above, the method __gt__ returns True if the price of the current product is greater than the price of the product passed as an argument. Otherwise the method returns False.

    Now the comparison operator > is available for use with objects of type Product:

    orange = Product(\"Orange\", 2.90)\napple = Product(\"Apple\", 3.95)\n\nif orange > apple:\n    print(\"Orange is greater\")\nelse:\n    print(\"Apple is greater\")

    Apple is greater

    As stated above, it is up to the programmer to determine the criteria by which it is decided which is greater and which is lesser. We could, for instance, decide that the order should not be based on price, but be alphabetical by name instead. This would mean that orange would now be \"greater than\" apple, as \"orange\" comes alphabetically last.

    class Product:\n    def __init__(self, name: str, price: float):\n        self.__name = name\n        self.__price = price\n\n    def __str__(self):\n        return f\"{self.__name} (price {self.__price})\"\n\n    @property\n    def price(self):\n        return self.__price\n\n    @property\n    def name(self):\n        return self.__name\n\n    def __gt__(self, another_product):\n        return self.name > another_product.name
    Orange = Product(\"Orange\", 4.90)\nApple = Product(\"Apple\", 3.95)\n\nif Orange > Apple:\n    print(\"Orange is greater\")\nelse:\n    print(\"Apple is greater\")

    Orange is greater

    More operators

    Here we have a table containing the standard comparison operators, along with the methods which need to be implemented if we want to make them available for use on our objects:

    \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\n\n\n\n\n\n\n
    OperatorTraditional meaningName of method
    <Less than__lt__(self, another)
    >Greater than__gt__(self, another)
    ==Equal to__eq__(self, another)
    !=Not equal to__ne__(self, another)
    <=Less than or equal to__le__(self, another)
    >=Greater than or equal to__ge__(self, another)

    You can also implement some other operators, including the following arithmetic operators:

    \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\n\n
    OperatorTraditional meaningName of method
    +Addition__add__(self, another)
    -Subtraction__sub__(self, another)
    *Multiplication__mul__(self, another)
    /Division (floating point result)__truediv__(self, another)
    //Division (integer result)__floordiv__(self, another)

    More operators and method names are easily found online. Remember also the dir command for listing the methods available for use on a given object.

    It is very rarely necessary to implement all the arithmetic and comparison operators in your own classes. For example, division is an operation which rarely makes sense outside numerical objects. What would be the result of dividing a Student object by three, or by another Student object? Nevertheless, some of these operators are often very useful with also your own classes. The selection of methods to implement depends on what makes sense, knowing the properties of your objects.

    Let's have a look at a class which models a single note. If we implement the __add__ method within our class definition, the addition operator + becomes available on our Note objects:

    from datetime import datetime\n\nclass Note:\n    def __init__(self, entry_date: datetime, entry: str):\n        self.entry_date = entry_date\n        self.entry = entry\n\n    def __str__(self):\n        return f\"{self.entry_date}: {self.entry}\"\n\n    def __add__(self, another):\n        # The date of the new note is the current time\n        new_note = Note(datetime.now(), \"\")\n        new_note.entry = self.entry + \" and \" + another.entry\n        return new_note
    entry1 = Note(datetime(2016, 12, 17), \"Remember to buy presents\")\nentry2 = Note(datetime(2016, 12, 23), \"Remember to get a tree\")\n\n# These notes can be added together with the + operator\n# This calls the  __add__ method in the Note class\nboth = entry1 + entry2\nprint(both)

    2020-09-09 14:13:02.163170: Remember to buy presents and Remember to get a tree

    A string representation of an object

    You have already implemented quite a few __str__ methods in your classes. As you know, the method returns a string representation of the object. Another quite similar method is __repr__ which returns a technical representation of the object. The method __repr__ is often implemented so that it returns the program code which can be executed to return an object with identical contents to the current object.

    The function repr returns this technical string representation of the object. The technical representation is used also whenever the __str__ method has not been defined for the object. The example below will make this clearer:

    class Person:\n    def __init__(self, name: str, age: int):\n        self.name = name\n        self.age = age\n        \n    def __repr__(self):\n        return f\"Person({repr(self.name)}, {self.age})\"
    person1 = Person("Anna", 25)\nperson2 = Person("Peter", 99)\nprint(person1)\nprint(person2)

    Person('Anna', 25)\nPerson('Peter', 99)

    Notice how the __repr__ method itself uses the repr function to retrieve the technical representation of the string. This is necessary to include the ' characters in the result.

    The following class has definitions for both __repr__ and __str__:

    class Person:\n    def __init__(self, name: str, age: int):\n        self.name = name\n        self.age = age\n        \n    def __repr__(self):\n        return f\"Person({repr(self.name)}, {self.age})\"\n\n    def __str__(self):\n        return f\"{self.name} ({self.age} years)\"
    Person = Person("Anna", 25)\nprint(Person)\nprint(repr(Person))

    Anna (25 years)\nPerson('Anna', 25)

    It is worth mentioning that with data structures, such as lists, Python always uses the __repr__ method for the string representation of the contents. This can sometimes look a bit baffling:

    persons = []\npersons.append(Person("Anna", 25))\npersons.append(Person("Peter", 99))\npersons.append(Person("Mary", 55))\nprint(persons)

    [Person('Anna', 25), Person('Peter', 99), Person('Mary', 55)]

    The exercise template contains an outline for a class named Money. This exercise asks you to implement some additional methods and to fix some small problems in the template.

    Fix the string representation

    The __str__ method in the class definition doesn't work quite right. Given the following two Money objects, the latter is printed out wrong:

    e1 = Money(4, 10)\ne2 = Money(2, 5)  # two euros and five cents\n\nprint(e1)\nprint(e2)

    4.10\n2.5

    Please fix the method so that it prints out

    4.10 eur\n2.05 eur

    Equal amounts

    Please define a new method named __eq__(self, another) which allows you to use the == comparison operator on Money objects. You can test your implementation with the following code:

    e1 = Money(4, 10)\ne2 = Money(2, 5)\ne3 = Money(4, 10)\n\nprint(e1)\nprint(e2)\nprint(e3)\nprint(e1 == e2)\nprint(e1 == e3)

    4.10 eur\n2.05 eur\n4.10 eur\nFalse\nTrue

    Other comparison operators

    Please implement the appropriate methods for the comparison operators <, > and !=.

    e1 = Money(4, 10)\ne2 = Money(2, 5)\n\nprint(e1 != e2)\nprint(e1 < e2)\nprint(e1 > e2)

    True\nFalse\nTrue

    Addition and subtraction

    Please implement the addition and subtraction operators + and - for Money objects. Both should return a new object of type Money. Neither the object itself nor the object passed as an argument should be changed as a result.

    NB: the value of a Money object cannot be negative. If an attempt to subtract would result in a negative result, the method should raise a ValueError exception.

    e1 = Money(4, 5)\ne2 = Money(2, 95)\n\ne3 = e1 + e2\ne4 = e1 - e2\n\nprint(e3)\nprint(e4)\n\ne5 = e2-e1
    7.00 eur\n1.10 eur\nTraceback (most recent call last):\nFile \"money.py\", line 416, in \ne5 = e2-e1\nFile \"money.py\", line 404, in sub\nraise ValueError(f\"a negative result is not allowed\")\nValueError: a negative result is not allowed

    The value must not be directly accessible

    The class still has a small integrity issue. The user can \"cheat\" by accessing the attributes directly and changing the value stored in the Money object:

    print(e1)\ne1.euros = 1000\nprint(e1)

    4.05 eur\n1000.05 eur

    Please encapsulate the implementation of the attributes defined in the class so that the cheat used above is not possible. The class should have no public attributes, and no getter or setter methods for the euros or the cents.

    In this exercise you are asked to implement the class SimpleDate which allows you to handle dates. For simplicity's sake we assume here that each month has 30 days.

    Because of this simplification you should not use the datetime module from the Python standard library. You will implement similar functionality by yourself instead.

    Comparisons

    Please implement the outline of the class, along with methods allowing for comparisons with the operators <, >, == and !=. You can use the following code to test your implementation:

    d1 = SimpleDate(4, 10, 2020)\nd2 = SimpleDate(28, 12, 1985)\nd3 = SimpleDate(28, 12, 1985)\n\nprint(d1)\nprint(d2)\nprint(d1 == d2)\nprint(d1 != d2)\nprint(d1 == d3)\nprint(d1 < d2)\nprint(d1 > d2)

    4.10.2020\n28.12.1985\nFalse\nTrue\nFalse\nFalse\nTrue

    Increment

    Please implement the addition operator + which allows you to add a given number of days to a SimpleDate object. The operator should return a new SimpleDate object. The original object should not be changed.

    d1 = SimpleDate(4, 10, 2020)\nd2 = SimpleDate(28, 12, 1985)\n\nd3 = d1 + 3\nd4 = d2 + 400\n\nprint(d1)\nprint(d2)\nprint(d3)\nprint(d4)

    4.10.2020\n28.12.1985\n7.10.2020\n8.2.1987

    Difference

    Please implement the subtraction operator - which allows you to find out the difference in days between two SimpleDate objects. As we assumed each month to have 30 days, a year within the confines of this exercise is 12*30 = 360 days long.

    You can use the following code to test your program:

    d1 = SimpleDate(4, 10, 2020)\nd2 = SimpleDate(2, 11, 2020)\nd3 = SimpleDate(28, 12, 1985)\n\nprint(d2-d1)\nprint(d1-d2)\nprint(d1-d3)

    28\n28\n12516

    Iterators

    We know that the for statement can be used to iterate through many different data structures, files and collections of items. A typical use case would be the following function:

    \ndef count_positives(my_list: list):\n    n = 0\n    for item in my_list:\n        if item > 0:\n            n += 1\n    return n\n

    The function goes through the items in the list one by one, and keeps track of how many of the items were positive.

    It is possible to make your own classes iterable, too. This is useful when the core purpose of the class involves storing a collection of items. The Bookshelf class from a previous example would be a good candidate, as it would make sense to use a for loop to go through the books on the shelf. The same applies to, say, a student register. Being able to iterate through the collection of students could be useful.

    To make a class iterable you must implement the iterator methods __iter__ and __next__. We will return to the specifics of these methods after the following example:

    class Book:\n    def __init__(self, name: str, author: str, page_count: int):\n        self.name = name\n        self.author = author\n        self.page_count = page_count\n\nclass Bookshelf:\n    def __init__(self):\n        self._books = []\n\n    def add_book(self, book: Book):\n        self._books.append(book)\n\n    # This is the iterator initialization method\n    # The iteration variable(s) should be initialized here\n    def __iter__(self):\n        self.n = 0\n        # the method returns a reference to the object itself as \n        # the iterator is implemented within the same class definition\n        return self\n\n    # This method returns the next item within the object\n    # If all items have been traversed, the StopIteration event is raised\n    def __next__(self):\n        if self.n < len(self._books):\n            # Select the current item from the list within the object\n            book = self._books[self.n]\n            # increase the counter (i.e. iteration variable) by one\n            self.n += 1\n            # return the current item\n            return book\n        else:\n            # All books have been traversed\n            raise StopIteration

    The method __iter__ initializes the iteration variable or variables. In this case it suffices to have a simple counter containing the index of the current item in the list. We also need the method __next__, which returns the next item in the iterator. In the example above the method returns the item at index n from the list within the Bookshelf object, and the iterator variable is also incremented.

    When all objects have been traversed, the __next__ method raises the StopIteration exception. The process is no different from raising any other exceptions, but this exception is automatically handled by Python and its purpose is to signal to the code calling the iterator (e.g. a for loop) that the iteration is now over.

    Our Bookshelf is now ready for iteration, for example with a for loop:

    if __name__ == \"__main__\":\n    b1 = Book(\"The Life of Python\", \"Montague Python\", 123)\n    b2 = Book(\"The Old Man and the C\", \"Ernest Hemingjavay\", 204)\n    b3 = Book(\"A Good Cup of Java\", \"Caffee Coder\", 997)\n\n    shelf = Bookshelf()\n    shelf.add_book(b1)\n    shelf.add_book(b2)\n    shelf.add_book(b3)\n\n    # Print the names of all the books\n    for book in shelf:\n        print(book.name)

    The Life of Python\nThe Old Man and the C\nA Good Cup of Java

    The exercise template contains the ShoppingList class from the exercise in part 8. Please adjust the class so that it is iterable and can thus be used as follows:

    shopping_list = ShoppingList()\nshopping_list.add(\"bananas\", 10)\nshopping_list.add(\"apples\", 5)\nshopping_list.add(\"pineapple\", 1)\n\nfor product in shopping_list:\n    print(f\"{product[0]}: {product[1]} units\")

    bananas: 10 units\napples: 5 units\npineapple: 1 units

    The __next__ method of your iterator should return tuples, where the first item is the name of the product and the second item is the amount.

    ","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-10/3-oo-programming-techniques.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 some of the different uses for the variable name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to overload operators in your own classes"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to create an iterable class"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A class can contain a method which returns an object of the very same class. For example, below we have the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Product"}]},{"type":"text","value":", whose method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"product_on_sale"}]},{"type":"text","value":" returns a new Product object with the same name as the original but with a price which is 25 % lower:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Product"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" price"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" price\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" (price "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price"},{"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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"product_on_sale"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 on_sale "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price "},{"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.75"}]},{"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":" on_sale"}]}]}]},{"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":"apple1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Apple\""}]},{"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":"2.99"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napple2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" apple1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"product_on_sale"},{"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":"apple1"},{"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":"apple2"},{"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":"Apple (price 2.99)\nApple (price 2.2425)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's review the purpose of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":": within a class definition it refers to the object itself. Typically it is used to refer to the object's own traits, its attributes and methods. The variable can be used to refer to the entire object as well, for example if the object itself needs to be returned to the client code. In the example below we've added the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cheaper"}]},{"type":"text","value":" to the class definition. It takes another Product as its argument and returns the cheaper of the two:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Product"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" price"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" price\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" (price "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price"},{"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","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"price"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price\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":"cheaper"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" Product"},{"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":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"price"},{"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":" self\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":" Product"}]}]}]},{"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":"apple "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Apple\""}]},{"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":"2.99"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\norange "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Orange\""}]},{"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":"3.95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbanana "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Banana\""}]},{"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":"5.25"}]},{"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":"orange"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"cheaper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"apple"},{"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":"orange"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"cheaper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"banana"},{"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":"Apple (2.99)\nOrange (3.95)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"While this works just fine, it is a very specialised case of comparing two objects. It would be better if we could use the Python comparison operators directly on these "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Product"}]},{"type":"text","value":" objects."}]},{"type":"element","tagName":"h2","properties":{"id":"overloading-operators","style":"position:relative;"},"children":[{"type":"text","value":"Overloading operators"},{"type":"element","tagName":"a","properties":{"href":"#overloading-operators","ariaLabel":"overloading operators 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":"Python contains some specially named built-in methods for working with the standard arithmetic and comparison operators. The technique is called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"operator overloading"}]},{"type":"text","value":". If you want to be able to use a certain operator on instances of self-defined classes, you can write a special method which returns the correct result of the operator. We have already used this technique with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method: Python knows to look for a method named like this when a string representation of an object is called for."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's start with the operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" which tells us if the first operand is greater than the second. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Product"}]},{"type":"text","value":" class definition below contains the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__gt__"}]},{"type":"text","value":", which is short for "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"g"}]},{"type":"text","value":"reater "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"t"}]},{"type":"text","value":"han. This specially named method should return the correct result of the comparison. Specifically, it should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" if and only if the current object is greater than the object passed as an argument. The criteria used can be determined by the programmer. By "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"current object"}]},{"type":"text","value":" we mean the object on which the method is called with the dot "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" notation."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Product"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" price"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" price\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" (price "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price"},{"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","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"price"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price\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":"__gt__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" another_product"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"price "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" another_product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"price"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the implementation above, the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__gt__"}]},{"type":"text","value":" returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" if the price of the current product is greater than the price of the product passed as an argument. Otherwise the method returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now the comparison operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" is available for use with objects of type Product:"}]},{"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":"orange "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Orange\""}]},{"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":"2.90"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napple "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Apple\""}]},{"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":"3.95"}]},{"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":"if"}]},{"type":"text","value":" orange "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" apple"},{"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":"\"Orange is greater\""}]},{"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":"else"}]},{"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":"\"Apple is greater\""}]},{"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":"Apple is greater"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As stated above, it is up to the programmer to determine the criteria by which it is decided which is greater and which is lesser. We could, for instance, decide that the order should not be based on price, but be alphabetical by name instead. This would mean that "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"orange"}]},{"type":"text","value":" would now be \"greater than\" "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"apple"}]},{"type":"text","value":", as \"orange\" comes alphabetically last."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Product"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" price"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" price\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" (price "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price"},{"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","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"price"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__price\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"name"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name\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":"__gt__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" another_product"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" another_product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"}]}]}]},{"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":"Orange "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Orange\""}]},{"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":"4.90"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nApple "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Apple\""}]},{"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":"3.95"}]},{"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":"if"}]},{"type":"text","value":" Orange "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" Apple"},{"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":"\"Orange is greater\""}]},{"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":"else"}]},{"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":"\"Apple is greater\""}]},{"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":"Orange is greater"}]}]},{"type":"element","tagName":"h2","properties":{"id":"more-operators","style":"position:relative;"},"children":[{"type":"text","value":"More operators"},{"type":"element","tagName":"a","properties":{"href":"#more-operators","ariaLabel":"more operators 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":"Here we have a table containing the standard comparison operators, along with the methods which need to be implemented if we want to make them available for use on our objects:"}]},{"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\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":"Operator"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Traditional meaning"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Name of method"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Less than"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__lt__(self, another)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Greater than"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__gt__(self, another)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Equal to"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__eq__(self, another)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!="}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Not equal to"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__ne__(self, another)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<="}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Less than or equal to"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__le__(self, another)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">="}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Greater than or equal to"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__ge__(self, another)"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can also implement some other operators, including the following arithmetic operators:"}]},{"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\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":"Operator"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Traditional meaning"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Name of method"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Addition"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__add__(self, another)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Subtraction"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__sub__(self, another)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Multiplication"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__mul__(self, another)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"/"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Division (floating point result)"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__truediv__(self, another)"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"//"}]}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"Division (integer result)"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__floordiv__(self, another)"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"More operators and method names are easily found online. Remember also the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dir"}]},{"type":"text","value":" command for listing the methods available for use on a given object."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is very rarely necessary to implement all the arithmetic and comparison operators in your own classes. For example, division is an operation which rarely makes sense outside numerical objects. What would be the result of dividing a Student object by three, or by another Student object? Nevertheless, some of these operators are often very useful with also your own classes. The selection of methods to implement depends on what makes sense, knowing the properties of your objects."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at a class which models a single note. If we implement the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__add__"}]},{"type":"text","value":" method within our class definition, the addition operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" becomes available on our Note objects:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Note"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" entry_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" entry"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"entry_date "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" entry_date\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"entry "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" entry\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"entry_date"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"entry"},{"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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__add__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" another"},{"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":"# The date of the new note is the current time"}]},{"type":"text","value":"\n new_note "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Note"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"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":" "},{"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 new_note"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"entry "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"entry "},{"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":"\" and \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" another"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"entry\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" new_note"}]}]}]},{"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":"entry1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Note"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2016"}]},{"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":"12"}]},{"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":"17"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Remember to buy presents\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nentry2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Note"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2016"}]},{"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":"12"}]},{"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":"23"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Remember to get a tree\""}]},{"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":"# These notes can be added together with the + operator"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This calls the __add__ method in the Note class"}]},{"type":"text","value":"\nboth "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" entry1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" entry2\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":"both"},{"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":"2020-09-09 14:13:02.163170: Remember to buy presents and Remember to get a tree"}]}]},{"type":"element","tagName":"h2","properties":{"id":"a-string-representation-of-an-object","style":"position:relative;"},"children":[{"type":"text","value":"A string representation of an object"},{"type":"element","tagName":"a","properties":{"href":"#a-string-representation-of-an-object","ariaLabel":"a string representation of an object 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":"You have already implemented quite a few "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" methods in your classes. As you know, the method returns a string representation of the object. Another quite similar method is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":" which returns a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"technical"}]},{"type":"text","value":" representation of the object. The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":" is often implemented so that it returns the program code which can be executed to return an object with "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"identical contents"}]},{"type":"text","value":" to the current object."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"repr"}]},{"type":"text","value":" returns this technical string representation of the object. The technical representation is used also whenever the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method has not been defined for the object. The example below will make this clearer:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" age"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" age\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":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Person("}]},{"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":"repr"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"age"},{"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":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python3"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python3"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python3"]},"children":[{"type":"text","value":"person1 = Person(\"Anna\", 25)\nperson2 = Person(\"Peter\", 99)\nprint(person1)\nprint(person2)"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Person('Anna', 25)\nPerson('Peter', 99)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice how the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":" method itself uses the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"repr"}]},{"type":"text","value":" function to retrieve the technical representation of the string. This is necessary to include the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"'"}]},{"type":"text","value":" characters in the result."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following class has definitions for both "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" age"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" age\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":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Person("}]},{"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":"repr"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"age"},{"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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"age"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" years)\""}]}]}]}]}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python3"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python3"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python3"]},"children":[{"type":"text","value":"Person = Person(\"Anna\", 25)\nprint(Person)\nprint(repr(Person))"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Anna (25 years)\nPerson('Anna', 25)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is worth mentioning that with data structures, such as lists, Python always uses the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":" method for the string representation of the contents. This can sometimes look a bit baffling:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"python3"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-python3"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-python3"]},"children":[{"type":"text","value":"persons = []\npersons.append(Person(\"Anna\", 25))\npersons.append(Person(\"Peter\", 99))\npersons.append(Person(\"Mary\", 55))\nprint(persons)"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[Person('Anna', 25), Person('Peter', 99), Person('Mary', 55)]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Money","tmcname":"part10-07_money"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains an outline for a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Money"}]},{"type":"text","value":". This exercise asks you to implement some additional methods and to fix some small problems in the template."}]},{"type":"element","tagName":"h2","properties":{"id":"fix-the-string-representation","style":"position:relative;"},"children":[{"type":"text","value":"Fix the string representation"},{"type":"element","tagName":"a","properties":{"href":"#fix-the-string-representation","ariaLabel":"fix the string representation 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 "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method in the class definition doesn't work quite right. Given the following two Money objects, the latter is printed out wrong:"}]},{"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":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Money"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Money"},{"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":" "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# two euros and five cents"}]},{"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":"e1"},{"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":"e2"},{"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.10\n2.5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please fix the method so that it prints out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4.10 eur\n2.05 eur"}]}]},{"type":"element","tagName":"h2","properties":{"id":"equal-amounts","style":"position:relative;"},"children":[{"type":"text","value":"Equal amounts"},{"type":"element","tagName":"a","properties":{"href":"#equal-amounts","ariaLabel":"equal amounts 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":"Please define a new method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__eq__(self, another)"}]},{"type":"text","value":" which allows you to use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" comparison operator on Money objects. You can test your implementation with the following code:"}]},{"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":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Money"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Money"},{"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":" "},{"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":"\ne3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Money"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"e1"},{"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":"e2"},{"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":"e3"},{"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":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" e2"},{"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":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" e3"},{"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.10 eur\n2.05 eur\n4.10 eur\nFalse\nTrue"}]}]},{"type":"element","tagName":"h2","properties":{"id":"other-comparison-operators","style":"position:relative;"},"children":[{"type":"text","value":"Other comparison operators"},{"type":"element","tagName":"a","properties":{"href":"#other-comparison-operators","ariaLabel":"other comparison operators 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":"Please implement the appropriate methods for the comparison operators "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":"."}]},{"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":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Money"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Money"},{"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":" "},{"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\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":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":" e2"},{"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":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" e2"},{"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":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" e2"},{"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\nTrue"}]}]},{"type":"element","tagName":"h2","properties":{"id":"addition-and-subtraction","style":"position:relative;"},"children":[{"type":"text","value":"Addition and subtraction"},{"type":"element","tagName":"a","properties":{"href":"#addition-and-subtraction","ariaLabel":"addition and subtraction 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":"Please implement the addition and subtraction operators "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" for Money objects. Both should return a new object of type Money. Neither the object itself nor the object passed as an argument should be changed as a result."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the value of a Money object cannot be negative. If an attempt to subtract would result in a negative result, the method should raise a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" exception."}]},{"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":"e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Money"},{"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":"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":"\ne2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Money"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"95"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ne3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" e2\ne4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" e1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" e2\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":"e3"},{"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":"e4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ne5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" e2"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"e1"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"7.00 eur\n1.10 eur\nTraceback (most recent call last):\nFile \"money.py\", line 416, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\ne5 = e2-e1\nFile \"money.py\", line 404, in "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sub"}]},{"type":"text","value":"\nraise ValueError(f\"a negative result is not allowed\")\nValueError: a negative result is not allowed"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-value-must-not-be-directly-accessible","style":"position:relative;"},"children":[{"type":"text","value":"The value must not be directly accessible"},{"type":"element","tagName":"a","properties":{"href":"#the-value-must-not-be-directly-accessible","ariaLabel":"the value must not be directly accessible 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 class still has a small integrity issue. The user can \"cheat\" by accessing the attributes directly and changing the value stored in the Money object:"}]},{"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":"text","value":"e1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ne1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"euros "},{"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":"1000"}]},{"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":"e1"},{"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.05 eur\n1000.05 eur"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please "},{"type":"element","tagName":"a","properties":{"href":"/part-9/3-encapsulation#encapsulation"},"children":[{"type":"text","value":"encapsulate"}]},{"type":"text","value":" the implementation of the attributes defined in the class so that the cheat used above is not possible. The class should have no public attributes, and no getter or setter methods for the euros or the cents."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Simple date","tmcname":"part10-08_simple_date"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you are asked to implement the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SimpleDate"}]},{"type":"text","value":" which allows you to handle dates. For simplicity's sake we assume here that "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"each month has 30 days"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Because of this simplification you should not use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":" module from the Python standard library. You will implement similar functionality by yourself instead."}]},{"type":"element","tagName":"h2","properties":{"id":"comparisons","style":"position:relative;"},"children":[{"type":"text","value":"Comparisons"},{"type":"element","tagName":"a","properties":{"href":"#comparisons","ariaLabel":"comparisons 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":"Please implement the outline of the class, along with methods allowing for comparisons with the operators "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":". You can use the following code to test your implementation:"}]},{"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":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpleDate"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpleDate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"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":"12"}]},{"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":"1985"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpleDate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"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":"12"}]},{"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":"1985"}]},{"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":"d1"},{"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":"d2"},{"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":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" d2"},{"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":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":" d2"},{"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":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" d3"},{"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":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" d2"},{"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":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" d2"},{"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.10.2020\n28.12.1985\nFalse\nTrue\nFalse\nFalse\nTrue"}]}]},{"type":"element","tagName":"h2","properties":{"id":"increment","style":"position:relative;"},"children":[{"type":"text","value":"Increment"},{"type":"element","tagName":"a","properties":{"href":"#increment","ariaLabel":"increment 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":"Please implement the addition operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" which allows you to add a given number of days to a SimpleDate object. The operator should return a new SimpleDate object. The original object should not 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":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpleDate"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpleDate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"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":"12"}]},{"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":"1985"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nd3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" d1 "},{"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":"\nd4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" d2 "},{"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":"400"}]},{"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":"d1"},{"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":"d2"},{"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":"d3"},{"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":"d4"},{"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.10.2020\n28.12.1985\n7.10.2020\n8.2.1987"}]}]},{"type":"element","tagName":"h2","properties":{"id":"difference","style":"position:relative;"},"children":[{"type":"text","value":"Difference"},{"type":"element","tagName":"a","properties":{"href":"#difference","ariaLabel":"difference 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":"Please implement the subtraction operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" which allows you to find out the difference in days between two SimpleDate objects. As we assumed each month to have 30 days, a year within the confines of this exercise is 12*30 = 360 days long."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can use the following code to test your program:"}]},{"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":"d1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpleDate"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpleDate"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"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":"2020"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SimpleDate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"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":"12"}]},{"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":"1985"}]},{"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":"d2"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"d1"},{"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":"d1"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"d2"},{"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":"d1"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"d3"},{"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":"28\n28\n12516"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"iterators","style":"position:relative;"},"children":[{"type":"text","value":"Iterators"},{"type":"element","tagName":"a","properties":{"href":"#iterators","ariaLabel":"iterators 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 that the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" statement can be used to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"iterate"}]},{"type":"text","value":" through many different data structures, files and collections of items. A typical use case would be the following 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":"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":"count_positives"}]},{"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":" "},{"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 n "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"if"}]},{"type":"text","value":" item "},{"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 n "},{"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":"return"}]},{"type":"text","value":" n\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function goes through the items in the list one by one, and keeps track of how many of the items were positive."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is possible to make your own classes iterable, too. This is useful when the core purpose of the class involves storing a collection of items. The Bookshelf class from a previous example would be a good candidate, as it would make sense to use a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop to go through the books on the shelf. The same applies to, say, a student register. Being able to iterate through the collection of students could be useful."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To make a class iterable you must implement the iterator methods "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__iter__"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__next__"}]},{"type":"text","value":". We will return to the specifics of these methods after the following example:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Book"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" author"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" page_count"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"author "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" author\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"page_count "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" page_count\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bookshelf"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_books "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_book"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Book"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_books"},{"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":"book"},{"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":"# This is the iterator initialization method"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# The iteration variable(s) should be initialized 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":"__iter__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the method returns a reference to the object itself as "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the iterator is implemented within the same class definition"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This method returns the next item within the object"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If all items have been traversed, the StopIteration event is raised"}]},{"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":"__next__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_books"},{"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":"# Select the current item from the list within the object"}]},{"type":"text","value":"\n book "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_books"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n"},{"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":"# increase the counter (i.e. iteration variable) by one"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"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","comment"]},"children":[{"type":"text","value":"# return the current item"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" book\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"# All books have been traversed"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" StopIteration"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__iter__"}]},{"type":"text","value":" initializes the iteration variable or variables. In this case it suffices to have a simple counter containing the index of the current item in the list. We also need the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__next__"}]},{"type":"text","value":", which returns the next item in the iterator. In the example above the method returns the item at index "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" from the list within the Bookshelf object, and the iterator variable is also incremented."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When all objects have been traversed, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__next__"}]},{"type":"text","value":" method raises the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"StopIteration"}]},{"type":"text","value":" exception. The process is no different from raising any other exceptions, but this exception is automatically handled by Python and its purpose is to signal to the code calling the iterator (e.g. a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop) that the iteration is now over."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Our Bookshelf is now ready for iteration, for example with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop:"}]},{"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":"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 b1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"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 Life of 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":"\"Montague Python\""}]},{"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":"123"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"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 Old Man and the C\""}]},{"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":"\"Ernest Hemingjavay\""}]},{"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":"204"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"A Good Cup of Java\""}]},{"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":"\"Caffee Coder\""}]},{"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":"997"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n shelf "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bookshelf"},{"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 shelf"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n shelf"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n shelf"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b3"},{"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":"# Print the names of all the books"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" book "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" shelf"},{"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":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"The Life of Python\nThe Old Man and the C\nA Good Cup of Java"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"An iterable shopping list","tmcname":"part10-09_iterable_shopping_list"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ShoppingList"}]},{"type":"text","value":" class from the "},{"type":"element","tagName":"a","properties":{"href":"/part-8/2-classes-and-objects#programming-exercise-shopping-list"},"children":[{"type":"text","value":"exercise in part 8"}]},{"type":"text","value":". Please adjust the class so that it is iterable and can thus be used as follows:"}]},{"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":"shopping_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ShoppingList"},{"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":"\nshopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"bananas\""}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nshopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apples\""}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nshopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"pineapple\""}]},{"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":"1"}]},{"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":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" shopping_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","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":"product"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"product"},{"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":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" units\""}]}]},{"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":"bananas: 10 units\napples: 5 units\npineapple: 1 units"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__next__"}]},{"type":"text","value":" method of your iterator should return tuples, where the first item is the name of the product and the second item is the amount."}]}]}]},"html":"

    After this section

    A class can contain a method which returns an object of the very same class. For example, below we have the class Product, whose method product_on_sale returns a new Product object with the same name as the original but with a price which is 25 % lower:

    class Product:\n    def __init__(self, name: str, price: float):\n        self.__name = name\n        self.__price = price\n\n    def __str__(self):\n        return f\"{self.__name} (price {self.__price})\"\n\n    def product_on_sale(self):\n        on_sale = Product(self.__name, self.__price * 0.75)\n        return on_sale
    apple1 = Product(\"Apple\", 2.99)\napple2 = apple1.product_on_sale()\nprint(apple1)\nprint(apple2)

    Apple (price 2.99)\nApple (price 2.2425)

    Let's review the purpose of the variable self: within a class definition it refers to the object itself. Typically it is used to refer to the object's own traits, its attributes and methods. The variable can be used to refer to the entire object as well, for example if the object itself needs to be returned to the client code. In the example below we've added the method cheaper to the class definition. It takes another Product as its argument and returns the cheaper of the two:

    class Product:\n    def __init__(self, name: str, price: float):\n        self.__name = name\n        self.__price = price\n\n    def __str__(self):\n        return f\"{self.__name} (price {self.__price})\"\n\n    @property\n    def price(self):\n        return self.__price\n\n    def cheaper(self, Product):\n        if self.__price < Product.price:\n            return self\n        else:\n            return Product
    apple = Product(\"Apple\", 2.99)\norange = Product(\"Orange\", 3.95)\nbanana = Product(\"Banana\", 5.25)\n\nprint(orange.cheaper(apple))\nprint(orange.cheaper(banana))

    Apple (2.99)\nOrange (3.95)

    While this works just fine, it is a very specialised case of comparing two objects. It would be better if we could use the Python comparison operators directly on these Product objects.

    Overloading operators

    Python contains some specially named built-in methods for working with the standard arithmetic and comparison operators. The technique is called operator overloading. If you want to be able to use a certain operator on instances of self-defined classes, you can write a special method which returns the correct result of the operator. We have already used this technique with the __str__ method: Python knows to look for a method named like this when a string representation of an object is called for.

    Let's start with the operator > which tells us if the first operand is greater than the second. The Product class definition below contains the method __gt__, which is short for greater than. This specially named method should return the correct result of the comparison. Specifically, it should return True if and only if the current object is greater than the object passed as an argument. The criteria used can be determined by the programmer. By current object we mean the object on which the method is called with the dot . notation.

    class Product:\n    def __init__(self, name: str, price: float):\n        self.__name = name\n        self.__price = price\n\n    def __str__(self):\n        return f\"{self.__name} (price {self.__price})\"\n\n    @property\n    def price(self):\n        return self.__price\n\n    def __gt__(self, another_product):\n        return self.price > another_product.price

    In the implementation above, the method __gt__ returns True if the price of the current product is greater than the price of the product passed as an argument. Otherwise the method returns False.

    Now the comparison operator > is available for use with objects of type Product:

    orange = Product(\"Orange\", 2.90)\napple = Product(\"Apple\", 3.95)\n\nif orange > apple:\n    print(\"Orange is greater\")\nelse:\n    print(\"Apple is greater\")

    Apple is greater

    As stated above, it is up to the programmer to determine the criteria by which it is decided which is greater and which is lesser. We could, for instance, decide that the order should not be based on price, but be alphabetical by name instead. This would mean that orange would now be \"greater than\" apple, as \"orange\" comes alphabetically last.

    class Product:\n    def __init__(self, name: str, price: float):\n        self.__name = name\n        self.__price = price\n\n    def __str__(self):\n        return f\"{self.__name} (price {self.__price})\"\n\n    @property\n    def price(self):\n        return self.__price\n\n    @property\n    def name(self):\n        return self.__name\n\n    def __gt__(self, another_product):\n        return self.name > another_product.name
    Orange = Product(\"Orange\", 4.90)\nApple = Product(\"Apple\", 3.95)\n\nif Orange > Apple:\n    print(\"Orange is greater\")\nelse:\n    print(\"Apple is greater\")

    Orange is greater

    More operators

    Here we have a table containing the standard comparison operators, along with the methods which need to be implemented if we want to make them available for use on our objects:

    \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\n\n\n\n\n\n\n
    OperatorTraditional meaningName of method
    <Less than__lt__(self, another)
    >Greater than__gt__(self, another)
    ==Equal to__eq__(self, another)
    !=Not equal to__ne__(self, another)
    <=Less than or equal to__le__(self, another)
    >=Greater than or equal to__ge__(self, another)

    You can also implement some other operators, including the following arithmetic operators:

    \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\n\n
    OperatorTraditional meaningName of method
    +Addition__add__(self, another)
    -Subtraction__sub__(self, another)
    *Multiplication__mul__(self, another)
    /Division (floating point result)__truediv__(self, another)
    //Division (integer result)__floordiv__(self, another)

    More operators and method names are easily found online. Remember also the dir command for listing the methods available for use on a given object.

    It is very rarely necessary to implement all the arithmetic and comparison operators in your own classes. For example, division is an operation which rarely makes sense outside numerical objects. What would be the result of dividing a Student object by three, or by another Student object? Nevertheless, some of these operators are often very useful with also your own classes. The selection of methods to implement depends on what makes sense, knowing the properties of your objects.

    Let's have a look at a class which models a single note. If we implement the __add__ method within our class definition, the addition operator + becomes available on our Note objects:

    from datetime import datetime\n\nclass Note:\n    def __init__(self, entry_date: datetime, entry: str):\n        self.entry_date = entry_date\n        self.entry = entry\n\n    def __str__(self):\n        return f\"{self.entry_date}: {self.entry}\"\n\n    def __add__(self, another):\n        # The date of the new note is the current time\n        new_note = Note(datetime.now(), \"\")\n        new_note.entry = self.entry + \" and \" + another.entry\n        return new_note
    entry1 = Note(datetime(2016, 12, 17), \"Remember to buy presents\")\nentry2 = Note(datetime(2016, 12, 23), \"Remember to get a tree\")\n\n# These notes can be added together with the + operator\n# This calls the  __add__ method in the Note class\nboth = entry1 + entry2\nprint(both)

    2020-09-09 14:13:02.163170: Remember to buy presents and Remember to get a tree

    A string representation of an object

    You have already implemented quite a few __str__ methods in your classes. As you know, the method returns a string representation of the object. Another quite similar method is __repr__ which returns a technical representation of the object. The method __repr__ is often implemented so that it returns the program code which can be executed to return an object with identical contents to the current object.

    The function repr returns this technical string representation of the object. The technical representation is used also whenever the __str__ method has not been defined for the object. The example below will make this clearer:

    class Person:\n    def __init__(self, name: str, age: int):\n        self.name = name\n        self.age = age\n        \n    def __repr__(self):\n        return f\"Person({repr(self.name)}, {self.age})\"
    person1 = Person("Anna", 25)\nperson2 = Person("Peter", 99)\nprint(person1)\nprint(person2)

    Person('Anna', 25)\nPerson('Peter', 99)

    Notice how the __repr__ method itself uses the repr function to retrieve the technical representation of the string. This is necessary to include the ' characters in the result.

    The following class has definitions for both __repr__ and __str__:

    class Person:\n    def __init__(self, name: str, age: int):\n        self.name = name\n        self.age = age\n        \n    def __repr__(self):\n        return f\"Person({repr(self.name)}, {self.age})\"\n\n    def __str__(self):\n        return f\"{self.name} ({self.age} years)\"
    Person = Person("Anna", 25)\nprint(Person)\nprint(repr(Person))

    Anna (25 years)\nPerson('Anna', 25)

    It is worth mentioning that with data structures, such as lists, Python always uses the __repr__ method for the string representation of the contents. This can sometimes look a bit baffling:

    persons = []\npersons.append(Person("Anna", 25))\npersons.append(Person("Peter", 99))\npersons.append(Person("Mary", 55))\nprint(persons)

    [Person('Anna', 25), Person('Peter', 99), Person('Mary', 55)]

    The exercise template contains an outline for a class named Money. This exercise asks you to implement some additional methods and to fix some small problems in the template.

    Fix the string representation

    The __str__ method in the class definition doesn't work quite right. Given the following two Money objects, the latter is printed out wrong:

    e1 = Money(4, 10)\ne2 = Money(2, 5)  # two euros and five cents\n\nprint(e1)\nprint(e2)

    4.10\n2.5

    Please fix the method so that it prints out

    4.10 eur\n2.05 eur

    Equal amounts

    Please define a new method named __eq__(self, another) which allows you to use the == comparison operator on Money objects. You can test your implementation with the following code:

    e1 = Money(4, 10)\ne2 = Money(2, 5)\ne3 = Money(4, 10)\n\nprint(e1)\nprint(e2)\nprint(e3)\nprint(e1 == e2)\nprint(e1 == e3)

    4.10 eur\n2.05 eur\n4.10 eur\nFalse\nTrue

    Other comparison operators

    Please implement the appropriate methods for the comparison operators <, > and !=.

    e1 = Money(4, 10)\ne2 = Money(2, 5)\n\nprint(e1 != e2)\nprint(e1 < e2)\nprint(e1 > e2)

    True\nFalse\nTrue

    Addition and subtraction

    Please implement the addition and subtraction operators + and - for Money objects. Both should return a new object of type Money. Neither the object itself nor the object passed as an argument should be changed as a result.

    NB: the value of a Money object cannot be negative. If an attempt to subtract would result in a negative result, the method should raise a ValueError exception.

    e1 = Money(4, 5)\ne2 = Money(2, 95)\n\ne3 = e1 + e2\ne4 = e1 - e2\n\nprint(e3)\nprint(e4)\n\ne5 = e2-e1
    7.00 eur\n1.10 eur\nTraceback (most recent call last):\nFile \"money.py\", line 416, in \ne5 = e2-e1\nFile \"money.py\", line 404, in sub\nraise ValueError(f\"a negative result is not allowed\")\nValueError: a negative result is not allowed

    The value must not be directly accessible

    The class still has a small integrity issue. The user can \"cheat\" by accessing the attributes directly and changing the value stored in the Money object:

    print(e1)\ne1.euros = 1000\nprint(e1)

    4.05 eur\n1000.05 eur

    Please encapsulate the implementation of the attributes defined in the class so that the cheat used above is not possible. The class should have no public attributes, and no getter or setter methods for the euros or the cents.

    In this exercise you are asked to implement the class SimpleDate which allows you to handle dates. For simplicity's sake we assume here that each month has 30 days.

    Because of this simplification you should not use the datetime module from the Python standard library. You will implement similar functionality by yourself instead.

    Comparisons

    Please implement the outline of the class, along with methods allowing for comparisons with the operators <, >, == and !=. You can use the following code to test your implementation:

    d1 = SimpleDate(4, 10, 2020)\nd2 = SimpleDate(28, 12, 1985)\nd3 = SimpleDate(28, 12, 1985)\n\nprint(d1)\nprint(d2)\nprint(d1 == d2)\nprint(d1 != d2)\nprint(d1 == d3)\nprint(d1 < d2)\nprint(d1 > d2)

    4.10.2020\n28.12.1985\nFalse\nTrue\nFalse\nFalse\nTrue

    Increment

    Please implement the addition operator + which allows you to add a given number of days to a SimpleDate object. The operator should return a new SimpleDate object. The original object should not be changed.

    d1 = SimpleDate(4, 10, 2020)\nd2 = SimpleDate(28, 12, 1985)\n\nd3 = d1 + 3\nd4 = d2 + 400\n\nprint(d1)\nprint(d2)\nprint(d3)\nprint(d4)

    4.10.2020\n28.12.1985\n7.10.2020\n8.2.1987

    Difference

    Please implement the subtraction operator - which allows you to find out the difference in days between two SimpleDate objects. As we assumed each month to have 30 days, a year within the confines of this exercise is 12*30 = 360 days long.

    You can use the following code to test your program:

    d1 = SimpleDate(4, 10, 2020)\nd2 = SimpleDate(2, 11, 2020)\nd3 = SimpleDate(28, 12, 1985)\n\nprint(d2-d1)\nprint(d1-d2)\nprint(d1-d3)

    28\n28\n12516

    Iterators

    We know that the for statement can be used to iterate through many different data structures, files and collections of items. A typical use case would be the following function:

    \ndef count_positives(my_list: list):\n    n = 0\n    for item in my_list:\n        if item > 0:\n            n += 1\n    return n\n

    The function goes through the items in the list one by one, and keeps track of how many of the items were positive.

    It is possible to make your own classes iterable, too. This is useful when the core purpose of the class involves storing a collection of items. The Bookshelf class from a previous example would be a good candidate, as it would make sense to use a for loop to go through the books on the shelf. The same applies to, say, a student register. Being able to iterate through the collection of students could be useful.

    To make a class iterable you must implement the iterator methods __iter__ and __next__. We will return to the specifics of these methods after the following example:

    class Book:\n    def __init__(self, name: str, author: str, page_count: int):\n        self.name = name\n        self.author = author\n        self.page_count = page_count\n\nclass Bookshelf:\n    def __init__(self):\n        self._books = []\n\n    def add_book(self, book: Book):\n        self._books.append(book)\n\n    # This is the iterator initialization method\n    # The iteration variable(s) should be initialized here\n    def __iter__(self):\n        self.n = 0\n        # the method returns a reference to the object itself as \n        # the iterator is implemented within the same class definition\n        return self\n\n    # This method returns the next item within the object\n    # If all items have been traversed, the StopIteration event is raised\n    def __next__(self):\n        if self.n < len(self._books):\n            # Select the current item from the list within the object\n            book = self._books[self.n]\n            # increase the counter (i.e. iteration variable) by one\n            self.n += 1\n            # return the current item\n            return book\n        else:\n            # All books have been traversed\n            raise StopIteration

    The method __iter__ initializes the iteration variable or variables. In this case it suffices to have a simple counter containing the index of the current item in the list. We also need the method __next__, which returns the next item in the iterator. In the example above the method returns the item at index n from the list within the Bookshelf object, and the iterator variable is also incremented.

    When all objects have been traversed, the __next__ method raises the StopIteration exception. The process is no different from raising any other exceptions, but this exception is automatically handled by Python and its purpose is to signal to the code calling the iterator (e.g. a for loop) that the iteration is now over.

    Our Bookshelf is now ready for iteration, for example with a for loop:

    if __name__ == \"__main__\":\n    b1 = Book(\"The Life of Python\", \"Montague Python\", 123)\n    b2 = Book(\"The Old Man and the C\", \"Ernest Hemingjavay\", 204)\n    b3 = Book(\"A Good Cup of Java\", \"Caffee Coder\", 997)\n\n    shelf = Bookshelf()\n    shelf.add_book(b1)\n    shelf.add_book(b2)\n    shelf.add_book(b3)\n\n    # Print the names of all the books\n    for book in shelf:\n        print(book.name)

    The Life of Python\nThe Old Man and the C\nA Good Cup of Java

    The exercise template contains the ShoppingList class from the exercise in part 8. Please adjust the class so that it is iterable and can thus be used as follows:

    shopping_list = ShoppingList()\nshopping_list.add(\"bananas\", 10)\nshopping_list.add(\"apples\", 5)\nshopping_list.add(\"pineapple\", 1)\n\nfor product in shopping_list:\n    print(f\"{product[0]}: {product[1]} units\")

    bananas: 10 units\napples: 5 units\npineapple: 1 units

    The __next__ method of your iterator should return tuples, where the first item is the name of the product and the second item is the amount.

    ","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-10/3-oo-programming-techniques.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-10/4-application-development/page-data.json b/page-data/part-10/4-application-development/page-data.json index 72d45a12a..ae24b3917 100644 --- a/page-data/part-10/4-application-development/page-data.json +++ b/page-data/part-10/4-application-development/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-10/4-application-development", - "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 some basic principles of application development"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be comfortable with differentiating between the different parts of an application (user interface, program logic and file handling)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will have practiced writing your own somewhat larger application"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Thus far in this course material we have covered a large number of Python features."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The Introduction to Programming course introduced control structures, such as while and for, functions, and basic data structures, such as lists, tuples and dictionaries. In principle, those tools are all that is needed to express anything a programmer may wish to express with Python."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"On this Advanced Course in Programming, beginning in part 8 of the material, you have become familiar with classes and objects. Let's take a moment to consider when and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"why"}]},{"type":"text","value":" they are necessary, if those basic tools from parts 1 to 7 should be enough."}]},{"type":"element","tagName":"h2","properties":{"id":"managing-complexity","style":"position:relative;"},"children":[{"type":"text","value":"Managing complexity"},{"type":"element","tagName":"a","properties":{"href":"#managing-complexity","ariaLabel":"managing complexity 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":"Objects and classes are by no means necessary in every programming context. For example, if you are programming a smallish script for one-time use, objects are usually surplus to requirement. However, when you are programming something larger and more complicated, objects become very useful."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When programs grow in complexity, the amount of details quickly becomes unmanageable, unless the program is organised in some systematic way. Even some of the more complicated exercises on this course so far would have benefited from the examples set in this part of the material."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fo decades the concept of "},{"type":"element","tagName":"a","properties":{"href":"https://en.wikipedia.org/wiki/Separation_of_concerns","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"separation of concerns"}]},{"type":"text","value":" has been one of the central principles in programming, and the larger field of computer science. Quoting from Wikipedia:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Separation of concerns is a design principle for separating a computer program into distinct sections such that each section addresses a separate concern. A concern is a set of information that affects the code of a computer program."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Separating the program into sections, so that each has its own concern to handle, helps in managing the inevitable complexity of a computer program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Functions are one way of organising a program into distinct, manageable wholes. Instead of writing a single script, the idea is to formulate small, separately verifiable functions which each solve some part of the larger problem."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Another common approach to managing larger programs is objects, through object oriented programming principles. There are benefits and drawbacks to both approaches, and each programmer has their own favourite. As we have seen so far, objects and classes allow us to collect all the data "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" the code processing that data within a single unit, in the attributes and methods of an object. Furthermore, objects provide a way of encapsulating the data they control, so that other parts of the program do not have to worry about the internal details of an object."}]},{"type":"element","tagName":"h2","properties":{"id":"a-worked-example-phone-book","style":"position:relative;"},"children":[{"type":"text","value":"A worked example: phone book"},{"type":"element","tagName":"a","properties":{"href":"#a-worked-example-phone-book","ariaLabel":"a worked example phone book 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":"How should a program be divided into classes and objects? This is by no means an easy question with a single acceptable answer, so we will proceed with an example. In part five you completed "},{"type":"element","tagName":"a","properties":{"href":"/part-5/3-dictionary#programming-exercise-phone-book-version-2"},"children":[{"type":"text","value":"a phone book application"}]},{"type":"text","value":", and now we will implement something similar using object oriented programming principles."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Following the separation of concerns principle, a program should be divided into sections which each have their own cause to take care of. In object oriented programming this translates to the "},{"type":"element","tagName":"a","properties":{"href":"https://en.wikipedia.org/wiki/Single-responsibility_principle","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"single-responsibility principle"}]},{"type":"text","value":". Without going into the finer details, its fundamental purpose is clear from the name: "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"a single class and objects created based on it should have a single responsibility in the program"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Object oriented programming is often used as a way of modelling real world objects and phenomena. A single object in the real world is modelled with a single class in program code. In the case of a phone book such objects might be"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a person"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a name"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a phone number"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A name and a phone number may be thought of as just bits of data which do not merit their own classes, but a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"person"}]},{"type":"text","value":" is a distinct physical entity in the real world, and in the programming world it could work as a class. A Person object would be responsible for tying together a name and the phone numbers attached to it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"phone book"}]},{"type":"text","value":" itself could be a good candidate for a class. Its responsibility would be to manage different person objects and the data they contain."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now we have outlined the core of our application: "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"phone book"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"person"}]},{"type":"text","value":" constitute the programming logic of our application, or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"application logic"}]},{"type":"text","value":" in short. Our application would need some other classes, too."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is usually a good idea to keep any interaction with a user separate from the application logic. It is, after all, a responsibility all on its own. In addition to the core application logic, our program should therefore contain a class which handles the user interface."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Furthermore, our phone book should have some means of persistent storage between executions. File handling is, again, a clearly separate responsibility, so it deserves a class of its own."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now that we have an outline of the basic components of our program, the question arises: where should we begin programming? Again, there is no right or wrong answer, but it is often a good idea to start with some part of the application logic."}]},{"type":"element","tagName":"h2","properties":{"id":"step-1-an-outline-for-the-application-logic","style":"position:relative;"},"children":[{"type":"text","value":"Step 1: an outline for the application logic"},{"type":"element","tagName":"a","properties":{"href":"#step-1-an-outline-for-the-application-logic","ariaLabel":"step 1 an outline for the application logic 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":"Let's start with the class "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"PhoneBook"}]},{"type":"text","value":". A skeleton implementation 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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBook"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__persons "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_number"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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":"pass"}]},{"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":"get_numbers"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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":"pass"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This class consists of a list of persons along with methods for both adding and fetching data."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each person may be connected with multiple numbers, so let's implement the internal structure of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"persons"}]},{"type":"text","value":" with a dictionary. A dictionary allows us to search for keys by name, and the value attached to a dictionary key can be a list. So far it looks like we don't really need a separate class to represent a person - an entry in a dictionary will do."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's implement the methods listed above, and test our phone book:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBook"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__persons "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_number"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__persons"},{"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":"# add a new dictionary entry with an empty list for the numbers"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__persons"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"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","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__persons"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"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":"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"get_numbers"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__persons"},{"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","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__persons"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"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":"# code for testing"}]},{"type":"text","value":"\nphonebook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBook"},{"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":"\nphonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"\"02-123456\""}]},{"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":"phonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"phonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"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":"This should print out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['02-123456']\nNone"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"get_numbers"}]},{"type":"text","value":" returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":" if a name is not included in the phone book. If the name is found, it returns the list of numbers attached to the name."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Whenever you make changes to a program it is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"always"}]},{"type":"text","value":" worth testing that the code works as expected, before moving on to any other changes. The code used for testing is usually something that is deleted soon after, and as such you might think it's not worth the trouble to write any tests in the first place. In most cases this is not true. Testing is essential to good programming results."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A bug in the program should be caught and fixed as soon as possible. If you get into the habit of verifying the functionality of practically every new line of code, you will find that the bugs are usually easy to locate and fix, as you can be quite certain that the bug was caused by the most recent change. If you only test the program after adding dozens of lines of code, the potential sources for bugs also increase by dozens of times."}]},{"type":"element","tagName":"h2","properties":{"id":"step-2-an-outline-for-the-user-interface","style":"position:relative;"},"children":[{"type":"text","value":"Step 2: an outline for the user interface"},{"type":"element","tagName":"a","properties":{"href":"#step-2-an-outline-for-the-user-interface","ariaLabel":"step 2 an outline for the user interface 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":"With the core application logic out of the way, it is time to implement a text-based user interface. We will need a new class, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":", with the following initial functionality:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBook"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"help"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"\"commands: \""}]},{"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":"\"0 exit\""}]},{"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":"execute"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"help"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n command "},{"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":"\"command: \""}]},{"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":"if"}]},{"type":"text","value":" command "},{"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":"\"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":"break"}]},{"type":"text","value":"\n\napplication "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBookApplication"},{"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":"\napplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"execute"},{"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":"This program doesn't do very much yet, but let's go through the contents. The constructor method creates a new PhoneBook, which is stored in a private attribute. The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"execute(self)"}]},{"type":"text","value":" starts the program's text-based user interface, the core of which is the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop, which keeps asking the user for commands until they type in the command for exiting. There is also a method for intructions, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"help(self)"}]},{"type":"text","value":", which is called before entering the loop, so that the instructions are printed out."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, let's add some actual functionality. First, we implement adding new data to the phone book:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBook"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"help"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"\"commands: \""}]},{"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":"\"0 exit\""}]},{"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":"\"1 add entry\""}]},{"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":"execute"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"help"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n command "},{"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":"\"command: \""}]},{"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":"if"}]},{"type":"text","value":" command "},{"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":"\"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":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" command "},{"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":"\"1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n name "},{"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":"\"name: \""}]},{"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":"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":"\"number: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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\napplication "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBookApplication"},{"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":"\napplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"execute"},{"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":"If the user types in "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" for adding a new number, the user interface asks for a name and a number, and adds these to the PhoneBook using the appropriate method defined in the class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The only responsibility of the user interface is to communicate with the user. Any other functionality, such as storing a new name-number pair, is the responsibility of the PhoneBook object."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There is room for improvement in the structure of our user interface class. Let's create a method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_entry(self)"}]},{"type":"text","value":" which handles the command for adding a new entry:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBook"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"help"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"\"commands: \""}]},{"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":"\"0 exit\""}]},{"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":"\"1 add entry\""}]},{"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":"# separation of concerns in action: a new method for adding an entry"}]},{"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":"add_entry"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 name "},{"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":"\"name: \""}]},{"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":"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":"\"number: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"execute"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"help"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n command "},{"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":"\"command: \""}]},{"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":"if"}]},{"type":"text","value":" command "},{"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":"\"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":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" command "},{"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":"\"1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_entry"},{"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\napplication "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBookApplication"},{"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":"\napplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"execute"},{"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":"The "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"separation of concerns"}]},{"type":"text","value":" principle extends to the level of methods, too. We could have the entire functionality of the user interface in a single complicated "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop, but it is better to separate each functionality into its own method. The responsibility of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"execute()"}]},{"type":"text","value":" method is just delegating the commands typed in by the user to the relevant methods. This helps with managing the growing complexity of our program. For example, if we want to later change the way adding entries works, it is immediately clear that we must then focus our efforts on the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_entry()"}]},{"type":"text","value":" method."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's include functionality for searching for entries in our user interface. This should have its own method, too:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBook"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"help"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"\"commands: \""}]},{"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":"\"0 exit\""}]},{"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":"\"1 add entry\""}]},{"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":"\"2 search\""}]},{"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":"add_entry"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 name "},{"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":"\"name: \""}]},{"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":"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":"\"number: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"search"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 name "},{"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":"\"name: \""}]},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"if"}]},{"type":"text","value":" numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"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":"\"number unknown\""}]},{"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":"\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":"execute"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"help"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n command "},{"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":"\"command: \""}]},{"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":"if"}]},{"type":"text","value":" command "},{"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":"\"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":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" command "},{"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":"\"1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_entry"},{"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":"elif"}]},{"type":"text","value":" command "},{"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":"\"2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"search"},{"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":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"help"}]},{"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\napplication "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBookApplication"},{"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":"\napplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"execute"},{"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":"We now have a simple working phone book application ready for testing. The following is an example run:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"commands:\n0 exit\n1 add entry\n2 search"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Eric"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"02-123456"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Eric"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"045-4356713"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Eric"}]},{"type":"text","value":"\n02-123456\n045-4356713"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: Emily\nnumber unknown"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For such a simple application we have written quite a lot of code. If we'd written it all within the one "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop we could probably have gotten away with a lot less code. It is, however, quite easy to read the code, the structure is clear, and we should have no trouble adding new features."}]},{"type":"element","tagName":"h2","properties":{"id":"step-3-importing-data-from-a-file","style":"position:relative;"},"children":[{"type":"text","value":"Step 3: importing data from a file"},{"type":"element","tagName":"a","properties":{"href":"#step-3-importing-data-from-a-file","ariaLabel":"step 3 importing data from a file 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":"Let's assume we already have some phone numbers stored in a file, and we want to read this as the program starts up. The data file is in the following CSV format:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Eric;02-1234567;045-4356713"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Emily;040-324344"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Handling files is clearly its own area of responsibility, so it merits a class of its own:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"FileHandler"}]},{"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 "},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" filename"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__filename "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" filename\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":"load_file"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 names "},{"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","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__filename"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" f"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" f"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strip"},{"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":"split"},{"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 name"},{"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":"numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" parts\n names"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"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":" numbers\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" names"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The constructor method takes the name of the file as its argument. The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"load_file(self)"}]},{"type":"text","value":" reads the contents of the file. It splits each line into two parts: a name and a list of numbers. It then adds these to a dictionary, using the name as the key and the list as the value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method uses a nifty Python feature: it is possible to first select some items from a list separately, and then take the rest of the items in a new list. You can see an example of this below. You may remember from "},{"type":"element","tagName":"a","properties":{"href":"/part-6/1-reading-files#reading-csv-files"},"children":[{"type":"text","value":"part 6"}]},{"type":"text","value":" that the string method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":" returns 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":"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":"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":" "},{"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":" "},{"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":"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":"\nfirst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" second"},{"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":"rest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_list\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":"first"},{"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":"second"},{"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":"rest"},{"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":"1\n2\n[3, 4, 5]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" in front of the variable name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rest"}]},{"type":"text","value":" in the assignment statement means that this last variable should contain all the remaining items in the list, from the third one onwards."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We should absolutely test the file handler separately before including it in our application:"}]},{"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":"t "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" FileHandler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"phonebook.txt\""}]},{"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":"t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load_file"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'Eric': ['02-1234567', '045-4356713'], 'Emily': ['040-324344']}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As the file handler seems to work fine, we can add it to our application. Let's assume we want to read the file first thing every time the program is run. The logical place for reading the file would be the constructor of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":" class:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBook"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__filehandler "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" FileHandler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"phonebook.txt\""}]},{"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":"# add the names and numbers from the file to the phone book"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" numbers "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__filehandler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load_file"},{"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":"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":"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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","comment"]},"children":[{"type":"text","value":"# the rest of the program"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This functionality should also be tested. Once we've made certain the contents of the file are accessible through the user interface of our application, we can move on to the next stage."}]},{"type":"element","tagName":"h2","properties":{"id":"step-4-export-data-to-a-file","style":"position:relative;"},"children":[{"type":"text","value":"Step 4: export data to a file"},{"type":"element","tagName":"a","properties":{"href":"#step-4-export-data-to-a-file","ariaLabel":"step 4 export data to a file 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 final feature in our basic version of the application is saving the contents of the phone book back in the same file the data was read from."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This involves a change to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBook"}]},{"type":"text","value":" class. We need to be able to export the contents of the phone book:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBook"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__persons "},{"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","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# return all entries (in dictionary format)"}]},{"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":"all_entries"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__persons"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The actual saving to the file should be handled by the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"FileHandler"}]},{"type":"text","value":" class. Let's add the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"save_file"}]},{"type":"text","value":" which takes a dictionary representation of the phone book as its argument:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"FileHandler"}]},{"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 "},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" filename"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__filename "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" filename\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":"load_file"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# ..."}]},{"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":"save_file"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" phonebook"},{"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":"dict"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__filename"},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" f"},{"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":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" numbers "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" phonebook"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n line "},{"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":"text","value":"name"},{"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":" numbers\n f"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"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":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"line"},{"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":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The saving should happen as the program exits. Let's add a method for this purpose to the user interface, and call it before breaking out of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop:"}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"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":"# the rest of the code for the user interface"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# a method which gets executed as the program exits"}]},{"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":"exit"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__filehandler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"save_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"all_entries"},{"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\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":"execute"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"help"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n command "},{"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":"\"command: \""}]},{"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":"if"}]},{"type":"text","value":" command "},{"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":"\"0\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"exit"},{"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":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" command "},{"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":"\"1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_entry"},{"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":"elif"}]},{"type":"text","value":" command "},{"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":"\"2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"search"},{"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":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"help"}]},{"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":"Phone book expansion, version 1","tmcname":"part10-10_phone_book_v1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will create a small expansion to the phone book application. The code from the above example is in the exercise template. Please add a command which lets the user search the phone book by number. After the addition the application should work as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"commands:\n0 exit\n1 add entry\n2 search\n3 search by number"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Eric"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"02-123456"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Eric"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"045-4356713"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"02-123456"}]},{"type":"text","value":"\nEric"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0100100"}]},{"type":"text","value":"\nunknown number"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement this addition with respect to the current structure of the program. This means that in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":" class you should add an appropriate helper method to allow for the new functionality, and also add a new branch to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop. In the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBook"}]},{"type":"text","value":" class you should add a method which allows for searching with a number."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" as you test your program and end up with lots of different numbers stored in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"phonebook.txt"}]},{"type":"text","value":" file, there is a chance the local tests will not be passed if conflicting data is read from the file as the app starts. You can try emptying the contents of any "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"phonebook.txt"}]},{"type":"text","value":" files you find in the directory for the exercise "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"before"}]},{"type":"text","value":" running the local tests. Which one of the files is used by your program may depend on your Visual Studio Code settings. Please have a look at the explanation in "},{"type":"element","tagName":"a","properties":{"href":"/part-6/1-reading-files#what-if-visual-studio-code-cannot-find-my-file"},"children":[{"type":"text","value":"part 6"}]},{"type":"text","value":". If your solution is correct, the tests on the server should still pass normally, regardless."}]}]},{"type":"element","tagName":"h2","properties":{"id":"objects-in-a-dictionary","style":"position:relative;"},"children":[{"type":"text","value":"Objects in a dictionary"},{"type":"element","tagName":"a","properties":{"href":"#objects-in-a-dictionary","ariaLabel":"objects in a dictionary 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 the next exercise you are asked to change your phone book so that the values in the dictionary are "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"objects"}]},{"type":"text","value":", not lists."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There is nothing intrinsically strange about this, but this is the first time on this course that something like this is suggested, so let's go through a simpler example before diving into the exercise."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here we have an application which keeps track of how many exercises students have completed on a course. Each student's exercise count is stored in a simple object:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"ExerciseCounter"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__exercises "},{"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":"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":"done"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__exercises "},{"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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"how_many"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__exercises"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following main function uses the above class:"}]},{"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":"students "},{"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":"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":"\"let's do some exercises\""}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n name "},{"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":"\"student: \""}]},{"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":"if"}]},{"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"},{"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":"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":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# create a new object if it doesn't exist yet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"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":" ExerciseCounter"},{"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","comment"]},"children":[{"type":"text","value":"# add a new done exercise to the counter"}]},{"type":"text","value":"\n students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"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":"done"},{"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":"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"},{"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":"\"exercises completed:\""}]},{"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":" student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exercises "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" students"},{"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":"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'s exercises: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"exercises"},{"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":"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":"p","properties":{},"children":[{"type":"text","value":"Running the above could look like this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"let's do some exercises\nstudent: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"peter"}]},{"type":"text","value":"\nstudent: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sarah"}]},{"type":"text","value":"\nstudent: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"andy"}]},{"type":"text","value":"\nstudent: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sarah"}]},{"type":"text","value":"\nstudent: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"charlotte"}]},{"type":"text","value":"\nstudent: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"charlotte"}]},{"type":"text","value":"\nstudent: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"andy"}]},{"type":"text","value":"\nstudent: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sarah"}]},{"type":"text","value":"\nstudent:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"exercises completed:\npeter's exercises: 1\nandy's exercises: 2\nsarah's exercises: 3\ncharlotte's exercises: 2"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are a couple of things to consider in the above example. When the user inputs a name, the program first checks if the name is already a key in the dictionary. If the name is not present, a new object is created and added as an entry in the dictionary:"}]},{"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":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"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":" ExerciseCounter"},{"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":"After this we can be "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"sure"}]},{"type":"text","value":" the object exists, attached to the name of the student which is used as the key. Either it was just created, or it already existed from a previous iteration of the loop. Either way, we can now retrieve the object with the key, and call the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"done"}]},{"type":"text","value":":"}]},{"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":"students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"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":"done"},{"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":"The above line actually contains two separate events. We could just as well use a helper variable and write it on two separate lines of code:"}]},{"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":"students_counter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nstudents_counter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"done"},{"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":"NB: Even though the object is here assigned to a helper variable, the object still exists in the dictionary just as before. The helper variable contains a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reference"}]},{"type":"text","value":" to the object in the dictionary."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you are not quite sure what actually happens in the code above, please do try it out with the "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Phone book expansion, version 2","tmcname":"part10-11_phone_book_v2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will create another version of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":". You will add addresses to the data which can be attached to a name. For simplicity's sake the functionality for saving to file has been removed, and some other methods have been renamed to better accommodate the change."}]},{"type":"element","tagName":"h2","properties":{"id":"a-separate-class-for-a-persons-data","style":"position:relative;"},"children":[{"type":"text","value":"A separate class for a person's data"},{"type":"element","tagName":"a","properties":{"href":"#a-separate-class-for-a-persons-data","ariaLabel":"a separate class for a persons data 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":"Please change the way the data of a person is handled. Implement a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":", which takes care of the phone numbers and addresses of persons. The class should work as follows:"}]},{"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":"person "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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"},{"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":"person"},{"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","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":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"address"},{"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":"\nperson"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"040-123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nperson"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_address"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mannerheimintie 10 Helsinki\""}]},{"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":"person"},{"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","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":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"address"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eric\n[]\nNone\n['040-123456']\nMannerheimintie 10 Helsinki"}]}]},{"type":"element","tagName":"h2","properties":{"id":"phonebook-uses-the-class-person","style":"position:relative;"},"children":[{"type":"text","value":"PhoneBook uses the class Person"},{"type":"element","tagName":"a","properties":{"href":"#phonebook-uses-the-class-person","ariaLabel":"phonebook uses the class person 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":"Please change the internal implementation of your application so that your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBook"}]},{"type":"text","value":" class uses objects of class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" to store the data in the phone book. That is, the attribute "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__persons"}]},{"type":"text","value":" should still contain a dictionary, but the values should be Person-objects and not lists. The user of your application should notice no difference; the changes must not affect the user interface."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"WARNING:"}]},{"type":"text","value":" whenever you make structural changes to your code, as described in this exercise, always take baby steps and test at every possible stage. Do not try and make all the changes at once. That is a sure way of "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"running into serious problems with your code"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A suitable first step might be to write some code for checking the functionality of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBook"}]},{"type":"text","value":" class directly. For example, the following should at least not cause any errors:"}]},{"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":"phonebook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBook"},{"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":"\nphonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"\"02-123456\""}]},{"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":"phonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_entry"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"phonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_entry"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"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 new name for the method for fetching an entry from the phone book. The automatic tests do not check what the printout from your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"get_entry"}]},{"type":"text","value":" method is, but make sure no errors are raised by the above code, and that the result makes sense within your implementation."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When you've made the necessary changes in your program and have absolutely verified the functionality within the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBook"}]},{"type":"text","value":" class, you can move on to the user interface, and see if everything still works as expected."}]},{"type":"element","tagName":"h2","properties":{"id":"adding-an-address","style":"position:relative;"},"children":[{"type":"text","value":"Adding an address"},{"type":"element","tagName":"a","properties":{"href":"#adding-an-address","ariaLabel":"adding an address 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":"Please implement the functionality for adding an address to an entry in your phone book. The program should work as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"commands:\n0 exit\n1 add number\n2 search\n3 add address"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Eric"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"02-123456"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Emily"}]},{"type":"text","value":"\naddress: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Viherlaaksontie 7, Espoo"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Eric"}]},{"type":"text","value":"\n02-123456\naddress unknown"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Emily"}]},{"type":"text","value":"\nnumber unknown\nViherlaaksontie 7, Espoo"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Eric"}]},{"type":"text","value":"\naddress: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Linnankatu 75, Turku"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: 2\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Eric"}]},{"type":"text","value":"\n02-123456\nLinnankatu 75, Turku"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Wilhelm"}]},{"type":"text","value":"\naddress unknown\nnumber unknown"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"WARNING and hint:"}]},{"type":"text","value":" as stated above in the previous exercise, do not try and make all the changes at once. That is a sure way of "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"running into serious problems with your code"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First make sure your can reliably add addresses using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBook"}]},{"type":"text","value":" class directly. Once you have verified this, you can move on to the necessary changes in the user interface."}]}]},{"type":"element","tagName":"h2","properties":{"id":"some-concluding-remarks","style":"position:relative;"},"children":[{"type":"text","value":"Some concluding remarks"},{"type":"element","tagName":"a","properties":{"href":"#some-concluding-remarks","ariaLabel":"some concluding remarks 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 structure of the PhoneBook example above follows the fundamental principles of object oriented programming rather well. The central tenet is to identify the different responsibilities in the program and divide these logically among the different classes and methods. One of the main motivations for this division is managing complexity. Another important motive is that a logical division of responsibilities - modularity, in professional terminology - often makes the code easier to maintain and expand upon."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the software packages being developed and used in the wider world, the overwhelmingly most expensive facet of development is maintenance and expansion, meaning debugging existing software and implementing new features. Correctly implemented modularity is financially a very important feature in software development."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are some more object oriented programming principles worth highlighting here. The PhoneBook is a good example of how the core application logic can (and should) be separated from "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"both"}]},{"type":"text","value":" the user interface "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" any data storage facilities. This is important for a couple of different reasons. First, this separation makes it possible to test the code in smaller units, one class and method at a time. Second, as the core logic is now independent of the interfaces to the outside world, it is possible to change, to an extent, the implementation of either the core logic or the interfaces, without breaking the entire application."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The file handling process in the PhoneBook application proceeds as follows: the program reads the file just once, as it starts up. After this all data is stored in variables in the program. When the program exits, it again stores all data, in practice overwriting the file. In most cases this is the recommended way to deal with external files, as editing the data in situ is often much more complicated."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are many good guidebooks for learning about good programming practices. One such is "},{"type":"element","tagName":"a","properties":{"href":"https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Clean Code"}]},{"type":"text","value":" by Robert Martin. The code examples in the book are implemented in Java, however, so working through the examples can be quite cumbersome at this point in your programming career, although the book itself is much recommended by the course staff. The themes of easily maintained, expandable, good quality code will be further explored on the courses\n"},{"type":"element","tagName":"a","properties":{"href":"https://studies.helsinki.fi/courses/cu/hy-CU-118024742-2020-08-01","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Software Development Methods"}]},{"type":"text","value":" and "},{"type":"element","tagName":"a","properties":{"href":"https://studies.helsinki.fi/courses/cu/hy-CU-118024909-2020-08-01","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Software Engineering"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Writing code according to established object oriented programming principles comes at a price. You will likely end up writing more code than you would, were you to write your implementation in one continuous bout of spaghetti code. One of the key skills of a porgrammer is to decide the best approach for each situation. Sometimes it is necessary to just hack something together quickly for immediate use. On the other hand, if in the foreseeable future it can be expected that the code will be reused, maintained or futher developed, either by you or, more critically, by someone else entirely, the readability and logical modularity of the program code become essential. More often than not, if it is worth doing, it is worth doing well, even in the very early stages of development."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To finish off this part of the material you will implement one more larger application."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"CourseRecords","tmcname":"part10-12_course_records"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write an interactive application for keeping track of your studies. The internal structure is up to you, but this would be a good opportunity to practice creating a similar structure as in the PhoneBook example above."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your program should work as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 add course\n2 get course data\n3 statistics\n0 exit"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\ngrade: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\ncredits: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\nItP (5 cr) grade 3"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\ngrade: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\ncredits: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\nItP (5 cr) grade 5"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\ngrade: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ncredits: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\nItP (5 cr) grade 5"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Introduction to Java"}]},{"type":"text","value":"\nno entry for this course"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ACiP"}]},{"type":"text","value":"\ngrade: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ncredits: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItAI"}]},{"type":"text","value":"\ngrade: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\ncredits: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Algo101"}]},{"type":"text","value":"\ngrade: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\ncredits: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"CompModels"}]},{"type":"text","value":"\ngrade: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\ncredits: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n5 completed courses, a total of 29 credits\nmean 3.4\ngrade distribution\n5: xx\n4: x\n3:\n2: x\n1: x"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each course name should result in a single entry in the records. A grade may be raised by re-entering the course details, but the grade should never be lowered."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This exercise is worth two exercise points. The first is granted after the commands 1, 2 and 0 work correctly in your program. The second is granted if command 3 also works as expected."}]}]},{"type":"element","tagName":"h2","properties":{"id":"epilogue","style":"position:relative;"},"children":[{"type":"text","value":"Epilogue"},{"type":"element","tagName":"a","properties":{"href":"#epilogue","ariaLabel":"epilogue 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":"To finish off this part of the material, let's return to the user interface of the phone book example for a moment."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBook"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__filehandler "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" FileHandler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"phonebook.txt\""}]},{"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":"# the rest of the program"}]},{"type":"text","value":"\n\napplication "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBookApplication"},{"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":"\napplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"execute"},{"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":"A "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":" object contains both a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBook"}]},{"type":"text","value":" object and a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"FileHandler"}]},{"type":"text","value":" object. The name of the file passed to the FileHandler is, at the moment, hard-coded into the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":" class. This is a completely irrelevant detail when it comes to the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"user interface"}]},{"type":"text","value":" of the application. In fact, it breaks the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"separation of concerns"}]},{"type":"text","value":" principle: where a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBook"}]},{"type":"text","value":" object saves its contents should be of no concern to a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":", yet if we wanted to change the location, we'd have to change the code of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It would be a better idea to create a FileHandler object somewhere "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"outside"}]},{"type":"text","value":" the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":" class, and pass it as an argument to the application:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" storage_service"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBook"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__storage_service "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" storage_service\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the rest of the user interface"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# create a FileHandler"}]},{"type":"text","value":"\nstorage_service "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" FileHandler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"phonebook.txt\""}]},{"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":"# pass it as an argument to PhoneBookApplication's constructor"}]},{"type":"text","value":"\napplication "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBookApplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"storage_service"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"execute"},{"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":"This removes an "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"unnecessary dependency"}]},{"type":"text","value":" from the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":" class. If the name of the file changes, the user interface no longer needs to be changed. We just need to pass a different argument to the constructor:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" filename"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBook"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__filename "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" filename\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the rest of the user interface"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# use a different filename"}]},{"type":"text","value":"\nstorage_service "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" FileHandler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"new_phonebook.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplication "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBookApplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"storage_service"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"execute"},{"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":"This change also allows us to consider more exotic storage locations, for instance, a cloud service on the internet. We just need to implement a class which uses the cloud service, and offers "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":" the exact same methods as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"FileHandler"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An instance of this new \"cloud handler\" class can be passed as an argument to the constructor, and not a single line of code has to be changed in the user interface:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"CloudHandler"}]},{"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":"# code for saving the contents of the phone book"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# in a cloud service on the internet"}]},{"type":"text","value":"\n\nstorage_service "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CloudHandler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"amazon-cloud\""}]},{"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":"\"username\""}]},{"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":"\"passwrd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplication "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBookApplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"storage_service"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"execute"},{"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":"As you have seen before, using techniques like this carries a price tag, as there is more code to write, so a programmer needs to consider whether that is an acceptable tradeoff."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The technique outlined above is called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"dependency injection"}]},{"type":"text","value":". As the name implies, the idea is to provide any dependency required by an object from "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"outside"}]},{"type":"text","value":" the object. It is a very useful tool in a programmer's toolbox, as it makes it easier to implement new features in programs and facilitates automatic testing. This theme will be further explored on the aforementioned courses "},{"type":"element","tagName":"a","properties":{"href":"https://studies.helsinki.fi/courses/cu/hy-CU-118024742-2020-08-01","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Software Development Methods"}]},{"type":"text","value":" and "},{"type":"element","tagName":"a","properties":{"href":"https://studies.helsinki.fi/courses/cu/hy-CU-118024909-2020-08-01","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Software Engineering"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this part of the course."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434"},"children":[]}]}]},"html":"

    After this section

    Thus far in this course material we have covered a large number of Python features.

    The Introduction to Programming course introduced control structures, such as while and for, functions, and basic data structures, such as lists, tuples and dictionaries. In principle, those tools are all that is needed to express anything a programmer may wish to express with Python.

    On this Advanced Course in Programming, beginning in part 8 of the material, you have become familiar with classes and objects. Let's take a moment to consider when and why they are necessary, if those basic tools from parts 1 to 7 should be enough.

    Managing complexity

    Objects and classes are by no means necessary in every programming context. For example, if you are programming a smallish script for one-time use, objects are usually surplus to requirement. However, when you are programming something larger and more complicated, objects become very useful.

    When programs grow in complexity, the amount of details quickly becomes unmanageable, unless the program is organised in some systematic way. Even some of the more complicated exercises on this course so far would have benefited from the examples set in this part of the material.

    Fo decades the concept of separation of concerns has been one of the central principles in programming, and the larger field of computer science. Quoting from Wikipedia:

    Separation of concerns is a design principle for separating a computer program into distinct sections such that each section addresses a separate concern. A concern is a set of information that affects the code of a computer program.

    Separating the program into sections, so that each has its own concern to handle, helps in managing the inevitable complexity of a computer program.

    Functions are one way of organising a program into distinct, manageable wholes. Instead of writing a single script, the idea is to formulate small, separately verifiable functions which each solve some part of the larger problem.

    Another common approach to managing larger programs is objects, through object oriented programming principles. There are benefits and drawbacks to both approaches, and each programmer has their own favourite. As we have seen so far, objects and classes allow us to collect all the data and the code processing that data within a single unit, in the attributes and methods of an object. Furthermore, objects provide a way of encapsulating the data they control, so that other parts of the program do not have to worry about the internal details of an object.

    A worked example: phone book

    How should a program be divided into classes and objects? This is by no means an easy question with a single acceptable answer, so we will proceed with an example. In part five you completed a phone book application, and now we will implement something similar using object oriented programming principles.

    Following the separation of concerns principle, a program should be divided into sections which each have their own cause to take care of. In object oriented programming this translates to the single-responsibility principle. Without going into the finer details, its fundamental purpose is clear from the name: a single class and objects created based on it should have a single responsibility in the program.

    Object oriented programming is often used as a way of modelling real world objects and phenomena. A single object in the real world is modelled with a single class in program code. In the case of a phone book such objects might be

    A name and a phone number may be thought of as just bits of data which do not merit their own classes, but a person is a distinct physical entity in the real world, and in the programming world it could work as a class. A Person object would be responsible for tying together a name and the phone numbers attached to it.

    A phone book itself could be a good candidate for a class. Its responsibility would be to manage different person objects and the data they contain.

    Now we have outlined the core of our application: phone book and person constitute the programming logic of our application, or application logic in short. Our application would need some other classes, too.

    It is usually a good idea to keep any interaction with a user separate from the application logic. It is, after all, a responsibility all on its own. In addition to the core application logic, our program should therefore contain a class which handles the user interface.

    Furthermore, our phone book should have some means of persistent storage between executions. File handling is, again, a clearly separate responsibility, so it deserves a class of its own.

    Now that we have an outline of the basic components of our program, the question arises: where should we begin programming? Again, there is no right or wrong answer, but it is often a good idea to start with some part of the application logic.

    Step 1: an outline for the application logic

    Let's start with the class PhoneBook. A skeleton implementation could look like this:

    class PhoneBook:\n    def __init__(self):\n        self.__persons = []\n\n    def add_number(self, name: str, number: str):\n        pass\n\n    def get_numbers(self, name: str):\n        pass\n

    This class consists of a list of persons along with methods for both adding and fetching data.

    Each person may be connected with multiple numbers, so let's implement the internal structure of persons with a dictionary. A dictionary allows us to search for keys by name, and the value attached to a dictionary key can be a list. So far it looks like we don't really need a separate class to represent a person - an entry in a dictionary will do.

    Let's implement the methods listed above, and test our phone book:

    class PhoneBook:\n    def __init__(self):\n        self.__persons = {}\n\n    def add_number(self, name: str, number: str):\n        if not name in self.__persons:\n            # add a new dictionary entry with an empty list for the numbers\n            self.__persons[name] = []\n\n        self.__persons[name].append(number)\n\n    def get_numbers(self, name: str):\n        if not name in self.__persons:\n            return None\n\n        return self.__persons[name]\n\n# code for testing\nphonebook = PhoneBook()\nphonebook.add_number(\"Eric\", \"02-123456\")\nprint(phonebook.get_numbers(\"Eric\"))\nprint(phonebook.get_numbers(\"Emily\"))

    This should print out the following:

    ['02-123456']\nNone

    The method get_numbers returns None if a name is not included in the phone book. If the name is found, it returns the list of numbers attached to the name.

    Whenever you make changes to a program it is always worth testing that the code works as expected, before moving on to any other changes. The code used for testing is usually something that is deleted soon after, and as such you might think it's not worth the trouble to write any tests in the first place. In most cases this is not true. Testing is essential to good programming results.

    A bug in the program should be caught and fixed as soon as possible. If you get into the habit of verifying the functionality of practically every new line of code, you will find that the bugs are usually easy to locate and fix, as you can be quite certain that the bug was caused by the most recent change. If you only test the program after adding dozens of lines of code, the potential sources for bugs also increase by dozens of times.

    Step 2: an outline for the user interface

    With the core application logic out of the way, it is time to implement a text-based user interface. We will need a new class, PhoneBookApplication, with the following initial functionality:

    class PhoneBookApplication:\n    def __init__(self):\n        self.__phonebook = PhoneBook()\n\n    def help(self):\n        print(\"commands: \")\n        print(\"0 exit\")\n\n    def execute(self):\n        self.help()\n        while True:\n            print(\"\")\n            command = input(\"command: \")\n            if command == \"0\":\n                break\n\napplication = PhoneBookApplication()\napplication.execute()

    This program doesn't do very much yet, but let's go through the contents. The constructor method creates a new PhoneBook, which is stored in a private attribute. The method execute(self) starts the program's text-based user interface, the core of which is the while loop, which keeps asking the user for commands until they type in the command for exiting. There is also a method for intructions, help(self), which is called before entering the loop, so that the instructions are printed out.

    Now, let's add some actual functionality. First, we implement adding new data to the phone book:

    class PhoneBookApplication:\n    def __init__(self):\n        self.__phonebook = PhoneBook()\n\n    def help(self):\n        print(\"commands: \")\n        print(\"0 exit\")\n        print(\"1 add entry\")\n\n    def execute(self):\n        self.help()\n        while True:\n            print(\"\")\n            command = input(\"command: \")\n            if command == \"0\":\n                break\n            elif command == \"1\":\n                name = input(\"name: \")\n                number = input(\"number: \")\n                self.__phonebook.add_number(name, number)\n\napplication = PhoneBookApplication()\napplication.execute()

    If the user types in 1 for adding a new number, the user interface asks for a name and a number, and adds these to the PhoneBook using the appropriate method defined in the class.

    The only responsibility of the user interface is to communicate with the user. Any other functionality, such as storing a new name-number pair, is the responsibility of the PhoneBook object.

    There is room for improvement in the structure of our user interface class. Let's create a method add_entry(self) which handles the command for adding a new entry:

    class PhoneBookApplication:\n    def __init__(self):\n        self.__phonebook = PhoneBook()\n\n    def help(self):\n        print(\"commands: \")\n        print(\"0 exit\")\n        print(\"1 add entry\")\n\n    # separation of concerns in action: a new method for adding an entry\n    def add_entry(self):\n        name = input(\"name: \")\n        number = input(\"number: \")\n        self.__phonebook.add_number(name, number)\n\n    def execute(self):\n        self.help()\n        while True:\n            print(\"\")\n            command = input(\"command: \")\n            if command == \"0\":\n                break\n            elif command == \"1\":\n                self.add_entry()\n\napplication = PhoneBookApplication()\napplication.execute()

    The separation of concerns principle extends to the level of methods, too. We could have the entire functionality of the user interface in a single complicated while loop, but it is better to separate each functionality into its own method. The responsibility of the execute() method is just delegating the commands typed in by the user to the relevant methods. This helps with managing the growing complexity of our program. For example, if we want to later change the way adding entries works, it is immediately clear that we must then focus our efforts on the add_entry() method.

    Let's include functionality for searching for entries in our user interface. This should have its own method, too:

    class PhoneBookApplication:\n    def __init__(self):\n        self.__phonebook = PhoneBook()\n\n    def help(self):\n        print(\"commands: \")\n        print(\"0 exit\")\n        print(\"1 add entry\")\n        print(\"2 search\")\n\n    def add_entry(self):\n        name = input(\"name: \")\n        number = input(\"number: \")\n        self.__phonebook.add_number(name, number)\n\n    def search(self):\n        name = input(\"name: \")\n        numbers = self.__phonebook.get_numbers(name)\n        if numbers == None:\n            print(\"number unknown\")\n            return\n        for number in numbers:\n            print(number)\n\n    def execute(self):\n        self.help()\n        while True:\n            print(\"\")\n            command = input(\"command: \")\n            if command == \"0\":\n                break\n            elif command == \"1\":\n                self.add_entry()\n            elif command == \"2\":\n                self.search()\n            else:\n                self.help()\n\napplication = PhoneBookApplication()\napplication.execute()

    We now have a simple working phone book application ready for testing. The following is an example run:

    commands:\n0 exit\n1 add entry\n2 search

    command: 1\nname: Eric\nnumber: 02-123456

    command: 1\nname: Eric\nnumber: 045-4356713

    command: 2\nname: Eric\n02-123456\n045-4356713

    command: 2\nname: Emily\nnumber unknown

    command: 0

    For such a simple application we have written quite a lot of code. If we'd written it all within the one while loop we could probably have gotten away with a lot less code. It is, however, quite easy to read the code, the structure is clear, and we should have no trouble adding new features.

    Step 3: importing data from a file

    Let's assume we already have some phone numbers stored in a file, and we want to read this as the program starts up. The data file is in the following CSV format:

    Eric;02-1234567;045-4356713\nEmily;040-324344

    Handling files is clearly its own area of responsibility, so it merits a class of its own:

    class FileHandler():\n    def __init__(self, filename):\n        self.__filename = filename\n\n    def load_file(self):\n        names = {}\n        with open(self.__filename) as f:\n            for line in f:\n                parts = line.strip().split(';')\n                name, *numbers = parts\n                names[name] = numbers\n\n        return names

    The constructor method takes the name of the file as its argument. The method load_file(self) reads the contents of the file. It splits each line into two parts: a name and a list of numbers. It then adds these to a dictionary, using the name as the key and the list as the value.

    The method uses a nifty Python feature: it is possible to first select some items from a list separately, and then take the rest of the items in a new list. You can see an example of this below. You may remember from part 6 that the string method split returns a list.

    my_list = [1, 2, 3, 4, 5]\nfirst, second, *rest = my_list\nprint(first)\nprint(second)\nprint(rest)

    1\n2\n[3, 4, 5]

    The * in front of the variable name rest in the assignment statement means that this last variable should contain all the remaining items in the list, from the third one onwards.

    We should absolutely test the file handler separately before including it in our application:

    t = FileHandler(\"phonebook.txt\")\nprint(t.load_file())

    {'Eric': ['02-1234567', '045-4356713'], 'Emily': ['040-324344']}

    As the file handler seems to work fine, we can add it to our application. Let's assume we want to read the file first thing every time the program is run. The logical place for reading the file would be the constructor of the PhoneBookApplication class:

    class PhoneBookApplication:\n    def __init__(self):\n        self.__phonebook = PhoneBook()\n        self.__filehandler = FileHandler(\"phonebook.txt\")\n\n        # add the names and numbers from the file to the phone book\n        for name, numbers in self.__filehandler.load_file().items():\n            for number in numbers:\n                self.__phonebook.add_number(name, number)\n\n    # the rest of the program

    This functionality should also be tested. Once we've made certain the contents of the file are accessible through the user interface of our application, we can move on to the next stage.

    Step 4: export data to a file

    The final feature in our basic version of the application is saving the contents of the phone book back in the same file the data was read from.

    This involves a change to the PhoneBook class. We need to be able to export the contents of the phone book:

    class PhoneBook:\n    def __init__(self):\n        self.__persons = {}\n\n    # ...\n\n    # return all entries (in dictionary format)\n    def all_entries(self):\n        return self.__persons

    The actual saving to the file should be handled by the FileHandler class. Let's add the method save_file which takes a dictionary representation of the phone book as its argument:

    class FileHandler():\n    def __init__(self, filename):\n        self.__filename = filename\n\n    def load_file(self):\n        # ...\n\n    def save_file(self, phonebook: dict):\n        with open(self.__filename, \"w\") as f:\n            for name, numbers in phonebook.items():\n                line = [name] + numbers\n                f.write(\";\".join(line) + \"\\n\")

    The saving should happen as the program exits. Let's add a method for this purpose to the user interface, and call it before breaking out of the while loop:

    \nclass PhoneBookApplication:\n    # the rest of the code for the user interface\n\n    # a method which gets executed as the program exits\n    def exit(self):\n        self.__filehandler.save_file(self.__phonebook.all_entries())\n\n    def execute(self):\n        self.help()\n        while True:\n            print(\"\")\n            command = input(\"command: \")\n            if command == \"0\":\n\n                self.exit()\n                break\n            elif command == \"1\":\n                self.add_entry()\n            elif command == \"2\":\n                self.search()\n            else:\n                self.help()

    In this exercise you will create a small expansion to the phone book application. The code from the above example is in the exercise template. Please add a command which lets the user search the phone book by number. After the addition the application should work as follows:

    commands:\n0 exit\n1 add entry\n2 search\n3 search by number

    command: 1\nname: Eric\nnumber: 02-123456

    command: 1\nname: Eric\nnumber: 045-4356713

    command: 3\nnumber: 02-123456\nEric

    command: 3\nnumber: 0100100\nunknown number

    command: 0

    Please implement this addition with respect to the current structure of the program. This means that in the PhoneBookApplication class you should add an appropriate helper method to allow for the new functionality, and also add a new branch to the while loop. In the PhoneBook class you should add a method which allows for searching with a number.

    NB: as you test your program and end up with lots of different numbers stored in the phonebook.txt file, there is a chance the local tests will not be passed if conflicting data is read from the file as the app starts. You can try emptying the contents of any phonebook.txt files you find in the directory for the exercise before running the local tests. Which one of the files is used by your program may depend on your Visual Studio Code settings. Please have a look at the explanation in part 6. If your solution is correct, the tests on the server should still pass normally, regardless.

    Objects in a dictionary

    In the next exercise you are asked to change your phone book so that the values in the dictionary are objects, not lists.

    There is nothing intrinsically strange about this, but this is the first time on this course that something like this is suggested, so let's go through a simpler example before diving into the exercise.

    Here we have an application which keeps track of how many exercises students have completed on a course. Each student's exercise count is stored in a simple object:

    class ExerciseCounter:\n    def __init__(self):\n        self.__exercises = 0\n\n    def done(self):\n        self.__exercises += 1\n\n    def how_many(self):\n        return self.__exercises

    The following main function uses the above class:

    students = {}\n\nprint(\"let's do some exercises\")\nwhile True:\n    name = input(\"student: \")\n    if len(name) == 0:\n        break\n\n    # create a new object if it doesn't exist yet\n    if not name in students:\n        students[name] = ExerciseCounter()\n\n    # add a new done exercise to the counter\n    students[name].done()\n\nprint()\nprint(\"exercises completed:\")\n\nfor student, exercises in students.items():\n    print(f\"{student}'s exercises: {exercises.how_many()}\")

    Running the above could look like this:

    let's do some exercises\nstudent: peter\nstudent: sarah\nstudent: andy\nstudent: sarah\nstudent: charlotte\nstudent: charlotte\nstudent: andy\nstudent: sarah\nstudent:

    exercises completed:\npeter's exercises: 1\nandy's exercises: 2\nsarah's exercises: 3\ncharlotte's exercises: 2

    There are a couple of things to consider in the above example. When the user inputs a name, the program first checks if the name is already a key in the dictionary. If the name is not present, a new object is created and added as an entry in the dictionary:

    if not name in students:\n    students[name] = ExerciseCounter()

    After this we can be sure the object exists, attached to the name of the student which is used as the key. Either it was just created, or it already existed from a previous iteration of the loop. Either way, we can now retrieve the object with the key, and call the method done:

    students[name].done()

    The above line actually contains two separate events. We could just as well use a helper variable and write it on two separate lines of code:

    students_counter = students[name]\nstudents_counter.done()

    NB: Even though the object is here assigned to a helper variable, the object still exists in the dictionary just as before. The helper variable contains a reference to the object in the dictionary.

    If you are not quite sure what actually happens in the code above, please do try it out with the visualisation tool.

    In this exercise you will create another version of the PhoneBookApplication. You will add addresses to the data which can be attached to a name. For simplicity's sake the functionality for saving to file has been removed, and some other methods have been renamed to better accommodate the change.

    A separate class for a person's data

    Please change the way the data of a person is handled. Implement a class named Person, which takes care of the phone numbers and addresses of persons. The class should work as follows:

    person = Person(\"Eric\")\nprint(person.name())\nprint(person.numbers())\nprint(person.address())\nperson.add_number(\"040-123456\")\nperson.add_address(\"Mannerheimintie 10 Helsinki\")\nprint(person.numbers())\nprint(person.address())

    Eric\n[]\nNone\n['040-123456']\nMannerheimintie 10 Helsinki

    PhoneBook uses the class Person

    Please change the internal implementation of your application so that your PhoneBook class uses objects of class Person to store the data in the phone book. That is, the attribute __persons should still contain a dictionary, but the values should be Person-objects and not lists. The user of your application should notice no difference; the changes must not affect the user interface.

    WARNING: whenever you make structural changes to your code, as described in this exercise, always take baby steps and test at every possible stage. Do not try and make all the changes at once. That is a sure way of running into serious problems with your code.

    A suitable first step might be to write some code for checking the functionality of the PhoneBook class directly. For example, the following should at least not cause any errors:

    phonebook = PhoneBook()\nphonebook.add_number(\"Eric\", \"02-123456\")\nprint(phonebook.get_entry(\"Eric\"))\nprint(phonebook.get_entry(\"Emily\"))

    Notice the new name for the method for fetching an entry from the phone book. The automatic tests do not check what the printout from your get_entry method is, but make sure no errors are raised by the above code, and that the result makes sense within your implementation.

    When you've made the necessary changes in your program and have absolutely verified the functionality within the PhoneBook class, you can move on to the user interface, and see if everything still works as expected.

    Adding an address

    Please implement the functionality for adding an address to an entry in your phone book. The program should work as follows:

    commands:\n0 exit\n1 add number\n2 search\n3 add address

    command: 1\nname: Eric\nnumber: 02-123456

    command: 3\nname: Emily\naddress: Viherlaaksontie 7, Espoo

    command: 2\nname: Eric\n02-123456\naddress unknown

    command: 2\nname: Emily\nnumber unknown\nViherlaaksontie 7, Espoo

    command: 3\nname: Eric\naddress: Linnankatu 75, Turku

    command: 2\nname: Eric\n02-123456\nLinnankatu 75, Turku

    command: 2\nname: Wilhelm\naddress unknown\nnumber unknown

    command: 0

    WARNING and hint: as stated above in the previous exercise, do not try and make all the changes at once. That is a sure way of running into serious problems with your code.

    First make sure your can reliably add addresses using the PhoneBook class directly. Once you have verified this, you can move on to the necessary changes in the user interface.

    Some concluding remarks

    The structure of the PhoneBook example above follows the fundamental principles of object oriented programming rather well. The central tenet is to identify the different responsibilities in the program and divide these logically among the different classes and methods. One of the main motivations for this division is managing complexity. Another important motive is that a logical division of responsibilities - modularity, in professional terminology - often makes the code easier to maintain and expand upon.

    In the software packages being developed and used in the wider world, the overwhelmingly most expensive facet of development is maintenance and expansion, meaning debugging existing software and implementing new features. Correctly implemented modularity is financially a very important feature in software development.

    There are some more object oriented programming principles worth highlighting here. The PhoneBook is a good example of how the core application logic can (and should) be separated from both the user interface and any data storage facilities. This is important for a couple of different reasons. First, this separation makes it possible to test the code in smaller units, one class and method at a time. Second, as the core logic is now independent of the interfaces to the outside world, it is possible to change, to an extent, the implementation of either the core logic or the interfaces, without breaking the entire application.

    The file handling process in the PhoneBook application proceeds as follows: the program reads the file just once, as it starts up. After this all data is stored in variables in the program. When the program exits, it again stores all data, in practice overwriting the file. In most cases this is the recommended way to deal with external files, as editing the data in situ is often much more complicated.

    There are many good guidebooks for learning about good programming practices. One such is Clean Code by Robert Martin. The code examples in the book are implemented in Java, however, so working through the examples can be quite cumbersome at this point in your programming career, although the book itself is much recommended by the course staff. The themes of easily maintained, expandable, good quality code will be further explored on the courses\nSoftware Development Methods and Software Engineering.

    Writing code according to established object oriented programming principles comes at a price. You will likely end up writing more code than you would, were you to write your implementation in one continuous bout of spaghetti code. One of the key skills of a porgrammer is to decide the best approach for each situation. Sometimes it is necessary to just hack something together quickly for immediate use. On the other hand, if in the foreseeable future it can be expected that the code will be reused, maintained or futher developed, either by you or, more critically, by someone else entirely, the readability and logical modularity of the program code become essential. More often than not, if it is worth doing, it is worth doing well, even in the very early stages of development.

    To finish off this part of the material you will implement one more larger application.

    Please write an interactive application for keeping track of your studies. The internal structure is up to you, but this would be a good opportunity to practice creating a similar structure as in the PhoneBook example above.

    Your program should work as follows:

    1 add course\n2 get course data\n3 statistics\n0 exit

    command: 1\ncourse: ItP\ngrade: 3\ncredits: 5

    command: 2\ncourse: ItP\nItP (5 cr) grade 3

    command: 1\ncourse: ItP\ngrade: 5\ncredits: 5

    command: 2\ncourse: ItP\nItP (5 cr) grade 5

    command: 1\ncourse: ItP\ngrade: 1\ncredits: 5

    command: 2\ncourse: ItP\nItP (5 cr) grade 5

    command: 2\ncourse: Introduction to Java\nno entry for this course

    command: 1\ncourse: ACiP\ngrade: 1\ncredits: 10

    command: 1\ncourse: ItAI\ngrade: 2\ncredits: 5

    command: 1\ncourse: Algo101\ngrade: 4\ncredits: 1

    command: 1\ncourse: CompModels\ngrade: 5\ncredits: 8

    command: 3\n5 completed courses, a total of 29 credits\nmean 3.4\ngrade distribution\n5: xx\n4: x\n3:\n2: x\n1: x

    command: 0

    Each course name should result in a single entry in the records. A grade may be raised by re-entering the course details, but the grade should never be lowered.

    This exercise is worth two exercise points. The first is granted after the commands 1, 2 and 0 work correctly in your program. The second is granted if command 3 also works as expected.

    Epilogue

    To finish off this part of the material, let's return to the user interface of the phone book example for a moment.

    class PhoneBookApplication:\n    def __init__(self):\n        self.__phonebook = PhoneBook()\n        self.__filehandler = FileHandler(\"phonebook.txt\")\n\n    # the rest of the program\n\napplication = PhoneBookApplication()\napplication.execute()

    A PhoneBookApplication object contains both a PhoneBook object and a FileHandler object. The name of the file passed to the FileHandler is, at the moment, hard-coded into the PhoneBookApplication class. This is a completely irrelevant detail when it comes to the user interface of the application. In fact, it breaks the separation of concerns principle: where a PhoneBook object saves its contents should be of no concern to a PhoneBookApplication, yet if we wanted to change the location, we'd have to change the code of PhoneBookApplication.

    It would be a better idea to create a FileHandler object somewhere outside the PhoneBookApplication class, and pass it as an argument to the application:

    class PhoneBookApplication:\n    def __init__(self, storage_service):\n        self.__phonebook = PhoneBook()\n        self.__storage_service = storage_service\n\n    # the rest of the user interface\n\n# create a FileHandler\nstorage_service = FileHandler(\"phonebook.txt\")\n# pass it as an argument to PhoneBookApplication's constructor\napplication = PhoneBookApplication(storage_service)\napplication.execute()

    This removes an unnecessary dependency from the PhoneBookApplication class. If the name of the file changes, the user interface no longer needs to be changed. We just need to pass a different argument to the constructor:

    class PhoneBookApplication:\n    def __init__(self, filename):\n        self.__phonebook = PhoneBook()\n        self.__filename = filename\n\n    # the rest of the user interface\n\n# use a different filename\nstorage_service = FileHandler(\"new_phonebook.txt\")\napplication = PhoneBookApplication(storage_service)\napplication.execute()

    This change also allows us to consider more exotic storage locations, for instance, a cloud service on the internet. We just need to implement a class which uses the cloud service, and offers PhoneBookApplication the exact same methods as FileHandler.

    An instance of this new \"cloud handler\" class can be passed as an argument to the constructor, and not a single line of code has to be changed in the user interface:

    class CloudHandler:\n    # code for saving the contents of the phone book\n    # in a cloud service on the internet\n\nstorage_service = CloudHandler(\"amazon-cloud\", \"username\", \"passwrd\")\napplication = PhoneBookApplication(storage_service)\napplication.execute()

    As you have seen before, using techniques like this carries a price tag, as there is more code to write, so a programmer needs to consider whether that is an acceptable tradeoff.

    The technique outlined above is called dependency injection. As the name implies, the idea is to provide any dependency required by an object from outside the object. It is a very useful tool in a programmer's toolbox, as it makes it easier to implement new features in programs and facilitates automatic testing. This theme will be further explored on the aforementioned courses Software Development Methods and Software Engineering.

    Please respond to a quick questionnaire on this part of the course.

    ","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-10/4-application-development.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 some basic principles of application development"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be comfortable with differentiating between the different parts of an application (user interface, program logic and file handling)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will have practiced writing your own somewhat larger application"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Thus far in this course material we have covered a large number of Python features."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The Introduction to Programming course introduced control structures, such as while and for, functions, and basic data structures, such as lists, tuples and dictionaries. In principle, those tools are all that is needed to express anything a programmer may wish to express with Python."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"On this Advanced Course in Programming, beginning in part 8 of the material, you have become familiar with classes and objects. Let's take a moment to consider when and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"why"}]},{"type":"text","value":" they are necessary, if those basic tools from parts 1 to 7 should be enough."}]},{"type":"element","tagName":"h2","properties":{"id":"managing-complexity","style":"position:relative;"},"children":[{"type":"text","value":"Managing complexity"},{"type":"element","tagName":"a","properties":{"href":"#managing-complexity","ariaLabel":"managing complexity 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":"Objects and classes are by no means necessary in every programming context. For example, if you are programming a smallish script for one-time use, objects are usually surplus to requirement. However, when you are programming something larger and more complicated, objects become very useful."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When programs grow in complexity, the amount of details quickly becomes unmanageable, unless the program is organised in some systematic way. Even some of the more complicated exercises on this course so far would have benefited from the examples set in this part of the material."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fo decades the concept of "},{"type":"element","tagName":"a","properties":{"href":"https://en.wikipedia.org/wiki/Separation_of_concerns","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"separation of concerns"}]},{"type":"text","value":" has been one of the central principles in programming, and the larger field of computer science. Quoting from Wikipedia:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Separation of concerns is a design principle for separating a computer program into distinct sections such that each section addresses a separate concern. A concern is a set of information that affects the code of a computer program."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Separating the program into sections, so that each has its own concern to handle, helps in managing the inevitable complexity of a computer program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Functions are one way of organising a program into distinct, manageable wholes. Instead of writing a single script, the idea is to formulate small, separately verifiable functions which each solve some part of the larger problem."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Another common approach to managing larger programs is objects, through object oriented programming principles. There are benefits and drawbacks to both approaches, and each programmer has their own favourite. As we have seen so far, objects and classes allow us to collect all the data "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" the code processing that data within a single unit, in the attributes and methods of an object. Furthermore, objects provide a way of encapsulating the data they control, so that other parts of the program do not have to worry about the internal details of an object."}]},{"type":"element","tagName":"h2","properties":{"id":"a-worked-example-phone-book","style":"position:relative;"},"children":[{"type":"text","value":"A worked example: phone book"},{"type":"element","tagName":"a","properties":{"href":"#a-worked-example-phone-book","ariaLabel":"a worked example phone book 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":"How should a program be divided into classes and objects? This is by no means an easy question with a single acceptable answer, so we will proceed with an example. In part five you completed "},{"type":"element","tagName":"a","properties":{"href":"/part-5/3-dictionary#programming-exercise-phone-book-version-2"},"children":[{"type":"text","value":"a phone book application"}]},{"type":"text","value":", and now we will implement something similar using object oriented programming principles."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Following the separation of concerns principle, a program should be divided into sections which each have their own cause to take care of. In object oriented programming this translates to the "},{"type":"element","tagName":"a","properties":{"href":"https://en.wikipedia.org/wiki/Single-responsibility_principle","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"single-responsibility principle"}]},{"type":"text","value":". Without going into the finer details, its fundamental purpose is clear from the name: "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"a single class and objects created based on it should have a single responsibility in the program"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Object oriented programming is often used as a way of modelling real world objects and phenomena. A single object in the real world is modelled with a single class in program code. In the case of a phone book such objects might be"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a person"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a name"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a phone number"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A name and a phone number may be thought of as just bits of data which do not merit their own classes, but a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"person"}]},{"type":"text","value":" is a distinct physical entity in the real world, and in the programming world it could work as a class. A Person object would be responsible for tying together a name and the phone numbers attached to it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"phone book"}]},{"type":"text","value":" itself could be a good candidate for a class. Its responsibility would be to manage different person objects and the data they contain."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now we have outlined the core of our application: "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"phone book"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"person"}]},{"type":"text","value":" constitute the programming logic of our application, or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"application logic"}]},{"type":"text","value":" in short. Our application would need some other classes, too."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is usually a good idea to keep any interaction with a user separate from the application logic. It is, after all, a responsibility all on its own. In addition to the core application logic, our program should therefore contain a class which handles the user interface."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Furthermore, our phone book should have some means of persistent storage between executions. File handling is, again, a clearly separate responsibility, so it deserves a class of its own."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now that we have an outline of the basic components of our program, the question arises: where should we begin programming? Again, there is no right or wrong answer, but it is often a good idea to start with some part of the application logic."}]},{"type":"element","tagName":"h2","properties":{"id":"step-1-an-outline-for-the-application-logic","style":"position:relative;"},"children":[{"type":"text","value":"Step 1: an outline for the application logic"},{"type":"element","tagName":"a","properties":{"href":"#step-1-an-outline-for-the-application-logic","ariaLabel":"step 1 an outline for the application logic 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":"Let's start with the class "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"PhoneBook"}]},{"type":"text","value":". A skeleton implementation 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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBook"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__persons "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_number"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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":"pass"}]},{"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":"get_numbers"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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":"pass"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This class consists of a list of persons along with methods for both adding and fetching data."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each person may be connected with multiple numbers, so let's implement the internal structure of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"persons"}]},{"type":"text","value":" with a dictionary. A dictionary allows us to search for keys by name, and the value attached to a dictionary key can be a list. So far it looks like we don't really need a separate class to represent a person - an entry in a dictionary will do."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's implement the methods listed above, and test our phone book:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBook"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__persons "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_number"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__persons"},{"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":"# add a new dictionary entry with an empty list for the numbers"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__persons"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"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","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__persons"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"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":"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"get_numbers"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__persons"},{"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","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__persons"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"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":"# code for testing"}]},{"type":"text","value":"\nphonebook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBook"},{"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":"\nphonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"\"02-123456\""}]},{"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":"phonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"phonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"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":"This should print out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['02-123456']\nNone"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"get_numbers"}]},{"type":"text","value":" returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":" if a name is not included in the phone book. If the name is found, it returns the list of numbers attached to the name."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Whenever you make changes to a program it is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"always"}]},{"type":"text","value":" worth testing that the code works as expected, before moving on to any other changes. The code used for testing is usually something that is deleted soon after, and as such you might think it's not worth the trouble to write any tests in the first place. In most cases this is not true. Testing is essential to good programming results."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A bug in the program should be caught and fixed as soon as possible. If you get into the habit of verifying the functionality of practically every new line of code, you will find that the bugs are usually easy to locate and fix, as you can be quite certain that the bug was caused by the most recent change. If you only test the program after adding dozens of lines of code, the potential sources for bugs also increase by dozens of times."}]},{"type":"element","tagName":"h2","properties":{"id":"step-2-an-outline-for-the-user-interface","style":"position:relative;"},"children":[{"type":"text","value":"Step 2: an outline for the user interface"},{"type":"element","tagName":"a","properties":{"href":"#step-2-an-outline-for-the-user-interface","ariaLabel":"step 2 an outline for the user interface 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":"With the core application logic out of the way, it is time to implement a text-based user interface. We will need a new class, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":", with the following initial functionality:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBook"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"help"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"\"commands: \""}]},{"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":"\"0 exit\""}]},{"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":"execute"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"help"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n command "},{"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":"\"command: \""}]},{"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":"if"}]},{"type":"text","value":" command "},{"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":"\"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":"break"}]},{"type":"text","value":"\n\napplication "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBookApplication"},{"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":"\napplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"execute"},{"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":"This program doesn't do very much yet, but let's go through the contents. The constructor method creates a new PhoneBook, which is stored in a private attribute. The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"execute(self)"}]},{"type":"text","value":" starts the program's text-based user interface, the core of which is the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop, which keeps asking the user for commands until they type in the command for exiting. There is also a method for intructions, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"help(self)"}]},{"type":"text","value":", which is called before entering the loop, so that the instructions are printed out."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, let's add some actual functionality. First, we implement adding new data to the phone book:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBook"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"help"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"\"commands: \""}]},{"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":"\"0 exit\""}]},{"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":"\"1 add entry\""}]},{"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":"execute"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"help"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n command "},{"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":"\"command: \""}]},{"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":"if"}]},{"type":"text","value":" command "},{"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":"\"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":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" command "},{"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":"\"1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n name "},{"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":"\"name: \""}]},{"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":"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":"\"number: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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\napplication "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBookApplication"},{"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":"\napplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"execute"},{"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":"If the user types in "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" for adding a new number, the user interface asks for a name and a number, and adds these to the PhoneBook using the appropriate method defined in the class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The only responsibility of the user interface is to communicate with the user. Any other functionality, such as storing a new name-number pair, is the responsibility of the PhoneBook object."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There is room for improvement in the structure of our user interface class. Let's create a method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_entry(self)"}]},{"type":"text","value":" which handles the command for adding a new entry:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBook"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"help"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"\"commands: \""}]},{"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":"\"0 exit\""}]},{"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":"\"1 add entry\""}]},{"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":"# separation of concerns in action: a new method for adding an entry"}]},{"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":"add_entry"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 name "},{"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":"\"name: \""}]},{"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":"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":"\"number: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"execute"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"help"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n command "},{"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":"\"command: \""}]},{"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":"if"}]},{"type":"text","value":" command "},{"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":"\"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":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" command "},{"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":"\"1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_entry"},{"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\napplication "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBookApplication"},{"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":"\napplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"execute"},{"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":"The "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"separation of concerns"}]},{"type":"text","value":" principle extends to the level of methods, too. We could have the entire functionality of the user interface in a single complicated "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop, but it is better to separate each functionality into its own method. The responsibility of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"execute()"}]},{"type":"text","value":" method is just delegating the commands typed in by the user to the relevant methods. This helps with managing the growing complexity of our program. For example, if we want to later change the way adding entries works, it is immediately clear that we must then focus our efforts on the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_entry()"}]},{"type":"text","value":" method."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's include functionality for searching for entries in our user interface. This should have its own method, too:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBook"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"help"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"\"commands: \""}]},{"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":"\"0 exit\""}]},{"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":"\"1 add entry\""}]},{"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":"\"2 search\""}]},{"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":"add_entry"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 name "},{"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":"\"name: \""}]},{"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":"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":"\"number: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"search"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 name "},{"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":"\"name: \""}]},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"if"}]},{"type":"text","value":" numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"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":"\"number unknown\""}]},{"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":"\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":"execute"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"help"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n command "},{"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":"\"command: \""}]},{"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":"if"}]},{"type":"text","value":" command "},{"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":"\"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":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" command "},{"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":"\"1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_entry"},{"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":"elif"}]},{"type":"text","value":" command "},{"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":"\"2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"search"},{"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":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"help"}]},{"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\napplication "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBookApplication"},{"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":"\napplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"execute"},{"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":"We now have a simple working phone book application ready for testing. The following is an example run:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"commands:\n0 exit\n1 add entry\n2 search"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Eric"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"02-123456"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Eric"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"045-4356713"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Eric"}]},{"type":"text","value":"\n02-123456\n045-4356713"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: Emily\nnumber unknown"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For such a simple application we have written quite a lot of code. If we'd written it all within the one "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop we could probably have gotten away with a lot less code. It is, however, quite easy to read the code, the structure is clear, and we should have no trouble adding new features."}]},{"type":"element","tagName":"h2","properties":{"id":"step-3-importing-data-from-a-file","style":"position:relative;"},"children":[{"type":"text","value":"Step 3: importing data from a file"},{"type":"element","tagName":"a","properties":{"href":"#step-3-importing-data-from-a-file","ariaLabel":"step 3 importing data from a file 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":"Let's assume we already have some phone numbers stored in a file, and we want to read this as the program starts up. The data file is in the following CSV format:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Eric;02-1234567;045-4356713"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Emily;040-324344"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Handling files is clearly its own area of responsibility, so it merits a class of its own:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"FileHandler"}]},{"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 "},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" filename"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__filename "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" filename\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":"load_file"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 names "},{"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","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__filename"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" f"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" f"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strip"},{"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":"split"},{"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 name"},{"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":"numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" parts\n names"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"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":" numbers\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" names"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The constructor method takes the name of the file as its argument. The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"load_file(self)"}]},{"type":"text","value":" reads the contents of the file. It splits each line into two parts: a name and a list of numbers. It then adds these to a dictionary, using the name as the key and the list as the value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method uses a nifty Python feature: it is possible to first select some items from a list separately, and then take the rest of the items in a new list. You can see an example of this below. You may remember from "},{"type":"element","tagName":"a","properties":{"href":"/part-6/1-reading-files#reading-csv-files"},"children":[{"type":"text","value":"part 6"}]},{"type":"text","value":" that the string method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":" returns 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":"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":"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":" "},{"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":" "},{"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":"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":"\nfirst"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" second"},{"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":"rest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_list\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":"first"},{"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":"second"},{"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":"rest"},{"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":"1\n2\n[3, 4, 5]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" in front of the variable name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rest"}]},{"type":"text","value":" in the assignment statement means that this last variable should contain all the remaining items in the list, from the third one onwards."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We should absolutely test the file handler separately before including it in our application:"}]},{"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":"t "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" FileHandler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"phonebook.txt\""}]},{"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":"t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load_file"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'Eric': ['02-1234567', '045-4356713'], 'Emily': ['040-324344']}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As the file handler seems to work fine, we can add it to our application. Let's assume we want to read the file first thing every time the program is run. The logical place for reading the file would be the constructor of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":" class:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBook"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__filehandler "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" FileHandler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"phonebook.txt\""}]},{"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":"# add the names and numbers from the file to the phone book"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" numbers "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__filehandler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load_file"},{"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":"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":"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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","comment"]},"children":[{"type":"text","value":"# the rest of the program"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This functionality should also be tested. Once we've made certain the contents of the file are accessible through the user interface of our application, we can move on to the next stage."}]},{"type":"element","tagName":"h2","properties":{"id":"step-4-export-data-to-a-file","style":"position:relative;"},"children":[{"type":"text","value":"Step 4: export data to a file"},{"type":"element","tagName":"a","properties":{"href":"#step-4-export-data-to-a-file","ariaLabel":"step 4 export data to a file 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 final feature in our basic version of the application is saving the contents of the phone book back in the same file the data was read from."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This involves a change to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBook"}]},{"type":"text","value":" class. We need to be able to export the contents of the phone book:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBook"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__persons "},{"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","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# return all entries (in dictionary format)"}]},{"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":"all_entries"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__persons"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The actual saving to the file should be handled by the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"FileHandler"}]},{"type":"text","value":" class. Let's add the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"save_file"}]},{"type":"text","value":" which takes a dictionary representation of the phone book as its argument:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"FileHandler"}]},{"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 "},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" filename"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__filename "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" filename\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":"load_file"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# ..."}]},{"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":"save_file"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" phonebook"},{"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":"dict"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__filename"},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" f"},{"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":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" numbers "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" phonebook"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n line "},{"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":"text","value":"name"},{"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":" numbers\n f"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"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":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"line"},{"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":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The saving should happen as the program exits. Let's add a method for this purpose to the user interface, and call it before breaking out of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop:"}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"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":"# the rest of the code for the user interface"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# a method which gets executed as the program exits"}]},{"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":"exit"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__filehandler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"save_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"all_entries"},{"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\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":"execute"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"help"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n command "},{"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":"\"command: \""}]},{"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":"if"}]},{"type":"text","value":" command "},{"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":"\"0\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"exit"},{"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":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" command "},{"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":"\"1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_entry"},{"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":"elif"}]},{"type":"text","value":" command "},{"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":"\"2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"search"},{"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":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"help"}]},{"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":"Phone book expansion, version 1","tmcname":"part10-10_phone_book_v1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will create a small expansion to the phone book application. The code from the above example is in the exercise template. Please add a command which lets the user search the phone book by number. After the addition the application should work as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"commands:\n0 exit\n1 add entry\n2 search\n3 search by number"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Eric"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"02-123456"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Eric"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"045-4356713"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"02-123456"}]},{"type":"text","value":"\nEric"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0100100"}]},{"type":"text","value":"\nunknown number"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement this addition with respect to the current structure of the program. This means that in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":" class you should add an appropriate helper method to allow for the new functionality, and also add a new branch to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop. In the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBook"}]},{"type":"text","value":" class you should add a method which allows for searching with a number."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" as you test your program and end up with lots of different numbers stored in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"phonebook.txt"}]},{"type":"text","value":" file, there is a chance the local tests will not be passed if conflicting data is read from the file as the app starts. You can try emptying the contents of any "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"phonebook.txt"}]},{"type":"text","value":" files you find in the directory for the exercise "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"before"}]},{"type":"text","value":" running the local tests. Which one of the files is used by your program may depend on your Visual Studio Code settings. Please have a look at the explanation in "},{"type":"element","tagName":"a","properties":{"href":"/part-6/1-reading-files#what-if-visual-studio-code-cannot-find-my-file"},"children":[{"type":"text","value":"part 6"}]},{"type":"text","value":". If your solution is correct, the tests on the server should still pass normally, regardless."}]}]},{"type":"element","tagName":"h2","properties":{"id":"objects-in-a-dictionary","style":"position:relative;"},"children":[{"type":"text","value":"Objects in a dictionary"},{"type":"element","tagName":"a","properties":{"href":"#objects-in-a-dictionary","ariaLabel":"objects in a dictionary 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 the next exercise you are asked to change your phone book so that the values in the dictionary are "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"objects"}]},{"type":"text","value":", not lists."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There is nothing intrinsically strange about this, but this is the first time on this course that something like this is suggested, so let's go through a simpler example before diving into the exercise."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here we have an application which keeps track of how many exercises students have completed on a course. Each student's exercise count is stored in a simple object:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"ExerciseCounter"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__exercises "},{"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":"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":"done"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__exercises "},{"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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"how_many"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__exercises"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following main function uses the above class:"}]},{"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":"students "},{"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":"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":"\"let's do some exercises\""}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n name "},{"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":"\"student: \""}]},{"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":"if"}]},{"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"},{"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":"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":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# create a new object if it doesn't exist yet"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"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":" ExerciseCounter"},{"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","comment"]},"children":[{"type":"text","value":"# add a new done exercise to the counter"}]},{"type":"text","value":"\n students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"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":"done"},{"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":"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"},{"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":"\"exercises completed:\""}]},{"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":" student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exercises "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" students"},{"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":"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'s exercises: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"exercises"},{"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":"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":"p","properties":{},"children":[{"type":"text","value":"Running the above could look like this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"let's do some exercises\nstudent: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"peter"}]},{"type":"text","value":"\nstudent: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sarah"}]},{"type":"text","value":"\nstudent: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"andy"}]},{"type":"text","value":"\nstudent: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sarah"}]},{"type":"text","value":"\nstudent: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"charlotte"}]},{"type":"text","value":"\nstudent: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"charlotte"}]},{"type":"text","value":"\nstudent: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"andy"}]},{"type":"text","value":"\nstudent: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sarah"}]},{"type":"text","value":"\nstudent:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"exercises completed:\npeter's exercises: 1\nandy's exercises: 2\nsarah's exercises: 3\ncharlotte's exercises: 2"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are a couple of things to consider in the above example. When the user inputs a name, the program first checks if the name is already a key in the dictionary. If the name is not present, a new object is created and added as an entry in the dictionary:"}]},{"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":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"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":" ExerciseCounter"},{"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":"After this we can be "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"sure"}]},{"type":"text","value":" the object exists, attached to the name of the student which is used as the key. Either it was just created, or it already existed from a previous iteration of the loop. Either way, we can now retrieve the object with the key, and call the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"done"}]},{"type":"text","value":":"}]},{"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":"students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"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":"done"},{"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":"The above line actually contains two separate events. We could just as well use a helper variable and write it on two separate lines of code:"}]},{"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":"students_counter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nstudents_counter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"done"},{"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":"NB: Even though the object is here assigned to a helper variable, the object still exists in the dictionary just as before. The helper variable contains a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reference"}]},{"type":"text","value":" to the object in the dictionary."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you are not quite sure what actually happens in the code above, please do try it out with the "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Phone book expansion, version 2","tmcname":"part10-11_phone_book_v2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will create another version of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":". You will add addresses to the data which can be attached to a name. For simplicity's sake the functionality for saving to file has been removed, and some other methods have been renamed to better accommodate the change."}]},{"type":"element","tagName":"h2","properties":{"id":"a-separate-class-for-a-persons-data","style":"position:relative;"},"children":[{"type":"text","value":"A separate class for a person's data"},{"type":"element","tagName":"a","properties":{"href":"#a-separate-class-for-a-persons-data","ariaLabel":"a separate class for a persons data 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":"Please change the way the data of a person is handled. Implement a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":", which takes care of the phone numbers and addresses of persons. The class should work as follows:"}]},{"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":"person "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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"},{"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":"person"},{"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","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":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"address"},{"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":"\nperson"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"040-123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nperson"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_address"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mannerheimintie 10 Helsinki\""}]},{"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":"person"},{"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","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":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"address"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eric\n[]\nNone\n['040-123456']\nMannerheimintie 10 Helsinki"}]}]},{"type":"element","tagName":"h2","properties":{"id":"phonebook-uses-the-class-person","style":"position:relative;"},"children":[{"type":"text","value":"PhoneBook uses the class Person"},{"type":"element","tagName":"a","properties":{"href":"#phonebook-uses-the-class-person","ariaLabel":"phonebook uses the class person 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":"Please change the internal implementation of your application so that your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBook"}]},{"type":"text","value":" class uses objects of class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" to store the data in the phone book. That is, the attribute "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__persons"}]},{"type":"text","value":" should still contain a dictionary, but the values should be Person-objects and not lists. The user of your application should notice no difference; the changes must not affect the user interface."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"WARNING:"}]},{"type":"text","value":" whenever you make structural changes to your code, as described in this exercise, always take baby steps and test at every possible stage. Do not try and make all the changes at once. That is a sure way of "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"running into serious problems with your code"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A suitable first step might be to write some code for checking the functionality of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBook"}]},{"type":"text","value":" class directly. For example, the following should at least not cause any errors:"}]},{"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":"phonebook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBook"},{"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":"\nphonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"\"02-123456\""}]},{"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":"phonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_entry"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"phonebook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_entry"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"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 new name for the method for fetching an entry from the phone book. The automatic tests do not check what the printout from your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"get_entry"}]},{"type":"text","value":" method is, but make sure no errors are raised by the above code, and that the result makes sense within your implementation."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When you've made the necessary changes in your program and have absolutely verified the functionality within the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBook"}]},{"type":"text","value":" class, you can move on to the user interface, and see if everything still works as expected."}]},{"type":"element","tagName":"h2","properties":{"id":"adding-an-address","style":"position:relative;"},"children":[{"type":"text","value":"Adding an address"},{"type":"element","tagName":"a","properties":{"href":"#adding-an-address","ariaLabel":"adding an address 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":"Please implement the functionality for adding an address to an entry in your phone book. The program should work as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"commands:\n0 exit\n1 add number\n2 search\n3 add address"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Eric"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"02-123456"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Emily"}]},{"type":"text","value":"\naddress: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Viherlaaksontie 7, Espoo"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Eric"}]},{"type":"text","value":"\n02-123456\naddress unknown"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Emily"}]},{"type":"text","value":"\nnumber unknown\nViherlaaksontie 7, Espoo"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Eric"}]},{"type":"text","value":"\naddress: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Linnankatu 75, Turku"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: 2\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Eric"}]},{"type":"text","value":"\n02-123456\nLinnankatu 75, Turku"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Wilhelm"}]},{"type":"text","value":"\naddress unknown\nnumber unknown"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"WARNING and hint:"}]},{"type":"text","value":" as stated above in the previous exercise, do not try and make all the changes at once. That is a sure way of "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"running into serious problems with your code"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First make sure your can reliably add addresses using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBook"}]},{"type":"text","value":" class directly. Once you have verified this, you can move on to the necessary changes in the user interface."}]}]},{"type":"element","tagName":"h2","properties":{"id":"some-concluding-remarks","style":"position:relative;"},"children":[{"type":"text","value":"Some concluding remarks"},{"type":"element","tagName":"a","properties":{"href":"#some-concluding-remarks","ariaLabel":"some concluding remarks 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 structure of the PhoneBook example above follows the fundamental principles of object oriented programming rather well. The central tenet is to identify the different responsibilities in the program and divide these logically among the different classes and methods. One of the main motivations for this division is managing complexity. Another important motive is that a logical division of responsibilities - modularity, in professional terminology - often makes the code easier to maintain and expand upon."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the software packages being developed and used in the wider world, the overwhelmingly most expensive facet of development is maintenance and expansion, meaning debugging existing software and implementing new features. Correctly implemented modularity is financially a very important feature in software development."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are some more object oriented programming principles worth highlighting here. The PhoneBook is a good example of how the core application logic can (and should) be separated from "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"both"}]},{"type":"text","value":" the user interface "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" any data storage facilities. This is important for a couple of different reasons. First, this separation makes it possible to test the code in smaller units, one class and method at a time. Second, as the core logic is now independent of the interfaces to the outside world, it is possible to change, to an extent, the implementation of either the core logic or the interfaces, without breaking the entire application."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The file handling process in the PhoneBook application proceeds as follows: the program reads the file just once, as it starts up. After this all data is stored in variables in the program. When the program exits, it again stores all data, in practice overwriting the file. In most cases this is the recommended way to deal with external files, as editing the data in situ is often much more complicated."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are many good guidebooks for learning about good programming practices. One such is "},{"type":"element","tagName":"a","properties":{"href":"https://www.amazon.com/Clean-Code-Handbook-Software-Craftsmanship/dp/0132350882","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Clean Code"}]},{"type":"text","value":" by Robert Martin. The code examples in the book are implemented in Java, however, so working through the examples can be quite cumbersome at this point in your programming career, although the book itself is much recommended by the course staff. The themes of easily maintained, expandable, good quality code will be further explored on the courses\n"},{"type":"element","tagName":"a","properties":{"href":"https://studies.helsinki.fi/courses/cu/hy-CU-118024742-2020-08-01","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Software Development Methods"}]},{"type":"text","value":" and "},{"type":"element","tagName":"a","properties":{"href":"https://studies.helsinki.fi/courses/cu/hy-CU-118024909-2020-08-01","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Software Engineering"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Writing code according to established object oriented programming principles comes at a price. You will likely end up writing more code than you would, were you to write your implementation in one continuous bout of spaghetti code. One of the key skills of a porgrammer is to decide the best approach for each situation. Sometimes it is necessary to just hack something together quickly for immediate use. On the other hand, if in the foreseeable future it can be expected that the code will be reused, maintained or futher developed, either by you or, more critically, by someone else entirely, the readability and logical modularity of the program code become essential. More often than not, if it is worth doing, it is worth doing well, even in the very early stages of development."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To finish off this part of the material you will implement one more larger application."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"CourseRecords","tmcname":"part10-12_course_records"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write an interactive application for keeping track of your studies. The internal structure is up to you, but this would be a good opportunity to practice creating a similar structure as in the PhoneBook example above."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your program should work as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 add course\n2 get course data\n3 statistics\n0 exit"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\ngrade: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\ncredits: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\nItP (5 cr) grade 3"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\ngrade: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\ncredits: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\nItP (5 cr) grade 5"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\ngrade: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ncredits: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItP"}]},{"type":"text","value":"\nItP (5 cr) grade 5"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Introduction to Java"}]},{"type":"text","value":"\nno entry for this course"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ACiP"}]},{"type":"text","value":"\ngrade: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ncredits: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ItAI"}]},{"type":"text","value":"\ngrade: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\ncredits: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Algo101"}]},{"type":"text","value":"\ngrade: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\ncredits: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ncourse: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"CompModels"}]},{"type":"text","value":"\ngrade: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\ncredits: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n5 completed courses, a total of 29 credits\nmean 3.4\ngrade distribution\n5: xx\n4: x\n3:\n2: x\n1: x"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each course name should result in a single entry in the records. A grade may be raised by re-entering the course details, but the grade should never be lowered."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This exercise is worth two exercise points. The first is granted after the commands 1, 2 and 0 work correctly in your program. The second is granted if command 3 also works as expected."}]}]},{"type":"element","tagName":"h2","properties":{"id":"epilogue","style":"position:relative;"},"children":[{"type":"text","value":"Epilogue"},{"type":"element","tagName":"a","properties":{"href":"#epilogue","ariaLabel":"epilogue 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":"To finish off this part of the material, let's return to the user interface of the phone book example for a moment."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBook"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__filehandler "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" FileHandler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"phonebook.txt\""}]},{"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":"# the rest of the program"}]},{"type":"text","value":"\n\napplication "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBookApplication"},{"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":"\napplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"execute"},{"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":"A "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":" object contains both a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBook"}]},{"type":"text","value":" object and a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"FileHandler"}]},{"type":"text","value":" object. The name of the file passed to the FileHandler is, at the moment, hard-coded into the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":" class. This is a completely irrelevant detail when it comes to the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"user interface"}]},{"type":"text","value":" of the application. In fact, it breaks the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"separation of concerns"}]},{"type":"text","value":" principle: where a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBook"}]},{"type":"text","value":" object saves its contents should be of no concern to a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":", yet if we wanted to change the location, we'd have to change the code of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It would be a better idea to create a FileHandler object somewhere "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"outside"}]},{"type":"text","value":" the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":" class, and pass it as an argument to the application:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" storage_service"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBook"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__storage_service "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" storage_service\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the rest of the user interface"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# create a FileHandler"}]},{"type":"text","value":"\nstorage_service "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" FileHandler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"phonebook.txt\""}]},{"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":"# pass it as an argument to PhoneBookApplication's constructor"}]},{"type":"text","value":"\napplication "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBookApplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"storage_service"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"execute"},{"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":"This removes an "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"unnecessary dependency"}]},{"type":"text","value":" from the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":" class. If the name of the file changes, the user interface no longer needs to be changed. We just need to pass a different argument to the constructor:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" filename"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phonebook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBook"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__filename "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" filename\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the rest of the user interface"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# use a different filename"}]},{"type":"text","value":"\nstorage_service "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" FileHandler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"new_phonebook.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplication "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBookApplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"storage_service"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"execute"},{"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":"This change also allows us to consider more exotic storage locations, for instance, a cloud service on the internet. We just need to implement a class which uses the cloud service, and offers "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneBookApplication"}]},{"type":"text","value":" the exact same methods as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"FileHandler"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An instance of this new \"cloud handler\" class can be passed as an argument to the constructor, and not a single line of code has to be changed in the user interface:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"CloudHandler"}]},{"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":"# code for saving the contents of the phone book"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# in a cloud service on the internet"}]},{"type":"text","value":"\n\nstorage_service "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CloudHandler"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"amazon-cloud\""}]},{"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":"\"username\""}]},{"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":"\"passwrd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplication "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneBookApplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"storage_service"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\napplication"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"execute"},{"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":"As you have seen before, using techniques like this carries a price tag, as there is more code to write, so a programmer needs to consider whether that is an acceptable tradeoff."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The technique outlined above is called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"dependency injection"}]},{"type":"text","value":". As the name implies, the idea is to provide any dependency required by an object from "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"outside"}]},{"type":"text","value":" the object. It is a very useful tool in a programmer's toolbox, as it makes it easier to implement new features in programs and facilitates automatic testing. This theme will be further explored on the aforementioned courses "},{"type":"element","tagName":"a","properties":{"href":"https://studies.helsinki.fi/courses/cu/hy-CU-118024742-2020-08-01","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Software Development Methods"}]},{"type":"text","value":" and "},{"type":"element","tagName":"a","properties":{"href":"https://studies.helsinki.fi/courses/cu/hy-CU-118024909-2020-08-01","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Software Engineering"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this part of the course."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434"},"children":[]}]}]},"html":"

    After this section

    Thus far in this course material we have covered a large number of Python features.

    The Introduction to Programming course introduced control structures, such as while and for, functions, and basic data structures, such as lists, tuples and dictionaries. In principle, those tools are all that is needed to express anything a programmer may wish to express with Python.

    On this Advanced Course in Programming, beginning in part 8 of the material, you have become familiar with classes and objects. Let's take a moment to consider when and why they are necessary, if those basic tools from parts 1 to 7 should be enough.

    Managing complexity

    Objects and classes are by no means necessary in every programming context. For example, if you are programming a smallish script for one-time use, objects are usually surplus to requirement. However, when you are programming something larger and more complicated, objects become very useful.

    When programs grow in complexity, the amount of details quickly becomes unmanageable, unless the program is organised in some systematic way. Even some of the more complicated exercises on this course so far would have benefited from the examples set in this part of the material.

    Fo decades the concept of separation of concerns has been one of the central principles in programming, and the larger field of computer science. Quoting from Wikipedia:

    Separation of concerns is a design principle for separating a computer program into distinct sections such that each section addresses a separate concern. A concern is a set of information that affects the code of a computer program.

    Separating the program into sections, so that each has its own concern to handle, helps in managing the inevitable complexity of a computer program.

    Functions are one way of organising a program into distinct, manageable wholes. Instead of writing a single script, the idea is to formulate small, separately verifiable functions which each solve some part of the larger problem.

    Another common approach to managing larger programs is objects, through object oriented programming principles. There are benefits and drawbacks to both approaches, and each programmer has their own favourite. As we have seen so far, objects and classes allow us to collect all the data and the code processing that data within a single unit, in the attributes and methods of an object. Furthermore, objects provide a way of encapsulating the data they control, so that other parts of the program do not have to worry about the internal details of an object.

    A worked example: phone book

    How should a program be divided into classes and objects? This is by no means an easy question with a single acceptable answer, so we will proceed with an example. In part five you completed a phone book application, and now we will implement something similar using object oriented programming principles.

    Following the separation of concerns principle, a program should be divided into sections which each have their own cause to take care of. In object oriented programming this translates to the single-responsibility principle. Without going into the finer details, its fundamental purpose is clear from the name: a single class and objects created based on it should have a single responsibility in the program.

    Object oriented programming is often used as a way of modelling real world objects and phenomena. A single object in the real world is modelled with a single class in program code. In the case of a phone book such objects might be

    A name and a phone number may be thought of as just bits of data which do not merit their own classes, but a person is a distinct physical entity in the real world, and in the programming world it could work as a class. A Person object would be responsible for tying together a name and the phone numbers attached to it.

    A phone book itself could be a good candidate for a class. Its responsibility would be to manage different person objects and the data they contain.

    Now we have outlined the core of our application: phone book and person constitute the programming logic of our application, or application logic in short. Our application would need some other classes, too.

    It is usually a good idea to keep any interaction with a user separate from the application logic. It is, after all, a responsibility all on its own. In addition to the core application logic, our program should therefore contain a class which handles the user interface.

    Furthermore, our phone book should have some means of persistent storage between executions. File handling is, again, a clearly separate responsibility, so it deserves a class of its own.

    Now that we have an outline of the basic components of our program, the question arises: where should we begin programming? Again, there is no right or wrong answer, but it is often a good idea to start with some part of the application logic.

    Step 1: an outline for the application logic

    Let's start with the class PhoneBook. A skeleton implementation could look like this:

    class PhoneBook:\n    def __init__(self):\n        self.__persons = []\n\n    def add_number(self, name: str, number: str):\n        pass\n\n    def get_numbers(self, name: str):\n        pass\n

    This class consists of a list of persons along with methods for both adding and fetching data.

    Each person may be connected with multiple numbers, so let's implement the internal structure of persons with a dictionary. A dictionary allows us to search for keys by name, and the value attached to a dictionary key can be a list. So far it looks like we don't really need a separate class to represent a person - an entry in a dictionary will do.

    Let's implement the methods listed above, and test our phone book:

    class PhoneBook:\n    def __init__(self):\n        self.__persons = {}\n\n    def add_number(self, name: str, number: str):\n        if not name in self.__persons:\n            # add a new dictionary entry with an empty list for the numbers\n            self.__persons[name] = []\n\n        self.__persons[name].append(number)\n\n    def get_numbers(self, name: str):\n        if not name in self.__persons:\n            return None\n\n        return self.__persons[name]\n\n# code for testing\nphonebook = PhoneBook()\nphonebook.add_number(\"Eric\", \"02-123456\")\nprint(phonebook.get_numbers(\"Eric\"))\nprint(phonebook.get_numbers(\"Emily\"))

    This should print out the following:

    ['02-123456']\nNone

    The method get_numbers returns None if a name is not included in the phone book. If the name is found, it returns the list of numbers attached to the name.

    Whenever you make changes to a program it is always worth testing that the code works as expected, before moving on to any other changes. The code used for testing is usually something that is deleted soon after, and as such you might think it's not worth the trouble to write any tests in the first place. In most cases this is not true. Testing is essential to good programming results.

    A bug in the program should be caught and fixed as soon as possible. If you get into the habit of verifying the functionality of practically every new line of code, you will find that the bugs are usually easy to locate and fix, as you can be quite certain that the bug was caused by the most recent change. If you only test the program after adding dozens of lines of code, the potential sources for bugs also increase by dozens of times.

    Step 2: an outline for the user interface

    With the core application logic out of the way, it is time to implement a text-based user interface. We will need a new class, PhoneBookApplication, with the following initial functionality:

    class PhoneBookApplication:\n    def __init__(self):\n        self.__phonebook = PhoneBook()\n\n    def help(self):\n        print(\"commands: \")\n        print(\"0 exit\")\n\n    def execute(self):\n        self.help()\n        while True:\n            print(\"\")\n            command = input(\"command: \")\n            if command == \"0\":\n                break\n\napplication = PhoneBookApplication()\napplication.execute()

    This program doesn't do very much yet, but let's go through the contents. The constructor method creates a new PhoneBook, which is stored in a private attribute. The method execute(self) starts the program's text-based user interface, the core of which is the while loop, which keeps asking the user for commands until they type in the command for exiting. There is also a method for intructions, help(self), which is called before entering the loop, so that the instructions are printed out.

    Now, let's add some actual functionality. First, we implement adding new data to the phone book:

    class PhoneBookApplication:\n    def __init__(self):\n        self.__phonebook = PhoneBook()\n\n    def help(self):\n        print(\"commands: \")\n        print(\"0 exit\")\n        print(\"1 add entry\")\n\n    def execute(self):\n        self.help()\n        while True:\n            print(\"\")\n            command = input(\"command: \")\n            if command == \"0\":\n                break\n            elif command == \"1\":\n                name = input(\"name: \")\n                number = input(\"number: \")\n                self.__phonebook.add_number(name, number)\n\napplication = PhoneBookApplication()\napplication.execute()

    If the user types in 1 for adding a new number, the user interface asks for a name and a number, and adds these to the PhoneBook using the appropriate method defined in the class.

    The only responsibility of the user interface is to communicate with the user. Any other functionality, such as storing a new name-number pair, is the responsibility of the PhoneBook object.

    There is room for improvement in the structure of our user interface class. Let's create a method add_entry(self) which handles the command for adding a new entry:

    class PhoneBookApplication:\n    def __init__(self):\n        self.__phonebook = PhoneBook()\n\n    def help(self):\n        print(\"commands: \")\n        print(\"0 exit\")\n        print(\"1 add entry\")\n\n    # separation of concerns in action: a new method for adding an entry\n    def add_entry(self):\n        name = input(\"name: \")\n        number = input(\"number: \")\n        self.__phonebook.add_number(name, number)\n\n    def execute(self):\n        self.help()\n        while True:\n            print(\"\")\n            command = input(\"command: \")\n            if command == \"0\":\n                break\n            elif command == \"1\":\n                self.add_entry()\n\napplication = PhoneBookApplication()\napplication.execute()

    The separation of concerns principle extends to the level of methods, too. We could have the entire functionality of the user interface in a single complicated while loop, but it is better to separate each functionality into its own method. The responsibility of the execute() method is just delegating the commands typed in by the user to the relevant methods. This helps with managing the growing complexity of our program. For example, if we want to later change the way adding entries works, it is immediately clear that we must then focus our efforts on the add_entry() method.

    Let's include functionality for searching for entries in our user interface. This should have its own method, too:

    class PhoneBookApplication:\n    def __init__(self):\n        self.__phonebook = PhoneBook()\n\n    def help(self):\n        print(\"commands: \")\n        print(\"0 exit\")\n        print(\"1 add entry\")\n        print(\"2 search\")\n\n    def add_entry(self):\n        name = input(\"name: \")\n        number = input(\"number: \")\n        self.__phonebook.add_number(name, number)\n\n    def search(self):\n        name = input(\"name: \")\n        numbers = self.__phonebook.get_numbers(name)\n        if numbers == None:\n            print(\"number unknown\")\n            return\n        for number in numbers:\n            print(number)\n\n    def execute(self):\n        self.help()\n        while True:\n            print(\"\")\n            command = input(\"command: \")\n            if command == \"0\":\n                break\n            elif command == \"1\":\n                self.add_entry()\n            elif command == \"2\":\n                self.search()\n            else:\n                self.help()\n\napplication = PhoneBookApplication()\napplication.execute()

    We now have a simple working phone book application ready for testing. The following is an example run:

    commands:\n0 exit\n1 add entry\n2 search

    command: 1\nname: Eric\nnumber: 02-123456

    command: 1\nname: Eric\nnumber: 045-4356713

    command: 2\nname: Eric\n02-123456\n045-4356713

    command: 2\nname: Emily\nnumber unknown

    command: 0

    For such a simple application we have written quite a lot of code. If we'd written it all within the one while loop we could probably have gotten away with a lot less code. It is, however, quite easy to read the code, the structure is clear, and we should have no trouble adding new features.

    Step 3: importing data from a file

    Let's assume we already have some phone numbers stored in a file, and we want to read this as the program starts up. The data file is in the following CSV format:

    Eric;02-1234567;045-4356713\nEmily;040-324344

    Handling files is clearly its own area of responsibility, so it merits a class of its own:

    class FileHandler():\n    def __init__(self, filename):\n        self.__filename = filename\n\n    def load_file(self):\n        names = {}\n        with open(self.__filename) as f:\n            for line in f:\n                parts = line.strip().split(';')\n                name, *numbers = parts\n                names[name] = numbers\n\n        return names

    The constructor method takes the name of the file as its argument. The method load_file(self) reads the contents of the file. It splits each line into two parts: a name and a list of numbers. It then adds these to a dictionary, using the name as the key and the list as the value.

    The method uses a nifty Python feature: it is possible to first select some items from a list separately, and then take the rest of the items in a new list. You can see an example of this below. You may remember from part 6 that the string method split returns a list.

    my_list = [1, 2, 3, 4, 5]\nfirst, second, *rest = my_list\nprint(first)\nprint(second)\nprint(rest)

    1\n2\n[3, 4, 5]

    The * in front of the variable name rest in the assignment statement means that this last variable should contain all the remaining items in the list, from the third one onwards.

    We should absolutely test the file handler separately before including it in our application:

    t = FileHandler(\"phonebook.txt\")\nprint(t.load_file())

    {'Eric': ['02-1234567', '045-4356713'], 'Emily': ['040-324344']}

    As the file handler seems to work fine, we can add it to our application. Let's assume we want to read the file first thing every time the program is run. The logical place for reading the file would be the constructor of the PhoneBookApplication class:

    class PhoneBookApplication:\n    def __init__(self):\n        self.__phonebook = PhoneBook()\n        self.__filehandler = FileHandler(\"phonebook.txt\")\n\n        # add the names and numbers from the file to the phone book\n        for name, numbers in self.__filehandler.load_file().items():\n            for number in numbers:\n                self.__phonebook.add_number(name, number)\n\n    # the rest of the program

    This functionality should also be tested. Once we've made certain the contents of the file are accessible through the user interface of our application, we can move on to the next stage.

    Step 4: export data to a file

    The final feature in our basic version of the application is saving the contents of the phone book back in the same file the data was read from.

    This involves a change to the PhoneBook class. We need to be able to export the contents of the phone book:

    class PhoneBook:\n    def __init__(self):\n        self.__persons = {}\n\n    # ...\n\n    # return all entries (in dictionary format)\n    def all_entries(self):\n        return self.__persons

    The actual saving to the file should be handled by the FileHandler class. Let's add the method save_file which takes a dictionary representation of the phone book as its argument:

    class FileHandler():\n    def __init__(self, filename):\n        self.__filename = filename\n\n    def load_file(self):\n        # ...\n\n    def save_file(self, phonebook: dict):\n        with open(self.__filename, \"w\") as f:\n            for name, numbers in phonebook.items():\n                line = [name] + numbers\n                f.write(\";\".join(line) + \"\\n\")

    The saving should happen as the program exits. Let's add a method for this purpose to the user interface, and call it before breaking out of the while loop:

    \nclass PhoneBookApplication:\n    # the rest of the code for the user interface\n\n    # a method which gets executed as the program exits\n    def exit(self):\n        self.__filehandler.save_file(self.__phonebook.all_entries())\n\n    def execute(self):\n        self.help()\n        while True:\n            print(\"\")\n            command = input(\"command: \")\n            if command == \"0\":\n\n                self.exit()\n                break\n            elif command == \"1\":\n                self.add_entry()\n            elif command == \"2\":\n                self.search()\n            else:\n                self.help()

    In this exercise you will create a small expansion to the phone book application. The code from the above example is in the exercise template. Please add a command which lets the user search the phone book by number. After the addition the application should work as follows:

    commands:\n0 exit\n1 add entry\n2 search\n3 search by number

    command: 1\nname: Eric\nnumber: 02-123456

    command: 1\nname: Eric\nnumber: 045-4356713

    command: 3\nnumber: 02-123456\nEric

    command: 3\nnumber: 0100100\nunknown number

    command: 0

    Please implement this addition with respect to the current structure of the program. This means that in the PhoneBookApplication class you should add an appropriate helper method to allow for the new functionality, and also add a new branch to the while loop. In the PhoneBook class you should add a method which allows for searching with a number.

    NB: as you test your program and end up with lots of different numbers stored in the phonebook.txt file, there is a chance the local tests will not be passed if conflicting data is read from the file as the app starts. You can try emptying the contents of any phonebook.txt files you find in the directory for the exercise before running the local tests. Which one of the files is used by your program may depend on your Visual Studio Code settings. Please have a look at the explanation in part 6. If your solution is correct, the tests on the server should still pass normally, regardless.

    Objects in a dictionary

    In the next exercise you are asked to change your phone book so that the values in the dictionary are objects, not lists.

    There is nothing intrinsically strange about this, but this is the first time on this course that something like this is suggested, so let's go through a simpler example before diving into the exercise.

    Here we have an application which keeps track of how many exercises students have completed on a course. Each student's exercise count is stored in a simple object:

    class ExerciseCounter:\n    def __init__(self):\n        self.__exercises = 0\n\n    def done(self):\n        self.__exercises += 1\n\n    def how_many(self):\n        return self.__exercises

    The following main function uses the above class:

    students = {}\n\nprint(\"let's do some exercises\")\nwhile True:\n    name = input(\"student: \")\n    if len(name) == 0:\n        break\n\n    # create a new object if it doesn't exist yet\n    if not name in students:\n        students[name] = ExerciseCounter()\n\n    # add a new done exercise to the counter\n    students[name].done()\n\nprint()\nprint(\"exercises completed:\")\n\nfor student, exercises in students.items():\n    print(f\"{student}'s exercises: {exercises.how_many()}\")

    Running the above could look like this:

    let's do some exercises\nstudent: peter\nstudent: sarah\nstudent: andy\nstudent: sarah\nstudent: charlotte\nstudent: charlotte\nstudent: andy\nstudent: sarah\nstudent:

    exercises completed:\npeter's exercises: 1\nandy's exercises: 2\nsarah's exercises: 3\ncharlotte's exercises: 2

    There are a couple of things to consider in the above example. When the user inputs a name, the program first checks if the name is already a key in the dictionary. If the name is not present, a new object is created and added as an entry in the dictionary:

    if not name in students:\n    students[name] = ExerciseCounter()

    After this we can be sure the object exists, attached to the name of the student which is used as the key. Either it was just created, or it already existed from a previous iteration of the loop. Either way, we can now retrieve the object with the key, and call the method done:

    students[name].done()

    The above line actually contains two separate events. We could just as well use a helper variable and write it on two separate lines of code:

    students_counter = students[name]\nstudents_counter.done()

    NB: Even though the object is here assigned to a helper variable, the object still exists in the dictionary just as before. The helper variable contains a reference to the object in the dictionary.

    If you are not quite sure what actually happens in the code above, please do try it out with the visualisation tool.

    In this exercise you will create another version of the PhoneBookApplication. You will add addresses to the data which can be attached to a name. For simplicity's sake the functionality for saving to file has been removed, and some other methods have been renamed to better accommodate the change.

    A separate class for a person's data

    Please change the way the data of a person is handled. Implement a class named Person, which takes care of the phone numbers and addresses of persons. The class should work as follows:

    person = Person(\"Eric\")\nprint(person.name())\nprint(person.numbers())\nprint(person.address())\nperson.add_number(\"040-123456\")\nperson.add_address(\"Mannerheimintie 10 Helsinki\")\nprint(person.numbers())\nprint(person.address())

    Eric\n[]\nNone\n['040-123456']\nMannerheimintie 10 Helsinki

    PhoneBook uses the class Person

    Please change the internal implementation of your application so that your PhoneBook class uses objects of class Person to store the data in the phone book. That is, the attribute __persons should still contain a dictionary, but the values should be Person-objects and not lists. The user of your application should notice no difference; the changes must not affect the user interface.

    WARNING: whenever you make structural changes to your code, as described in this exercise, always take baby steps and test at every possible stage. Do not try and make all the changes at once. That is a sure way of running into serious problems with your code.

    A suitable first step might be to write some code for checking the functionality of the PhoneBook class directly. For example, the following should at least not cause any errors:

    phonebook = PhoneBook()\nphonebook.add_number(\"Eric\", \"02-123456\")\nprint(phonebook.get_entry(\"Eric\"))\nprint(phonebook.get_entry(\"Emily\"))

    Notice the new name for the method for fetching an entry from the phone book. The automatic tests do not check what the printout from your get_entry method is, but make sure no errors are raised by the above code, and that the result makes sense within your implementation.

    When you've made the necessary changes in your program and have absolutely verified the functionality within the PhoneBook class, you can move on to the user interface, and see if everything still works as expected.

    Adding an address

    Please implement the functionality for adding an address to an entry in your phone book. The program should work as follows:

    commands:\n0 exit\n1 add number\n2 search\n3 add address

    command: 1\nname: Eric\nnumber: 02-123456

    command: 3\nname: Emily\naddress: Viherlaaksontie 7, Espoo

    command: 2\nname: Eric\n02-123456\naddress unknown

    command: 2\nname: Emily\nnumber unknown\nViherlaaksontie 7, Espoo

    command: 3\nname: Eric\naddress: Linnankatu 75, Turku

    command: 2\nname: Eric\n02-123456\nLinnankatu 75, Turku

    command: 2\nname: Wilhelm\naddress unknown\nnumber unknown

    command: 0

    WARNING and hint: as stated above in the previous exercise, do not try and make all the changes at once. That is a sure way of running into serious problems with your code.

    First make sure your can reliably add addresses using the PhoneBook class directly. Once you have verified this, you can move on to the necessary changes in the user interface.

    Some concluding remarks

    The structure of the PhoneBook example above follows the fundamental principles of object oriented programming rather well. The central tenet is to identify the different responsibilities in the program and divide these logically among the different classes and methods. One of the main motivations for this division is managing complexity. Another important motive is that a logical division of responsibilities - modularity, in professional terminology - often makes the code easier to maintain and expand upon.

    In the software packages being developed and used in the wider world, the overwhelmingly most expensive facet of development is maintenance and expansion, meaning debugging existing software and implementing new features. Correctly implemented modularity is financially a very important feature in software development.

    There are some more object oriented programming principles worth highlighting here. The PhoneBook is a good example of how the core application logic can (and should) be separated from both the user interface and any data storage facilities. This is important for a couple of different reasons. First, this separation makes it possible to test the code in smaller units, one class and method at a time. Second, as the core logic is now independent of the interfaces to the outside world, it is possible to change, to an extent, the implementation of either the core logic or the interfaces, without breaking the entire application.

    The file handling process in the PhoneBook application proceeds as follows: the program reads the file just once, as it starts up. After this all data is stored in variables in the program. When the program exits, it again stores all data, in practice overwriting the file. In most cases this is the recommended way to deal with external files, as editing the data in situ is often much more complicated.

    There are many good guidebooks for learning about good programming practices. One such is Clean Code by Robert Martin. The code examples in the book are implemented in Java, however, so working through the examples can be quite cumbersome at this point in your programming career, although the book itself is much recommended by the course staff. The themes of easily maintained, expandable, good quality code will be further explored on the courses\nSoftware Development Methods and Software Engineering.

    Writing code according to established object oriented programming principles comes at a price. You will likely end up writing more code than you would, were you to write your implementation in one continuous bout of spaghetti code. One of the key skills of a porgrammer is to decide the best approach for each situation. Sometimes it is necessary to just hack something together quickly for immediate use. On the other hand, if in the foreseeable future it can be expected that the code will be reused, maintained or futher developed, either by you or, more critically, by someone else entirely, the readability and logical modularity of the program code become essential. More often than not, if it is worth doing, it is worth doing well, even in the very early stages of development.

    To finish off this part of the material you will implement one more larger application.

    Please write an interactive application for keeping track of your studies. The internal structure is up to you, but this would be a good opportunity to practice creating a similar structure as in the PhoneBook example above.

    Your program should work as follows:

    1 add course\n2 get course data\n3 statistics\n0 exit

    command: 1\ncourse: ItP\ngrade: 3\ncredits: 5

    command: 2\ncourse: ItP\nItP (5 cr) grade 3

    command: 1\ncourse: ItP\ngrade: 5\ncredits: 5

    command: 2\ncourse: ItP\nItP (5 cr) grade 5

    command: 1\ncourse: ItP\ngrade: 1\ncredits: 5

    command: 2\ncourse: ItP\nItP (5 cr) grade 5

    command: 2\ncourse: Introduction to Java\nno entry for this course

    command: 1\ncourse: ACiP\ngrade: 1\ncredits: 10

    command: 1\ncourse: ItAI\ngrade: 2\ncredits: 5

    command: 1\ncourse: Algo101\ngrade: 4\ncredits: 1

    command: 1\ncourse: CompModels\ngrade: 5\ncredits: 8

    command: 3\n5 completed courses, a total of 29 credits\nmean 3.4\ngrade distribution\n5: xx\n4: x\n3:\n2: x\n1: x

    command: 0

    Each course name should result in a single entry in the records. A grade may be raised by re-entering the course details, but the grade should never be lowered.

    This exercise is worth two exercise points. The first is granted after the commands 1, 2 and 0 work correctly in your program. The second is granted if command 3 also works as expected.

    Epilogue

    To finish off this part of the material, let's return to the user interface of the phone book example for a moment.

    class PhoneBookApplication:\n    def __init__(self):\n        self.__phonebook = PhoneBook()\n        self.__filehandler = FileHandler(\"phonebook.txt\")\n\n    # the rest of the program\n\napplication = PhoneBookApplication()\napplication.execute()

    A PhoneBookApplication object contains both a PhoneBook object and a FileHandler object. The name of the file passed to the FileHandler is, at the moment, hard-coded into the PhoneBookApplication class. This is a completely irrelevant detail when it comes to the user interface of the application. In fact, it breaks the separation of concerns principle: where a PhoneBook object saves its contents should be of no concern to a PhoneBookApplication, yet if we wanted to change the location, we'd have to change the code of PhoneBookApplication.

    It would be a better idea to create a FileHandler object somewhere outside the PhoneBookApplication class, and pass it as an argument to the application:

    class PhoneBookApplication:\n    def __init__(self, storage_service):\n        self.__phonebook = PhoneBook()\n        self.__storage_service = storage_service\n\n    # the rest of the user interface\n\n# create a FileHandler\nstorage_service = FileHandler(\"phonebook.txt\")\n# pass it as an argument to PhoneBookApplication's constructor\napplication = PhoneBookApplication(storage_service)\napplication.execute()

    This removes an unnecessary dependency from the PhoneBookApplication class. If the name of the file changes, the user interface no longer needs to be changed. We just need to pass a different argument to the constructor:

    class PhoneBookApplication:\n    def __init__(self, filename):\n        self.__phonebook = PhoneBook()\n        self.__filename = filename\n\n    # the rest of the user interface\n\n# use a different filename\nstorage_service = FileHandler(\"new_phonebook.txt\")\napplication = PhoneBookApplication(storage_service)\napplication.execute()

    This change also allows us to consider more exotic storage locations, for instance, a cloud service on the internet. We just need to implement a class which uses the cloud service, and offers PhoneBookApplication the exact same methods as FileHandler.

    An instance of this new \"cloud handler\" class can be passed as an argument to the constructor, and not a single line of code has to be changed in the user interface:

    class CloudHandler:\n    # code for saving the contents of the phone book\n    # in a cloud service on the internet\n\nstorage_service = CloudHandler(\"amazon-cloud\", \"username\", \"passwrd\")\napplication = PhoneBookApplication(storage_service)\napplication.execute()

    As you have seen before, using techniques like this carries a price tag, as there is more code to write, so a programmer needs to consider whether that is an acceptable tradeoff.

    The technique outlined above is called dependency injection. As the name implies, the idea is to provide any dependency required by an object from outside the object. It is a very useful tool in a programmer's toolbox, as it makes it easier to implement new features in programs and facilitates automatic testing. This theme will be further explored on the aforementioned courses Software Development Methods and Software Engineering.

    Please respond to a quick questionnaire on this part of the course.

    ","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-10/4-application-development.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-10/page-data.json b/page-data/part-10/page-data.json index 099ea42b6..ff5b20c17 100644 --- a/page-data/part-10/page-data.json +++ b/page-data/part-10/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/part-10", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-10","title":"Part 10"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-10/index.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-10","title":"Part 10"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-10/index.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-11/1-list-comprehensions/page-data.json b/page-data/part-11/1-list-comprehensions/page-data.json index ab5cab6bf..4c6e9516e 100644 --- a/page-data/part-11/1-list-comprehensions/page-data.json +++ b/page-data/part-11/1-list-comprehensions/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-11/1-list-comprehensions", - "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 know what list comprehensions are"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use list comprehensions to create new lists"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"One of the situations where programming is at its most powerful is processing sequences of items and events. Computers are good at repeating things. For example, in the previous parts of this material we have been iterating over strings, lists and dictionaries in various ways."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's assume we have a list of integers, and we would need the same list of items in string format. A traditional way of completing the task 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":"text","value":"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"2"}]},{"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":"3"}]},{"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":"6"}]},{"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":"5"}]},{"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":"4"}]},{"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":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nstrings "},{"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","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 strings"},{"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":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"list-comprehensions","style":"position:relative;"},"children":[{"type":"text","value":"List comprehensions"},{"type":"element","tagName":"a","properties":{"href":"#list-comprehensions","ariaLabel":"list comprehensions 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":"There is also a more \"pythonic\" way of generating lists from existing lists. These are called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"list comprehensions"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The idea is to fit on a single line both the description of what should be done to each item on the list, and the assignment of the result to a new list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the above example, the operation performed on each item on the list was very simple: each integer was converted into a string. Let's see what this would look like implemented with a list comprehension:"}]},{"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":"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"2"}]},{"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":"3"}]},{"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":"6"}]},{"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":"5"}]},{"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":"4"}]},{"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":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nstrings "},{"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","builtin"]},"children":[{"type":"text","value":"str"}]},{"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":" "},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The second line above contains many of the same elements as the more traditional iterative apporach, but the syntax is different. One way of generalising a list comprehension statement would be"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[ for in ]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The square brackets around the list comprehension statement signal to Python that the result should be a new list. One by one, each item in the original list is processed, and the result is stored in the new list, just like in the iterative approach above. As a result we have a new list with exactly as many items as were in the original, and all items have been processed in an identical fashion."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(NB: the originals for the images in this part are temporarily missing, which is why there is some Finnish vocabulary in the illustrations in this part. We are working on fixing this.)"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 301px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/3f079cde22be6cf33b7be317492aa108/fb933/11_1_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: 131.7391304347826%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAaCAYAAAC3g3x9AAAACXBIWXMAAAsTAAALEwEAmpwYAAACRUlEQVRIx42V16oCQQyG5/0fyStBRUQUbCCiggWx995y+AJZouyuJxAmk/JvJpnJBkmhx+Mh1WpVCoWCtFot+Q+F7XYrSbzb7eRyucj5fJbT6SRpvsZhNptpJtfrVZBZ2S8WCzkejyqv12vl5/OpOmxxMchhs9lE6XqZ7HCCyA62MmCLi0EOq9UqUiyXS3m/3yqT0e12U5msYAgdNghfYozACvP5XFMdjUZ6FHg8HuuKMzI+MDI6k82fWDDQB/sSGez3e/0SK42ACMBO/ew02LwvsXayYAJER+kUxAqI1ZIggExnPsQYgRX8Brrf7+pIrQ6HgwbAAGIjI2w0gL2P1Qy9wtbX66VgMLWyOgICIHp84mIjwG/mWLySSqUimUxGyuWyNJtN1WGLi4kAPagV2u5dHPmm+LgPQJ82R6P4dDiXy0mpVJJ8Pi/ZbDZqkjXPgyUC4kzBAbULTlPsqOx/AvrUAQMIEDrc7Xal1+upTEOw2bOMPbLvlO80GQ0GA6nVasrI6HyHU7tsZPeNZwaADQdkdNjsDn7Hf7wUvmz3jGDGEmyAtseGD76W7cdLofg4UGiuBAH2lnn42GFkuzbmQwyx2DVDXkC/39cacU2m06leXmQmCKOf1cvY8DGZWDB02vh5ljQP7Rn+mofIqRPbjsHRrAR+2sRO7OFwqA58FRklexu4yJPJRJl62UBFjy8xxLJHDrQ/iX0H/U1Iiwlp/1iuRaPRkHa7LfV6XYrFYvTkEv/LcWPIGkMjOp2OHhFG/p4y3/wHBFfbIcHIViUAAAAASUVORK5CYII='); 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":["/static/3f079cde22be6cf33b7be317492aa108/a0b58/11_1_2.webp 230w","/static/3f079cde22be6cf33b7be317492aa108/c4c4c/11_1_2.webp 301w"],"sizes":"(max-width: 301px) 100vw, 301px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/3f079cde22be6cf33b7be317492aa108/81c8e/11_1_2.png 230w","/static/3f079cde22be6cf33b7be317492aa108/fb933/11_1_2.png 301w"],"sizes":"(max-width: 301px) 100vw, 301px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/3f079cde22be6cf33b7be317492aa108/fb933/11_1_2.png","alt":"11 1 2","title":"11 1 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":"List comprehensions can handle much more complicated operations as well. We can perform calculations, such as multiplying the original items by ten:"}]},{"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":"numbers "},{"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":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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":"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":"10"}]},{"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":"numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nnumbers_multiplied "},{"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":"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":"10"}]},{"type":"text","value":" "},{"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":"numbers_multiplied"},{"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":"[1, 2, 3, 4, 5, 6, 7, 8, 9]\n[10, 20, 30, 40, 50, 60, 70, 80, 90]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In fact, the expression within the list comprehension statement can be any Python expression. You can even call functions you've defined yourself:"}]},{"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":"factorial"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The function calculates the factorial n! for integers above zero \"\"\""}]},{"type":"text","value":"\n k "},{"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":" n "},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" n\n n "},{"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":"return"}]},{"type":"text","value":" k\n\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 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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"2"}]},{"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":"4"}]},{"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":"3"}]},{"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n factorials "},{"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":"text","value":"factorial"},{"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":" "},{"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":"factorials"},{"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":"[120, 2, 24, 6, 1]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With the more familiar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop the same process could be expressed 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":"factorial"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The function calculates the factorial n! for integers above zero \"\"\""}]},{"type":"text","value":"\n k "},{"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":" n "},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" n\n n "},{"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":"return"}]},{"type":"text","value":" k\n\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 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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"2"}]},{"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":"4"}]},{"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":"3"}]},{"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n factorials "},{"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","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 factorials"},{"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":"factorial"},{"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","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":"factorials"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"List comprehensions allow us to express the same functionality more consisely, usually without losing any of the readability."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We can also return a list comprehension statement from a function directly. If we needed a function for producing factorials for lists of numbers, we could achieve it very concisely:"}]},{"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":"factorials"}]},{"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":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"factorial"},{"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":" "},{"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":"element","tagName":"programming-exercise","properties":{"name":"Square roots","tmcname":"part11-01_square_roots"},"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":"square_roots(numbers: list)"}]},{"type":"text","value":" which takes a list of integers as its argument. The function should return a new list containing the square roots of the original integers."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/math.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" module from the Python standard library contains a suitable function for calculating the square root."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should use a list comprehension. The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should work as follows:"}]},{"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":"lines "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" square_roots"},{"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","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","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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lines"},{"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":"line"},{"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":"1.0\n1.4142135623730951\n1.7320508075688772\n2.0"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Rows of stars","tmcname":"part11-02_rows_of_stars"},"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":"rows_of_stars(numbers: list)"}]},{"type":"text","value":" which takes a list of integers as its argument. The function should return a new list containing rows of stars. The length of each row should correspond to the integer at the same index in the original list. The function should use a list comprehension to achieve this."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should work as follows:"}]},{"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":"rows "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rows_of_stars"},{"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","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","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":" row "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" rows"},{"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":"row"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrows "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rows_of_stars"},{"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","number"]},"children":[{"type":"text","value":"4"}]},{"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":"3"}]},{"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":"2"}]},{"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":"1"}]},{"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":"10"}]},{"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":"for"}]},{"type":"text","value":" row "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" rows"},{"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":"row"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"*\n**\n***\n****\n\n****\n***\n**\n*\n**********\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Best exam result","tmcname":"part11-03_best_exam_result"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the class definition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ExamResult"}]},{"type":"text","value":". The class has the following public attributes:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"name"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"grade1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"grade2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"grade3"}]},{"type":"text","value":"\n"}]},{"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":"best_results(results: list)"}]},{"type":"text","value":" which takes a list of ExamResult objects as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return a new list containing only the best result from each ExamResult object. The function should use a list comprehension to achieve this."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should work as follows:"}]},{"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":"result1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ExamResult"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"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":"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":"text","value":"\nresult2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ExamResult"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pippa\""}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresult3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ExamResult"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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":"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":"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresults "},{"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":"text","value":"result1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" result2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" result3"},{"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":"best_results"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"results"},{"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, 4, 3]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lengths","tmcname":"part11-04_lengths"},"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":"lengths(lists: list)"}]},{"type":"text","value":" which takes a list containing lists of integers as its argument. The function should return a new list, containing the lengths of the lists within the argument list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should use a list comprehension to achieve this. The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should work as follows:"}]},{"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":"lists "},{"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":"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","punctuation"]},"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":"324"}]},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"31"}]},{"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":"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":"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"},{"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":"lengths"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lists"},{"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, 4, 0]"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"filtering-items","style":"position:relative;"},"children":[{"type":"text","value":"Filtering items"},{"type":"element","tagName":"a","properties":{"href":"#filtering-items","ariaLabel":"filtering items 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 the examples above all of our lists remained the same length before and after a list comprehension operation. In each case, all the items in the original list were used as the basis of the new list. But sometimes we only need "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"some"}]},{"type":"text","value":" of the original items. How can this be achieved?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A list comprehension statement also allows for a condition, so that we can check the items against the condition and select only those which match. The general syntax is as follows:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[ for in if ]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The statement above is otherwise identical to the general form introduced in the beginning of this section, but now there is an if statement at the end. Only those items from the original list for which the Boolean expression is true are used as the basis of the new list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the example below we select all the even items from the original list as the basis of the new list. In fact, these items are not further processed in any way; they are assigned to the new list as is:"}]},{"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":"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"1"}]},{"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":"2"}]},{"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":"3"}]},{"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":"4"}]},{"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":"6"}]},{"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":"4"}]},{"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":"5"}]},{"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":"7"}]},{"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":"10"}]},{"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":"12"}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\neven_items "},{"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":"text","value":"item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" numbers "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" item "},{"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":"2"}]},{"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":"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":"even_items"},{"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":"[2, 4, 6, 4, 10, 12]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The expression in the list comprehension statement above is just a simple "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"item"}]},{"type":"text","value":", which means that no operations are to be performed on the items in the list. The expression could be any Python expression, just like in the previous examples. For example, the following list comprehension statement takes all the even items in a list, multiplies each by ten, and stores the result in a new 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":"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"1"}]},{"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":"2"}]},{"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":"3"}]},{"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":"4"}]},{"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":"6"}]},{"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":"4"}]},{"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":"5"}]},{"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":"7"}]},{"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":"10"}]},{"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":"12"}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\neven_items "},{"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":"text","value":"item "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" numbers "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" item "},{"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":"2"}]},{"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":"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":"even_items"},{"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":"[20, 40, 60, 40, 100, 120]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you come across more and more complicated list comprehensions, you may find it useful to try reading the condition first. After all, the items are processed only if they pass the test, so it often makes sense to first figure out which items pass the filtering stage. Sometimes the expression in a list comprehension statement would not even be possible for all the items in the original list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, the factorial operation is only defined for non-negative integers. If we can't be sure a list only contains values of zero or above, the contents have to be filtered before passing them on to the factorial function we made before:"}]},{"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":"factorial"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The function calculates the factorial n! for integers above zero \"\"\""}]},{"type":"text","value":"\n k "},{"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":" n "},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" n\n n "},{"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":"return"}]},{"type":"text","value":" k\n\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 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","operator"]},"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":" "},{"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":" "},{"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":" "},{"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":"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":"10"}]},{"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":"5"}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n factorials "},{"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":"text","value":"factorial"},{"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":" "},{"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","keyword"]},"children":[{"type":"text","value":"if"}]},{"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":"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":"factorials"},{"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":"[6, 24, 120, 1]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As we saw in our very first list comprehension example, where integers were converted into strings, the items in the new list do not have to be of the same type as the items in the original list. Continuing from the factorial example above, we can create a tuple from each original item and its processed counterpart, and store these in a list, combining everything we've learned so far in a single list comprehension 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":"factorial"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The function calculates the factorial n! for integers above zero \"\"\""}]},{"type":"text","value":"\n k "},{"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":" n "},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" n\n n "},{"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":"return"}]},{"type":"text","value":" k\n\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 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","operator"]},"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"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":"10"}]},{"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":"5"}]},{"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":"1"}]},{"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":"6"}]},{"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":"# the variable name abbreviated here so that this would be easier to read"}]},{"type":"text","value":"\n factorials "},{"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","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" factorial"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" numbers "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" n "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" n "},{"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":"2"}]},{"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":"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":"factorials"},{"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":"[(2, 2), (4, 24), (6, 720)]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Picking the above example apart, we have the Boolean expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n > 0 and n % 2 == 0"}]},{"type":"text","value":". This means that only items which are both positive and divisible by two are accepted for further processing from the original list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"These positive, even numbers are then each in turn processed into the format "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(n, factorial(n))"}]},{"type":"text","value":". This is a tuple, where the first item is the number itself, and the second item is the result returned by the factorial function."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Remove smaller than","tmcname":"part11-05_remove_smaller_than"},"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":"remove_smaller_than(numbers: list, limit: int)"}]},{"type":"text","value":" which takes a list of integers and a limit value (also in integer format) as its arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should use a list comprehension to produce a new list without the values which are smaller than the limit value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in use:"}]},{"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":"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","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":"65"}]},{"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":"32"}]},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"11"}]},{"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":"remove_smaller_than"},{"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","number"]},"children":[{"type":"text","value":"10"}]},{"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":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"remove_smaller_than"},{"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":"4"}]},{"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":"7"}]},{"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":"8"}]},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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":" "},{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[65, 32, 11]\n[7, 8]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Begin with a vowel","tmcname":"part11-06_begin_with_vowel"},"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":"begin_with_vowel(words: list)"}]},{"type":"text","value":" which takes a list of strings as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should use a list comprehension technique to create and return a new list, containing only those words from the original list which begin with a vowel (a, e, i, o, u). Both lowercase and uppercase letters should be accepted."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in use:"}]},{"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":"word_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":"\"automobile\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"motorbike\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Animal\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cat\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dog\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"APPLE\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"orange\""}]},{"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":" vowelled "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" begin_with_vowel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"word_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":"text","value":"vowelled"},{"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":"automobile\nAnimal\nAPPLE\norange"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"alternative-execution-with-list-comprehensions","style":"position:relative;"},"children":[{"type":"text","value":"Alternative execution with list comprehensions"},{"type":"element","tagName":"a","properties":{"href":"#alternative-execution-with-list-comprehensions","ariaLabel":"alternative execution with list comprehensions 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":"Often when we have a conditional statement, we also include an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" branch. As we can use conditions in list comprehensions, the else branch is also available with list comprehensions. The general syntax of the conditional used with list comprehensions looks like this:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":" if else "}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We came across these single line conditionals, or ternary operators, already in "},{"type":"element","tagName":"a","properties":{"href":"/part-7/6-more-features"},"children":[{"type":"text","value":"part 7"}]},{"type":"text","value":". The expression above evaluates to either "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"expression 1"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"expression 2"}]},{"type":"text","value":", depending on whether the condition is true or false."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As a refresher on the subject, if we needed to print out the larger of two numbers, and we wanted to use just a single print statement, we could fit it all on a single line:"}]},{"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":"number1 "},{"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"]},"children":[{"type":"text","value":"\"Type in number 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":"text","value":"\nnumber2 "},{"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"]},"children":[{"type":"text","value":"\"Type in number 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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"number1 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" number1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" number2 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" number2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Combining the ternary operator syntax with a list comprehension statement yields the following general structure:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[ if else for in ]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This may look a little confusing, as the conditional structure now comes before the actual list comprehension part. This is just the way the syntax has been defined, at least at the moment. If there is also an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" branch, the conditional comes first. If there is just an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":", it goes to the end. You can try swapping them around and see what happens."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Including an else operator means that we will again process every item from the original list. Depending on whether the condition is true or false, either "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"expression 1"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"expression 2"}]},{"type":"text","value":" is performed on each item on the list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following example checks if the items on a list are zero or above. Any such item is accepted as is, but all negative items are negated, so that the sign is changed from negative to positive. The result is a list containing the absolute values of the items in the original 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":"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"45"}]},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"110"}]},{"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":"2"}]},{"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":"9"}]},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nabs_vals "},{"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":"text","value":"number "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"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":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"number "},{"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":"abs_vals"},{"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":"[1, 3, 45, 110, 2, 9, 11]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Reiterating what happens above: if the condition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number >= 0"}]},{"type":"text","value":" is true, the item undergoes expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":", and the result is the item itself. If the condition is false, the item undergoes expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-number"}]},{"type":"text","value":", so that it becomes positive in value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example we have the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"string_lengths"}]},{"type":"text","value":" which takes a list as its argument, and returns another list with the lengths of any strings in the original list. This function is okay with list items of any type, however. If the item is a string, it calculates its length. If the item is anything else, it inserts -1 in the list it returns."}]},{"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":"string_lengths"}]},{"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":" "},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The function returns the lengths of strings in a new list \"\"\""}]},{"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","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":"item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"item"},{"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":"str"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_list"},{"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":"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 test_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","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"123.344"}]},{"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":"\"toodlepip\""}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n lengths "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" string_lengths"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"test_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":"lengths"},{"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":"[2, -1, -1, 5, -1, 9, -1, -1]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lottery numbers","tmcname":"part11-07_lottery_numbers"},"children":[{"type":"element","tagName":"h2","properties":{"id":"lotterynumbers-matched","style":"position:relative;"},"children":[{"type":"text","value":"LotteryNumbers matched"},{"type":"element","tagName":"a","properties":{"href":"#lotterynumbers-matched","ariaLabel":"lotterynumbers matched 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":"Please write a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LotteryNumbers"}]},{"type":"text","value":" which takes the week number (an integer value) and a list of seven integers as its constructor arguments. The list should contain the correct lottery numbers for the given week."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please also write a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number_of_hits(numbers: list)"}]},{"type":"text","value":" which takes a list of integers as its argument. The method returns the number of correct entries in the parameter list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method should use a list comprehension to achieve this. The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how the class and function are used:"}]},{"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":"week5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LotteryNumbers"},{"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":" "},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmy_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","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":"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":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"13"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"19"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"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":"week5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_hits"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"h2","properties":{"id":"lotterynumbers-matched-in-place","style":"position:relative;"},"children":[{"type":"text","value":"LotteryNumbers matched in place"},{"type":"element","tagName":"a","properties":{"href":"#lotterynumbers-matched-in-place","ariaLabel":"lotterynumbers matched in place 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":"Please write a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hits_in_place(numbers)"}]},{"type":"text","value":" which takes a list of seven integers as its argument, and returns a new list of seven integers. The new list contains only those items from the original list which match the week's correct numbers. These must remain at the same indexes as they were in the original list. The rest of the indexes should be filled with values "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method should use a list comprehension to achieve this. The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please take a look at the example below:"}]},{"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":"week8 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LotteryNumbers"},{"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":" "},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"33"}]},{"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_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","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":"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"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":"week8"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hits_in_place"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, -1, -1, 10, -1, 20, 30]"}]}]}]}]},"html":"

    After this section

      \n
    • You will know what list comprehensions are
    • \n
    • You will be able to use list comprehensions to create new lists
    • \n

    One of the situations where programming is at its most powerful is processing sequences of items and events. Computers are good at repeating things. For example, in the previous parts of this material we have been iterating over strings, lists and dictionaries in various ways.

    Let's assume we have a list of integers, and we would need the same list of items in string format. A traditional way of completing the task could look like this:

    numbers = [1, 2, 3, 6, 5, 4, 7]\n\nstrings = []\nfor number in numbers:\n    strings.append(str(number))

    List comprehensions

    There is also a more \"pythonic\" way of generating lists from existing lists. These are called list comprehensions.

    The idea is to fit on a single line both the description of what should be done to each item on the list, and the assignment of the result to a new list.

    In the above example, the operation performed on each item on the list was very simple: each integer was converted into a string. Let's see what this would look like implemented with a list comprehension:

    numbers = [1, 2, 3, 6, 5, 4, 7]\nstrings = [str(number) for number in numbers]

    The second line above contains many of the same elements as the more traditional iterative apporach, but the syntax is different. One way of generalising a list comprehension statement would be

    [<expression> for <item> in <series>]

    The square brackets around the list comprehension statement signal to Python that the result should be a new list. One by one, each item in the original list is processed, and the result is stored in the new list, just like in the iterative approach above. As a result we have a new list with exactly as many items as were in the original, and all items have been processed in an identical fashion.

    (NB: the originals for the images in this part are temporarily missing, which is why there is some Finnish vocabulary in the illustrations in this part. We are working on fixing this.)

    \n \n
    def factorial(n: int):\n    \"\"\" The function calculates the factorial n! for integers above zero \"\"\"\n    k = 1\n    while n >= 2:\n        k *= n\n        n -= 1\n    return k\n\nif __name__ == \"__main__\":\n    numbers = [5, 2, 4, 3, 0]\n    factorials = [factorial(number) for number in numbers]\n    print(factorials)

    [120, 2, 24, 6, 1]

    With the more familiar for loop the same process could be expressed like this:

    def factorial(n: int):\n    \"\"\" The function calculates the factorial n! for integers above zero \"\"\"\n    k = 1\n    while n >= 2:\n        k *= n\n        n -= 1\n    return k\n\nif __name__ == \"__main__\":\n    numbers = [5, 2, 4, 3, 0]\n    factorials = []\n    for number in numbers:\n        factorials.append(factorial(number))\n    print(factorials)

    List comprehensions allow us to express the same functionality more consisely, usually without losing any of the readability.

    We can also return a list comprehension statement from a function directly. If we needed a function for producing factorials for lists of numbers, we could achieve it very concisely:

    def factorials(numbers: list):\n    return [factorial(number) for number in numbers]

    Please write a function named square_roots(numbers: list) which takes a list of integers as its argument. The function should return a new list containing the square roots of the original integers.

    The math module from the Python standard library contains a suitable function for calculating the square root.

    The function should use a list comprehension. The maximum length of the function is two lines of code, including the header line beginning with the def keyword.

    The function should work as follows:

    lines = square_roots([1,2,3,4])\nfor line in lines:\n    print(line)

    1.0\n1.4142135623730951\n1.7320508075688772\n2.0

    Please write a function named rows_of_stars(numbers: list) which takes a list of integers as its argument. The function should return a new list containing rows of stars. The length of each row should correspond to the integer at the same index in the original list. The function should use a list comprehension to achieve this.

    The maximum length of the function is two lines of code, including the header line beginning with the def keyword.

    The function should work as follows:

    rows = rows_of_stars([1,2,3,4])\nfor row in rows:\n    print(row)\n\nprint()\n\nrows = rows_of_stars([4, 3, 2, 1, 10])\nfor row in rows:\n    print(row)
    \n*\n**\n***\n****\n\n****\n***\n**\n*\n**********\n

    The exercise template contains the class definition ExamResult. The class has the following public attributes:

      \n
    • name
    • \n
    • grade1
    • \n
    • grade2
    • \n
    • grade3
    • \n

    Please write a function named best_results(results: list) which takes a list of ExamResult objects as its argument.

    The function should return a new list containing only the best result from each ExamResult object. The function should use a list comprehension to achieve this.

    The maximum length of the function is two lines of code, including the header line beginning with the def keyword.

    The function should work as follows:

    result1 = ExamResult(\"Peter\",5,3,4)\nresult2 = ExamResult(\"Pippa\",3,4,1)\nresult3 = ExamResult(\"Paul\",2,1,3)\nresults = [result1, result2, result3]\nprint(best_results(results))

    [5, 4, 3]

    Please write a function named lengths(lists: list) which takes a list containing lists of integers as its argument. The function should return a new list, containing the lengths of the lists within the argument list.

    The function should use a list comprehension to achieve this. The maximum length of the function is two lines of code, including the header line beginning with the def keyword.

    The function should work as follows:

    lists = [[1,2,3,4,5], [324, -1, 31, 7],[]]\nprint(lengths(lists))

    [5, 4, 0]

    Filtering items

    In the examples above all of our lists remained the same length before and after a list comprehension operation. In each case, all the items in the original list were used as the basis of the new list. But sometimes we only need some of the original items. How can this be achieved?

    A list comprehension statement also allows for a condition, so that we can check the items against the condition and select only those which match. The general syntax is as follows:

    [<expression> for <item> in <series> if <Boolean expression>]

    The statement above is otherwise identical to the general form introduced in the beginning of this section, but now there is an if statement at the end. Only those items from the original list for which the Boolean expression is true are used as the basis of the new list.

    In the example below we select all the even items from the original list as the basis of the new list. In fact, these items are not further processed in any way; they are assigned to the new list as is:

    numbers = [1, 1, 2, 3, 4, 6, 4, 5, 7, 10, 12, 3]\n\neven_items = [item for item in numbers if item % 2 == 0]\nprint(even_items)

    [2, 4, 6, 4, 10, 12]

    The expression in the list comprehension statement above is just a simple item, which means that no operations are to be performed on the items in the list. The expression could be any Python expression, just like in the previous examples. For example, the following list comprehension statement takes all the even items in a list, multiplies each by ten, and stores the result in a new list:

    numbers = [1, 1, 2, 3, 4, 6, 4, 5, 7, 10, 12, 3]\n\neven_items = [item * 10 for item in numbers if item % 2 == 0]\nprint(even_items)

    [20, 40, 60, 40, 100, 120]

    As you come across more and more complicated list comprehensions, you may find it useful to try reading the condition first. After all, the items are processed only if they pass the test, so it often makes sense to first figure out which items pass the filtering stage. Sometimes the expression in a list comprehension statement would not even be possible for all the items in the original list.

    For example, the factorial operation is only defined for non-negative integers. If we can't be sure a list only contains values of zero or above, the contents have to be filtered before passing them on to the factorial function we made before:

    def factorial(n: int):\n    \"\"\" The function calculates the factorial n! for integers above zero \"\"\"\n    k = 1\n    while n >= 2:\n        k *= n\n        n -= 1\n    return k\n\nif __name__ == \"__main__\":\n    numbers = [-2, 3, -1, 4, -10, 5, 1]\n    factorials = [factorial(number) for number in numbers if number >= 0]\n    print(factorials)

    [6, 24, 120, 1]

    As we saw in our very first list comprehension example, where integers were converted into strings, the items in the new list do not have to be of the same type as the items in the original list. Continuing from the factorial example above, we can create a tuple from each original item and its processed counterpart, and store these in a list, combining everything we've learned so far in a single list comprehension statement:

    def factorial(n: int):\n    \"\"\" The function calculates the factorial n! for integers above zero \"\"\"\n    k = 1\n    while n >= 2:\n        k *= n\n        n -= 1\n    return k\n\nif __name__ == \"__main__\":\n    numbers = [-2, 3, 2, 1, 4, -10, 5, 1, 6]\n    # the variable name abbreviated here so that this would be easier to read\n    factorials = [(n, factorial(n)) for n in numbers if n > 0 and n % 2 == 0]\n    print(factorials)

    [(2, 2), (4, 24), (6, 720)]

    Picking the above example apart, we have the Boolean expression n > 0 and n % 2 == 0. This means that only items which are both positive and divisible by two are accepted for further processing from the original list.

    These positive, even numbers are then each in turn processed into the format (n, factorial(n)). This is a tuple, where the first item is the number itself, and the second item is the result returned by the factorial function.

    Please write a function named remove_smaller_than(numbers: list, limit: int) which takes a list of integers and a limit value (also in integer format) as its arguments.

    The function should use a list comprehension to produce a new list without the values which are smaller than the limit value.

    The maximum length of the function is two lines of code, including the header line beginning with the def keyword.

    An example of the function in use:

    numbers = [1,65, 32, -6, 9, 11]\nprint(remove_smaller_than(numbers, 10))\n\nprint(remove_smaller_than([-4, 7, 8, -100], 0))

    [65, 32, 11]\n[7, 8]

    Please write a function named begin_with_vowel(words: list) which takes a list of strings as its argument.

    The function should use a list comprehension technique to create and return a new list, containing only those words from the original list which begin with a vowel (a, e, i, o, u). Both lowercase and uppercase letters should be accepted.

    The maximum length of the function is two lines of code, including the header line beginning with the def keyword.

    An example of the function in use:

    word_list = [\"automobile\",\"motorbike\",\"Animal\",\"cat\",\"Dog\",\"APPLE\",\"orange\"]\nfor vowelled in begin_with_vowel(word_list):\n    print(vowelled)

    automobile\nAnimal\nAPPLE\norange

    Alternative execution with list comprehensions

    Often when we have a conditional statement, we also include an else branch. As we can use conditions in list comprehensions, the else branch is also available with list comprehensions. The general syntax of the conditional used with list comprehensions looks like this:

    <expression 1> if <condition> else <expression 2>

    We came across these single line conditionals, or ternary operators, already in part 7. The expression above evaluates to either expression 1 or expression 2, depending on whether the condition is true or false.

    As a refresher on the subject, if we needed to print out the larger of two numbers, and we wanted to use just a single print statement, we could fit it all on a single line:

    number1 = int(input(\"Type in number 1:\"))\nnumber2 = int(input(\"Type in number 2:\"))\nprint (number1 if number1 > number2 else number2)

    Combining the ternary operator syntax with a list comprehension statement yields the following general structure:

    [<expression 1> if <condition> else <expression 2> for <item> in <series>]

    This may look a little confusing, as the conditional structure now comes before the actual list comprehension part. This is just the way the syntax has been defined, at least at the moment. If there is also an else branch, the conditional comes first. If there is just an if, it goes to the end. You can try swapping them around and see what happens.

    Including an else operator means that we will again process every item from the original list. Depending on whether the condition is true or false, either expression 1 or expression 2 is performed on each item on the list.

    The following example checks if the items on a list are zero or above. Any such item is accepted as is, but all negative items are negated, so that the sign is changed from negative to positive. The result is a list containing the absolute values of the items in the original list.

    numbers = [1, -3, 45, -110, 2, 9, -11]\nabs_vals = [number if number >= 0 else -number for number in numbers]\nprint(abs_vals)

    [1, 3, 45, 110, 2, 9, 11]

    Reiterating what happens above: if the condition number >= 0 is true, the item undergoes expression number, and the result is the item itself. If the condition is false, the item undergoes expression -number, so that it becomes positive in value.

    In the following example we have the function string_lengths which takes a list as its argument, and returns another list with the lengths of any strings in the original list. This function is okay with list items of any type, however. If the item is a string, it calculates its length. If the item is anything else, it inserts -1 in the list it returns.

    def string_lengths(my_list: list):\n    \"\"\" The function returns the lengths of strings in a new list \"\"\"\n    return [len(item) if type(item) == str else -1 for item in my_list]\n\nif __name__ == \"__main__\":\n    test_list = [\"hi\", 3, True, \"there\", -123.344, \"toodlepip\", 2, False]\n    lengths = string_lengths(test_list)\n    print(lengths)

    [2, -1, -1, 5, -1, 9, -1, -1]

    LotteryNumbers matched

    Please write a class named LotteryNumbers which takes the week number (an integer value) and a list of seven integers as its constructor arguments. The list should contain the correct lottery numbers for the given week.

    Please also write a method named number_of_hits(numbers: list) which takes a list of integers as its argument. The method returns the number of correct entries in the parameter list.

    The method should use a list comprehension to achieve this. The maximum length of the function is two lines of code, including the header line beginning with the def keyword.

    An example of how the class and function are used:

    week5 = LotteryNumbers(5, [1,2,3,4,5,6,7])\nmy_numbers = [1,4,7,11,13,19,24]\n\nprint(week5.number_of_hits(my_numbers))

    3

    LotteryNumbers matched in place

    Please write a method named hits_in_place(numbers) which takes a list of seven integers as its argument, and returns a new list of seven integers. The new list contains only those items from the original list which match the week's correct numbers. These must remain at the same indexes as they were in the original list. The rest of the indexes should be filled with values -1.

    The method should use a list comprehension to achieve this. The maximum length of the function is two lines of code, including the header line beginning with the def keyword.

    Please take a look at the example below:

    week8 = LotteryNumbers(8, [1,2,3,10,20,30,33])\nmy_numbers = [1,4,7,10,11,20,30]\n\nprint(week8.hits_in_place(my_numbers))

    [1, -1, -1, 10, -1, 20, 30]

    ","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-11/1-list-comprehensions.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know what list comprehensions are"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use list comprehensions to create new lists"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"One of the situations where programming is at its most powerful is processing sequences of items and events. Computers are good at repeating things. For example, in the previous parts of this material we have been iterating over strings, lists and dictionaries in various ways."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's assume we have a list of integers, and we would need the same list of items in string format. A traditional way of completing the task 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":"text","value":"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"2"}]},{"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":"3"}]},{"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":"6"}]},{"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":"5"}]},{"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":"4"}]},{"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":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nstrings "},{"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","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 strings"},{"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":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"list-comprehensions","style":"position:relative;"},"children":[{"type":"text","value":"List comprehensions"},{"type":"element","tagName":"a","properties":{"href":"#list-comprehensions","ariaLabel":"list comprehensions 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":"There is also a more \"pythonic\" way of generating lists from existing lists. These are called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"list comprehensions"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The idea is to fit on a single line both the description of what should be done to each item on the list, and the assignment of the result to a new list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the above example, the operation performed on each item on the list was very simple: each integer was converted into a string. Let's see what this would look like implemented with a list comprehension:"}]},{"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":"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"2"}]},{"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":"3"}]},{"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":"6"}]},{"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":"5"}]},{"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":"4"}]},{"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":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nstrings "},{"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","builtin"]},"children":[{"type":"text","value":"str"}]},{"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":" "},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The second line above contains many of the same elements as the more traditional iterative apporach, but the syntax is different. One way of generalising a list comprehension statement would be"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[ for in ]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The square brackets around the list comprehension statement signal to Python that the result should be a new list. One by one, each item in the original list is processed, and the result is stored in the new list, just like in the iterative approach above. As a result we have a new list with exactly as many items as were in the original, and all items have been processed in an identical fashion."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(NB: the originals for the images in this part are temporarily missing, which is why there is some Finnish vocabulary in the illustrations in this part. We are working on fixing this.)"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 301px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/3f079cde22be6cf33b7be317492aa108/fb933/11_1_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: 131.7391304347826%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAaCAYAAAC3g3x9AAAACXBIWXMAAAsTAAALEwEAmpwYAAACRUlEQVRIx42V16oCQQyG5/0fyStBRUQUbCCiggWx995y+AJZouyuJxAmk/JvJpnJBkmhx+Mh1WpVCoWCtFot+Q+F7XYrSbzb7eRyucj5fJbT6SRpvsZhNptpJtfrVZBZ2S8WCzkejyqv12vl5/OpOmxxMchhs9lE6XqZ7HCCyA62MmCLi0EOq9UqUiyXS3m/3yqT0e12U5msYAgdNghfYozACvP5XFMdjUZ6FHg8HuuKMzI+MDI6k82fWDDQB/sSGez3e/0SK42ACMBO/ew02LwvsXayYAJER+kUxAqI1ZIggExnPsQYgRX8Brrf7+pIrQ6HgwbAAGIjI2w0gL2P1Qy9wtbX66VgMLWyOgICIHp84mIjwG/mWLySSqUimUxGyuWyNJtN1WGLi4kAPagV2u5dHPmm+LgPQJ82R6P4dDiXy0mpVJJ8Pi/ZbDZqkjXPgyUC4kzBAbULTlPsqOx/AvrUAQMIEDrc7Xal1+upTEOw2bOMPbLvlO80GQ0GA6nVasrI6HyHU7tsZPeNZwaADQdkdNjsDn7Hf7wUvmz3jGDGEmyAtseGD76W7cdLofg4UGiuBAH2lnn42GFkuzbmQwyx2DVDXkC/39cacU2m06leXmQmCKOf1cvY8DGZWDB02vh5ljQP7Rn+mofIqRPbjsHRrAR+2sRO7OFwqA58FRklexu4yJPJRJl62UBFjy8xxLJHDrQ/iX0H/U1Iiwlp/1iuRaPRkHa7LfV6XYrFYvTkEv/LcWPIGkMjOp2OHhFG/p4y3/wHBFfbIcHIViUAAAAASUVORK5CYII='); 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":["/static/3f079cde22be6cf33b7be317492aa108/a0b58/11_1_2.webp 230w","/static/3f079cde22be6cf33b7be317492aa108/c4c4c/11_1_2.webp 301w"],"sizes":"(max-width: 301px) 100vw, 301px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/3f079cde22be6cf33b7be317492aa108/81c8e/11_1_2.png 230w","/static/3f079cde22be6cf33b7be317492aa108/fb933/11_1_2.png 301w"],"sizes":"(max-width: 301px) 100vw, 301px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/3f079cde22be6cf33b7be317492aa108/fb933/11_1_2.png","alt":"11 1 2","title":"11 1 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":"List comprehensions can handle much more complicated operations as well. We can perform calculations, such as multiplying the original items by ten:"}]},{"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":"numbers "},{"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":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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":"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":"10"}]},{"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":"numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nnumbers_multiplied "},{"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":"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":"10"}]},{"type":"text","value":" "},{"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":"numbers_multiplied"},{"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":"[1, 2, 3, 4, 5, 6, 7, 8, 9]\n[10, 20, 30, 40, 50, 60, 70, 80, 90]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In fact, the expression within the list comprehension statement can be any Python expression. You can even call functions you've defined yourself:"}]},{"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":"factorial"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The function calculates the factorial n! for integers above zero \"\"\""}]},{"type":"text","value":"\n k "},{"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":" n "},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" n\n n "},{"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":"return"}]},{"type":"text","value":" k\n\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 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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"2"}]},{"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":"4"}]},{"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":"3"}]},{"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n factorials "},{"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":"text","value":"factorial"},{"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":" "},{"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":"factorials"},{"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":"[120, 2, 24, 6, 1]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With the more familiar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop the same process could be expressed 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":"factorial"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The function calculates the factorial n! for integers above zero \"\"\""}]},{"type":"text","value":"\n k "},{"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":" n "},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" n\n n "},{"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":"return"}]},{"type":"text","value":" k\n\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 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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"2"}]},{"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":"4"}]},{"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":"3"}]},{"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n factorials "},{"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","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 factorials"},{"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":"factorial"},{"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","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":"factorials"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"List comprehensions allow us to express the same functionality more consisely, usually without losing any of the readability."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We can also return a list comprehension statement from a function directly. If we needed a function for producing factorials for lists of numbers, we could achieve it very concisely:"}]},{"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":"factorials"}]},{"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":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"factorial"},{"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":" "},{"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":"element","tagName":"programming-exercise","properties":{"name":"Square roots","tmcname":"part11-01_square_roots"},"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":"square_roots(numbers: list)"}]},{"type":"text","value":" which takes a list of integers as its argument. The function should return a new list containing the square roots of the original integers."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/math.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" module from the Python standard library contains a suitable function for calculating the square root."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should use a list comprehension. The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should work as follows:"}]},{"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":"lines "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" square_roots"},{"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","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","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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lines"},{"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":"line"},{"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":"1.0\n1.4142135623730951\n1.7320508075688772\n2.0"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Rows of stars","tmcname":"part11-02_rows_of_stars"},"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":"rows_of_stars(numbers: list)"}]},{"type":"text","value":" which takes a list of integers as its argument. The function should return a new list containing rows of stars. The length of each row should correspond to the integer at the same index in the original list. The function should use a list comprehension to achieve this."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should work as follows:"}]},{"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":"rows "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rows_of_stars"},{"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","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","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":" row "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" rows"},{"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":"row"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrows "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rows_of_stars"},{"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","number"]},"children":[{"type":"text","value":"4"}]},{"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":"3"}]},{"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":"2"}]},{"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":"1"}]},{"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":"10"}]},{"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":"for"}]},{"type":"text","value":" row "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" rows"},{"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":"row"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"*\n**\n***\n****\n\n****\n***\n**\n*\n**********\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Best exam result","tmcname":"part11-03_best_exam_result"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the class definition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ExamResult"}]},{"type":"text","value":". The class has the following public attributes:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"name"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"grade1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"grade2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"grade3"}]},{"type":"text","value":"\n"}]},{"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":"best_results(results: list)"}]},{"type":"text","value":" which takes a list of ExamResult objects as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return a new list containing only the best result from each ExamResult object. The function should use a list comprehension to achieve this."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should work as follows:"}]},{"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":"result1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ExamResult"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"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":"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":"text","value":"\nresult2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ExamResult"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pippa\""}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresult3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ExamResult"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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":"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":"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresults "},{"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":"text","value":"result1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" result2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" result3"},{"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":"best_results"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"results"},{"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, 4, 3]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lengths","tmcname":"part11-04_lengths"},"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":"lengths(lists: list)"}]},{"type":"text","value":" which takes a list containing lists of integers as its argument. The function should return a new list, containing the lengths of the lists within the argument list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should use a list comprehension to achieve this. The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should work as follows:"}]},{"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":"lists "},{"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":"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","punctuation"]},"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":"324"}]},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"31"}]},{"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":"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":"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"},{"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":"lengths"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"lists"},{"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, 4, 0]"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"filtering-items","style":"position:relative;"},"children":[{"type":"text","value":"Filtering items"},{"type":"element","tagName":"a","properties":{"href":"#filtering-items","ariaLabel":"filtering items 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 the examples above all of our lists remained the same length before and after a list comprehension operation. In each case, all the items in the original list were used as the basis of the new list. But sometimes we only need "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"some"}]},{"type":"text","value":" of the original items. How can this be achieved?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A list comprehension statement also allows for a condition, so that we can check the items against the condition and select only those which match. The general syntax is as follows:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[ for in if ]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The statement above is otherwise identical to the general form introduced in the beginning of this section, but now there is an if statement at the end. Only those items from the original list for which the Boolean expression is true are used as the basis of the new list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the example below we select all the even items from the original list as the basis of the new list. In fact, these items are not further processed in any way; they are assigned to the new list as is:"}]},{"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":"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"1"}]},{"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":"2"}]},{"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":"3"}]},{"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":"4"}]},{"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":"6"}]},{"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":"4"}]},{"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":"5"}]},{"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":"7"}]},{"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":"10"}]},{"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":"12"}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\neven_items "},{"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":"text","value":"item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" numbers "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" item "},{"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":"2"}]},{"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":"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":"even_items"},{"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":"[2, 4, 6, 4, 10, 12]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The expression in the list comprehension statement above is just a simple "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"item"}]},{"type":"text","value":", which means that no operations are to be performed on the items in the list. The expression could be any Python expression, just like in the previous examples. For example, the following list comprehension statement takes all the even items in a list, multiplies each by ten, and stores the result in a new 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":"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"1"}]},{"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":"2"}]},{"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":"3"}]},{"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":"4"}]},{"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":"6"}]},{"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":"4"}]},{"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":"5"}]},{"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":"7"}]},{"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":"10"}]},{"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":"12"}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\neven_items "},{"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":"text","value":"item "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" numbers "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" item "},{"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":"2"}]},{"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":"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":"even_items"},{"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":"[20, 40, 60, 40, 100, 120]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you come across more and more complicated list comprehensions, you may find it useful to try reading the condition first. After all, the items are processed only if they pass the test, so it often makes sense to first figure out which items pass the filtering stage. Sometimes the expression in a list comprehension statement would not even be possible for all the items in the original list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, the factorial operation is only defined for non-negative integers. If we can't be sure a list only contains values of zero or above, the contents have to be filtered before passing them on to the factorial function we made before:"}]},{"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":"factorial"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The function calculates the factorial n! for integers above zero \"\"\""}]},{"type":"text","value":"\n k "},{"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":" n "},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" n\n n "},{"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":"return"}]},{"type":"text","value":" k\n\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 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","operator"]},"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":" "},{"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":" "},{"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":" "},{"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":"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":"10"}]},{"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":"5"}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n factorials "},{"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":"text","value":"factorial"},{"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":" "},{"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","keyword"]},"children":[{"type":"text","value":"if"}]},{"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":"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":"factorials"},{"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":"[6, 24, 120, 1]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As we saw in our very first list comprehension example, where integers were converted into strings, the items in the new list do not have to be of the same type as the items in the original list. Continuing from the factorial example above, we can create a tuple from each original item and its processed counterpart, and store these in a list, combining everything we've learned so far in a single list comprehension 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":"factorial"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The function calculates the factorial n! for integers above zero \"\"\""}]},{"type":"text","value":"\n k "},{"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":" n "},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" n\n n "},{"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":"return"}]},{"type":"text","value":" k\n\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 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","operator"]},"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"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":"10"}]},{"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":"5"}]},{"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":"1"}]},{"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":"6"}]},{"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":"# the variable name abbreviated here so that this would be easier to read"}]},{"type":"text","value":"\n factorials "},{"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","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" factorial"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" numbers "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" n "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" n "},{"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":"2"}]},{"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":"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":"factorials"},{"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":"[(2, 2), (4, 24), (6, 720)]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Picking the above example apart, we have the Boolean expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n > 0 and n % 2 == 0"}]},{"type":"text","value":". This means that only items which are both positive and divisible by two are accepted for further processing from the original list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"These positive, even numbers are then each in turn processed into the format "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(n, factorial(n))"}]},{"type":"text","value":". This is a tuple, where the first item is the number itself, and the second item is the result returned by the factorial function."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Remove smaller than","tmcname":"part11-05_remove_smaller_than"},"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":"remove_smaller_than(numbers: list, limit: int)"}]},{"type":"text","value":" which takes a list of integers and a limit value (also in integer format) as its arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should use a list comprehension to produce a new list without the values which are smaller than the limit value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in use:"}]},{"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":"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","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":"65"}]},{"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":"32"}]},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"11"}]},{"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":"remove_smaller_than"},{"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","number"]},"children":[{"type":"text","value":"10"}]},{"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":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"remove_smaller_than"},{"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":"4"}]},{"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":"7"}]},{"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":"8"}]},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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":" "},{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[65, 32, 11]\n[7, 8]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Begin with a vowel","tmcname":"part11-06_begin_with_vowel"},"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":"begin_with_vowel(words: list)"}]},{"type":"text","value":" which takes a list of strings as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should use a list comprehension technique to create and return a new list, containing only those words from the original list which begin with a vowel (a, e, i, o, u). Both lowercase and uppercase letters should be accepted."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in use:"}]},{"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":"word_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":"\"automobile\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"motorbike\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Animal\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cat\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dog\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"APPLE\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"orange\""}]},{"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":" vowelled "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" begin_with_vowel"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"word_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":"text","value":"vowelled"},{"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":"automobile\nAnimal\nAPPLE\norange"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"alternative-execution-with-list-comprehensions","style":"position:relative;"},"children":[{"type":"text","value":"Alternative execution with list comprehensions"},{"type":"element","tagName":"a","properties":{"href":"#alternative-execution-with-list-comprehensions","ariaLabel":"alternative execution with list comprehensions 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":"Often when we have a conditional statement, we also include an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" branch. As we can use conditions in list comprehensions, the else branch is also available with list comprehensions. The general syntax of the conditional used with list comprehensions looks like this:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":" if else "}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We came across these single line conditionals, or ternary operators, already in "},{"type":"element","tagName":"a","properties":{"href":"/part-7/6-more-features"},"children":[{"type":"text","value":"part 7"}]},{"type":"text","value":". The expression above evaluates to either "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"expression 1"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"expression 2"}]},{"type":"text","value":", depending on whether the condition is true or false."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As a refresher on the subject, if we needed to print out the larger of two numbers, and we wanted to use just a single print statement, we could fit it all on a single line:"}]},{"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":"number1 "},{"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"]},"children":[{"type":"text","value":"\"Type in number 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":"text","value":"\nnumber2 "},{"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"]},"children":[{"type":"text","value":"\"Type in number 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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"number1 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" number1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" number2 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" number2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Combining the ternary operator syntax with a list comprehension statement yields the following general structure:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[ if else for in ]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This may look a little confusing, as the conditional structure now comes before the actual list comprehension part. This is just the way the syntax has been defined, at least at the moment. If there is also an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" branch, the conditional comes first. If there is just an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":", it goes to the end. You can try swapping them around and see what happens."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Including an else operator means that we will again process every item from the original list. Depending on whether the condition is true or false, either "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"expression 1"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"expression 2"}]},{"type":"text","value":" is performed on each item on the list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following example checks if the items on a list are zero or above. Any such item is accepted as is, but all negative items are negated, so that the sign is changed from negative to positive. The result is a list containing the absolute values of the items in the original 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":"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"45"}]},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"110"}]},{"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":"2"}]},{"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":"9"}]},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nabs_vals "},{"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":"text","value":"number "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"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":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"number "},{"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":"abs_vals"},{"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":"[1, 3, 45, 110, 2, 9, 11]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Reiterating what happens above: if the condition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number >= 0"}]},{"type":"text","value":" is true, the item undergoes expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":", and the result is the item itself. If the condition is false, the item undergoes expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-number"}]},{"type":"text","value":", so that it becomes positive in value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example we have the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"string_lengths"}]},{"type":"text","value":" which takes a list as its argument, and returns another list with the lengths of any strings in the original list. This function is okay with list items of any type, however. If the item is a string, it calculates its length. If the item is anything else, it inserts -1 in the list it returns."}]},{"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":"string_lengths"}]},{"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":" "},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The function returns the lengths of strings in a new list \"\"\""}]},{"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","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":"item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"item"},{"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":"str"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_list"},{"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":"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 test_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","number"]},"children":[{"type":"text","value":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"123.344"}]},{"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":"\"toodlepip\""}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n lengths "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" string_lengths"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"test_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":"lengths"},{"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":"[2, -1, -1, 5, -1, 9, -1, -1]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lottery numbers","tmcname":"part11-07_lottery_numbers"},"children":[{"type":"element","tagName":"h2","properties":{"id":"lotterynumbers-matched","style":"position:relative;"},"children":[{"type":"text","value":"LotteryNumbers matched"},{"type":"element","tagName":"a","properties":{"href":"#lotterynumbers-matched","ariaLabel":"lotterynumbers matched 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":"Please write a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LotteryNumbers"}]},{"type":"text","value":" which takes the week number (an integer value) and a list of seven integers as its constructor arguments. The list should contain the correct lottery numbers for the given week."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please also write a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number_of_hits(numbers: list)"}]},{"type":"text","value":" which takes a list of integers as its argument. The method returns the number of correct entries in the parameter list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method should use a list comprehension to achieve this. The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how the class and function are used:"}]},{"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":"week5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LotteryNumbers"},{"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":" "},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmy_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","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":"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":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"13"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"19"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"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":"week5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_hits"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"h2","properties":{"id":"lotterynumbers-matched-in-place","style":"position:relative;"},"children":[{"type":"text","value":"LotteryNumbers matched in place"},{"type":"element","tagName":"a","properties":{"href":"#lotterynumbers-matched-in-place","ariaLabel":"lotterynumbers matched in place 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":"Please write a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hits_in_place(numbers)"}]},{"type":"text","value":" which takes a list of seven integers as its argument, and returns a new list of seven integers. The new list contains only those items from the original list which match the week's correct numbers. These must remain at the same indexes as they were in the original list. The rest of the indexes should be filled with values "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method should use a list comprehension to achieve this. The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please take a look at the example below:"}]},{"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":"week8 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LotteryNumbers"},{"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":" "},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"33"}]},{"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_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","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":"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"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":"week8"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hits_in_place"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, -1, -1, 10, -1, 20, 30]"}]}]}]}]},"html":"

    After this section

      \n
    • You will know what list comprehensions are
    • \n
    • You will be able to use list comprehensions to create new lists
    • \n

    One of the situations where programming is at its most powerful is processing sequences of items and events. Computers are good at repeating things. For example, in the previous parts of this material we have been iterating over strings, lists and dictionaries in various ways.

    Let's assume we have a list of integers, and we would need the same list of items in string format. A traditional way of completing the task could look like this:

    numbers = [1, 2, 3, 6, 5, 4, 7]\n\nstrings = []\nfor number in numbers:\n    strings.append(str(number))

    List comprehensions

    There is also a more \"pythonic\" way of generating lists from existing lists. These are called list comprehensions.

    The idea is to fit on a single line both the description of what should be done to each item on the list, and the assignment of the result to a new list.

    In the above example, the operation performed on each item on the list was very simple: each integer was converted into a string. Let's see what this would look like implemented with a list comprehension:

    numbers = [1, 2, 3, 6, 5, 4, 7]\nstrings = [str(number) for number in numbers]

    The second line above contains many of the same elements as the more traditional iterative apporach, but the syntax is different. One way of generalising a list comprehension statement would be

    [<expression> for <item> in <series>]

    The square brackets around the list comprehension statement signal to Python that the result should be a new list. One by one, each item in the original list is processed, and the result is stored in the new list, just like in the iterative approach above. As a result we have a new list with exactly as many items as were in the original, and all items have been processed in an identical fashion.

    (NB: the originals for the images in this part are temporarily missing, which is why there is some Finnish vocabulary in the illustrations in this part. We are working on fixing this.)

    \n \n
    def factorial(n: int):\n    \"\"\" The function calculates the factorial n! for integers above zero \"\"\"\n    k = 1\n    while n >= 2:\n        k *= n\n        n -= 1\n    return k\n\nif __name__ == \"__main__\":\n    numbers = [5, 2, 4, 3, 0]\n    factorials = [factorial(number) for number in numbers]\n    print(factorials)

    [120, 2, 24, 6, 1]

    With the more familiar for loop the same process could be expressed like this:

    def factorial(n: int):\n    \"\"\" The function calculates the factorial n! for integers above zero \"\"\"\n    k = 1\n    while n >= 2:\n        k *= n\n        n -= 1\n    return k\n\nif __name__ == \"__main__\":\n    numbers = [5, 2, 4, 3, 0]\n    factorials = []\n    for number in numbers:\n        factorials.append(factorial(number))\n    print(factorials)

    List comprehensions allow us to express the same functionality more consisely, usually without losing any of the readability.

    We can also return a list comprehension statement from a function directly. If we needed a function for producing factorials for lists of numbers, we could achieve it very concisely:

    def factorials(numbers: list):\n    return [factorial(number) for number in numbers]

    Please write a function named square_roots(numbers: list) which takes a list of integers as its argument. The function should return a new list containing the square roots of the original integers.

    The math module from the Python standard library contains a suitable function for calculating the square root.

    The function should use a list comprehension. The maximum length of the function is two lines of code, including the header line beginning with the def keyword.

    The function should work as follows:

    lines = square_roots([1,2,3,4])\nfor line in lines:\n    print(line)

    1.0\n1.4142135623730951\n1.7320508075688772\n2.0

    Please write a function named rows_of_stars(numbers: list) which takes a list of integers as its argument. The function should return a new list containing rows of stars. The length of each row should correspond to the integer at the same index in the original list. The function should use a list comprehension to achieve this.

    The maximum length of the function is two lines of code, including the header line beginning with the def keyword.

    The function should work as follows:

    rows = rows_of_stars([1,2,3,4])\nfor row in rows:\n    print(row)\n\nprint()\n\nrows = rows_of_stars([4, 3, 2, 1, 10])\nfor row in rows:\n    print(row)
    \n*\n**\n***\n****\n\n****\n***\n**\n*\n**********\n

    The exercise template contains the class definition ExamResult. The class has the following public attributes:

      \n
    • name
    • \n
    • grade1
    • \n
    • grade2
    • \n
    • grade3
    • \n

    Please write a function named best_results(results: list) which takes a list of ExamResult objects as its argument.

    The function should return a new list containing only the best result from each ExamResult object. The function should use a list comprehension to achieve this.

    The maximum length of the function is two lines of code, including the header line beginning with the def keyword.

    The function should work as follows:

    result1 = ExamResult(\"Peter\",5,3,4)\nresult2 = ExamResult(\"Pippa\",3,4,1)\nresult3 = ExamResult(\"Paul\",2,1,3)\nresults = [result1, result2, result3]\nprint(best_results(results))

    [5, 4, 3]

    Please write a function named lengths(lists: list) which takes a list containing lists of integers as its argument. The function should return a new list, containing the lengths of the lists within the argument list.

    The function should use a list comprehension to achieve this. The maximum length of the function is two lines of code, including the header line beginning with the def keyword.

    The function should work as follows:

    lists = [[1,2,3,4,5], [324, -1, 31, 7],[]]\nprint(lengths(lists))

    [5, 4, 0]

    Filtering items

    In the examples above all of our lists remained the same length before and after a list comprehension operation. In each case, all the items in the original list were used as the basis of the new list. But sometimes we only need some of the original items. How can this be achieved?

    A list comprehension statement also allows for a condition, so that we can check the items against the condition and select only those which match. The general syntax is as follows:

    [<expression> for <item> in <series> if <Boolean expression>]

    The statement above is otherwise identical to the general form introduced in the beginning of this section, but now there is an if statement at the end. Only those items from the original list for which the Boolean expression is true are used as the basis of the new list.

    In the example below we select all the even items from the original list as the basis of the new list. In fact, these items are not further processed in any way; they are assigned to the new list as is:

    numbers = [1, 1, 2, 3, 4, 6, 4, 5, 7, 10, 12, 3]\n\neven_items = [item for item in numbers if item % 2 == 0]\nprint(even_items)

    [2, 4, 6, 4, 10, 12]

    The expression in the list comprehension statement above is just a simple item, which means that no operations are to be performed on the items in the list. The expression could be any Python expression, just like in the previous examples. For example, the following list comprehension statement takes all the even items in a list, multiplies each by ten, and stores the result in a new list:

    numbers = [1, 1, 2, 3, 4, 6, 4, 5, 7, 10, 12, 3]\n\neven_items = [item * 10 for item in numbers if item % 2 == 0]\nprint(even_items)

    [20, 40, 60, 40, 100, 120]

    As you come across more and more complicated list comprehensions, you may find it useful to try reading the condition first. After all, the items are processed only if they pass the test, so it often makes sense to first figure out which items pass the filtering stage. Sometimes the expression in a list comprehension statement would not even be possible for all the items in the original list.

    For example, the factorial operation is only defined for non-negative integers. If we can't be sure a list only contains values of zero or above, the contents have to be filtered before passing them on to the factorial function we made before:

    def factorial(n: int):\n    \"\"\" The function calculates the factorial n! for integers above zero \"\"\"\n    k = 1\n    while n >= 2:\n        k *= n\n        n -= 1\n    return k\n\nif __name__ == \"__main__\":\n    numbers = [-2, 3, -1, 4, -10, 5, 1]\n    factorials = [factorial(number) for number in numbers if number >= 0]\n    print(factorials)

    [6, 24, 120, 1]

    As we saw in our very first list comprehension example, where integers were converted into strings, the items in the new list do not have to be of the same type as the items in the original list. Continuing from the factorial example above, we can create a tuple from each original item and its processed counterpart, and store these in a list, combining everything we've learned so far in a single list comprehension statement:

    def factorial(n: int):\n    \"\"\" The function calculates the factorial n! for integers above zero \"\"\"\n    k = 1\n    while n >= 2:\n        k *= n\n        n -= 1\n    return k\n\nif __name__ == \"__main__\":\n    numbers = [-2, 3, 2, 1, 4, -10, 5, 1, 6]\n    # the variable name abbreviated here so that this would be easier to read\n    factorials = [(n, factorial(n)) for n in numbers if n > 0 and n % 2 == 0]\n    print(factorials)

    [(2, 2), (4, 24), (6, 720)]

    Picking the above example apart, we have the Boolean expression n > 0 and n % 2 == 0. This means that only items which are both positive and divisible by two are accepted for further processing from the original list.

    These positive, even numbers are then each in turn processed into the format (n, factorial(n)). This is a tuple, where the first item is the number itself, and the second item is the result returned by the factorial function.

    Please write a function named remove_smaller_than(numbers: list, limit: int) which takes a list of integers and a limit value (also in integer format) as its arguments.

    The function should use a list comprehension to produce a new list without the values which are smaller than the limit value.

    The maximum length of the function is two lines of code, including the header line beginning with the def keyword.

    An example of the function in use:

    numbers = [1,65, 32, -6, 9, 11]\nprint(remove_smaller_than(numbers, 10))\n\nprint(remove_smaller_than([-4, 7, 8, -100], 0))

    [65, 32, 11]\n[7, 8]

    Please write a function named begin_with_vowel(words: list) which takes a list of strings as its argument.

    The function should use a list comprehension technique to create and return a new list, containing only those words from the original list which begin with a vowel (a, e, i, o, u). Both lowercase and uppercase letters should be accepted.

    The maximum length of the function is two lines of code, including the header line beginning with the def keyword.

    An example of the function in use:

    word_list = [\"automobile\",\"motorbike\",\"Animal\",\"cat\",\"Dog\",\"APPLE\",\"orange\"]\nfor vowelled in begin_with_vowel(word_list):\n    print(vowelled)

    automobile\nAnimal\nAPPLE\norange

    Alternative execution with list comprehensions

    Often when we have a conditional statement, we also include an else branch. As we can use conditions in list comprehensions, the else branch is also available with list comprehensions. The general syntax of the conditional used with list comprehensions looks like this:

    <expression 1> if <condition> else <expression 2>

    We came across these single line conditionals, or ternary operators, already in part 7. The expression above evaluates to either expression 1 or expression 2, depending on whether the condition is true or false.

    As a refresher on the subject, if we needed to print out the larger of two numbers, and we wanted to use just a single print statement, we could fit it all on a single line:

    number1 = int(input(\"Type in number 1:\"))\nnumber2 = int(input(\"Type in number 2:\"))\nprint (number1 if number1 > number2 else number2)

    Combining the ternary operator syntax with a list comprehension statement yields the following general structure:

    [<expression 1> if <condition> else <expression 2> for <item> in <series>]

    This may look a little confusing, as the conditional structure now comes before the actual list comprehension part. This is just the way the syntax has been defined, at least at the moment. If there is also an else branch, the conditional comes first. If there is just an if, it goes to the end. You can try swapping them around and see what happens.

    Including an else operator means that we will again process every item from the original list. Depending on whether the condition is true or false, either expression 1 or expression 2 is performed on each item on the list.

    The following example checks if the items on a list are zero or above. Any such item is accepted as is, but all negative items are negated, so that the sign is changed from negative to positive. The result is a list containing the absolute values of the items in the original list.

    numbers = [1, -3, 45, -110, 2, 9, -11]\nabs_vals = [number if number >= 0 else -number for number in numbers]\nprint(abs_vals)

    [1, 3, 45, 110, 2, 9, 11]

    Reiterating what happens above: if the condition number >= 0 is true, the item undergoes expression number, and the result is the item itself. If the condition is false, the item undergoes expression -number, so that it becomes positive in value.

    In the following example we have the function string_lengths which takes a list as its argument, and returns another list with the lengths of any strings in the original list. This function is okay with list items of any type, however. If the item is a string, it calculates its length. If the item is anything else, it inserts -1 in the list it returns.

    def string_lengths(my_list: list):\n    \"\"\" The function returns the lengths of strings in a new list \"\"\"\n    return [len(item) if type(item) == str else -1 for item in my_list]\n\nif __name__ == \"__main__\":\n    test_list = [\"hi\", 3, True, \"there\", -123.344, \"toodlepip\", 2, False]\n    lengths = string_lengths(test_list)\n    print(lengths)

    [2, -1, -1, 5, -1, 9, -1, -1]

    LotteryNumbers matched

    Please write a class named LotteryNumbers which takes the week number (an integer value) and a list of seven integers as its constructor arguments. The list should contain the correct lottery numbers for the given week.

    Please also write a method named number_of_hits(numbers: list) which takes a list of integers as its argument. The method returns the number of correct entries in the parameter list.

    The method should use a list comprehension to achieve this. The maximum length of the function is two lines of code, including the header line beginning with the def keyword.

    An example of how the class and function are used:

    week5 = LotteryNumbers(5, [1,2,3,4,5,6,7])\nmy_numbers = [1,4,7,11,13,19,24]\n\nprint(week5.number_of_hits(my_numbers))

    3

    LotteryNumbers matched in place

    Please write a method named hits_in_place(numbers) which takes a list of seven integers as its argument, and returns a new list of seven integers. The new list contains only those items from the original list which match the week's correct numbers. These must remain at the same indexes as they were in the original list. The rest of the indexes should be filled with values -1.

    The method should use a list comprehension to achieve this. The maximum length of the function is two lines of code, including the header line beginning with the def keyword.

    Please take a look at the example below:

    week8 = LotteryNumbers(8, [1,2,3,10,20,30,33])\nmy_numbers = [1,4,7,10,11,20,30]\n\nprint(week8.hits_in_place(my_numbers))

    [1, -1, -1, 10, -1, 20, 30]

    ","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-11/1-list-comprehensions.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-11/2-more-comprehensions/page-data.json b/page-data/part-11/2-more-comprehensions/page-data.json index 63207cf48..c7501fc85 100644 --- a/page-data/part-11/2-more-comprehensions/page-data.json +++ b/page-data/part-11/2-more-comprehensions/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-11/2-more-comprehensions", - "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 able to use comprehensions with strings"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to use comprehensions with your own classes"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to create dictionary comprehensions"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lists are perhaps the most common target for comprehensions, but comprehensions work on any series of items, including strings. Just like in the list examples in the previous section, if a list comprehension is performed on a string, the items (i.e. the characters) in the string are plucked one by one, processed according to the expression given, and stored in 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":"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":"\"Peter Python\""}]},{"type":"text","value":"\n\nuppercased "},{"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":"text","value":"character"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"upper"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" name"},{"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":"uppercased"},{"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":"['P', 'E', 'T', 'E', 'R', ' ', 'P', 'Y', 'T', 'H', 'O', 'N']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The result is indeed a list, as dictated by the bracket notation around the comprehension statement. If we wanted a string instead, we could use the string method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"join"}]},{"type":"text","value":" to parse the list into a string. Remember, the method is called on the string we want to use as the \"glue\" between the characters. Let's take a look at 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":"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":"\"Peter\""}]},{"type":"text","value":"\nchar_list "},{"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":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"char_list"},{"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":"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":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"char_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":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"char_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":"\",\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"char_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":"\" and \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"char_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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['P', 'e', 't', 'e', 'r']\nPeter\nP e t e r\nP,e,t,e,r\nP and e and t and e and r"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"List comprehensions and the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"join"}]},{"type":"text","value":" method make it easy to create new strings based on other strings. We could, for example, make a string which contains only the vowels from 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":"test_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":"\"Hello there, this is a test!\""}]},{"type":"text","value":"\n\nvowels "},{"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":"text","value":"character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" test_string "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"aeiou\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nnew_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":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vowels"},{"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":"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":"eoeeiiae"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the example above the list comprehension and the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"join"}]},{"type":"text","value":" method are on separate lines, but they could be combined into a single expression:"}]},{"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":"test_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":"\"Hello there, this is a test!\""}]},{"type":"text","value":"\n\nvowel_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":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"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":"character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" test_string "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"aeiou\""}]},{"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":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vowel_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Many Python programmers swear by these oneliners, so it is well worth your while to learn to read them. We could even add the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":" method to the mix, so that we can process entire sentences efficiently with a single statement. In the example below the first character from each word in a sentence is removed:"}]},{"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 keeps on selling seashells on the seashore\""}]},{"type":"text","value":"\n\nsentence_no_initials "},{"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":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"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":"word"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sentence"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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","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":"sentence_no_initials"},{"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":"heila eeps n elling eashells n he eashore"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's go through this step by step:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"word[1:]"}]},{"type":"text","value":" extracts a substring from the second character (at index 1) onwards"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sentence.split()"}]},{"type":"text","value":" splits the sentence into sections at the given character. In this case there is no argument given to the method, so the sentence is split at space characters by default"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\" \".join()"}]},{"type":"text","value":" combines the items in the list into a new string using a space character between the items"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A more traditional iterative approach 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":"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 keeps on selling seashells on the seashore\""}]},{"type":"text","value":"\n\nword_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","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nwords "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sentence"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n word_no_initials "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" word"},{"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":"text","value":"\n word_list"},{"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":"word_no_initials"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsentence_no_initials "},{"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":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"word_list"},{"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":"sentence_no_initials"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Filter forbidden","tmcname":"part11-08_filter_forbidden"},"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":"filter_forbidden(string: str, forbidden: str)"}]},{"type":"text","value":" which takes two strings as its arguments. The function should return a new version of the first string. It should not contain any characters from the second string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should be implemented using list comprehensions. The maximum length of the function is three lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please have a look at the example below."}]},{"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":"\"Once! upon, a time: there was a python!??!?!\""}]},{"type":"text","value":"\nfiltered "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" filter_forbidden"},{"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":" "},{"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"},{"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":"filtered"},{"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":"Once upon a time there was a python"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"own-classes-and-comprehensions","style":"position:relative;"},"children":[{"type":"text","value":"Own classes and comprehensions"},{"type":"element","tagName":"a","properties":{"href":"#own-classes-and-comprehensions","ariaLabel":"own classes and comprehensions 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":"Comprehensions can be a useful tool for processing or formulating instances of your own classes, as we'll see in the following examples."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First, let's have a look at the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Country"}]},{"type":"text","value":" which is a simple model for a single country, with attributes for the name and the population. In the main function below we first create some Country objects, and then use a list comprehension to select only those whose population is greater than five million."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Country"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" This class models a single country with population \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" population"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"population "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" population\n\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 finland "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Finland\""}]},{"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":"6000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n malta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Malta\""}]},{"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":"500000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n sweden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sweden\""}]},{"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":"10000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n iceland "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Iceland\""}]},{"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":"350000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n countries "},{"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":"text","value":"finland"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" malta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sweden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" iceland"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n bigger_countries "},{"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":"text","value":"country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" country "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" countries "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"population "},{"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":"5000000"}]},{"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":" country "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" bigger_countries"},{"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":"country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Finland\nSweden"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the list comprehension above we selected only the name attribute from the Country objects, so the contents of the list could be printed directly. We could also create a new list of the countries themselves and access the name attribute in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop. This would be useful if the same list of countries was used also later in the program, or if we needed the population attribute in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop as well:"}]},{"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":"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 finland "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Finland\""}]},{"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":"6000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n malta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Malta\""}]},{"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":"500000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n sweden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sweden\""}]},{"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":"10000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n iceland "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Iceland\""}]},{"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":"350000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n countries "},{"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":"text","value":"finland"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" malta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sweden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" iceland"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n bigger_countries "},{"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":"text","value":"country "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" country "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" countries "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"population "},{"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":"5000000"}]},{"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":" country "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" bigger_countries"},{"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":"country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"population"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the next example we have a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"RunningEvent"}]},{"type":"text","value":" which models a single foot race event with attributes for the length and the name of the race. We will use list comprehensions to create "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"RunningEvent"}]},{"type":"text","value":" objects based on a list of race lengths."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The parameter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" has a default value in the constructor of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"RunningEvent"}]},{"type":"text","value":" class, whIch is why we do not need to pass the name as an argument."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"RunningEvent"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The class models a foot race event of a length of n metres \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" length"},{"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":"text","value":" name"},{"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":"str"}]},{"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":"\"no name\""}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" length\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\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":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" m. ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","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 lengths "},{"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":"100"}]},{"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":"200"}]},{"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":"1500"}]},{"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":"3000"}]},{"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":"42195"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n events "},{"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":"text","value":"RunningEvent"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"length"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" length "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lengths"},{"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":"# Print out all events"}]},{"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":"events"},{"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":"# Pick one from the list and give it a name"}]},{"type":"text","value":"\n marathon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" events"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the last item in the list"}]},{"type":"text","value":"\n marathon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"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":"\"Marathon\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Print out everything again, including the new name"}]},{"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":"events"},{"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":"[100 m. (no name), 200 m. (no name), 1500 m. (no name), 3000 m. (no name), 42195 m. (no name)]\n[100 m. (no name), 200 m. (no name), 1500 m. (no name), 3000 m. (no name), 42195 m. (Marathon)]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, let's find out what makes a series of items \"comprehendible\". In the previous part we learnt how to make our own classes iterable. It is exactly this same feature which also allows for list comprehensions. If your own class is iterable, it can be used as the basis of a list comprehension statement. The following class definitions are copied directly from "},{"type":"element","tagName":"a","properties":{"href":"/part-10/3-oo-programming-techniques#iterators"},"children":[{"type":"text","value":"part 10"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Book"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" author"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" page_count"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"author "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" author\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"page_count "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" page_count\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bookshelf"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_books "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_book"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Book"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_books"},{"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":"book"},{"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":"# This is the iterator initialization method"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# The iteration variable(s) should be initialized 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":"__iter__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the method returns a reference to the object itself as "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the iterator is implemented within the same class definition"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This method returns the next item within the object"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If all items have been traversed, the StopIteration event is raised"}]},{"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":"__next__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_books"},{"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":"# Select the current item from the list within the object"}]},{"type":"text","value":"\n book "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_books"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n"},{"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":"# increase the counter (i.e. iteration variable) by one"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"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","comment"]},"children":[{"type":"text","value":"# return the current item"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" book\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"# All books have been traversed"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" StopIteration\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Test your classes"}]},{"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 b1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"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 Life of 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":"\"Montague Python\""}]},{"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":"123"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"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 Old Man and the C\""}]},{"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":"\"Ernest Hemingjavay\""}]},{"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":"204"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"A Good Cup of Java\""}]},{"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":"\"Caffee Coder\""}]},{"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":"997"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n shelf "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bookshelf"},{"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 shelf"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n shelf"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n shelf"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b3"},{"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":"# Create a list containing the names of all books"}]},{"type":"text","value":"\n book_names "},{"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":"text","value":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" book "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" shelf"},{"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":"book_names"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Products in shopping list","tmcname":"part11-09_products_in_shopping_list"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In part 10 you created an "},{"type":"element","tagName":"a","properties":{"href":"/part-10/3-oo-programming-techniques#programming-exercise-an-iterable-shopping-list"},"children":[{"type":"text","value":"iterable shopping list"}]},{"type":"text","value":", and we just learnt that an object created from an iterable class can be used with list comprehensions. The exercise template contains a stripped down version of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ShoppingList"}]},{"type":"text","value":" with just enough functionality to fulfil the requirements of this exercise."}]},{"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":"products_in_shopping_list(shopping_list, amount: int)"}]},{"type":"text","value":" which takes a ShoppingList object and an integer value as its arguments. The function returns a list of product names. The list should include only the products with at least the number of items specified by the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"amount"}]},{"type":"text","value":" parameter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should be implemented using list comprehensions. The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ShoppingList"}]},{"type":"text","value":" class definition should "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" be modified."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should work as follows:"}]},{"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":" ShoppingList"},{"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","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"bananas\""}]},{"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":"10"}]},{"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":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apples\""}]},{"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":"5"}]},{"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":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"alcohol free beer\""}]},{"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":"24"}]},{"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":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"pineapple\""}]},{"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":"1"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"the shopping list contains at least 8 of the following items:\""}]},{"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":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" products_in_shopping_list"},{"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":" "},{"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":"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":"product"},{"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":"the shopping list contains at least 8 of the following items:\nbananas\nalcohol free beer"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Price difference of cheaper properties","tmcname":"part11-10_cheaper_properties"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This exercise is a slightly modified version of the exercise "},{"type":"element","tagName":"a","properties":{"href":"/part-9/1-objects-and-references#programming-exercise-comparing-properties"},"children":[{"type":"text","value":"Comparing properties"}]},{"type":"text","value":" from part 9."}]},{"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":"cheaper_properties(properties: list, reference: RealProperty)"}]},{"type":"text","value":" which takes a list of properties and a single RealProperty object as its arguments. The function should return a list containing only those properties in the original list which are cheaper than the reference property, along with the price difference. The items in the returned list should be tuples, where the first item is the property itself and the second is the difference in price."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should be implemented using list comprehensions. The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The code for the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"RealProperty"}]},{"type":"text","value":" class is included in the exercise template and should not be changed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"a1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"16"}]},{"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":"5500"}]},{"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":"\"Central studio\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"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":"4200"}]},{"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":"\"Two bedrooms downtown\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"78"}]},{"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":"2500"}]},{"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":"\"Three bedrooms in the suburbs\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"215"}]},{"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":"500"}]},{"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":"\"Farm in the middle of nowhere\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"105"}]},{"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":"1700"}]},{"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":"\"Loft in a small town\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na6 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"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":"1200"}]},{"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":"2500"}]},{"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":"\"Countryside mansion\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nproperties "},{"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":"text","value":"a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a6"},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"cheaper options when compared 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":"a3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"description"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" cheaper_properties"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"properties"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a3"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"item"},{"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":"description"},{"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":"35"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" price difference "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"item"},{"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":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euros\""}]}]},{"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":"cheaper options when compared to Three bedrooms in the suburbs:\nCentral studio price difference 107000 euros\nTwo bedrooms downtown price difference 35400 euros\nFarm in the middle of nowhere price difference 87500 euros\nLoft in a small town price difference 16500 euros"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"comprehensions-and-dictionaries","style":"position:relative;"},"children":[{"type":"text","value":"Comprehensions and dictionaries"},{"type":"element","tagName":"a","properties":{"href":"#comprehensions-and-dictionaries","ariaLabel":"comprehensions and dictionaries 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":"There is nothing intrinsically \"listey\" about comprehensions. The result is a list because the comprehension statement is encased in square brackets, which indicate a Python list. Comprehensions work just as well with Python dictionaries if you use curly brackets instead. Remember, though, that dictionaries require key-value pairs. Both must be specified when a dictionary is created, also with comprehensions."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The basis of a comprehension can be any iterable series, be it a list, a string, a tuple, a dictionary, any of your own iterable classes, and so forth."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example we use a string as the basis of a dictionary. The dictionary contains all the unique characters in the string, along with the number of times they occurred:"}]},{"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":"\"hello there\""}]},{"type":"text","value":"\n\nchar_counts "},{"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":"text","value":"character "},{"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":"count"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"character"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sentence"},{"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":"char_counts"},{"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":"{'h': 2, 'e': 3, 'l': 2, 'o': 1, ' ': 1, 't': 1, 'r': 1}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The principle of the comprehension statement is exactly the same as with lists, but instead of a single value, the expression now consists of a key and a value. The general syntax looks like this:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"{ : for in }"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To finish off this section, lets take a look at factorials again. This time we store the results in a dictionary. The number itself is the key, while the value is the result of the factorial from our 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"factorial"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The function calculates the factorial n! for integers above zero \"\"\""}]},{"type":"text","value":"\n k "},{"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":" n "},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" n\n n "},{"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":"return"}]},{"type":"text","value":" k\n\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 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","operator"]},"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"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":"10"}]},{"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":"5"}]},{"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":"1"}]},{"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":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n factorials "},{"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":"text","value":"number "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" factorial"},{"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":" "},{"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","keyword"]},"children":[{"type":"text","value":"if"}]},{"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":"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":"factorials"},{"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":"{3: 6, 2: 2, 1: 1, 4: 24, 5: 120, 6: 720}"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lengths of strings","tmcname":"part11-11_lengths_of_strings"},"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":"lengths(strings: list)"}]},{"type":"text","value":" which takes a list of strings as its argument. The function should return a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"dictionary"}]},{"type":"text","value":" with the strings in the list as the keys and their lengths as the values."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should be implemented with a dictionary comprehension. The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should work as follows:"}]},{"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":"word_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":"\"once\""}]},{"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":"\"upon\""}]},{"type":"text","value":" "},{"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":"\"a\""}]},{"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":"\"time\""}]},{"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":"\"in\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nword_lengths "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" lengths"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"word_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":"word_lengths"},{"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":"{'once': 4, 'upon': 4, 'a': 1, 'time': 4, 'in': 2}"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Most common words","tmcname":"part11-12_most_common_words"},"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_words(filename: str, lower_limit: int)"}]},{"type":"text","value":" which takes a filename and an integer value for a lower limit as its arguments. The function should return a dictionary containing the occurrences of the words which appear at least the number of times specified in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lower_limit"}]},{"type":"text","value":" parameter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, say the function was used to process a file named "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"comprehensions.txt"}]},{"type":"text","value":" with the following contents:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"txt"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-txt"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-txt"]},"children":[{"type":"text","value":"List comprehension is an elegant way to define and create lists based on existing lists.\nList comprehension is generally more compact and faster than normal functions and loops for creating list.\nHowever, we should avoid writing very long list comprehensions in one line to ensure that code is user-friendly.\nRemember, every list comprehension can be rewritten in for loop, but every for loop can’t be rewritten in the form of list comprehension."}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the function is called with the arguments "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"most_common_words(\"comprehensions.txt\", 3)"}]},{"type":"text","value":" it should return"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'comprehension': 4, 'is': 3, 'and': 3, 'for': 3, 'list': 4, 'in': 3}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the case of letters affects the results, and all inflected forms are unique words in this exercise. That is, the words "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"List"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lists"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list"}]},{"type":"text","value":" are each separate words here, and only "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list"}]},{"type":"text","value":" has enough occurrences to make it to the returned list. All punctutation should be removed before counting up the occurrences."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is up to you to decide how to implement this. The easiest way would likely be to make use of list and dictionary comprehensions."}]}]}]},"html":"

    After this section

      \n
    • You will be able to use comprehensions with strings
    • \n
    • You will know how to use comprehensions with your own classes
    • \n
    • You will be able to create dictionary comprehensions
    • \n

    Lists are perhaps the most common target for comprehensions, but comprehensions work on any series of items, including strings. Just like in the list examples in the previous section, if a list comprehension is performed on a string, the items (i.e. the characters) in the string are plucked one by one, processed according to the expression given, and stored in a list.

    name = \"Peter Python\"\n\nuppercased = [character.upper() for character in name]\nprint(uppercased)

    ['P', 'E', 'T', 'E', 'R', ' ', 'P', 'Y', 'T', 'H', 'O', 'N']

    The result is indeed a list, as dictated by the bracket notation around the comprehension statement. If we wanted a string instead, we could use the string method join to parse the list into a string. Remember, the method is called on the string we want to use as the \"glue\" between the characters. Let's take a look at some examples:

    name = \"Peter\"\nchar_list = list(name)\nprint(char_list)\n\nprint(\"\".join(char_list))\nprint(\" \".join(char_list))\nprint(\",\".join(char_list))\nprint(\" and \".join(char_list))

    ['P', 'e', 't', 'e', 'r']\nPeter\nP e t e r\nP,e,t,e,r\nP and e and t and e and r

    List comprehensions and the join method make it easy to create new strings based on other strings. We could, for example, make a string which contains only the vowels from another string:

    test_string = \"Hello there, this is a test!\"\n\nvowels = [character for character in test_string if character in \"aeiou\"]\nnew_string = \"\".join(vowels)\n\nprint(new_string)

    eoeeiiae

    In the example above the list comprehension and the join method are on separate lines, but they could be combined into a single expression:

    test_string = \"Hello there, this is a test!\"\n\nvowel_string = \"\".join([character for character in test_string if character in \"aeiou\"])\n\nprint(vowel_string)

    Many Python programmers swear by these oneliners, so it is well worth your while to learn to read them. We could even add the split method to the mix, so that we can process entire sentences efficiently with a single statement. In the example below the first character from each word in a sentence is removed:

    sentence = \"Sheila keeps on selling seashells on the seashore\"\n\nsentence_no_initials = \" \".join([word[1:] for word in sentence.split()])\nprint(sentence_no_initials)

    heila eeps n elling eashells n he eashore

    Let's go through this step by step:

      \n
    • word[1:] extracts a substring from the second character (at index 1) onwards
    • \n
    • sentence.split() splits the sentence into sections at the given character. In this case there is no argument given to the method, so the sentence is split at space characters by default
    • \n
    • \" \".join() combines the items in the list into a new string using a space character between the items
    • \n

    A more traditional iterative approach could look like this

    sentence = \"Sheila keeps on selling seashells on the seashore\"\n\nword_list = []\nwords = sentence.split()\nfor word in words:\n    word_no_initials = word[1:]\n    word_list.append(word_no_initials)\n\nsentence_no_initials = \" \".join(word_list)\n\nprint(sentence_no_initials)

    Please write a function named filter_forbidden(string: str, forbidden: str) which takes two strings as its arguments. The function should return a new version of the first string. It should not contain any characters from the second string.

    The function should be implemented using list comprehensions. The maximum length of the function is three lines of code, including the header line beginning with the def keyword.

    Please have a look at the example below.

    sentence = \"Once! upon, a time: there was a python!??!?!\"\nfiltered = filter_forbidden(sentence, \"!?:,.\")\nprint(filtered)

    Once upon a time there was a python

    Own classes and comprehensions

    Comprehensions can be a useful tool for processing or formulating instances of your own classes, as we'll see in the following examples.

    First, let's have a look at the class Country which is a simple model for a single country, with attributes for the name and the population. In the main function below we first create some Country objects, and then use a list comprehension to select only those whose population is greater than five million.

    class Country:\n    \"\"\" This class models a single country with population \"\"\"\n    def __init__(self, name: str, population: int):\n        self.name = name\n        self.population = population\n\nif __name__ == \"__main__\":\n    finland = Country(\"Finland\", 6000000)\n    malta = Country(\"Malta\", 500000)\n    sweden = Country(\"Sweden\", 10000000)\n    iceland = Country(\"Iceland\", 350000)\n\n    countries = [finland, malta, sweden, iceland]\n\n    bigger_countries = [country.name for country in countries if country.population > 5000000]\n    for country in bigger_countries:\n        print(country)\n

    Finland\nSweden

    In the list comprehension above we selected only the name attribute from the Country objects, so the contents of the list could be printed directly. We could also create a new list of the countries themselves and access the name attribute in the for loop. This would be useful if the same list of countries was used also later in the program, or if we needed the population attribute in the for loop as well:

    if __name__ == \"__main__\":\n    finland = Country(\"Finland\", 6000000)\n    malta = Country(\"Malta\", 500000)\n    sweden = Country(\"Sweden\", 10000000)\n    iceland = Country(\"Iceland\", 350000)\n\n    countries = [finland, malta, sweden, iceland]\n\n    bigger_countries = [country for country in countries if country.population > 5000000]\n    for country in bigger_countries:\n        print(country.name, country.population)

    In the next example we have a class named RunningEvent which models a single foot race event with attributes for the length and the name of the race. We will use list comprehensions to create RunningEvent objects based on a list of race lengths.

    The parameter name has a default value in the constructor of the RunningEvent class, whIch is why we do not need to pass the name as an argument.

    class RunningEvent:\n    \"\"\" The class models a foot race event of a length of n metres  \"\"\"\n    def __init__(self, length: int, name: str = \"no name\"):\n        self.length = length\n        self.name = name\n\n    def __repr__(self):\n        return f\"{self.length} m. ({self.name})\"\n\nif __name__ == \"__main__\":\n    lengths = [100, 200, 1500, 3000, 42195]\n    events = [RunningEvent(length) for length in lengths]\n\n    # Print out all events\n    print(events)\n\n    # Pick one from the list and give it a name\n    marathon = events[-1] # the last item in the list\n    marathon.name = \"Marathon\"\n\n    # Print out everything again, including the new name\n    print(events)

    [100 m. (no name), 200 m. (no name), 1500 m. (no name), 3000 m. (no name), 42195 m. (no name)]\n[100 m. (no name), 200 m. (no name), 1500 m. (no name), 3000 m. (no name), 42195 m. (Marathon)]

    Now, let's find out what makes a series of items \"comprehendible\". In the previous part we learnt how to make our own classes iterable. It is exactly this same feature which also allows for list comprehensions. If your own class is iterable, it can be used as the basis of a list comprehension statement. The following class definitions are copied directly from part 10:

    class Book:\n    def __init__(self, name: str, author: str, page_count: int):\n        self.name = name\n        self.author = author\n        self.page_count = page_count\n\nclass Bookshelf:\n    def __init__(self):\n        self._books = []\n\n    def add_book(self, book: Book):\n        self._books.append(book)\n\n    # This is the iterator initialization method\n    # The iteration variable(s) should be initialized here\n    def __iter__(self):\n        self.n = 0\n        # the method returns a reference to the object itself as \n        # the iterator is implemented within the same class definition\n        return self\n\n    # This method returns the next item within the object\n    # If all items have been traversed, the StopIteration event is raised\n    def __next__(self):\n        if self.n < len(self._books):\n            # Select the current item from the list within the object\n            book = self._books[self.n]\n            # increase the counter (i.e. iteration variable) by one\n            self.n += 1\n            # return the current item\n            return book\n        else:\n            # All books have been traversed\n            raise StopIteration\n\n# Test your classes\nif __name__ == \"__main__\":\n    b1 = Book(\"The Life of Python\", \"Montague Python\", 123)\n    b2 = Book(\"The Old Man and the C\", \"Ernest Hemingjavay\", 204)\n    b3 = Book(\"A Good Cup of Java\", \"Caffee Coder\", 997)\n\n    shelf = Bookshelf()\n    shelf.add_book(b1)\n    shelf.add_book(b2)\n    shelf.add_book(b3)\n\n    # Create a list containing the names of all books\n    book_names = [book.name for book in shelf]\n    print(book_names)\n

    In part 10 you created an iterable shopping list, and we just learnt that an object created from an iterable class can be used with list comprehensions. The exercise template contains a stripped down version of the ShoppingList with just enough functionality to fulfil the requirements of this exercise.

    Please write a function named products_in_shopping_list(shopping_list, amount: int) which takes a ShoppingList object and an integer value as its arguments. The function returns a list of product names. The list should include only the products with at least the number of items specified by the amount parameter.

    The function should be implemented using list comprehensions. The maximum length of the function is two lines of code, including the header line beginning with the def keyword. The ShoppingList class definition should not be modified.

    The function should work as follows:

    my_list = ShoppingList()\nmy_list.add(\"bananas\", 10)\nmy_list.add(\"apples\", 5)\nmy_list.add(\"alcohol free beer\", 24)\nmy_list.add(\"pineapple\", 1)\n\nprint(\"the shopping list contains at least 8 of the following items:\")\nfor product in products_in_shopping_list(my_list, 8):\n    print(product)

    the shopping list contains at least 8 of the following items:\nbananas\nalcohol free beer

    This exercise is a slightly modified version of the exercise Comparing properties from part 9.

    Please write a function named cheaper_properties(properties: list, reference: RealProperty) which takes a list of properties and a single RealProperty object as its arguments. The function should return a list containing only those properties in the original list which are cheaper than the reference property, along with the price difference. The items in the returned list should be tuples, where the first item is the property itself and the second is the difference in price.

    The function should be implemented using list comprehensions. The maximum length of the function is two lines of code, including the header line beginning with the def keyword.

    The code for the RealProperty class is included in the exercise template and should not be changed.

    An example of the function in action:

    a1 = RealProperty(1, 16, 5500, \"Central studio\")\na2 = RealProperty(2, 38, 4200, \"Two bedrooms downtown\")\na3 = RealProperty(3, 78, 2500, \"Three bedrooms in the suburbs\")\na4 = RealProperty(6, 215, 500, \"Farm in the middle of nowhere\")\na5 = RealProperty(4, 105, 1700, \"Loft in a small town\")\na6 = RealProperty(25, 1200, 2500, \"Countryside mansion\")\n\nproperties = [a1, a2, a3, a4, a5, a6]\n\nprint(f\"cheaper options when compared to {a3.description}:\")\nfor item in cheaper_properties(properties, a3):\n    print(f\"{item[0].description:35} price difference {item[1]} euros\")

    cheaper options when compared to Three bedrooms in the suburbs:\nCentral studio price difference 107000 euros\nTwo bedrooms downtown price difference 35400 euros\nFarm in the middle of nowhere price difference 87500 euros\nLoft in a small town price difference 16500 euros

    Comprehensions and dictionaries

    There is nothing intrinsically \"listey\" about comprehensions. The result is a list because the comprehension statement is encased in square brackets, which indicate a Python list. Comprehensions work just as well with Python dictionaries if you use curly brackets instead. Remember, though, that dictionaries require key-value pairs. Both must be specified when a dictionary is created, also with comprehensions.

    The basis of a comprehension can be any iterable series, be it a list, a string, a tuple, a dictionary, any of your own iterable classes, and so forth.

    In the following example we use a string as the basis of a dictionary. The dictionary contains all the unique characters in the string, along with the number of times they occurred:

    sentence = \"hello there\"\n\nchar_counts = {character : sentence.count(character) for character in sentence}\nprint(char_counts)

    {'h': 2, 'e': 3, 'l': 2, 'o': 1, ' ': 1, 't': 1, 'r': 1}

    The principle of the comprehension statement is exactly the same as with lists, but instead of a single value, the expression now consists of a key and a value. The general syntax looks like this:

    {<key expression> : <value expression> for <item> in <series>}

    To finish off this section, lets take a look at factorials again. This time we store the results in a dictionary. The number itself is the key, while the value is the result of the factorial from our function:

    def factorial(n: int):\n    \"\"\" The function calculates the factorial n! for integers above zero \"\"\"\n    k = 1\n    while n >= 2:\n        k *= n\n        n -= 1\n    return k\n\nif __name__ == \"__main__\":\n    numbers = [-2, 3, 2, 1, 4, -10, 5, 1, 6]\n    factorials = {number : factorial(number) for number in numbers if number > 0}\n    print(factorials)

    {3: 6, 2: 2, 1: 1, 4: 24, 5: 120, 6: 720}

    Please write a function named lengths(strings: list) which takes a list of strings as its argument. The function should return a dictionary with the strings in the list as the keys and their lengths as the values.

    The function should be implemented with a dictionary comprehension. The maximum length of the function is two lines of code, including the header line beginning with the def keyword.

    The function should work as follows:

    word_list = [\"once\", \"upon\" , \"a\", \"time\", \"in\"]\n\nword_lengths = lengths(word_list)\nprint(word_lengths)

    {'once': 4, 'upon': 4, 'a': 1, 'time': 4, 'in': 2}

    Please write a function named most_common_words(filename: str, lower_limit: int) which takes a filename and an integer value for a lower limit as its arguments. The function should return a dictionary containing the occurrences of the words which appear at least the number of times specified in the lower_limit parameter.

    For example, say the function was used to process a file named comprehensions.txt with the following contents:

    List comprehension is an elegant way to define and create lists based on existing lists.\nList comprehension is generally more compact and faster than normal functions and loops for creating list.\nHowever, we should avoid writing very long list comprehensions in one line to ensure that code is user-friendly.\nRemember, every list comprehension can be rewritten in for loop, but every for loop can’t be rewritten in the form of list comprehension.

    When the function is called with the arguments most_common_words(\"comprehensions.txt\", 3) it should return

    {'comprehension': 4, 'is': 3, 'and': 3, 'for': 3, 'list': 4, 'in': 3}

    NB: the case of letters affects the results, and all inflected forms are unique words in this exercise. That is, the words List, lists and list are each separate words here, and only list has enough occurrences to make it to the returned list. All punctutation should be removed before counting up the occurrences.

    It is up to you to decide how to implement this. The easiest way would likely be to make use of list and dictionary comprehensions.

    ","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-11/2-more-comprehensions.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 able to use comprehensions with strings"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to use comprehensions with your own classes"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to create dictionary comprehensions"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lists are perhaps the most common target for comprehensions, but comprehensions work on any series of items, including strings. Just like in the list examples in the previous section, if a list comprehension is performed on a string, the items (i.e. the characters) in the string are plucked one by one, processed according to the expression given, and stored in 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":"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":"\"Peter Python\""}]},{"type":"text","value":"\n\nuppercased "},{"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":"text","value":"character"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"upper"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" name"},{"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":"uppercased"},{"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":"['P', 'E', 'T', 'E', 'R', ' ', 'P', 'Y', 'T', 'H', 'O', 'N']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The result is indeed a list, as dictated by the bracket notation around the comprehension statement. If we wanted a string instead, we could use the string method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"join"}]},{"type":"text","value":" to parse the list into a string. Remember, the method is called on the string we want to use as the \"glue\" between the characters. Let's take a look at 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":"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":"\"Peter\""}]},{"type":"text","value":"\nchar_list "},{"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":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"char_list"},{"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":"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":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"char_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":"\" \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"char_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":"\",\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"char_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":"\" and \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"char_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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['P', 'e', 't', 'e', 'r']\nPeter\nP e t e r\nP,e,t,e,r\nP and e and t and e and r"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"List comprehensions and the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"join"}]},{"type":"text","value":" method make it easy to create new strings based on other strings. We could, for example, make a string which contains only the vowels from 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":"test_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":"\"Hello there, this is a test!\""}]},{"type":"text","value":"\n\nvowels "},{"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":"text","value":"character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" test_string "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"aeiou\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nnew_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":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vowels"},{"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":"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":"eoeeiiae"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the example above the list comprehension and the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"join"}]},{"type":"text","value":" method are on separate lines, but they could be combined into a single expression:"}]},{"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":"test_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":"\"Hello there, this is a test!\""}]},{"type":"text","value":"\n\nvowel_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":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"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":"character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" test_string "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"aeiou\""}]},{"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":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"vowel_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Many Python programmers swear by these oneliners, so it is well worth your while to learn to read them. We could even add the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":" method to the mix, so that we can process entire sentences efficiently with a single statement. In the example below the first character from each word in a sentence is removed:"}]},{"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 keeps on selling seashells on the seashore\""}]},{"type":"text","value":"\n\nsentence_no_initials "},{"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":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"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":"word"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sentence"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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","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":"sentence_no_initials"},{"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":"heila eeps n elling eashells n he eashore"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's go through this step by step:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"word[1:]"}]},{"type":"text","value":" extracts a substring from the second character (at index 1) onwards"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sentence.split()"}]},{"type":"text","value":" splits the sentence into sections at the given character. In this case there is no argument given to the method, so the sentence is split at space characters by default"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\" \".join()"}]},{"type":"text","value":" combines the items in the list into a new string using a space character between the items"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A more traditional iterative approach 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":"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 keeps on selling seashells on the seashore\""}]},{"type":"text","value":"\n\nword_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","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nwords "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sentence"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n word_no_initials "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" word"},{"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":"text","value":"\n word_list"},{"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":"word_no_initials"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsentence_no_initials "},{"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":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"word_list"},{"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":"sentence_no_initials"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Filter forbidden","tmcname":"part11-08_filter_forbidden"},"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":"filter_forbidden(string: str, forbidden: str)"}]},{"type":"text","value":" which takes two strings as its arguments. The function should return a new version of the first string. It should not contain any characters from the second string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should be implemented using list comprehensions. The maximum length of the function is three lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please have a look at the example below."}]},{"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":"\"Once! upon, a time: there was a python!??!?!\""}]},{"type":"text","value":"\nfiltered "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" filter_forbidden"},{"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":" "},{"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"},{"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":"filtered"},{"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":"Once upon a time there was a python"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"own-classes-and-comprehensions","style":"position:relative;"},"children":[{"type":"text","value":"Own classes and comprehensions"},{"type":"element","tagName":"a","properties":{"href":"#own-classes-and-comprehensions","ariaLabel":"own classes and comprehensions 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":"Comprehensions can be a useful tool for processing or formulating instances of your own classes, as we'll see in the following examples."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First, let's have a look at the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Country"}]},{"type":"text","value":" which is a simple model for a single country, with attributes for the name and the population. In the main function below we first create some Country objects, and then use a list comprehension to select only those whose population is greater than five million."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Country"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" This class models a single country with population \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" population"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"population "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" population\n\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 finland "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Finland\""}]},{"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":"6000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n malta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Malta\""}]},{"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":"500000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n sweden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sweden\""}]},{"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":"10000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n iceland "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Iceland\""}]},{"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":"350000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n countries "},{"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":"text","value":"finland"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" malta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sweden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" iceland"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n bigger_countries "},{"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":"text","value":"country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" country "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" countries "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"population "},{"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":"5000000"}]},{"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":" country "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" bigger_countries"},{"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":"country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Finland\nSweden"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the list comprehension above we selected only the name attribute from the Country objects, so the contents of the list could be printed directly. We could also create a new list of the countries themselves and access the name attribute in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop. This would be useful if the same list of countries was used also later in the program, or if we needed the population attribute in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop as well:"}]},{"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":"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 finland "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Finland\""}]},{"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":"6000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n malta "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Malta\""}]},{"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":"500000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n sweden "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sweden\""}]},{"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":"10000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n iceland "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Iceland\""}]},{"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":"350000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n countries "},{"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":"text","value":"finland"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" malta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sweden"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" iceland"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n bigger_countries "},{"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":"text","value":"country "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" country "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" countries "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"population "},{"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":"5000000"}]},{"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":" country "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" bigger_countries"},{"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":"country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"population"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the next example we have a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"RunningEvent"}]},{"type":"text","value":" which models a single foot race event with attributes for the length and the name of the race. We will use list comprehensions to create "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"RunningEvent"}]},{"type":"text","value":" objects based on a list of race lengths."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The parameter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" has a default value in the constructor of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"RunningEvent"}]},{"type":"text","value":" class, whIch is why we do not need to pass the name as an argument."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"RunningEvent"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The class models a foot race event of a length of n metres \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" length"},{"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":"text","value":" name"},{"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":"str"}]},{"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":"\"no name\""}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" length\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\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":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" m. ("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","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 lengths "},{"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":"100"}]},{"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":"200"}]},{"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":"1500"}]},{"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":"3000"}]},{"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":"42195"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n events "},{"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":"text","value":"RunningEvent"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"length"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" length "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" lengths"},{"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":"# Print out all events"}]},{"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":"events"},{"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":"# Pick one from the list and give it a name"}]},{"type":"text","value":"\n marathon "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" events"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the last item in the list"}]},{"type":"text","value":"\n marathon"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"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":"\"Marathon\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Print out everything again, including the new name"}]},{"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":"events"},{"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":"[100 m. (no name), 200 m. (no name), 1500 m. (no name), 3000 m. (no name), 42195 m. (no name)]\n[100 m. (no name), 200 m. (no name), 1500 m. (no name), 3000 m. (no name), 42195 m. (Marathon)]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, let's find out what makes a series of items \"comprehendible\". In the previous part we learnt how to make our own classes iterable. It is exactly this same feature which also allows for list comprehensions. If your own class is iterable, it can be used as the basis of a list comprehension statement. The following class definitions are copied directly from "},{"type":"element","tagName":"a","properties":{"href":"/part-10/3-oo-programming-techniques#iterators"},"children":[{"type":"text","value":"part 10"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Book"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" author"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" page_count"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"author "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" author\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"page_count "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" page_count\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Bookshelf"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_books "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_book"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Book"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_books"},{"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":"book"},{"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":"# This is the iterator initialization method"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# The iteration variable(s) should be initialized 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":"__iter__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the method returns a reference to the object itself as "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the iterator is implemented within the same class definition"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This method returns the next item within the object"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If all items have been traversed, the StopIteration event is raised"}]},{"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":"__next__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_books"},{"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":"# Select the current item from the list within the object"}]},{"type":"text","value":"\n book "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_books"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n"},{"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":"# increase the counter (i.e. iteration variable) by one"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"n "},{"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","comment"]},"children":[{"type":"text","value":"# return the current item"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" book\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"# All books have been traversed"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" StopIteration\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Test your classes"}]},{"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 b1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"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 Life of 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":"\"Montague Python\""}]},{"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":"123"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"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 Old Man and the C\""}]},{"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":"\"Ernest Hemingjavay\""}]},{"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":"204"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n b3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"A Good Cup of Java\""}]},{"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":"\"Caffee Coder\""}]},{"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":"997"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n shelf "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Bookshelf"},{"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 shelf"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n shelf"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n shelf"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b3"},{"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":"# Create a list containing the names of all books"}]},{"type":"text","value":"\n book_names "},{"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":"text","value":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" book "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" shelf"},{"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":"book_names"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Products in shopping list","tmcname":"part11-09_products_in_shopping_list"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In part 10 you created an "},{"type":"element","tagName":"a","properties":{"href":"/part-10/3-oo-programming-techniques#programming-exercise-an-iterable-shopping-list"},"children":[{"type":"text","value":"iterable shopping list"}]},{"type":"text","value":", and we just learnt that an object created from an iterable class can be used with list comprehensions. The exercise template contains a stripped down version of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ShoppingList"}]},{"type":"text","value":" with just enough functionality to fulfil the requirements of this exercise."}]},{"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":"products_in_shopping_list(shopping_list, amount: int)"}]},{"type":"text","value":" which takes a ShoppingList object and an integer value as its arguments. The function returns a list of product names. The list should include only the products with at least the number of items specified by the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"amount"}]},{"type":"text","value":" parameter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should be implemented using list comprehensions. The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ShoppingList"}]},{"type":"text","value":" class definition should "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" be modified."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should work as follows:"}]},{"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":" ShoppingList"},{"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","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"bananas\""}]},{"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":"10"}]},{"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":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apples\""}]},{"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":"5"}]},{"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":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"alcohol free beer\""}]},{"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":"24"}]},{"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":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"pineapple\""}]},{"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":"1"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"the shopping list contains at least 8 of the following items:\""}]},{"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":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" products_in_shopping_list"},{"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":" "},{"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":"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":"product"},{"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":"the shopping list contains at least 8 of the following items:\nbananas\nalcohol free beer"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Price difference of cheaper properties","tmcname":"part11-10_cheaper_properties"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This exercise is a slightly modified version of the exercise "},{"type":"element","tagName":"a","properties":{"href":"/part-9/1-objects-and-references#programming-exercise-comparing-properties"},"children":[{"type":"text","value":"Comparing properties"}]},{"type":"text","value":" from part 9."}]},{"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":"cheaper_properties(properties: list, reference: RealProperty)"}]},{"type":"text","value":" which takes a list of properties and a single RealProperty object as its arguments. The function should return a list containing only those properties in the original list which are cheaper than the reference property, along with the price difference. The items in the returned list should be tuples, where the first item is the property itself and the second is the difference in price."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should be implemented using list comprehensions. The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The code for the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"RealProperty"}]},{"type":"text","value":" class is included in the exercise template and should not be changed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"a1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"16"}]},{"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":"5500"}]},{"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":"\"Central studio\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"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":"4200"}]},{"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":"\"Two bedrooms downtown\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"78"}]},{"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":"2500"}]},{"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":"\"Three bedrooms in the suburbs\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"215"}]},{"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":"500"}]},{"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":"\"Farm in the middle of nowhere\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"105"}]},{"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":"1700"}]},{"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":"\"Loft in a small town\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na6 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"25"}]},{"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":"1200"}]},{"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":"2500"}]},{"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":"\"Countryside mansion\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nproperties "},{"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":"text","value":"a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a6"},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"cheaper options when compared 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":"a3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"description"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" cheaper_properties"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"properties"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a3"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"item"},{"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":"description"},{"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":"35"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" price difference "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"item"},{"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":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euros\""}]}]},{"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":"cheaper options when compared to Three bedrooms in the suburbs:\nCentral studio price difference 107000 euros\nTwo bedrooms downtown price difference 35400 euros\nFarm in the middle of nowhere price difference 87500 euros\nLoft in a small town price difference 16500 euros"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"comprehensions-and-dictionaries","style":"position:relative;"},"children":[{"type":"text","value":"Comprehensions and dictionaries"},{"type":"element","tagName":"a","properties":{"href":"#comprehensions-and-dictionaries","ariaLabel":"comprehensions and dictionaries 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":"There is nothing intrinsically \"listey\" about comprehensions. The result is a list because the comprehension statement is encased in square brackets, which indicate a Python list. Comprehensions work just as well with Python dictionaries if you use curly brackets instead. Remember, though, that dictionaries require key-value pairs. Both must be specified when a dictionary is created, also with comprehensions."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The basis of a comprehension can be any iterable series, be it a list, a string, a tuple, a dictionary, any of your own iterable classes, and so forth."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example we use a string as the basis of a dictionary. The dictionary contains all the unique characters in the string, along with the number of times they occurred:"}]},{"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":"\"hello there\""}]},{"type":"text","value":"\n\nchar_counts "},{"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":"text","value":"character "},{"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":"count"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"character"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sentence"},{"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":"char_counts"},{"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":"{'h': 2, 'e': 3, 'l': 2, 'o': 1, ' ': 1, 't': 1, 'r': 1}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The principle of the comprehension statement is exactly the same as with lists, but instead of a single value, the expression now consists of a key and a value. The general syntax looks like this:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"{ : for in }"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To finish off this section, lets take a look at factorials again. This time we store the results in a dictionary. The number itself is the key, while the value is the result of the factorial from our 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"factorial"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The function calculates the factorial n! for integers above zero \"\"\""}]},{"type":"text","value":"\n k "},{"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":" n "},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" n\n n "},{"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":"return"}]},{"type":"text","value":" k\n\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 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","operator"]},"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"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":"10"}]},{"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":"5"}]},{"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":"1"}]},{"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":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n factorials "},{"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":"text","value":"number "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" factorial"},{"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":" "},{"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","keyword"]},"children":[{"type":"text","value":"if"}]},{"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":"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":"factorials"},{"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":"{3: 6, 2: 2, 1: 1, 4: 24, 5: 120, 6: 720}"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lengths of strings","tmcname":"part11-11_lengths_of_strings"},"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":"lengths(strings: list)"}]},{"type":"text","value":" which takes a list of strings as its argument. The function should return a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"dictionary"}]},{"type":"text","value":" with the strings in the list as the keys and their lengths as the values."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should be implemented with a dictionary comprehension. The maximum length of the function is two lines of code, including the header line beginning with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should work as follows:"}]},{"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":"word_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":"\"once\""}]},{"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":"\"upon\""}]},{"type":"text","value":" "},{"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":"\"a\""}]},{"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":"\"time\""}]},{"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":"\"in\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nword_lengths "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" lengths"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"word_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":"word_lengths"},{"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":"{'once': 4, 'upon': 4, 'a': 1, 'time': 4, 'in': 2}"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Most common words","tmcname":"part11-12_most_common_words"},"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_words(filename: str, lower_limit: int)"}]},{"type":"text","value":" which takes a filename and an integer value for a lower limit as its arguments. The function should return a dictionary containing the occurrences of the words which appear at least the number of times specified in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lower_limit"}]},{"type":"text","value":" parameter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, say the function was used to process a file named "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"comprehensions.txt"}]},{"type":"text","value":" with the following contents:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"txt"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-txt"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-txt"]},"children":[{"type":"text","value":"List comprehension is an elegant way to define and create lists based on existing lists.\nList comprehension is generally more compact and faster than normal functions and loops for creating list.\nHowever, we should avoid writing very long list comprehensions in one line to ensure that code is user-friendly.\nRemember, every list comprehension can be rewritten in for loop, but every for loop can’t be rewritten in the form of list comprehension."}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the function is called with the arguments "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"most_common_words(\"comprehensions.txt\", 3)"}]},{"type":"text","value":" it should return"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'comprehension': 4, 'is': 3, 'and': 3, 'for': 3, 'list': 4, 'in': 3}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the case of letters affects the results, and all inflected forms are unique words in this exercise. That is, the words "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"List"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lists"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list"}]},{"type":"text","value":" are each separate words here, and only "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list"}]},{"type":"text","value":" has enough occurrences to make it to the returned list. All punctutation should be removed before counting up the occurrences."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is up to you to decide how to implement this. The easiest way would likely be to make use of list and dictionary comprehensions."}]}]}]},"html":"

    After this section

      \n
    • You will be able to use comprehensions with strings
    • \n
    • You will know how to use comprehensions with your own classes
    • \n
    • You will be able to create dictionary comprehensions
    • \n

    Lists are perhaps the most common target for comprehensions, but comprehensions work on any series of items, including strings. Just like in the list examples in the previous section, if a list comprehension is performed on a string, the items (i.e. the characters) in the string are plucked one by one, processed according to the expression given, and stored in a list.

    name = \"Peter Python\"\n\nuppercased = [character.upper() for character in name]\nprint(uppercased)

    ['P', 'E', 'T', 'E', 'R', ' ', 'P', 'Y', 'T', 'H', 'O', 'N']

    The result is indeed a list, as dictated by the bracket notation around the comprehension statement. If we wanted a string instead, we could use the string method join to parse the list into a string. Remember, the method is called on the string we want to use as the \"glue\" between the characters. Let's take a look at some examples:

    name = \"Peter\"\nchar_list = list(name)\nprint(char_list)\n\nprint(\"\".join(char_list))\nprint(\" \".join(char_list))\nprint(\",\".join(char_list))\nprint(\" and \".join(char_list))

    ['P', 'e', 't', 'e', 'r']\nPeter\nP e t e r\nP,e,t,e,r\nP and e and t and e and r

    List comprehensions and the join method make it easy to create new strings based on other strings. We could, for example, make a string which contains only the vowels from another string:

    test_string = \"Hello there, this is a test!\"\n\nvowels = [character for character in test_string if character in \"aeiou\"]\nnew_string = \"\".join(vowels)\n\nprint(new_string)

    eoeeiiae

    In the example above the list comprehension and the join method are on separate lines, but they could be combined into a single expression:

    test_string = \"Hello there, this is a test!\"\n\nvowel_string = \"\".join([character for character in test_string if character in \"aeiou\"])\n\nprint(vowel_string)

    Many Python programmers swear by these oneliners, so it is well worth your while to learn to read them. We could even add the split method to the mix, so that we can process entire sentences efficiently with a single statement. In the example below the first character from each word in a sentence is removed:

    sentence = \"Sheila keeps on selling seashells on the seashore\"\n\nsentence_no_initials = \" \".join([word[1:] for word in sentence.split()])\nprint(sentence_no_initials)

    heila eeps n elling eashells n he eashore

    Let's go through this step by step:

      \n
    • word[1:] extracts a substring from the second character (at index 1) onwards
    • \n
    • sentence.split() splits the sentence into sections at the given character. In this case there is no argument given to the method, so the sentence is split at space characters by default
    • \n
    • \" \".join() combines the items in the list into a new string using a space character between the items
    • \n

    A more traditional iterative approach could look like this

    sentence = \"Sheila keeps on selling seashells on the seashore\"\n\nword_list = []\nwords = sentence.split()\nfor word in words:\n    word_no_initials = word[1:]\n    word_list.append(word_no_initials)\n\nsentence_no_initials = \" \".join(word_list)\n\nprint(sentence_no_initials)

    Please write a function named filter_forbidden(string: str, forbidden: str) which takes two strings as its arguments. The function should return a new version of the first string. It should not contain any characters from the second string.

    The function should be implemented using list comprehensions. The maximum length of the function is three lines of code, including the header line beginning with the def keyword.

    Please have a look at the example below.

    sentence = \"Once! upon, a time: there was a python!??!?!\"\nfiltered = filter_forbidden(sentence, \"!?:,.\")\nprint(filtered)

    Once upon a time there was a python

    Own classes and comprehensions

    Comprehensions can be a useful tool for processing or formulating instances of your own classes, as we'll see in the following examples.

    First, let's have a look at the class Country which is a simple model for a single country, with attributes for the name and the population. In the main function below we first create some Country objects, and then use a list comprehension to select only those whose population is greater than five million.

    class Country:\n    \"\"\" This class models a single country with population \"\"\"\n    def __init__(self, name: str, population: int):\n        self.name = name\n        self.population = population\n\nif __name__ == \"__main__\":\n    finland = Country(\"Finland\", 6000000)\n    malta = Country(\"Malta\", 500000)\n    sweden = Country(\"Sweden\", 10000000)\n    iceland = Country(\"Iceland\", 350000)\n\n    countries = [finland, malta, sweden, iceland]\n\n    bigger_countries = [country.name for country in countries if country.population > 5000000]\n    for country in bigger_countries:\n        print(country)\n

    Finland\nSweden

    In the list comprehension above we selected only the name attribute from the Country objects, so the contents of the list could be printed directly. We could also create a new list of the countries themselves and access the name attribute in the for loop. This would be useful if the same list of countries was used also later in the program, or if we needed the population attribute in the for loop as well:

    if __name__ == \"__main__\":\n    finland = Country(\"Finland\", 6000000)\n    malta = Country(\"Malta\", 500000)\n    sweden = Country(\"Sweden\", 10000000)\n    iceland = Country(\"Iceland\", 350000)\n\n    countries = [finland, malta, sweden, iceland]\n\n    bigger_countries = [country for country in countries if country.population > 5000000]\n    for country in bigger_countries:\n        print(country.name, country.population)

    In the next example we have a class named RunningEvent which models a single foot race event with attributes for the length and the name of the race. We will use list comprehensions to create RunningEvent objects based on a list of race lengths.

    The parameter name has a default value in the constructor of the RunningEvent class, whIch is why we do not need to pass the name as an argument.

    class RunningEvent:\n    \"\"\" The class models a foot race event of a length of n metres  \"\"\"\n    def __init__(self, length: int, name: str = \"no name\"):\n        self.length = length\n        self.name = name\n\n    def __repr__(self):\n        return f\"{self.length} m. ({self.name})\"\n\nif __name__ == \"__main__\":\n    lengths = [100, 200, 1500, 3000, 42195]\n    events = [RunningEvent(length) for length in lengths]\n\n    # Print out all events\n    print(events)\n\n    # Pick one from the list and give it a name\n    marathon = events[-1] # the last item in the list\n    marathon.name = \"Marathon\"\n\n    # Print out everything again, including the new name\n    print(events)

    [100 m. (no name), 200 m. (no name), 1500 m. (no name), 3000 m. (no name), 42195 m. (no name)]\n[100 m. (no name), 200 m. (no name), 1500 m. (no name), 3000 m. (no name), 42195 m. (Marathon)]

    Now, let's find out what makes a series of items \"comprehendible\". In the previous part we learnt how to make our own classes iterable. It is exactly this same feature which also allows for list comprehensions. If your own class is iterable, it can be used as the basis of a list comprehension statement. The following class definitions are copied directly from part 10:

    class Book:\n    def __init__(self, name: str, author: str, page_count: int):\n        self.name = name\n        self.author = author\n        self.page_count = page_count\n\nclass Bookshelf:\n    def __init__(self):\n        self._books = []\n\n    def add_book(self, book: Book):\n        self._books.append(book)\n\n    # This is the iterator initialization method\n    # The iteration variable(s) should be initialized here\n    def __iter__(self):\n        self.n = 0\n        # the method returns a reference to the object itself as \n        # the iterator is implemented within the same class definition\n        return self\n\n    # This method returns the next item within the object\n    # If all items have been traversed, the StopIteration event is raised\n    def __next__(self):\n        if self.n < len(self._books):\n            # Select the current item from the list within the object\n            book = self._books[self.n]\n            # increase the counter (i.e. iteration variable) by one\n            self.n += 1\n            # return the current item\n            return book\n        else:\n            # All books have been traversed\n            raise StopIteration\n\n# Test your classes\nif __name__ == \"__main__\":\n    b1 = Book(\"The Life of Python\", \"Montague Python\", 123)\n    b2 = Book(\"The Old Man and the C\", \"Ernest Hemingjavay\", 204)\n    b3 = Book(\"A Good Cup of Java\", \"Caffee Coder\", 997)\n\n    shelf = Bookshelf()\n    shelf.add_book(b1)\n    shelf.add_book(b2)\n    shelf.add_book(b3)\n\n    # Create a list containing the names of all books\n    book_names = [book.name for book in shelf]\n    print(book_names)\n

    In part 10 you created an iterable shopping list, and we just learnt that an object created from an iterable class can be used with list comprehensions. The exercise template contains a stripped down version of the ShoppingList with just enough functionality to fulfil the requirements of this exercise.

    Please write a function named products_in_shopping_list(shopping_list, amount: int) which takes a ShoppingList object and an integer value as its arguments. The function returns a list of product names. The list should include only the products with at least the number of items specified by the amount parameter.

    The function should be implemented using list comprehensions. The maximum length of the function is two lines of code, including the header line beginning with the def keyword. The ShoppingList class definition should not be modified.

    The function should work as follows:

    my_list = ShoppingList()\nmy_list.add(\"bananas\", 10)\nmy_list.add(\"apples\", 5)\nmy_list.add(\"alcohol free beer\", 24)\nmy_list.add(\"pineapple\", 1)\n\nprint(\"the shopping list contains at least 8 of the following items:\")\nfor product in products_in_shopping_list(my_list, 8):\n    print(product)

    the shopping list contains at least 8 of the following items:\nbananas\nalcohol free beer

    This exercise is a slightly modified version of the exercise Comparing properties from part 9.

    Please write a function named cheaper_properties(properties: list, reference: RealProperty) which takes a list of properties and a single RealProperty object as its arguments. The function should return a list containing only those properties in the original list which are cheaper than the reference property, along with the price difference. The items in the returned list should be tuples, where the first item is the property itself and the second is the difference in price.

    The function should be implemented using list comprehensions. The maximum length of the function is two lines of code, including the header line beginning with the def keyword.

    The code for the RealProperty class is included in the exercise template and should not be changed.

    An example of the function in action:

    a1 = RealProperty(1, 16, 5500, \"Central studio\")\na2 = RealProperty(2, 38, 4200, \"Two bedrooms downtown\")\na3 = RealProperty(3, 78, 2500, \"Three bedrooms in the suburbs\")\na4 = RealProperty(6, 215, 500, \"Farm in the middle of nowhere\")\na5 = RealProperty(4, 105, 1700, \"Loft in a small town\")\na6 = RealProperty(25, 1200, 2500, \"Countryside mansion\")\n\nproperties = [a1, a2, a3, a4, a5, a6]\n\nprint(f\"cheaper options when compared to {a3.description}:\")\nfor item in cheaper_properties(properties, a3):\n    print(f\"{item[0].description:35} price difference {item[1]} euros\")

    cheaper options when compared to Three bedrooms in the suburbs:\nCentral studio price difference 107000 euros\nTwo bedrooms downtown price difference 35400 euros\nFarm in the middle of nowhere price difference 87500 euros\nLoft in a small town price difference 16500 euros

    Comprehensions and dictionaries

    There is nothing intrinsically \"listey\" about comprehensions. The result is a list because the comprehension statement is encased in square brackets, which indicate a Python list. Comprehensions work just as well with Python dictionaries if you use curly brackets instead. Remember, though, that dictionaries require key-value pairs. Both must be specified when a dictionary is created, also with comprehensions.

    The basis of a comprehension can be any iterable series, be it a list, a string, a tuple, a dictionary, any of your own iterable classes, and so forth.

    In the following example we use a string as the basis of a dictionary. The dictionary contains all the unique characters in the string, along with the number of times they occurred:

    sentence = \"hello there\"\n\nchar_counts = {character : sentence.count(character) for character in sentence}\nprint(char_counts)

    {'h': 2, 'e': 3, 'l': 2, 'o': 1, ' ': 1, 't': 1, 'r': 1}

    The principle of the comprehension statement is exactly the same as with lists, but instead of a single value, the expression now consists of a key and a value. The general syntax looks like this:

    {<key expression> : <value expression> for <item> in <series>}

    To finish off this section, lets take a look at factorials again. This time we store the results in a dictionary. The number itself is the key, while the value is the result of the factorial from our function:

    def factorial(n: int):\n    \"\"\" The function calculates the factorial n! for integers above zero \"\"\"\n    k = 1\n    while n >= 2:\n        k *= n\n        n -= 1\n    return k\n\nif __name__ == \"__main__\":\n    numbers = [-2, 3, 2, 1, 4, -10, 5, 1, 6]\n    factorials = {number : factorial(number) for number in numbers if number > 0}\n    print(factorials)

    {3: 6, 2: 2, 1: 1, 4: 24, 5: 120, 6: 720}

    Please write a function named lengths(strings: list) which takes a list of strings as its argument. The function should return a dictionary with the strings in the list as the keys and their lengths as the values.

    The function should be implemented with a dictionary comprehension. The maximum length of the function is two lines of code, including the header line beginning with the def keyword.

    The function should work as follows:

    word_list = [\"once\", \"upon\" , \"a\", \"time\", \"in\"]\n\nword_lengths = lengths(word_list)\nprint(word_lengths)

    {'once': 4, 'upon': 4, 'a': 1, 'time': 4, 'in': 2}

    Please write a function named most_common_words(filename: str, lower_limit: int) which takes a filename and an integer value for a lower limit as its arguments. The function should return a dictionary containing the occurrences of the words which appear at least the number of times specified in the lower_limit parameter.

    For example, say the function was used to process a file named comprehensions.txt with the following contents:

    List comprehension is an elegant way to define and create lists based on existing lists.\nList comprehension is generally more compact and faster than normal functions and loops for creating list.\nHowever, we should avoid writing very long list comprehensions in one line to ensure that code is user-friendly.\nRemember, every list comprehension can be rewritten in for loop, but every for loop can’t be rewritten in the form of list comprehension.

    When the function is called with the arguments most_common_words(\"comprehensions.txt\", 3) it should return

    {'comprehension': 4, 'is': 3, 'and': 3, 'for': 3, 'list': 4, 'in': 3}

    NB: the case of letters affects the results, and all inflected forms are unique words in this exercise. That is, the words List, lists and list are each separate words here, and only list has enough occurrences to make it to the returned list. All punctutation should be removed before counting up the occurrences.

    It is up to you to decide how to implement this. The easiest way would likely be to make use of list and dictionary comprehensions.

    ","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-11/2-more-comprehensions.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-11/3-recursion/page-data.json b/page-data/part-11/3-recursion/page-data.json index fb1ffe053..84f9de79b 100644 --- a/page-data/part-11/3-recursion/page-data.json +++ b/page-data/part-11/3-recursion/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-11/3-recursion", - "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 know what recursion means"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to write a simple recursive function"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As we have seen many times before, functions can call other functions. For example:"}]},{"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":"hello"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name "},{"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":"str"}]},{"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":"\"Hello\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"hello_many_times"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name "},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" times "},{"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":"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":"times"},{"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 hello"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A function can also call itself, but we as programmers have to be careful when we do that. It is easy to fall into an endless loop of function calls, just like we fell into an infinite loop of repetitions with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loops if we left out the appropriate break conditions. So, trying to call a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hello"}]},{"type":"text","value":" function with the following definition"}]},{"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":"hello"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name "},{"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":"str"}]},{"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":"\"Hello\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hello"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# function calls itself"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"would cause a new kind of error:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"RecursionError: maximum recursion depth exceeded"}]}]},{"type":"element","tagName":"h2","properties":{"id":"what-does-recursion-mean","style":"position:relative;"},"children":[{"type":"text","value":"What does recursion mean?"},{"type":"element","tagName":"a","properties":{"href":"#what-does-recursion-mean","ariaLabel":"what does recursion mean 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 "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"recursion"}]},{"type":"text","value":" mentioned in the error above refers to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"defining something in terms of itself"}]},{"type":"text","value":". In a programming context it usually refers to a function which calls itself. For this to work without causing any infinite loops, the arguments passed to the function must change each time, so that the nested function calls will stop at some point. The basic principle here is the same as in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loops: there must always be a stop condition of some kind, and that condition must be triggered at some point in the execution."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at a simple function which adds zeroes to a list as long as there are less than 10 items in the list. This time we are not using a loop, however. If the condition is not yet met, the function calls itself:"}]},{"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":"fill_list"}]},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" If the length of the list is less than 10, add items to the list \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"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":" "},{"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":["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":"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","comment"]},"children":[{"type":"text","value":"# call the function again"}]},{"type":"text","value":"\n fill_list"},{"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\n\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 test_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":"text","value":"\n fill_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"test_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":"test_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":"[1, 2, 3, 4, 0, 0, 0, 0, 0, 0]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This functionality could just as well be achieved with a regular "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop:"}]},{"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":"fill_list"}]},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" If the length of the list is less than 10, add items to the list \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"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":" "},{"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":["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":"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\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 test_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":"text","value":"\n fill_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"test_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":"test_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The more traditional iterative approach produces a shorter program which is arguably also easier to understand. With the recursive version it is not as clear that throughout the process we are working on the very same list. That is, however, the case, and that is why the recursive function works just as well."}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Iterative or recursive?"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Computer science theory often differentiates between "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"iterative"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"recursive"}]},{"type":"text","value":" algorithms, so it is best to become familiar with these terms right from the start. Iterative solutions are ones that are based on sequential processing of items, often using loops. So far we have dealt with iterative methods quite exclusively. Recursive, on the other hand, refers to a method where the function calls itself with changing parameter values."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In principle it should be possible to solve any problem with either iterative or recursive methods. In practice, however, one or the other is usually clearly better suited to each problem. The ability to determine which is better comes largely with practice."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Add numbers to a list","tmcname":"part11-13_add_numbers_to_list"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"recursive function"}]},{"type":"text","value":" named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_numbers_to_list(numbers: list)"}]},{"type":"text","value":". The function takes a list of numbers as its argument, and adds new numbers to the list until the length of the list is divisible by five. Each number added to the list should be one greater than the last number in the list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function must call itself recursively. Please see the example below."}]},{"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":"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","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":"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nadd_numbers_to_list"},{"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":"print"}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 3, 4, 5, 10, 11, 12, 13, 14, 15]"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"recursion-and-return-values","style":"position:relative;"},"children":[{"type":"text","value":"Recursion and return values"},{"type":"element","tagName":"a","properties":{"href":"#recursion-and-return-values","ariaLabel":"recursion and return 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":"Recursive functions can also have return values. In the last few sections we've worked with factorials, so let's write a recursive factorial 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":"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":"factorial"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The function calculates the factorial n! for n >= 0 \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" n "},{"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":"2"}]},{"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":"# The factorial for 0 and 1 is 1"}]},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Call the function again with an argument that is one smaller"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" factorial"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n "},{"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":"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":"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":"# Tesing our function"}]},{"type":"text","value":"\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":"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":" "},{"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":"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\"The factorial of "}]},{"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","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" 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":"factorial"},{"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":"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"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The factorial of 1 is 1\nThe factorial of 2 is 2\nThe factorial of 3 is 6\nThe factorial of 4 is 24\nThe factorial of 5 is 120\nThe factorial of 6 is 720"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the parameter of the recursive factorial function is 0 or 1, the function returns 1, because this is how the factorial operation is defined. In any other case the function returns the value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n * factorial(n - 1)"}]},{"type":"text","value":", which is the value of its parameter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" multiplied by the return value of the function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"factorial(n - 1)"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The crucial part here is that the function definition contains a stop condition. If this is met, the recursion ends. In this case that condition is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n < 2"}]},{"type":"text","value":". We know it will be reached eventually, beacuse the value passed as the argument to the function is decreased by one on each level of the recursion."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":" can be a great help in making sense of recursive programs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It might make the above example a little clearer if we used helper 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":"factorial"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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":"if"}]},{"type":"text","value":" n "},{"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":"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":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n factorial_one_level_down "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" factorial"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n "},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n factorial_now "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" factorial_one_level_down\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" factorial_now\n \nfactorial"},{"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":"p","properties":{},"children":[{"type":"text","value":"Take a look at how the "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#code=def%20factorial%28n%3A%20int%29%3A%0A%20%20%20%20if%20n%20%3C%202%3A%0A%20%20%20%20%20%20%20%20return%201%0A%0A%20%20%20%20factorial_one_level_down%20%3D%20factorial%28n%20-%201%29%0A%20%20%20%20factorial_now%20%3D%20n%20*%20factorial_one_level_down%0A%20%20%20%20return%20factorial_now%0A%20%20%20%20%0Afactorial%285%29&cumulative=false&curInstr=5&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":" demonstrates the progress of the recursion."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The visualisation tool has a small quirk in the way it handles the call stack, as it seems to \"grow\" downwards. Usually call stacks are depicted as just that: stacks, where the new calls are placed on top. In the visualisation tool, the currently active function call is the shaded block at the bottom, which has its own copies of the variables visible."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the recursive factorial function is called, the call stack is built until the limit posed by "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n < 2"}]},{"type":"text","value":" is reached. Then the final function call in the stack returns with a value - it is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":", as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" is now less than 2. This return value is passed to the previous function call in the stack, where it is used to calculate that function call's return value, and so forth back out of the stack."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The return value of each function call is stored in the helper variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"factorial_now"}]},{"type":"text","value":". Please go through the visualisation carefully until you understand what happens at each step, and pay special attention to the value returned at each step."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/3c34c3d9cf7acacf51c58bb5bf68bac4/18539/11_1_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: 40.43478260869565%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAAAsTAAALEwEAmpwYAAABY0lEQVQoz02SW2/bMAyF8///Vfvat6XrliXZFhSOb/FNlqy7vkF2ZpQAJYE6OuSheACIMeK9J4RA3wnKomNRFkjEmEgpkTJujWy7cX59J6Uk5WC+S4lDXjKZkpKh69GLJoTIf0tPtJcSdb9jmhZVltjHgxgCYhIYrXfcSpg9VzeLGWMM1tjdnXVrlfZ8Rt1u+LpGlyXL+3eSlHvSnTBmqcasZHVR0j9axNgxi4lxGJnGiUUq3Ocn7ik5m2tbkhB8kbJJzucQPItS6EUxz4Gqjhi741azpxO6KAhNQ+g69PFILmbFhLAlyoRttfD+w3H6ZQkB+gF+nuCpZu/n8uj5fbxSXO4U5zvNrYbXV9LLC+ly2bMf7GKQyjHPhnGYkPOElMMqWQixEqYUkdrz8afjeK54+/aXWzPD9Upqmr26jdAGjPZ4F9B6oe97qrLafs+YvUV5RJyPaO1QSuPsU+6XScif8g9GrGwVGWimKQAAAABJRU5ErkJggg=='); 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":["/static/3c34c3d9cf7acacf51c58bb5bf68bac4/a0b58/11_1_1.webp 230w","/static/3c34c3d9cf7acacf51c58bb5bf68bac4/bc10c/11_1_1.webp 460w","/static/3c34c3d9cf7acacf51c58bb5bf68bac4/966d8/11_1_1.webp 920w","/static/3c34c3d9cf7acacf51c58bb5bf68bac4/2b317/11_1_1.webp 1074w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/3c34c3d9cf7acacf51c58bb5bf68bac4/81c8e/11_1_1.png 230w","/static/3c34c3d9cf7acacf51c58bb5bf68bac4/08a84/11_1_1.png 460w","/static/3c34c3d9cf7acacf51c58bb5bf68bac4/c0255/11_1_1.png 920w","/static/3c34c3d9cf7acacf51c58bb5bf68bac4/18539/11_1_1.png 1074w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/3c34c3d9cf7acacf51c58bb5bf68bac4/c0255/11_1_1.png","alt":"11 1 1","title":"11 1 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":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at another common recursive example: the Fibonacci number. In a Fibonacci sequence each number is the sum of the two preceding numbers. The first two numbers are here defined as 1 and 1, and the sequence then begins like this: 1, 1, 2, 3, 5, 8, 13, 21, 34."}]},{"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":"fibonacci"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The function returns the nth number in the Fibonacci sequence (1, 1, 2, 3, 5, 8 etc.); n > 0\"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" n "},{"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":"2"}]},{"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":"# the first two are ones"}]},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# All other numbers equal the sum of the two preceding numbers in the sequence"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" fibonacci"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n "},{"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":"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":" fibonacci"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n "},{"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":"2"}]},{"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":"# Test that everything works"}]},{"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","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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"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\"The "}]},{"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","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":". number in the Fibonacci sequence 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":"fibonacci"},{"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":"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":"The 1. number in the Fibonacci sequence is 1\nThe 2. number in the Fibonacci sequence is 1\nThe 3. number in the Fibonacci sequence is 2\nThe 4. number in the Fibonacci sequence is 3\nThe 5. number in the Fibonacci sequence is 5\nThe 6. number in the Fibonacci sequence is 8\nThe 7. number in the Fibonacci sequence is 13\nThe 8. number in the Fibonacci sequence is 21\nThe 9. number in the Fibonacci sequence is 34\nThe 10. number in the Fibonacci sequence is 55"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This time the stop condition is that the parameter is less than or equal to 2, because the entire sequence is defined from the first two numbers onwards, and we defined the first two numbers to be equal to 1."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, how does this function work in practice?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the function is called with 1 or 2 as its argument, it returns 1, as dictated by the condition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n <= 2"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the argument is 3 or greater, the function returns the value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fibonacci(n - 1) + fibonacci(n - 2)"}]},{"type":"text","value":". If the argument is 3 exactly, this value is equal to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fibonacci(2) + fibonacci(1)"}]},{"type":"text","value":", and we already know the result of both of those from the previous step. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1 + 1"}]},{"type":"text","value":" equals 2, which is indeed the third number in the Fibonacci sequence."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the argument is 4, the return value is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fibonacci(3) + fibonacci(2)"}]},{"type":"text","value":", which we now know to be "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 + 1"}]},{"type":"text","value":", which equals 3."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the argument is 5, the return value is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fibonacci(4) + fibonacci(3)"}]},{"type":"text","value":", which we now know to be "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"3 + 2"}]},{"type":"text","value":", which equals 5."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"And so forth, and so forth."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We can verify at every step that the function produces the correct results, which is often enough in basic programming tasks. The formal verifiability of algorithms is a topic for more advanced courses, such as "},{"type":"element","tagName":"a","properties":{"href":"https://studies.helsinki.fi/courses/cur/hy-opt-cur-2122-808d3413-3db0-4ab9-89d9-e816e94bf51d","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Data Structures and Algorithms"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Recursive sum","tmcname":"part11-14_recursive_sum"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a recursive function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"recursive_sum(number: int)"}]},{"type":"text","value":" which calculates the sum "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1 + 2 + ... + number"}]},{"type":"text","value":". The exercise template contains the following outline:"}]},{"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":"recursive_sum"}]},{"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":" "},{"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","comment"]},"children":[{"type":"text","value":"# if the number is 1, there is nothing else to add"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"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":"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":" number\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# fill in the rest of the function"}]}]}]}]},{"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":"text","value":"result "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" recursive_sum"},{"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":"\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":"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":"recursive_sum"},{"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","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":"recursive_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"6\n15\n55"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Balance all the brackets","tmcname":"part11-15_balanced_brackets"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balanced_brackets"}]},{"type":"text","value":" which takes a string as its argument. It checks if the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"round"}]},{"type":"text","value":" brackets, or parentheses, within the string are balanced. That is, for each opening bracket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":" there should be a closing bracket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":", and all pairs of brackets should be matched in order, i.e. the bracket pairs must not be crossed."}]},{"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":"balanced_brackets"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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":"if"}]},{"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":"my_string"},{"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":"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":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"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":"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":"'('"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" my_string"},{"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":" "},{"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":["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":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# remove first and last character"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" balanced_brackets"},{"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":"1"}]},{"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":"text","value":"\n\nok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanced_brackets"},{"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"},{"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":"ok"},{"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":"# there is one closing bracket too many, so this produces False"}]},{"type":"text","value":"\nok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanced_brackets"},{"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"},{"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":"ok"},{"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":"# this one starts with a closing bracket, False again"}]},{"type":"text","value":"\nok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanced_brackets"},{"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"},{"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":"ok"},{"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":"# this produces False because the function only handles entirely nested brackets"}]},{"type":"text","value":"\nok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanced_brackets"},{"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"},{"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":"ok"},{"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\nFalse\nFalse"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please expand the function so that it also works with square brackets "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]},{"type":"text","value":". The function should also ignore all characters which are not brackets "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"()"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]},{"type":"text","value":". The different types of brackets must be matched correctly in order."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please have a look at the examples below::"}]},{"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":"ok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanced_brackets"},{"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"},{"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":"ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanced_brackets"},{"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 version [3.7]) please use this one!\""}]},{"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":"ok"},{"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":"# this is no good, the closing bracket doesn't match"}]},{"type":"text","value":"\nok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanced_brackets"},{"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"},{"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":"ok"},{"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":"# different types of brackets are mismatched"}]},{"type":"text","value":"\nok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanced_brackets"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"([bad egg)]\""}]},{"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":"ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the function only needs to handle entirely nested brackets. The string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(x + 1)(y + 1)"}]},{"type":"text","value":" should produce "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" as the brackets are not nested within each other."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nTrue\nFalse\nFalse"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"binary-search","style":"position:relative;"},"children":[{"type":"text","value":"Binary search"},{"type":"element","tagName":"a","properties":{"href":"#binary-search","ariaLabel":"binary search 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 a binary search we have a sorted list of items and we are trying to find a certain item within. The order of the items could be, for example, numbers from smallest to greatest, or strings from alphabetically first to last. The method of sorting doesn't matter, as long as it is known and relevant to the item we are trying to find."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The idea of a binary search is to always look at the item at the very centre of the list. We then have three possible scenarios. If the item at the centre is"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the one we are looking for: we can return an indication that we found the item"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"smaller than the one we are looking for: we can re-do the search in the greater half of the list"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"greater than the one we are looking for: we can re-do the search in the smaller half of the list."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the list is empty, we can determine that the item was not found, and return an indication of that."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following image we can see how a binary search progresses as it looks for the number 24:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 777px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/ca600c1fe96166b67e6fe37b5f5dcc89/108f8/11_3_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: 43.47826086956522%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABXklEQVQoz5WQ3UvCUBiH989FdN9dRBAUEhFd1G13EXQREQTdCAYl5AojMQrLKfRBNmSVusCczjn3gbYv5xlzO2/pBgVB4I/Dj/fmOec5LwHjRFGURqMhiiLP86ZpEhgAYwh7FB/7fjj8HDyCHcexLMu2bU3TEELEdCy1dHq7mMgskNeRk+xsKjHxuDGfPlsm86vn1EqSWrvIR8ib/IcAAJ1uV9d1wzACEYIR1bLcYSWNbeuV9iejtp8MlpZaBV6hBZkWlGJLfW7KimUDQK/Xs20bIRTCOzn64K60+ZRZf4/uFu6jD5U96pXrmADgh7LDBOLaKKZphnC8yCaZeqxE79fTx28vJFM9pMu8NhQb+P7vbwOAYRi6rluWFcKu5/Wh72DXcwHwWLsHYubocjK3NZXdnotfUdUmALie//eSQFsQBI7jarVaYE7Ipi0hTUK6avWRO/j/Kc/zMMZBf8Nfq6LsorLTvyMAAAAASUVORK5CYII='); 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":["/static/ca600c1fe96166b67e6fe37b5f5dcc89/a0b58/11_3_1.webp 230w","/static/ca600c1fe96166b67e6fe37b5f5dcc89/bc10c/11_3_1.webp 460w","/static/ca600c1fe96166b67e6fe37b5f5dcc89/2e4ba/11_3_1.webp 777w"],"sizes":"(max-width: 777px) 100vw, 777px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/ca600c1fe96166b67e6fe37b5f5dcc89/81c8e/11_3_1.png 230w","/static/ca600c1fe96166b67e6fe37b5f5dcc89/08a84/11_3_1.png 460w","/static/ca600c1fe96166b67e6fe37b5f5dcc89/108f8/11_3_1.png 777w"],"sizes":"(max-width: 777px) 100vw, 777px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/ca600c1fe96166b67e6fe37b5f5dcc89/108f8/11_3_1.png","alt":"11 3 1","title":"11 3 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":"p","properties":{},"children":[{"type":"text","value":"Here is a recursive algorithm for a binary search:"}]},{"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":"binary_search"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"text","value":" item"},{"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":"text","value":" left "},{"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":"text","value":" right "},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The function returns True if the item is contained in the target list, False otherwise \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If the search area is empty, item was not found"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" left "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" right"},{"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","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Calculate the centre of the search area, integer result"}]},{"type":"text","value":"\n centre "},{"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":"text","value":"left"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"right"},{"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":"2"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If the item is found at the centre, return"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" target"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"centre"},{"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":" item"},{"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","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If the item is greater, search the greater half"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" target"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"centre"},{"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":" item"},{"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":" binary_search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" centre"},{"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":" right"},{"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":"# Else the item is smaller, search the smaller half"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":" binary_search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" left"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" centre"},{"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\n\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":"# Test your function"}]},{"type":"text","value":"\n target "},{"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":"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":" "},{"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":"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":" "},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"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":"13"}]},{"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":"14"}]},{"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":"18"}]},{"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":"binary_search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"2"}]},{"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":"0"}]},{"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":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"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":"binary_search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"13"}]},{"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":"0"}]},{"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":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"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":"binary_search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"6"}]},{"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":"0"}]},{"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":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"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":"binary_search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"15"}]},{"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":"0"}]},{"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":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"True\nTrue\nFalse\nFalse"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"binary_search"}]},{"type":"text","value":" function takes four arguments: the target list, the item being searched for, and the left and right edges of the search area. When the function is first called, the search area covers the entire target list. The left edge is at index "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" and the right edge is at index "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len(target)-1"}]},{"type":"text","value":". The function calculates the central index and checks that position on the list. Either the item was found, or the search continues to the smaller or greater half of the target list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's compare this to a simple linear search. In a linear search, the search area is traversed from the beginning onwards, until either the item is found, or we run out of search area. The number of steps needed to cover the entire search area grows "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"linearly"}]},{"type":"text","value":" at the same pace as the size of the search area. Each search step covers only one search candidate from the beginning of the search area. Let's assume the item searched for is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" found. If the search area is a million items long, we would have to take a million search steps to make sure the item is not in the search area."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In a binary search, on the other hand, the number of steps needed grows "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"logarithmically"}]},{"type":"text","value":". Let's assume again that the item searched for is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" found. The search area is cut in half with each step, as we know that the item is either smaller or greater than the current search candidate at the centre. 2 to the power of 20 (2^20) is already well over 1 million, so it will take at most 20 steps to cover the entire search area with a binary search. So, when we are dealing with sorted search areas, as we often are when dealing with computers and materials that are meant to be automatically processed, a binary search is much more efficient than a linear search."}]}]},"html":"

    After this section

      \n
    • You will know what recursion means
    • \n
    • You will be able to write a simple recursive function
    • \n

    As we have seen many times before, functions can call other functions. For example:

    def hello(name : str):\n    print(\"Hello\", name)\n\ndef hello_many_times(name : str, times : int):\n    for i in range(times):\n        hello(name)

    A function can also call itself, but we as programmers have to be careful when we do that. It is easy to fall into an endless loop of function calls, just like we fell into an infinite loop of repetitions with while loops if we left out the appropriate break conditions. So, trying to call a hello function with the following definition

    def hello(name : str):\n    print(\"Hello\", name)\n    hello(name) # function calls itself

    would cause a new kind of error:

    RecursionError: maximum recursion depth exceeded

    What does recursion mean?

    The recursion mentioned in the error above refers to defining something in terms of itself. In a programming context it usually refers to a function which calls itself. For this to work without causing any infinite loops, the arguments passed to the function must change each time, so that the nested function calls will stop at some point. The basic principle here is the same as in while loops: there must always be a stop condition of some kind, and that condition must be triggered at some point in the execution.

    Let's have a look at a simple function which adds zeroes to a list as long as there are less than 10 items in the list. This time we are not using a loop, however. If the condition is not yet met, the function calls itself:

    def fill_list(numbers: list):\n    \"\"\" If the length of the list is less than 10, add items to the list \"\"\"\n    if len(numbers) < 10:\n        numbers.append(0)\n        # call the function again\n        fill_list(numbers)\n\n\nif __name__ == \"__main__\":\n    test_list = [1,2,3,4]\n    fill_list(test_list)\n    print(test_list)

    [1, 2, 3, 4, 0, 0, 0, 0, 0, 0]

    This functionality could just as well be achieved with a regular while loop:

    def fill_list(numbers: list):\n    \"\"\" If the length of the list is less than 10, add items to the list \"\"\"\n    while len(numbers) < 10:\n        numbers.append(0)\n\nif __name__ == \"__main__\":\n    test_list = [1,2,3,4]\n    fill_list(test_list)\n    print(test_list)

    The more traditional iterative approach produces a shorter program which is arguably also easier to understand. With the recursive version it is not as clear that throughout the process we are working on the very same list. That is, however, the case, and that is why the recursive function works just as well.

    Computer science theory often differentiates between iterative and recursive algorithms, so it is best to become familiar with these terms right from the start. Iterative solutions are ones that are based on sequential processing of items, often using loops. So far we have dealt with iterative methods quite exclusively. Recursive, on the other hand, refers to a method where the function calls itself with changing parameter values.

    In principle it should be possible to solve any problem with either iterative or recursive methods. In practice, however, one or the other is usually clearly better suited to each problem. The ability to determine which is better comes largely with practice.

    Please write a recursive function named add_numbers_to_list(numbers: list). The function takes a list of numbers as its argument, and adds new numbers to the list until the length of the list is divisible by five. Each number added to the list should be one greater than the last number in the list.

    The function must call itself recursively. Please see the example below.

    numbers = [1,3,4,5,10,11]\nadd_numbers_to_list(numbers)\nprint(numbers)

    [1, 3, 4, 5, 10, 11, 12, 13, 14, 15]

    Recursion and return values

    Recursive functions can also have return values. In the last few sections we've worked with factorials, so let's write a recursive factorial function:

    \ndef factorial(n: int):\n    \"\"\" The function calculates the factorial n! for n >= 0 \"\"\"\n    if n < 2:\n        # The factorial for 0 and 1 is 1\n        return 1\n\n    # Call the function again with an argument that is one smaller\n    return n * factorial(n - 1)\n\nif __name__ == \"__main__\":\n    # Tesing our function\n    for i in range(1, 7):\n        print(f\"The factorial of {i} is {factorial(i)}\")\n

    The factorial of 1 is 1\nThe factorial of 2 is 2\nThe factorial of 3 is 6\nThe factorial of 4 is 24\nThe factorial of 5 is 120\nThe factorial of 6 is 720

    If the parameter of the recursive factorial function is 0 or 1, the function returns 1, because this is how the factorial operation is defined. In any other case the function returns the value n * factorial(n - 1), which is the value of its parameter n multiplied by the return value of the function call factorial(n - 1).

    The crucial part here is that the function definition contains a stop condition. If this is met, the recursion ends. In this case that condition is n < 2. We know it will be reached eventually, beacuse the value passed as the argument to the function is decreased by one on each level of the recursion.

    The visualisation tool can be a great help in making sense of recursive programs.

    It might make the above example a little clearer if we used helper variables:

    def factorial(n: int):\n    if n < 2:\n        return 1\n\n    factorial_one_level_down = factorial(n - 1)\n    factorial_now = n * factorial_one_level_down\n    return factorial_now\n    \nfactorial(5)

    Take a look at how the visualisation tool demonstrates the progress of the recursion.

    The visualisation tool has a small quirk in the way it handles the call stack, as it seems to \"grow\" downwards. Usually call stacks are depicted as just that: stacks, where the new calls are placed on top. In the visualisation tool, the currently active function call is the shaded block at the bottom, which has its own copies of the variables visible.

    When the recursive factorial function is called, the call stack is built until the limit posed by n < 2 is reached. Then the final function call in the stack returns with a value - it is 1, as n is now less than 2. This return value is passed to the previous function call in the stack, where it is used to calculate that function call's return value, and so forth back out of the stack.

    The return value of each function call is stored in the helper variable factorial_now. Please go through the visualisation carefully until you understand what happens at each step, and pay special attention to the value returned at each step.

    \n \n \n \n \n \n \"11\n \n \n

    Let's have a look at another common recursive example: the Fibonacci number. In a Fibonacci sequence each number is the sum of the two preceding numbers. The first two numbers are here defined as 1 and 1, and the sequence then begins like this: 1, 1, 2, 3, 5, 8, 13, 21, 34.

    def fibonacci(n: int):\n    \"\"\" The function returns the nth number in the Fibonacci sequence (1, 1, 2, 3, 5, 8 etc.); n > 0\"\"\"\n\n    if n <= 2:\n        # the first two are ones\n        return 1\n\n    # All other numbers equal the sum of the two preceding numbers in the sequence\n    return fibonacci(n - 1) + fibonacci(n - 2)\n\n# Test that everything works\nif __name__ == \"__main__\":\n    for i in range(1, 11):\n        print(f\"The {i}. number in the Fibonacci sequence is {fibonacci(i)}\")

    The 1. number in the Fibonacci sequence is 1\nThe 2. number in the Fibonacci sequence is 1\nThe 3. number in the Fibonacci sequence is 2\nThe 4. number in the Fibonacci sequence is 3\nThe 5. number in the Fibonacci sequence is 5\nThe 6. number in the Fibonacci sequence is 8\nThe 7. number in the Fibonacci sequence is 13\nThe 8. number in the Fibonacci sequence is 21\nThe 9. number in the Fibonacci sequence is 34\nThe 10. number in the Fibonacci sequence is 55

    This time the stop condition is that the parameter is less than or equal to 2, because the entire sequence is defined from the first two numbers onwards, and we defined the first two numbers to be equal to 1.

    So, how does this function work in practice?

    If the function is called with 1 or 2 as its argument, it returns 1, as dictated by the condition n <= 2.

    If the argument is 3 or greater, the function returns the value of fibonacci(n - 1) + fibonacci(n - 2). If the argument is 3 exactly, this value is equal to fibonacci(2) + fibonacci(1), and we already know the result of both of those from the previous step. 1 + 1 equals 2, which is indeed the third number in the Fibonacci sequence.

    If the argument is 4, the return value is fibonacci(3) + fibonacci(2), which we now know to be 2 + 1, which equals 3.

    If the argument is 5, the return value is fibonacci(4) + fibonacci(3), which we now know to be 3 + 2, which equals 5.

    And so forth, and so forth.

    We can verify at every step that the function produces the correct results, which is often enough in basic programming tasks. The formal verifiability of algorithms is a topic for more advanced courses, such as Data Structures and Algorithms.

    Please write a recursive function named recursive_sum(number: int) which calculates the sum 1 + 2 + ... + number. The exercise template contains the following outline:

    def recursive_sum(number: int):\n    # if the number is 1, there is nothing else to add\n    if number <= 1:\n        return number\n\n    # fill in the rest of the function

    Some examples:

    result = recursive_sum(3)\nprint(result)\n\nprint(recursive_sum(5))\nprint(recursive_sum(10))

    6\n15\n55

    The exercise template contains the function balanced_brackets which takes a string as its argument. It checks if the round brackets, or parentheses, within the string are balanced. That is, for each opening bracket ( there should be a closing bracket ), and all pairs of brackets should be matched in order, i.e. the bracket pairs must not be crossed.

    def balanced_brackets(my_string: str):\n    if len(my_string) == 0:\n        return True\n    if not (my_string[0] == '(' and my_string[-1] == ')'):\n        return False\n\n    # remove first and last character\n    return balanced_brackets(my_string[1:-1])\n\nok = balanced_brackets(\"(((())))\")\nprint(ok)\n\n# there is one closing bracket too many, so this produces False\nok = balanced_brackets(\"()())\")\nprint(ok)\n\n# this one starts with a closing bracket, False again\nok = balanced_brackets(\")()\")\nprint(ok)\n\n# this produces False because the function only handles entirely nested brackets\nok = balanced_brackets(\"()(())\")\nprint(ok)

    True\nFalse\nFalse\nFalse

    Please expand the function so that it also works with square brackets []. The function should also ignore all characters which are not brackets () or []. The different types of brackets must be matched correctly in order.

    Please have a look at the examples below::

    ok = balanced_brackets(\"([([])])\")\nprint(ok)\n\nok = balanced_brackets(\"(python version [3.7]) please use this one!\")\nprint(ok)\n\n# this is no good, the closing bracket doesn't match\nok = balanced_brackets(\"(()]\")\nprint(ok)\n\n# different types of brackets are mismatched\nok = balanced_brackets(\"([bad egg)]\")\nprint(ok)

    NB: the function only needs to handle entirely nested brackets. The string (x + 1)(y + 1) should produce False as the brackets are not nested within each other.

    True\nTrue\nFalse\nFalse

    Binary search

    In a binary search we have a sorted list of items and we are trying to find a certain item within. The order of the items could be, for example, numbers from smallest to greatest, or strings from alphabetically first to last. The method of sorting doesn't matter, as long as it is known and relevant to the item we are trying to find.

    The idea of a binary search is to always look at the item at the very centre of the list. We then have three possible scenarios. If the item at the centre is

      \n
    • the one we are looking for: we can return an indication that we found the item
    • \n
    • smaller than the one we are looking for: we can re-do the search in the greater half of the list
    • \n
    • greater than the one we are looking for: we can re-do the search in the smaller half of the list.
    • \n

    If the list is empty, we can determine that the item was not found, and return an indication of that.

    In the following image we can see how a binary search progresses as it looks for the number 24:

    \n \n linearly at the same pace as the size of the search area. Each search step covers only one search candidate from the beginning of the search area. Let's assume the item searched for is not found. If the search area is a million items long, we would have to take a million search steps to make sure the item is not in the search area.

    In a binary search, on the other hand, the number of steps needed grows logarithmically. Let's assume again that the item searched for is not found. The search area is cut in half with each step, as we know that the item is either smaller or greater than the current search candidate at the centre. 2 to the power of 20 (2^20) is already well over 1 million, so it will take at most 20 steps to cover the entire search area with a binary search. So, when we are dealing with sorted search areas, as we often are when dealing with computers and materials that are meant to be automatically processed, a binary search is much more efficient than a linear search.

    ","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-11/3-recursion.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know what recursion means"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to write a simple recursive function"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As we have seen many times before, functions can call other functions. For example:"}]},{"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":"hello"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name "},{"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":"str"}]},{"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":"\"Hello\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"hello_many_times"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name "},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" times "},{"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":"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":"times"},{"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 hello"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A function can also call itself, but we as programmers have to be careful when we do that. It is easy to fall into an endless loop of function calls, just like we fell into an infinite loop of repetitions with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loops if we left out the appropriate break conditions. So, trying to call a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hello"}]},{"type":"text","value":" function with the following definition"}]},{"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":"hello"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name "},{"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":"str"}]},{"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":"\"Hello\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n hello"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# function calls itself"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"would cause a new kind of error:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"RecursionError: maximum recursion depth exceeded"}]}]},{"type":"element","tagName":"h2","properties":{"id":"what-does-recursion-mean","style":"position:relative;"},"children":[{"type":"text","value":"What does recursion mean?"},{"type":"element","tagName":"a","properties":{"href":"#what-does-recursion-mean","ariaLabel":"what does recursion mean 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 "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"recursion"}]},{"type":"text","value":" mentioned in the error above refers to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"defining something in terms of itself"}]},{"type":"text","value":". In a programming context it usually refers to a function which calls itself. For this to work without causing any infinite loops, the arguments passed to the function must change each time, so that the nested function calls will stop at some point. The basic principle here is the same as in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loops: there must always be a stop condition of some kind, and that condition must be triggered at some point in the execution."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at a simple function which adds zeroes to a list as long as there are less than 10 items in the list. This time we are not using a loop, however. If the condition is not yet met, the function calls itself:"}]},{"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":"fill_list"}]},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" If the length of the list is less than 10, add items to the list \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"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":" "},{"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":["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":"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","comment"]},"children":[{"type":"text","value":"# call the function again"}]},{"type":"text","value":"\n fill_list"},{"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\n\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 test_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":"text","value":"\n fill_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"test_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":"test_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":"[1, 2, 3, 4, 0, 0, 0, 0, 0, 0]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This functionality could just as well be achieved with a regular "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop:"}]},{"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":"fill_list"}]},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" If the length of the list is less than 10, add items to the list \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"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":" "},{"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":["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":"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\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 test_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":"text","value":"\n fill_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"test_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":"test_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The more traditional iterative approach produces a shorter program which is arguably also easier to understand. With the recursive version it is not as clear that throughout the process we are working on the very same list. That is, however, the case, and that is why the recursive function works just as well."}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Iterative or recursive?"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Computer science theory often differentiates between "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"iterative"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"recursive"}]},{"type":"text","value":" algorithms, so it is best to become familiar with these terms right from the start. Iterative solutions are ones that are based on sequential processing of items, often using loops. So far we have dealt with iterative methods quite exclusively. Recursive, on the other hand, refers to a method where the function calls itself with changing parameter values."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In principle it should be possible to solve any problem with either iterative or recursive methods. In practice, however, one or the other is usually clearly better suited to each problem. The ability to determine which is better comes largely with practice."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Add numbers to a list","tmcname":"part11-13_add_numbers_to_list"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"recursive function"}]},{"type":"text","value":" named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_numbers_to_list(numbers: list)"}]},{"type":"text","value":". The function takes a list of numbers as its argument, and adds new numbers to the list until the length of the list is divisible by five. Each number added to the list should be one greater than the last number in the list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function must call itself recursively. Please see the example below."}]},{"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":"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","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":"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nadd_numbers_to_list"},{"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":"print"}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 3, 4, 5, 10, 11, 12, 13, 14, 15]"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"recursion-and-return-values","style":"position:relative;"},"children":[{"type":"text","value":"Recursion and return values"},{"type":"element","tagName":"a","properties":{"href":"#recursion-and-return-values","ariaLabel":"recursion and return 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":"Recursive functions can also have return values. In the last few sections we've worked with factorials, so let's write a recursive factorial 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":"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":"factorial"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The function calculates the factorial n! for n >= 0 \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" n "},{"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":"2"}]},{"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":"# The factorial for 0 and 1 is 1"}]},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Call the function again with an argument that is one smaller"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" factorial"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n "},{"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":"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":"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":"# Tesing our function"}]},{"type":"text","value":"\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":"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":" "},{"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":"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\"The factorial of "}]},{"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","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" 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":"factorial"},{"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":"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"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The factorial of 1 is 1\nThe factorial of 2 is 2\nThe factorial of 3 is 6\nThe factorial of 4 is 24\nThe factorial of 5 is 120\nThe factorial of 6 is 720"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the parameter of the recursive factorial function is 0 or 1, the function returns 1, because this is how the factorial operation is defined. In any other case the function returns the value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n * factorial(n - 1)"}]},{"type":"text","value":", which is the value of its parameter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" multiplied by the return value of the function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"factorial(n - 1)"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The crucial part here is that the function definition contains a stop condition. If this is met, the recursion ends. In this case that condition is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n < 2"}]},{"type":"text","value":". We know it will be reached eventually, beacuse the value passed as the argument to the function is decreased by one on each level of the recursion."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":" can be a great help in making sense of recursive programs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It might make the above example a little clearer if we used helper 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":"factorial"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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":"if"}]},{"type":"text","value":" n "},{"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":"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":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n factorial_one_level_down "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" factorial"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n "},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n factorial_now "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" factorial_one_level_down\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" factorial_now\n \nfactorial"},{"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":"p","properties":{},"children":[{"type":"text","value":"Take a look at how the "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#code=def%20factorial%28n%3A%20int%29%3A%0A%20%20%20%20if%20n%20%3C%202%3A%0A%20%20%20%20%20%20%20%20return%201%0A%0A%20%20%20%20factorial_one_level_down%20%3D%20factorial%28n%20-%201%29%0A%20%20%20%20factorial_now%20%3D%20n%20*%20factorial_one_level_down%0A%20%20%20%20return%20factorial_now%0A%20%20%20%20%0Afactorial%285%29&cumulative=false&curInstr=5&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=3&rawInputLstJSON=%5B%5D&textReferences=false","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":" demonstrates the progress of the recursion."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The visualisation tool has a small quirk in the way it handles the call stack, as it seems to \"grow\" downwards. Usually call stacks are depicted as just that: stacks, where the new calls are placed on top. In the visualisation tool, the currently active function call is the shaded block at the bottom, which has its own copies of the variables visible."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the recursive factorial function is called, the call stack is built until the limit posed by "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n < 2"}]},{"type":"text","value":" is reached. Then the final function call in the stack returns with a value - it is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":", as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" is now less than 2. This return value is passed to the previous function call in the stack, where it is used to calculate that function call's return value, and so forth back out of the stack."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The return value of each function call is stored in the helper variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"factorial_now"}]},{"type":"text","value":". Please go through the visualisation carefully until you understand what happens at each step, and pay special attention to the value returned at each step."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/3c34c3d9cf7acacf51c58bb5bf68bac4/18539/11_1_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: 40.43478260869565%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAAAsTAAALEwEAmpwYAAABY0lEQVQoz02SW2/bMAyF8///Vfvat6XrliXZFhSOb/FNlqy7vkF2ZpQAJYE6OuSheACIMeK9J4RA3wnKomNRFkjEmEgpkTJujWy7cX59J6Uk5WC+S4lDXjKZkpKh69GLJoTIf0tPtJcSdb9jmhZVltjHgxgCYhIYrXfcSpg9VzeLGWMM1tjdnXVrlfZ8Rt1u+LpGlyXL+3eSlHvSnTBmqcasZHVR0j9axNgxi4lxGJnGiUUq3Ocn7ik5m2tbkhB8kbJJzucQPItS6EUxz4Gqjhi741azpxO6KAhNQ+g69PFILmbFhLAlyoRttfD+w3H6ZQkB+gF+nuCpZu/n8uj5fbxSXO4U5zvNrYbXV9LLC+ly2bMf7GKQyjHPhnGYkPOElMMqWQixEqYUkdrz8afjeK54+/aXWzPD9Upqmr26jdAGjPZ4F9B6oe97qrLafs+YvUV5RJyPaO1QSuPsU+6XScif8g9GrGwVGWimKQAAAABJRU5ErkJggg=='); 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":["/static/3c34c3d9cf7acacf51c58bb5bf68bac4/a0b58/11_1_1.webp 230w","/static/3c34c3d9cf7acacf51c58bb5bf68bac4/bc10c/11_1_1.webp 460w","/static/3c34c3d9cf7acacf51c58bb5bf68bac4/966d8/11_1_1.webp 920w","/static/3c34c3d9cf7acacf51c58bb5bf68bac4/2b317/11_1_1.webp 1074w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/3c34c3d9cf7acacf51c58bb5bf68bac4/81c8e/11_1_1.png 230w","/static/3c34c3d9cf7acacf51c58bb5bf68bac4/08a84/11_1_1.png 460w","/static/3c34c3d9cf7acacf51c58bb5bf68bac4/c0255/11_1_1.png 920w","/static/3c34c3d9cf7acacf51c58bb5bf68bac4/18539/11_1_1.png 1074w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/3c34c3d9cf7acacf51c58bb5bf68bac4/c0255/11_1_1.png","alt":"11 1 1","title":"11 1 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":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at another common recursive example: the Fibonacci number. In a Fibonacci sequence each number is the sum of the two preceding numbers. The first two numbers are here defined as 1 and 1, and the sequence then begins like this: 1, 1, 2, 3, 5, 8, 13, 21, 34."}]},{"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":"fibonacci"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The function returns the nth number in the Fibonacci sequence (1, 1, 2, 3, 5, 8 etc.); n > 0\"\"\""}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" n "},{"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":"2"}]},{"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":"# the first two are ones"}]},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# All other numbers equal the sum of the two preceding numbers in the sequence"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" fibonacci"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n "},{"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":"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":" fibonacci"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n "},{"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":"2"}]},{"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":"# Test that everything works"}]},{"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","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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"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\"The "}]},{"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","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":". number in the Fibonacci sequence 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":"fibonacci"},{"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":"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":"The 1. number in the Fibonacci sequence is 1\nThe 2. number in the Fibonacci sequence is 1\nThe 3. number in the Fibonacci sequence is 2\nThe 4. number in the Fibonacci sequence is 3\nThe 5. number in the Fibonacci sequence is 5\nThe 6. number in the Fibonacci sequence is 8\nThe 7. number in the Fibonacci sequence is 13\nThe 8. number in the Fibonacci sequence is 21\nThe 9. number in the Fibonacci sequence is 34\nThe 10. number in the Fibonacci sequence is 55"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This time the stop condition is that the parameter is less than or equal to 2, because the entire sequence is defined from the first two numbers onwards, and we defined the first two numbers to be equal to 1."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, how does this function work in practice?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the function is called with 1 or 2 as its argument, it returns 1, as dictated by the condition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n <= 2"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the argument is 3 or greater, the function returns the value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fibonacci(n - 1) + fibonacci(n - 2)"}]},{"type":"text","value":". If the argument is 3 exactly, this value is equal to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fibonacci(2) + fibonacci(1)"}]},{"type":"text","value":", and we already know the result of both of those from the previous step. "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1 + 1"}]},{"type":"text","value":" equals 2, which is indeed the third number in the Fibonacci sequence."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the argument is 4, the return value is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fibonacci(3) + fibonacci(2)"}]},{"type":"text","value":", which we now know to be "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 + 1"}]},{"type":"text","value":", which equals 3."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the argument is 5, the return value is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fibonacci(4) + fibonacci(3)"}]},{"type":"text","value":", which we now know to be "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"3 + 2"}]},{"type":"text","value":", which equals 5."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"And so forth, and so forth."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We can verify at every step that the function produces the correct results, which is often enough in basic programming tasks. The formal verifiability of algorithms is a topic for more advanced courses, such as "},{"type":"element","tagName":"a","properties":{"href":"https://studies.helsinki.fi/courses/cur/hy-opt-cur-2122-808d3413-3db0-4ab9-89d9-e816e94bf51d","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Data Structures and Algorithms"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Recursive sum","tmcname":"part11-14_recursive_sum"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a recursive function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"recursive_sum(number: int)"}]},{"type":"text","value":" which calculates the sum "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1 + 2 + ... + number"}]},{"type":"text","value":". The exercise template contains the following outline:"}]},{"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":"recursive_sum"}]},{"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":" "},{"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","comment"]},"children":[{"type":"text","value":"# if the number is 1, there is nothing else to add"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"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":"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":" number\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# fill in the rest of the function"}]}]}]}]},{"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":"text","value":"result "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" recursive_sum"},{"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":"\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":"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":"recursive_sum"},{"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","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":"recursive_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"6\n15\n55"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Balance all the brackets","tmcname":"part11-15_balanced_brackets"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balanced_brackets"}]},{"type":"text","value":" which takes a string as its argument. It checks if the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"round"}]},{"type":"text","value":" brackets, or parentheses, within the string are balanced. That is, for each opening bracket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":" there should be a closing bracket "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":", and all pairs of brackets should be matched in order, i.e. the bracket pairs must not be crossed."}]},{"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":"balanced_brackets"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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":"if"}]},{"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":"my_string"},{"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":"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":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"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":"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":"'('"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" my_string"},{"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":" "},{"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":["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":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# remove first and last character"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" balanced_brackets"},{"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":"1"}]},{"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":"text","value":"\n\nok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanced_brackets"},{"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"},{"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":"ok"},{"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":"# there is one closing bracket too many, so this produces False"}]},{"type":"text","value":"\nok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanced_brackets"},{"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"},{"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":"ok"},{"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":"# this one starts with a closing bracket, False again"}]},{"type":"text","value":"\nok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanced_brackets"},{"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"},{"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":"ok"},{"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":"# this produces False because the function only handles entirely nested brackets"}]},{"type":"text","value":"\nok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanced_brackets"},{"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"},{"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":"ok"},{"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\nFalse\nFalse"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please expand the function so that it also works with square brackets "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]},{"type":"text","value":". The function should also ignore all characters which are not brackets "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"()"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]},{"type":"text","value":". The different types of brackets must be matched correctly in order."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please have a look at the examples below::"}]},{"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":"ok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanced_brackets"},{"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"},{"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":"ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanced_brackets"},{"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 version [3.7]) please use this one!\""}]},{"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":"ok"},{"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":"# this is no good, the closing bracket doesn't match"}]},{"type":"text","value":"\nok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanced_brackets"},{"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"},{"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":"ok"},{"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":"# different types of brackets are mismatched"}]},{"type":"text","value":"\nok "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balanced_brackets"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"([bad egg)]\""}]},{"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":"ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the function only needs to handle entirely nested brackets. The string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(x + 1)(y + 1)"}]},{"type":"text","value":" should produce "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" as the brackets are not nested within each other."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nTrue\nFalse\nFalse"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"binary-search","style":"position:relative;"},"children":[{"type":"text","value":"Binary search"},{"type":"element","tagName":"a","properties":{"href":"#binary-search","ariaLabel":"binary search 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 a binary search we have a sorted list of items and we are trying to find a certain item within. The order of the items could be, for example, numbers from smallest to greatest, or strings from alphabetically first to last. The method of sorting doesn't matter, as long as it is known and relevant to the item we are trying to find."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The idea of a binary search is to always look at the item at the very centre of the list. We then have three possible scenarios. If the item at the centre is"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the one we are looking for: we can return an indication that we found the item"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"smaller than the one we are looking for: we can re-do the search in the greater half of the list"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"greater than the one we are looking for: we can re-do the search in the smaller half of the list."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the list is empty, we can determine that the item was not found, and return an indication of that."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following image we can see how a binary search progresses as it looks for the number 24:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 777px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/ca600c1fe96166b67e6fe37b5f5dcc89/108f8/11_3_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: 43.47826086956522%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABXklEQVQoz5WQ3UvCUBiH989FdN9dRBAUEhFd1G13EXQREQTdCAYl5AojMQrLKfRBNmSVusCczjn3gbYv5xlzO2/pBgVB4I/Dj/fmOec5LwHjRFGURqMhiiLP86ZpEhgAYwh7FB/7fjj8HDyCHcexLMu2bU3TEELEdCy1dHq7mMgskNeRk+xsKjHxuDGfPlsm86vn1EqSWrvIR8ib/IcAAJ1uV9d1wzACEYIR1bLcYSWNbeuV9iejtp8MlpZaBV6hBZkWlGJLfW7KimUDQK/Xs20bIRTCOzn64K60+ZRZf4/uFu6jD5U96pXrmADgh7LDBOLaKKZphnC8yCaZeqxE79fTx28vJFM9pMu8NhQb+P7vbwOAYRi6rluWFcKu5/Wh72DXcwHwWLsHYubocjK3NZXdnotfUdUmALie//eSQFsQBI7jarVaYE7Ipi0hTUK6avWRO/j/Kc/zMMZBf8Nfq6LsorLTvyMAAAAASUVORK5CYII='); 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":["/static/ca600c1fe96166b67e6fe37b5f5dcc89/a0b58/11_3_1.webp 230w","/static/ca600c1fe96166b67e6fe37b5f5dcc89/bc10c/11_3_1.webp 460w","/static/ca600c1fe96166b67e6fe37b5f5dcc89/2e4ba/11_3_1.webp 777w"],"sizes":"(max-width: 777px) 100vw, 777px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/ca600c1fe96166b67e6fe37b5f5dcc89/81c8e/11_3_1.png 230w","/static/ca600c1fe96166b67e6fe37b5f5dcc89/08a84/11_3_1.png 460w","/static/ca600c1fe96166b67e6fe37b5f5dcc89/108f8/11_3_1.png 777w"],"sizes":"(max-width: 777px) 100vw, 777px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/ca600c1fe96166b67e6fe37b5f5dcc89/108f8/11_3_1.png","alt":"11 3 1","title":"11 3 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":"p","properties":{},"children":[{"type":"text","value":"Here is a recursive algorithm for a binary search:"}]},{"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":"binary_search"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"text","value":" item"},{"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":"text","value":" left "},{"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":"text","value":" right "},{"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","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The function returns True if the item is contained in the target list, False otherwise \"\"\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If the search area is empty, item was not found"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" left "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" right"},{"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","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Calculate the centre of the search area, integer result"}]},{"type":"text","value":"\n centre "},{"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":"text","value":"left"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"right"},{"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":"2"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If the item is found at the centre, return"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" target"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"centre"},{"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":" item"},{"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","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If the item is greater, search the greater half"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" target"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"centre"},{"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":" item"},{"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":" binary_search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" centre"},{"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":" right"},{"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":"# Else the item is smaller, search the smaller half"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":" binary_search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" left"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" centre"},{"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\n\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":"# Test your function"}]},{"type":"text","value":"\n target "},{"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":"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":" "},{"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":"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":" "},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"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":"13"}]},{"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":"14"}]},{"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":"18"}]},{"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":"binary_search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"2"}]},{"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":"0"}]},{"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":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"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":"binary_search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"13"}]},{"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":"0"}]},{"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":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"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":"binary_search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"6"}]},{"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":"0"}]},{"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":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"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":"binary_search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"15"}]},{"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":"0"}]},{"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":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"True\nTrue\nFalse\nFalse"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"binary_search"}]},{"type":"text","value":" function takes four arguments: the target list, the item being searched for, and the left and right edges of the search area. When the function is first called, the search area covers the entire target list. The left edge is at index "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" and the right edge is at index "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len(target)-1"}]},{"type":"text","value":". The function calculates the central index and checks that position on the list. Either the item was found, or the search continues to the smaller or greater half of the target list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's compare this to a simple linear search. In a linear search, the search area is traversed from the beginning onwards, until either the item is found, or we run out of search area. The number of steps needed to cover the entire search area grows "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"linearly"}]},{"type":"text","value":" at the same pace as the size of the search area. Each search step covers only one search candidate from the beginning of the search area. Let's assume the item searched for is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" found. If the search area is a million items long, we would have to take a million search steps to make sure the item is not in the search area."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In a binary search, on the other hand, the number of steps needed grows "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"logarithmically"}]},{"type":"text","value":". Let's assume again that the item searched for is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" found. The search area is cut in half with each step, as we know that the item is either smaller or greater than the current search candidate at the centre. 2 to the power of 20 (2^20) is already well over 1 million, so it will take at most 20 steps to cover the entire search area with a binary search. So, when we are dealing with sorted search areas, as we often are when dealing with computers and materials that are meant to be automatically processed, a binary search is much more efficient than a linear search."}]}]},"html":"

    After this section

      \n
    • You will know what recursion means
    • \n
    • You will be able to write a simple recursive function
    • \n

    As we have seen many times before, functions can call other functions. For example:

    def hello(name : str):\n    print(\"Hello\", name)\n\ndef hello_many_times(name : str, times : int):\n    for i in range(times):\n        hello(name)

    A function can also call itself, but we as programmers have to be careful when we do that. It is easy to fall into an endless loop of function calls, just like we fell into an infinite loop of repetitions with while loops if we left out the appropriate break conditions. So, trying to call a hello function with the following definition

    def hello(name : str):\n    print(\"Hello\", name)\n    hello(name) # function calls itself

    would cause a new kind of error:

    RecursionError: maximum recursion depth exceeded

    What does recursion mean?

    The recursion mentioned in the error above refers to defining something in terms of itself. In a programming context it usually refers to a function which calls itself. For this to work without causing any infinite loops, the arguments passed to the function must change each time, so that the nested function calls will stop at some point. The basic principle here is the same as in while loops: there must always be a stop condition of some kind, and that condition must be triggered at some point in the execution.

    Let's have a look at a simple function which adds zeroes to a list as long as there are less than 10 items in the list. This time we are not using a loop, however. If the condition is not yet met, the function calls itself:

    def fill_list(numbers: list):\n    \"\"\" If the length of the list is less than 10, add items to the list \"\"\"\n    if len(numbers) < 10:\n        numbers.append(0)\n        # call the function again\n        fill_list(numbers)\n\n\nif __name__ == \"__main__\":\n    test_list = [1,2,3,4]\n    fill_list(test_list)\n    print(test_list)

    [1, 2, 3, 4, 0, 0, 0, 0, 0, 0]

    This functionality could just as well be achieved with a regular while loop:

    def fill_list(numbers: list):\n    \"\"\" If the length of the list is less than 10, add items to the list \"\"\"\n    while len(numbers) < 10:\n        numbers.append(0)\n\nif __name__ == \"__main__\":\n    test_list = [1,2,3,4]\n    fill_list(test_list)\n    print(test_list)

    The more traditional iterative approach produces a shorter program which is arguably also easier to understand. With the recursive version it is not as clear that throughout the process we are working on the very same list. That is, however, the case, and that is why the recursive function works just as well.

    Computer science theory often differentiates between iterative and recursive algorithms, so it is best to become familiar with these terms right from the start. Iterative solutions are ones that are based on sequential processing of items, often using loops. So far we have dealt with iterative methods quite exclusively. Recursive, on the other hand, refers to a method where the function calls itself with changing parameter values.

    In principle it should be possible to solve any problem with either iterative or recursive methods. In practice, however, one or the other is usually clearly better suited to each problem. The ability to determine which is better comes largely with practice.

    Please write a recursive function named add_numbers_to_list(numbers: list). The function takes a list of numbers as its argument, and adds new numbers to the list until the length of the list is divisible by five. Each number added to the list should be one greater than the last number in the list.

    The function must call itself recursively. Please see the example below.

    numbers = [1,3,4,5,10,11]\nadd_numbers_to_list(numbers)\nprint(numbers)

    [1, 3, 4, 5, 10, 11, 12, 13, 14, 15]

    Recursion and return values

    Recursive functions can also have return values. In the last few sections we've worked with factorials, so let's write a recursive factorial function:

    \ndef factorial(n: int):\n    \"\"\" The function calculates the factorial n! for n >= 0 \"\"\"\n    if n < 2:\n        # The factorial for 0 and 1 is 1\n        return 1\n\n    # Call the function again with an argument that is one smaller\n    return n * factorial(n - 1)\n\nif __name__ == \"__main__\":\n    # Tesing our function\n    for i in range(1, 7):\n        print(f\"The factorial of {i} is {factorial(i)}\")\n

    The factorial of 1 is 1\nThe factorial of 2 is 2\nThe factorial of 3 is 6\nThe factorial of 4 is 24\nThe factorial of 5 is 120\nThe factorial of 6 is 720

    If the parameter of the recursive factorial function is 0 or 1, the function returns 1, because this is how the factorial operation is defined. In any other case the function returns the value n * factorial(n - 1), which is the value of its parameter n multiplied by the return value of the function call factorial(n - 1).

    The crucial part here is that the function definition contains a stop condition. If this is met, the recursion ends. In this case that condition is n < 2. We know it will be reached eventually, beacuse the value passed as the argument to the function is decreased by one on each level of the recursion.

    The visualisation tool can be a great help in making sense of recursive programs.

    It might make the above example a little clearer if we used helper variables:

    def factorial(n: int):\n    if n < 2:\n        return 1\n\n    factorial_one_level_down = factorial(n - 1)\n    factorial_now = n * factorial_one_level_down\n    return factorial_now\n    \nfactorial(5)

    Take a look at how the visualisation tool demonstrates the progress of the recursion.

    The visualisation tool has a small quirk in the way it handles the call stack, as it seems to \"grow\" downwards. Usually call stacks are depicted as just that: stacks, where the new calls are placed on top. In the visualisation tool, the currently active function call is the shaded block at the bottom, which has its own copies of the variables visible.

    When the recursive factorial function is called, the call stack is built until the limit posed by n < 2 is reached. Then the final function call in the stack returns with a value - it is 1, as n is now less than 2. This return value is passed to the previous function call in the stack, where it is used to calculate that function call's return value, and so forth back out of the stack.

    The return value of each function call is stored in the helper variable factorial_now. Please go through the visualisation carefully until you understand what happens at each step, and pay special attention to the value returned at each step.

    \n \n \n \n \n \n \"11\n \n \n

    Let's have a look at another common recursive example: the Fibonacci number. In a Fibonacci sequence each number is the sum of the two preceding numbers. The first two numbers are here defined as 1 and 1, and the sequence then begins like this: 1, 1, 2, 3, 5, 8, 13, 21, 34.

    def fibonacci(n: int):\n    \"\"\" The function returns the nth number in the Fibonacci sequence (1, 1, 2, 3, 5, 8 etc.); n > 0\"\"\"\n\n    if n <= 2:\n        # the first two are ones\n        return 1\n\n    # All other numbers equal the sum of the two preceding numbers in the sequence\n    return fibonacci(n - 1) + fibonacci(n - 2)\n\n# Test that everything works\nif __name__ == \"__main__\":\n    for i in range(1, 11):\n        print(f\"The {i}. number in the Fibonacci sequence is {fibonacci(i)}\")

    The 1. number in the Fibonacci sequence is 1\nThe 2. number in the Fibonacci sequence is 1\nThe 3. number in the Fibonacci sequence is 2\nThe 4. number in the Fibonacci sequence is 3\nThe 5. number in the Fibonacci sequence is 5\nThe 6. number in the Fibonacci sequence is 8\nThe 7. number in the Fibonacci sequence is 13\nThe 8. number in the Fibonacci sequence is 21\nThe 9. number in the Fibonacci sequence is 34\nThe 10. number in the Fibonacci sequence is 55

    This time the stop condition is that the parameter is less than or equal to 2, because the entire sequence is defined from the first two numbers onwards, and we defined the first two numbers to be equal to 1.

    So, how does this function work in practice?

    If the function is called with 1 or 2 as its argument, it returns 1, as dictated by the condition n <= 2.

    If the argument is 3 or greater, the function returns the value of fibonacci(n - 1) + fibonacci(n - 2). If the argument is 3 exactly, this value is equal to fibonacci(2) + fibonacci(1), and we already know the result of both of those from the previous step. 1 + 1 equals 2, which is indeed the third number in the Fibonacci sequence.

    If the argument is 4, the return value is fibonacci(3) + fibonacci(2), which we now know to be 2 + 1, which equals 3.

    If the argument is 5, the return value is fibonacci(4) + fibonacci(3), which we now know to be 3 + 2, which equals 5.

    And so forth, and so forth.

    We can verify at every step that the function produces the correct results, which is often enough in basic programming tasks. The formal verifiability of algorithms is a topic for more advanced courses, such as Data Structures and Algorithms.

    Please write a recursive function named recursive_sum(number: int) which calculates the sum 1 + 2 + ... + number. The exercise template contains the following outline:

    def recursive_sum(number: int):\n    # if the number is 1, there is nothing else to add\n    if number <= 1:\n        return number\n\n    # fill in the rest of the function

    Some examples:

    result = recursive_sum(3)\nprint(result)\n\nprint(recursive_sum(5))\nprint(recursive_sum(10))

    6\n15\n55

    The exercise template contains the function balanced_brackets which takes a string as its argument. It checks if the round brackets, or parentheses, within the string are balanced. That is, for each opening bracket ( there should be a closing bracket ), and all pairs of brackets should be matched in order, i.e. the bracket pairs must not be crossed.

    def balanced_brackets(my_string: str):\n    if len(my_string) == 0:\n        return True\n    if not (my_string[0] == '(' and my_string[-1] == ')'):\n        return False\n\n    # remove first and last character\n    return balanced_brackets(my_string[1:-1])\n\nok = balanced_brackets(\"(((())))\")\nprint(ok)\n\n# there is one closing bracket too many, so this produces False\nok = balanced_brackets(\"()())\")\nprint(ok)\n\n# this one starts with a closing bracket, False again\nok = balanced_brackets(\")()\")\nprint(ok)\n\n# this produces False because the function only handles entirely nested brackets\nok = balanced_brackets(\"()(())\")\nprint(ok)

    True\nFalse\nFalse\nFalse

    Please expand the function so that it also works with square brackets []. The function should also ignore all characters which are not brackets () or []. The different types of brackets must be matched correctly in order.

    Please have a look at the examples below::

    ok = balanced_brackets(\"([([])])\")\nprint(ok)\n\nok = balanced_brackets(\"(python version [3.7]) please use this one!\")\nprint(ok)\n\n# this is no good, the closing bracket doesn't match\nok = balanced_brackets(\"(()]\")\nprint(ok)\n\n# different types of brackets are mismatched\nok = balanced_brackets(\"([bad egg)]\")\nprint(ok)

    NB: the function only needs to handle entirely nested brackets. The string (x + 1)(y + 1) should produce False as the brackets are not nested within each other.

    True\nTrue\nFalse\nFalse

    Binary search

    In a binary search we have a sorted list of items and we are trying to find a certain item within. The order of the items could be, for example, numbers from smallest to greatest, or strings from alphabetically first to last. The method of sorting doesn't matter, as long as it is known and relevant to the item we are trying to find.

    The idea of a binary search is to always look at the item at the very centre of the list. We then have three possible scenarios. If the item at the centre is

      \n
    • the one we are looking for: we can return an indication that we found the item
    • \n
    • smaller than the one we are looking for: we can re-do the search in the greater half of the list
    • \n
    • greater than the one we are looking for: we can re-do the search in the smaller half of the list.
    • \n

    If the list is empty, we can determine that the item was not found, and return an indication of that.

    In the following image we can see how a binary search progresses as it looks for the number 24:

    \n \n linearly at the same pace as the size of the search area. Each search step covers only one search candidate from the beginning of the search area. Let's assume the item searched for is not found. If the search area is a million items long, we would have to take a million search steps to make sure the item is not in the search area.

    In a binary search, on the other hand, the number of steps needed grows logarithmically. Let's assume again that the item searched for is not found. The search area is cut in half with each step, as we know that the item is either smaller or greater than the current search candidate at the centre. 2 to the power of 20 (2^20) is already well over 1 million, so it will take at most 20 steps to cover the entire search area with a binary search. So, when we are dealing with sorted search areas, as we often are when dealing with computers and materials that are meant to be automatically processed, a binary search is much more efficient than a linear search.

    ","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-11/3-recursion.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-11/4-more-recursion-examples/page-data.json b/page-data/part-11/4-more-recursion-examples/page-data.json index 655a66206..69fca5503 100644 --- a/page-data/part-11/4-more-recursion-examples/page-data.json +++ b/page-data/part-11/4-more-recursion-examples/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-11/4-more-recursion-examples", - "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 binary trees and some recursive algorithms used to process them"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The real advantages of recursion become evident when we come across problems where iterative solutions are difficult to write. Let's take a look at "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"binary trees"}]},{"type":"text","value":", for instance. A binary tree is a branched structure where we have nodes, and at each node the structure branches, at most, into two child branches with nodes of their own. A binary tree could then look like this (computer science is often considered a branch of the natural sciences, but our understanding of trees is a little topsy-turvy, as you'll notice):"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 307px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/42ada93f7fad74a8205a13a46dc4de57/4651d/11_4_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: 78.69565217391303%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAAAsTAAALEwEAmpwYAAABsElEQVQ4y3WU54rDQAyE/f5PlUD+JIT03ntvpHcdn0C+9Z5PYFxWO5rRaB2IF9/vV++fz0fa7bZ0u13pdDryer3CdcuJi8AHs+Tr9apgAHHn3c35DzRwF7nDjHg8HpJOp6XX60k2m5XNZhMy53L3uOCBL9WiXq/L7XYL35E9n89j2xMBdCvsdjsZDodSLBb12RhZjEYjLTQYDMJ1n0hgH5CICSQ2m82QnVvwcrlIo9GQ9XqtuX5fI4DP51Oq1arKqlQqcr/f/ySfz2ft6Ww2k1KppCR8o0JAAABarVZSq9UiJrjSDoeDFiWHZz8nMMfoC5IskH08Hv9tvhnnylbJzBjMuKyaycec7XYbC0qP2VMul2W/3/8yHI/HcjqddCwslsulOkrPYOqfIGI6ncpkMtFcJiMEZAPOsWiBjFwup98xIO6EULBQKChL1IRjgzy3d7BCZr/fD110Gb7fbzUFtynMflhikjJ0mwx1zMlkMhHn/PPNyCDZgsngG2uBm9xqtfQZdq7DlgMI7DDEcm0iMAaWkaNHhUQioQlxhx8gfhSpVEpPi8VisZBkMin5fF5+ALPgzivet7o7AAAAAElFTkSuQmCC'); 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":["/static/42ada93f7fad74a8205a13a46dc4de57/a0b58/11_4_1.webp 230w","/static/42ada93f7fad74a8205a13a46dc4de57/b1a97/11_4_1.webp 307w"],"sizes":"(max-width: 307px) 100vw, 307px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/42ada93f7fad74a8205a13a46dc4de57/81c8e/11_4_1.png 230w","/static/42ada93f7fad74a8205a13a46dc4de57/4651d/11_4_1.png 307w"],"sizes":"(max-width: 307px) 100vw, 307px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/42ada93f7fad74a8205a13a46dc4de57/4651d/11_4_1.png","alt":"11 4 1","title":"11 4 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":"p","properties":{},"children":[{"type":"text","value":"Binary trees should at least theoretically be easy to handle recursively: if we want to perform some operation on every node in the tree, our algorithm simply needs to"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Process the current node"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Call itself on the child node on the left"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Call itself on the child node on the right"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 362px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/876ad26e1b0c7b303582f9e0f2cb0148/10600/11_4_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: 77.82608695652173%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAAAsTAAALEwEAmpwYAAABtklEQVQ4y4WU147CUAxE8/9/xQMvCCFEh9B7C703r45XvrrJAmvJCjjxeGauk+D1ekkyCa7dbleq1apMJhNX+y8DSYQB3m43CcNQTqeTXn3AbxG8Y0fs93upVCrS7/el0Wjof3/oR4ar1Uq2261sNhvZ7XYyn89lPB5LPp+X9XqtNRLpWDCdTsXvSWZwPp9jrGBULpcliiLHxoZR73Q6MhgMPkqPAS6XS2XRbDaVgQ9Icm84HEq9XpfH4/FeMqZbzGYzBWu1Wio7yfBwOGgd+dbnb4UyvF6vQiIRuRZIM1Bj6Afyk6AKiMHIYDVGo5EWGdDr9ZQtg/AWQGTaAFNiljnJsACIqzFCGoCYzz3++4A8h594iRXE8/n8ZXi5XGSxWGizNZgkaiy4rYhvB4B2SP5+xk75fr/rA6VSya2NSQSUwcViMQZSq9U0senP2thSM9lnBBgDaGS17PBYfJ6FsfkfHI9Hd0p4hdEkJ0gdG2i0PeQegwnsarfb+mryjDtlY4DByCLtZEl+o4QmXj17HalTY4A97742uVxO0um0ykqlUpLJZNxUP2jMZrP64aCnUCg4/2D4Ax3myJowab4fAAAAAElFTkSuQmCC'); 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":["/static/876ad26e1b0c7b303582f9e0f2cb0148/a0b58/11_4_2.webp 230w","/static/876ad26e1b0c7b303582f9e0f2cb0148/c2de8/11_4_2.webp 362w"],"sizes":"(max-width: 362px) 100vw, 362px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/876ad26e1b0c7b303582f9e0f2cb0148/81c8e/11_4_2.png 230w","/static/876ad26e1b0c7b303582f9e0f2cb0148/10600/11_4_2.png 362w"],"sizes":"(max-width: 362px) 100vw, 362px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/876ad26e1b0c7b303582f9e0f2cb0148/10600/11_4_2.png","alt":"11 4 2","title":"11 4 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":"As you can see from the image above, both the left and right \"subtrees\" are fully fledged binary trees themselves, and the only node left outside the recursive calls is the parent node, which is processed in step 1, before calling the function recursively. So, we can be sure that when the execution of the function finishes, each node has been visited exactly once."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An iterative version of a binary tree traversal would be much more complicated, as we would have to somehow keep track of all the nodes we have already visited. The same principles are true for all computational tree structures, not just binary ones."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A binary tree is easily modelled in Python code as well. We only need to write a class definition for a single node. It has a value attribute and attributes for the left and right child nodes:"}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Node"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The class represents a single node in a binary tree \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" left_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Node'"}]},{"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","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" right_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Node'"}]},{"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","boolean"]},"children":[{"type":"text","value":"None"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"value "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" value\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" left_child\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" right_child"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now let's assume we want to model the following tree:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 267px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/2d27ece550ee70e685bb54be04c9ea65/19e8f/11_4_3.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: 68.26086956521739%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAAsTAAALEwEAmpwYAAABfUlEQVQ4y4VTWcuDQAzc///sDyr0wRdrRVuqvbx6aA96gtArHxPIsm4t34LEI5lMZqKijvP5fDjebjfyPI+v+Xyuv8n3rqO6wKQgjmO63+/0er0oyzI6nU7/giqTkcT3+80xz3MKw5CKomCWAO7KN8GV2U3u5Xk0GvGo6/WakiTRY0tTu1YD2l1wAFBVVevdZrPhJr80/xoZAIvFgobDIaVpyu8wJtiIDMfjkXzf57zZbEZN03QzPJ/PbILE7Xb7ZYBoGEURy1DXNU0mk1aeBtzv9+wkOoIpDLF1lVzIAaZwHc1bDGWU5/NJ/X6fx0CEXqbjJiDAwAxMB4NByyS9h8vlkhOlcDwe0+PxaIGaTEAApyxLWq1WOk/hAXpMp9NWwfV6pV6vx3+L7aS9Ea7rskk8MoSFq9BPktEJI2FsMDX/EHt8EIJM0BwSKDDY7XZ6TYQdlvhwOLDb0szUU1yHQaiHBI7jkEIx2NhrEgQBXS4XXmQA/2IIIriwJTDoD1qJNDsmtBRAAAAAAElFTkSuQmCC'); 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":["/static/2d27ece550ee70e685bb54be04c9ea65/a0b58/11_4_3.webp 230w","/static/2d27ece550ee70e685bb54be04c9ea65/9156e/11_4_3.webp 267w"],"sizes":"(max-width: 267px) 100vw, 267px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/2d27ece550ee70e685bb54be04c9ea65/81c8e/11_4_3.png 230w","/static/2d27ece550ee70e685bb54be04c9ea65/19e8f/11_4_3.png 267w"],"sizes":"(max-width: 267px) 100vw, 267px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/2d27ece550ee70e685bb54be04c9ea65/19e8f/11_4_3.png","alt":"11 4 3","title":"11 4 3","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":"We could achieve this with the following code:"}]},{"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":"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 tree "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"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\n tree"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"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":"\n tree"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"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 tree"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"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\n tree"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"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":"text","value":"\n tree"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"recursive-binary-tree-algorithms","style":"position:relative;"},"children":[{"type":"text","value":"Recursive binary tree algorithms"},{"type":"element","tagName":"a","properties":{"href":"#recursive-binary-tree-algorithms","ariaLabel":"recursive binary tree algorithms 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":"First, let's take a look at an algorithm which prints out all the nodes in a binary tree one by one. In these following examples we will be working with the binary tree defined above."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The argument to the printing function is the root node of the binary tree. This is the node at the very top in our illustration above. All other nodes are "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"children"}]},{"type":"text","value":" to this node:"}]},{"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":"\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_nodes"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Node"},{"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":"root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"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":"if"}]},{"type":"text","value":" root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n print_nodes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child"},{"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":"if"}]},{"type":"text","value":" root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n print_nodes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function prints the value of the node passed as an argument, and then calls itself on the left and right child nodes, assuming the nodes are defined. This is a very simple algorithm, but it efficiently and reliably traverses all nodes in the tree, no matter the size of the tree. Crucially, no node is visited twice. Each value is printed only once."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we pass the root node "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tree"}]},{"type":"text","value":" of the binary tree illustrated above as an argument to the function, it prints out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n3\n5\n8\n4\n11"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see from the order of the nodesin the printout, the algorithm first moves down the \"left leg\" of the tree down to the very bottom, and from there traverses the other nodes in order."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Similarly, we can write an algorithm for calculating the sum of all the values stored in the nodes of the tree:"}]},{"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":"sum_of_nodes"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Node"},{"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 node_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"value\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n node_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" sum_of_nodes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child"},{"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":"if"}]},{"type":"text","value":" root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n node_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" sum_of_nodes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child"},{"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":" node_sum"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"node_sum"}]},{"type":"text","value":" is initialised to equal the value of the current node. The value in the variable is then augmented by recursive calls to the node sums of the left and right child trees (first making sure they exist, of course). This result is then returned"}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Greatest node","tmcname":"part11-16_greatest_node"},"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":"greatest_node(root: Node)"}]},{"type":"text","value":" which takes the root node of a binary tree as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return the value of the node with the greatest value within the tree. The tree should be traversed recursively."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hint: the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum_of_nodes"}]},{"type":"text","value":" in the example above may come in handy."}]},{"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":"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 tree "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"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\n tree"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"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":"\n tree"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"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 tree"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"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\n tree"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"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":"text","value":"\n tree"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"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":"greatest_node"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tree"},{"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":"11"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"a-sorted-binary-tree","style":"position:relative;"},"children":[{"type":"text","value":"A sorted binary tree"},{"type":"element","tagName":"a","properties":{"href":"#a-sorted-binary-tree","ariaLabel":"a sorted binary tree 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":"A binary tree is especially useful when the nodes are sorted in a certain way. This makes finding nodes in the tree fast and efficient."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's take a look a tree which is sorted as follows: the left child of each node is smaller than the node itself, and the right child is correspondingly greater."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 307px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/42ada93f7fad74a8205a13a46dc4de57/4651d/11_4_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: 78.69565217391303%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAAAsTAAALEwEAmpwYAAABsElEQVQ4y3WU54rDQAyE/f5PlUD+JIT03ntvpHcdn0C+9Z5PYFxWO5rRaB2IF9/vV++fz0fa7bZ0u13pdDryer3CdcuJi8AHs+Tr9apgAHHn3c35DzRwF7nDjHg8HpJOp6XX60k2m5XNZhMy53L3uOCBL9WiXq/L7XYL35E9n89j2xMBdCvsdjsZDodSLBb12RhZjEYjLTQYDMJ1n0hgH5CICSQ2m82QnVvwcrlIo9GQ9XqtuX5fI4DP51Oq1arKqlQqcr/f/ySfz2ft6Ww2k1KppCR8o0JAAABarVZSq9UiJrjSDoeDFiWHZz8nMMfoC5IskH08Hv9tvhnnylbJzBjMuKyaycec7XYbC0qP2VMul2W/3/8yHI/HcjqddCwslsulOkrPYOqfIGI6ncpkMtFcJiMEZAPOsWiBjFwup98xIO6EULBQKChL1IRjgzy3d7BCZr/fD110Gb7fbzUFtynMflhikjJ0mwx1zMlkMhHn/PPNyCDZgsngG2uBm9xqtfQZdq7DlgMI7DDEcm0iMAaWkaNHhUQioQlxhx8gfhSpVEpPi8VisZBkMin5fF5+ALPgzivet7o7AAAAAElFTkSuQmCC'); 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":["/static/42ada93f7fad74a8205a13a46dc4de57/a0b58/11_4_1.webp 230w","/static/42ada93f7fad74a8205a13a46dc4de57/b1a97/11_4_1.webp 307w"],"sizes":"(max-width: 307px) 100vw, 307px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/42ada93f7fad74a8205a13a46dc4de57/81c8e/11_4_1.png 230w","/static/42ada93f7fad74a8205a13a46dc4de57/4651d/11_4_1.png 307w"],"sizes":"(max-width: 307px) 100vw, 307px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/42ada93f7fad74a8205a13a46dc4de57/4651d/11_4_1.png","alt":"11 4 1","title":"11 4 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":"p","properties":{},"children":[{"type":"text","value":"Now we can write a recursive algorithm for searching for nodes. The idea is very similar to the binary search from the previous section: if the current node is the node we are looking for, return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":". Else, continue recursively with either the left or the right child tree. If the node is not defined, return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"."}]},{"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":"find_node"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Node"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" 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","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" root "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"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","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" value "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" value "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"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":" find_node"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" 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":"return"}]},{"type":"text","value":" find_node"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bosses and subordinates","tmcname":"part11-17_bosses_and_subordinates"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Employee"}]},{"type":"text","value":" models an employee of a company:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Employee"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"subordinates "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_subordinate"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" employee"},{"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":"'Employee'"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"subordinates"},{"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":"employee"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"count_subordinates(employee: Employee)"}]},{"type":"text","value":" which recursively counts the number of subordinates each employee has."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"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 t1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Employee"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sally\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n t2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Employee"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n t3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Employee"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Matthew\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n t4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Employee"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n t5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Employee"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Adele\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n t6 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Employee"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Claire\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n t1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_subordinate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"t4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n t1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_subordinate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"t6"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n t4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_subordinate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"t2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n t4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_subordinate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"t3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n t4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_subordinate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"t5"},{"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":"count_subordinates"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"t1"},{"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":"count_subordinates"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"t4"},{"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":"count_subordinates"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"t5"},{"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\n0"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"revisiting-the-times-before-recursion","style":"position:relative;"},"children":[{"type":"text","value":"Revisiting the times before recursion"},{"type":"element","tagName":"a","properties":{"href":"#revisiting-the-times-before-recursion","ariaLabel":"revisiting the times before recursion 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":"Let's finish off this part of the material with a slightly larger exercise concentrating on object oriented programming principles. We do not recommend using recursion in this series of tasks, but list comprehension techniques will come in useful."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"OrderBook","tmcname":"part11-18_order_book"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will write two different classes, which will in turn form the backbone of the programming exercise which follows this one, where you will write an interactive application."}]},{"type":"element","tagName":"h2","properties":{"id":"task","style":"position:relative;"},"children":[{"type":"text","value":"Task"},{"type":"element","tagName":"a","properties":{"href":"#task","ariaLabel":"task 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":"Please write a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Task"}]},{"type":"text","value":" which models a single task in a software company's list of tasks. Tasks have"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a description"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"an estimate of the hours required for completing the task"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the name of the programmer assigned to the task"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a field for keeping track of whether the task is finished"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a unique identifier"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The class is used as follows:"}]},{"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":"t1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Task"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program hello world\""}]},{"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":"\"Eric\""}]},{"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":"3"}]},{"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":"t1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" t1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"description"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" t1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"programmer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" t1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"workload"},{"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":"t1"},{"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":"t1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"is_finished"},{"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":"\nt1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mark_finished"},{"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":"t1"},{"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":"t1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"is_finished"},{"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":"\nt2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Task"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program webstore\""}]},{"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":"\"Adele\""}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nt3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Task"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program mobile app for workload accounting\""}]},{"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":"\"Eric\""}]},{"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":"25"}]},{"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":"t2"},{"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":"t3"},{"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":"1 program hello world Eric 3\n1: program hello world (3 hours), programmer Eric NOT FINISHED\nFalse\n1: program hello world (3 hours), programmer Eric FINISHED\nTrue\n2: program webstore (10 hours), programmer Adele NOT FINISHED\n3: program mobile app for workload accounting (25 hours), programmer Eric NOT FINISHED"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some clarifications:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the state of the task (finished or not yet finished) can be checked with the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"is_finished(self)"}]},{"type":"text","value":" which returns a Boolean value"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a task is not finished when it is created"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a task is marked as finished by calling the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mark_finished(self)"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the id of a task is a running number which starts with 1. The id of the first task is 1, the id of the second is 2, and so forth."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint"}]},{"type":"text","value":": "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"id"}]},{"type":"text","value":" can be implemented as a "},{"type":"element","tagName":"a","properties":{"href":"/part-9/5-class-attributes#class-variables"},"children":[{"type":"text","value":"class variable"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"orderbook","style":"position:relative;"},"children":[{"type":"text","value":"OrderBook"},{"type":"element","tagName":"a","properties":{"href":"#orderbook","ariaLabel":"orderbook 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":"Please write a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"OrderBook"}]},{"type":"text","value":" which collects all the tasks ordered from the software company. The tasks should be modelled with the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Task"}]},{"type":"text","value":" you just wrote."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The basic version of an OrderBook is used as follows:"}]},{"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":"orders "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" OrderBook"},{"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":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_order"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program webstore\""}]},{"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":"\"Adele\""}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_order"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program mobile app for workload accounting\""}]},{"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":"\"Eric\""}]},{"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":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_order"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program app for practising mathematics\""}]},{"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":"\"Adele\""}]},{"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":"100"}]},{"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":" order "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" orders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"all_orders"},{"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 "},{"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":"order"},{"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":"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":" programmer "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" orders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"programmers"},{"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 "},{"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":"programmer"},{"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":"1: program webstore (10 hours), programmer Adele NOT FINISHED\n2: program mobile app for workload accounting (25 hours), programmer Eric NOT FINISHED\n3: program app for practising mathematics (100 hours), programmer Adele NOT FINISHED"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Adele\nEric"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"At this stage your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"OrderBook"}]},{"type":"text","value":" should provide three methods:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_order(self, description, programmer, workload)"}]},{"type":"text","value":" which adds a new order to the OrderBook. An OrderBook stores the orders internally as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Task"}]},{"type":"text","value":" objects. NB: the method should take exactly the arguments mentioned, or else the automated tests will not work correctly."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"all_orders(self)"}]},{"type":"text","value":" returns a list of all the tasks stored in the OrderBook"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"programmers(self)"}]},{"type":"text","value":" returns a list of the names of all the programmers with tasks stored in the OrderBook. The list should contain each programmer only once"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint:"}]},{"type":"text","value":" an easy method for removing duplicates is handling the list initially as a "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3.8/library/stdtypes.html#set","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"set"}]},{"type":"text","value":". A set is a collection of items where each unique item appears only once. A "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"set"}]},{"type":"text","value":" can be then converted back into a list, and we can then be sure each item is now unique:"}]},{"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":"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":"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":"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":"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":"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nmy_list2 "},{"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":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"set"}]},{"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","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":"\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_list2"},{"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":"[1, 1, 3, 6, 4, 1, 3]\n[1, 3, 4, 6]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"some-more-features-for-orderbook","style":"position:relative;"},"children":[{"type":"text","value":"Some more features for OrderBook"},{"type":"element","tagName":"a","properties":{"href":"#some-more-features-for-orderbook","ariaLabel":"some more features for orderbook 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":"Please write three more methods in your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"OrderBook"}]},{"type":"text","value":" class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mark_finished(self, id: int)"}]},{"type":"text","value":" takes the id of the task as its argument and marks the relevant task as finished:"}]},{"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":"orders "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" OrderBook"},{"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":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_order"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program webstore\""}]},{"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":"\"Adele\""}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_order"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program mobile app for workload accounting\""}]},{"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":"\"Eric\""}]},{"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":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_order"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program app for practising mathematics\""}]},{"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":"\"Adele\""}]},{"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":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mark_finished"},{"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":"text","value":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mark_finished"},{"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" order "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" orders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"all_orders"},{"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 "},{"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":"order"},{"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":"1: program webstore (10 hours), programmer Adele FINISHED\n2: program mobile app for workload accounting (25 hours), programmer Eric FINISHED\n3: program app for practising mathematics (100 hours), programmer Adele NOT FINISHED"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If there is no task with the given id, the method should raise a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" exception. If you need a refresher on raising exceptions, please have a look at "},{"type":"element","tagName":"a","properties":{"href":"/part-6/3-errors#raising-exceptions"},"children":[{"type":"text","value":"part 6"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The methods "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"finished_orders(self)"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"unfinished_orders(self)"}]},{"type":"text","value":" work as expected: both return a list containing the relevant tasks from the OrderBook."}]},{"type":"element","tagName":"h2","properties":{"id":"finishing-touches-to-orderbook","style":"position:relative;"},"children":[{"type":"text","value":"Finishing touches to OrderBook"},{"type":"element","tagName":"a","properties":{"href":"#finishing-touches-to-orderbook","ariaLabel":"finishing touches to orderbook 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":"Please write one last method in your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"OrderBook"}]},{"type":"text","value":" class: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"status_of_programmer(self, programmer: str)"}]},{"type":"text","value":" which returns a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"tuple"}]},{"type":"text","value":". The tuple should contain the number of finished and unfinished tasks the programmer has assigned to them, along with the estimated hours in both categories."}]},{"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":"orders "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" OrderBook"},{"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":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_order"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program webstore\""}]},{"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":"\"Adele\""}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_order"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program mobile app for workload accounting\""}]},{"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":"\"Adele\""}]},{"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":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_order"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program app for practising mathematics\""}]},{"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":"\"Adele\""}]},{"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":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_order"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program the next facebook\""}]},{"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":"\"Eric\""}]},{"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":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mark_finished"},{"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":"text","value":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mark_finished"},{"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\nstatus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" orders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"status_of_programmer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Adele\""}]},{"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":"status"},{"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":"(2, 1, 35, 100)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The first item in the tuple is the number of "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"finished"}]},{"type":"text","value":" tasks, while the second item is the number of "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"unfinished"}]},{"type":"text","value":" tasks. The third and fourth items are the sums of workload estimates for the finished and unfinished tasks, respectively."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If there is no programmer with the given name, the method should raise a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" exception."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Order book application","tmcname":"part11-19_order_book_application"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will create an interactive application for administering the tasks ordered from a software company. The implementation is completely up to you, but you may use the building blocks from the previous exercise in your application. The examples in the "},{"type":"element","tagName":"a","properties":{"href":"/part-10/4-application-development"},"children":[{"type":"text","value":"last section of part 10"}]},{"type":"text","value":" can also prove useful."}]},{"type":"element","tagName":"h2","properties":{"id":"without-error-handling","style":"position:relative;"},"children":[{"type":"text","value":"Without error handling"},{"type":"element","tagName":"a","properties":{"href":"#without-error-handling","ariaLabel":"without error handling 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 application should work "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"exactly"}]},{"type":"text","value":" as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"commands:\n0 exit\n1 add order\n2 list finished tasks\n3 list unfinished tasks\n4 mark task as finished\n5 programmers\n6 status of programmer"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ndescription: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"program the next facebook"}]},{"type":"text","value":"\nprogrammer and workload estimate: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jonah 1000"}]},{"type":"text","value":"\nadded!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ndescription: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"program mobile app for workload accounting"}]},{"type":"text","value":"\nprogrammer and workload estimate: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"eric 25"}]},{"type":"text","value":"\nadded!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ndescription: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"program an app for music theory revision"}]},{"type":"text","value":"\nprogrammer and workload estimate: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"nina 12"}]},{"type":"text","value":"\nadded!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ndescription: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"program the next twitter"}]},{"type":"text","value":"\nprogrammer and workload estimate: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jonah 55"}]},{"type":"text","value":"\nadded!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nno finished tasks"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n1: program the next facebook (1000 hours), programmer jonah NOT FINISHED\n2: program mobile app for workload accounting (25 hours), programmer eric NOT FINISHED\n3: program an app for music theory revision (12 hours), programmer nina NOT FINISHED\n4: program the next twitter (55 hours), programmer jonah NOT FINISHED"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nid: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nmarked as finished"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nid: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nmarked as finished"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n2: program mobile app for workload accounting (25 hours), programmer eric FINISHED\n4: program the next twitter (55 hours), programmer jonah FINISHED"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n1: program the next facebook (1000 hours), programmer jonah NOT FINISHED\n3: program an app for music theory revision (12 hours), programmer nina NOT FINISHED"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\njonah\neric\nnina"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nprogrammer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jonah"}]},{"type":"text","value":"\ntasks: finished 2 not finished 1, hours: done 55 scheduled 1000"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The first exercise point is granted for a working application when all user input is flawless."}]},{"type":"element","tagName":"h2","properties":{"id":"handling-errors-in-user-input","style":"position:relative;"},"children":[{"type":"text","value":"Handling errors in user input"},{"type":"element","tagName":"a","properties":{"href":"#handling-errors-in-user-input","ariaLabel":"handling errors in user input 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":"To gain the second exercise point for this exercise your application is expected to recover from erroneus user input. Any input which does not follow the specified format should produce an error message "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"erroneous input"}]},{"type":"text","value":", and result in yet another repeat of the loop asking for a new command:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ndescription: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"program mobile app for workload accounting"}]},{"type":"text","value":"\nprogrammer and workload estimate: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"eric xxx"}]},{"type":"text","value":"\nerroneous input"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ndescription: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"program mobile app for workload accounting"}]},{"type":"text","value":"\nprogrammer and workload estimate: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"eric"}]},{"type":"text","value":"\nerroneous input"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nid: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1000000"}]},{"type":"text","value":"\nerroneous input"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nid: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"XXXX"}]},{"type":"text","value":"\nerroneous input"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nprogrammer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"unknownprogrammer"}]},{"type":"text","value":"\nerroneous input"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this part of the course."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"de061758-95fc-5a58-9c40-eb68a5629972"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will be familiar with binary trees and some recursive algorithms used to process them
    • \n

    The real advantages of recursion become evident when we come across problems where iterative solutions are difficult to write. Let's take a look at binary trees, for instance. A binary tree is a branched structure where we have nodes, and at each node the structure branches, at most, into two child branches with nodes of their own. A binary tree could then look like this (computer science is often considered a branch of the natural sciences, but our understanding of trees is a little topsy-turvy, as you'll notice):

    \n
    \n \n \n \n \n \"11\n \n \n

    Binary trees should at least theoretically be easy to handle recursively: if we want to perform some operation on every node in the tree, our algorithm simply needs to

      \n
    1. Process the current node
    2. \n
    3. Call itself on the child node on the left
    4. \n
    5. Call itself on the child node on the right
    6. \n
    \n \n \n \n \n \n \"11\n \n \n

    As you can see from the image above, both the left and right \"subtrees\" are fully fledged binary trees themselves, and the only node left outside the recursive calls is the parent node, which is processed in step 1, before calling the function recursively. So, we can be sure that when the execution of the function finishes, each node has been visited exactly once.

    An iterative version of a binary tree traversal would be much more complicated, as we would have to somehow keep track of all the nodes we have already visited. The same principles are true for all computational tree structures, not just binary ones.

    A binary tree is easily modelled in Python code as well. We only need to write a class definition for a single node. It has a value attribute and attributes for the left and right child nodes:

    \nclass Node:\n    \"\"\" The class represents a single node in a binary tree \"\"\"\n    def __init__(self, value, left_child:'Node' = None, right_child:'Node' = None):\n        self.value = value\n        self.left_child = left_child\n        self.right_child = right_child

    Now let's assume we want to model the following tree:

    \n \n \n \n \n \n \"11\n \n \n

    We could achieve this with the following code:

    if __name__ == \"__main__\":\n    tree = Node(2)\n\n    tree.left_child = Node(3)\n    tree.left_child.left_child = Node(5)\n    tree.left_child.right_child = Node(8)\n\n    tree.right_child = Node(4)\n    tree.right_child.right_child = Node(11)

    Recursive binary tree algorithms

    First, let's take a look at an algorithm which prints out all the nodes in a binary tree one by one. In these following examples we will be working with the binary tree defined above.

    The argument to the printing function is the root node of the binary tree. This is the node at the very top in our illustration above. All other nodes are children to this node:

    \ndef print_nodes(root: Node):\n    print(root.value)\n\n    if root.left_child is not None:\n        print_nodes(root.left_child)\n\n    if root.right_child is not None:\n        print_nodes(root.right_child)\n

    The function prints the value of the node passed as an argument, and then calls itself on the left and right child nodes, assuming the nodes are defined. This is a very simple algorithm, but it efficiently and reliably traverses all nodes in the tree, no matter the size of the tree. Crucially, no node is visited twice. Each value is printed only once.

    If we pass the root node tree of the binary tree illustrated above as an argument to the function, it prints out

    2\n3\n5\n8\n4\n11

    As you can see from the order of the nodesin the printout, the algorithm first moves down the \"left leg\" of the tree down to the very bottom, and from there traverses the other nodes in order.

    Similarly, we can write an algorithm for calculating the sum of all the values stored in the nodes of the tree:

    def sum_of_nodes(root: Node):\n    node_sum = root.value\n\n    if root.left_child is not None:\n        node_sum += sum_of_nodes(root.left_child)\n\n    if root.right_child is not None:\n        node_sum += sum_of_nodes(root.right_child)\n\n    return node_sum

    The variable node_sum is initialised to equal the value of the current node. The value in the variable is then augmented by recursive calls to the node sums of the left and right child trees (first making sure they exist, of course). This result is then returned

    Please write a function named greatest_node(root: Node) which takes the root node of a binary tree as its argument.

    The function should return the value of the node with the greatest value within the tree. The tree should be traversed recursively.

    Hint: the function sum_of_nodes in the example above may come in handy.

    An example of how the function should work:

    if __name__ == \"__main__\":\n    tree = Node(2)\n\n    tree.left_child = Node(3)\n    tree.left_child.left_child = Node(5)\n    tree.left_child.right_child = Node(8)\n\n    tree.right_child = Node(4)\n    tree.right_child.right_child = Node(11)\n\n    print(greatest_node(tree))

    11

    A sorted binary tree

    A binary tree is especially useful when the nodes are sorted in a certain way. This makes finding nodes in the tree fast and efficient.

    Let's take a look a tree which is sorted as follows: the left child of each node is smaller than the node itself, and the right child is correspondingly greater.

    \n \n \n \n \n \n \"11\n \n \n

    Now we can write a recursive algorithm for searching for nodes. The idea is very similar to the binary search from the previous section: if the current node is the node we are looking for, return True. Else, continue recursively with either the left or the right child tree. If the node is not defined, return False.

    def find_node(root: Node, value):\n    if root is None:\n        return False\n\n    if value == root.value:\n        return True\n\n    if value > root.value:\n        return find_node(root.right_child, value)\n\n    return find_node(root.left_child, value)

    The class Employee models an employee of a company:

    class Employee:\n    def __init__(self, name: str):\n        self.name = name\n        self.subordinates = []\n\n    def add_subordinate(self, employee: 'Employee'):\n        self.subordinates.append(employee)

    Please write a function named count_subordinates(employee: Employee) which recursively counts the number of subordinates each employee has.

    An example of the function in action:

    if __name__ == \"__main__\":\n    t1 = Employee(\"Sally\")\n    t2 = Employee(\"Eric\")\n    t3 = Employee(\"Matthew\")\n    t4 = Employee(\"Emily\")\n    t5 = Employee(\"Adele\")\n    t6 = Employee(\"Claire\")\n    t1.add_subordinate(t4)\n    t1.add_subordinate(t6)\n    t4.add_subordinate(t2)\n    t4.add_subordinate(t3)\n    t4.add_subordinate(t5)\n    print(count_subordinates(t1))\n    print(count_subordinates(t4))\n    print(count_subordinates(t5))

    5\n3\n0

    Revisiting the times before recursion

    Let's finish off this part of the material with a slightly larger exercise concentrating on object oriented programming principles. We do not recommend using recursion in this series of tasks, but list comprehension techniques will come in useful.

    In this exercise you will write two different classes, which will in turn form the backbone of the programming exercise which follows this one, where you will write an interactive application.

    Task

    Please write a class named Task which models a single task in a software company's list of tasks. Tasks have

      \n
    • a description
    • \n
    • an estimate of the hours required for completing the task
    • \n
    • the name of the programmer assigned to the task
    • \n
    • a field for keeping track of whether the task is finished
    • \n
    • a unique identifier
    • \n

    The class is used as follows:

    t1 = Task(\"program hello world\", \"Eric\", 3)\nprint(t1.id, t1.description, t1.programmer, t1.workload)\nprint(t1)\nprint(t1.is_finished())\nt1.mark_finished()\nprint(t1)\nprint(t1.is_finished())\nt2 = Task(\"program webstore\", \"Adele\", 10)\nt3 = Task(\"program mobile app for workload accounting\", \"Eric\", 25)\nprint(t2)\nprint(t3)

    1 program hello world Eric 3\n1: program hello world (3 hours), programmer Eric NOT FINISHED\nFalse\n1: program hello world (3 hours), programmer Eric FINISHED\nTrue\n2: program webstore (10 hours), programmer Adele NOT FINISHED\n3: program mobile app for workload accounting (25 hours), programmer Eric NOT FINISHED

    Some clarifications:

      \n
    • the state of the task (finished or not yet finished) can be checked with the function is_finished(self) which returns a Boolean value
    • \n
    • a task is not finished when it is created
    • \n
    • a task is marked as finished by calling the method mark_finished(self)
    • \n
    • the id of a task is a running number which starts with 1. The id of the first task is 1, the id of the second is 2, and so forth.
    • \n

    Hint: id can be implemented as a class variable.

    OrderBook

    Please write a class named OrderBook which collects all the tasks ordered from the software company. The tasks should be modelled with the class Task you just wrote.

    The basic version of an OrderBook is used as follows:

    orders = OrderBook()\norders.add_order(\"program webstore\", \"Adele\", 10)\norders.add_order(\"program mobile app for workload accounting\", \"Eric\", 25)\norders.add_order(\"program app for practising mathematics\", \"Adele\", 100)\n\nfor order in orders.all_orders():\n    print(order)\n\nprint()\n\nfor programmer in orders.programmers():\n    print(programmer)

    1: program webstore (10 hours), programmer Adele NOT FINISHED\n2: program mobile app for workload accounting (25 hours), programmer Eric NOT FINISHED\n3: program app for practising mathematics (100 hours), programmer Adele NOT FINISHED

    Adele\nEric

    At this stage your OrderBook should provide three methods:

      \n
    • add_order(self, description, programmer, workload) which adds a new order to the OrderBook. An OrderBook stores the orders internally as Task objects. NB: the method should take exactly the arguments mentioned, or else the automated tests will not work correctly.
    • \n
    • all_orders(self) returns a list of all the tasks stored in the OrderBook
    • \n
    • programmers(self) returns a list of the names of all the programmers with tasks stored in the OrderBook. The list should contain each programmer only once
    • \n

    Hint: an easy method for removing duplicates is handling the list initially as a set. A set is a collection of items where each unique item appears only once. A set can be then converted back into a list, and we can then be sure each item is now unique:

    my_list = [1,1,3,6,4,1,3]\nmy_list2 = list(set(my_list))\nprint(my_list)\nprint(my_list2)

    [1, 1, 3, 6, 4, 1, 3]\n[1, 3, 4, 6]

    Some more features for OrderBook

    Please write three more methods in your OrderBook class.

    The method mark_finished(self, id: int) takes the id of the task as its argument and marks the relevant task as finished:

    orders = OrderBook()\norders.add_order(\"program webstore\", \"Adele\", 10)\norders.add_order(\"program mobile app for workload accounting\", \"Eric\", 25)\norders.add_order(\"program app for practising mathematics\", \"Adele\", 100)\n\norders.mark_finished(1)\norders.mark_finished(2)\n\nfor order in orders.all_orders():\n    print(order)

    1: program webstore (10 hours), programmer Adele FINISHED\n2: program mobile app for workload accounting (25 hours), programmer Eric FINISHED\n3: program app for practising mathematics (100 hours), programmer Adele NOT FINISHED

    If there is no task with the given id, the method should raise a ValueError exception. If you need a refresher on raising exceptions, please have a look at part 6.

    The methods finished_orders(self) and unfinished_orders(self) work as expected: both return a list containing the relevant tasks from the OrderBook.

    Finishing touches to OrderBook

    Please write one last method in your OrderBook class: status_of_programmer(self, programmer: str) which returns a tuple. The tuple should contain the number of finished and unfinished tasks the programmer has assigned to them, along with the estimated hours in both categories.

    orders = OrderBook()\norders.add_order(\"program webstore\", \"Adele\", 10)\norders.add_order(\"program mobile app for workload accounting\", \"Adele\", 25)\norders.add_order(\"program app for practising mathematics\", \"Adele\", 100)\norders.add_order(\"program the next facebook\", \"Eric\", 1000)\n\norders.mark_finished(1)\norders.mark_finished(2)\n\nstatus = orders.status_of_programmer(\"Adele\")\nprint(status)

    (2, 1, 35, 100)

    The first item in the tuple is the number of finished tasks, while the second item is the number of unfinished tasks. The third and fourth items are the sums of workload estimates for the finished and unfinished tasks, respectively.

    If there is no programmer with the given name, the method should raise a ValueError exception.

    In this exercise you will create an interactive application for administering the tasks ordered from a software company. The implementation is completely up to you, but you may use the building blocks from the previous exercise in your application. The examples in the last section of part 10 can also prove useful.

    Without error handling

    The application should work exactly as follows:

    commands:\n0 exit\n1 add order\n2 list finished tasks\n3 list unfinished tasks\n4 mark task as finished\n5 programmers\n6 status of programmer

    command: 1\ndescription: program the next facebook\nprogrammer and workload estimate: jonah 1000\nadded!

    command: 1\ndescription: program mobile app for workload accounting\nprogrammer and workload estimate: eric 25\nadded!

    command: 1\ndescription: program an app for music theory revision\nprogrammer and workload estimate: nina 12\nadded!

    command: 1\ndescription: program the next twitter\nprogrammer and workload estimate: jonah 55\nadded!

    command: 2\nno finished tasks

    command: 3\n1: program the next facebook (1000 hours), programmer jonah NOT FINISHED\n2: program mobile app for workload accounting (25 hours), programmer eric NOT FINISHED\n3: program an app for music theory revision (12 hours), programmer nina NOT FINISHED\n4: program the next twitter (55 hours), programmer jonah NOT FINISHED

    command: 4\nid: 2\nmarked as finished

    command: 4\nid: 4\nmarked as finished

    command: 2\n2: program mobile app for workload accounting (25 hours), programmer eric FINISHED\n4: program the next twitter (55 hours), programmer jonah FINISHED

    command: 3\n1: program the next facebook (1000 hours), programmer jonah NOT FINISHED\n3: program an app for music theory revision (12 hours), programmer nina NOT FINISHED

    command: 5\njonah\neric\nnina

    command: 6\nprogrammer: jonah\ntasks: finished 2 not finished 1, hours: done 55 scheduled 1000

    The first exercise point is granted for a working application when all user input is flawless.

    Handling errors in user input

    To gain the second exercise point for this exercise your application is expected to recover from erroneus user input. Any input which does not follow the specified format should produce an error message erroneous input, and result in yet another repeat of the loop asking for a new command:

    command: 1\ndescription: program mobile app for workload accounting\nprogrammer and workload estimate: eric xxx\nerroneous input

    command: 1\ndescription: program mobile app for workload accounting\nprogrammer and workload estimate: eric\nerroneous input

    command: 4\nid: 1000000\nerroneous input

    command: 4\nid: XXXX\nerroneous input

    command: 6\nprogrammer: unknownprogrammer\nerroneous input

    Please respond to a quick questionnaire on this part of the course.

    ","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-11/4-more-recursion-examples.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 binary trees and some recursive algorithms used to process them"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The real advantages of recursion become evident when we come across problems where iterative solutions are difficult to write. Let's take a look at "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"binary trees"}]},{"type":"text","value":", for instance. A binary tree is a branched structure where we have nodes, and at each node the structure branches, at most, into two child branches with nodes of their own. A binary tree could then look like this (computer science is often considered a branch of the natural sciences, but our understanding of trees is a little topsy-turvy, as you'll notice):"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 307px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/42ada93f7fad74a8205a13a46dc4de57/4651d/11_4_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: 78.69565217391303%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAAAsTAAALEwEAmpwYAAABsElEQVQ4y3WU54rDQAyE/f5PlUD+JIT03ntvpHcdn0C+9Z5PYFxWO5rRaB2IF9/vV++fz0fa7bZ0u13pdDryer3CdcuJi8AHs+Tr9apgAHHn3c35DzRwF7nDjHg8HpJOp6XX60k2m5XNZhMy53L3uOCBL9WiXq/L7XYL35E9n89j2xMBdCvsdjsZDodSLBb12RhZjEYjLTQYDMJ1n0hgH5CICSQ2m82QnVvwcrlIo9GQ9XqtuX5fI4DP51Oq1arKqlQqcr/f/ySfz2ft6Ww2k1KppCR8o0JAAABarVZSq9UiJrjSDoeDFiWHZz8nMMfoC5IskH08Hv9tvhnnylbJzBjMuKyaycec7XYbC0qP2VMul2W/3/8yHI/HcjqddCwslsulOkrPYOqfIGI6ncpkMtFcJiMEZAPOsWiBjFwup98xIO6EULBQKChL1IRjgzy3d7BCZr/fD110Gb7fbzUFtynMflhikjJ0mwx1zMlkMhHn/PPNyCDZgsngG2uBm9xqtfQZdq7DlgMI7DDEcm0iMAaWkaNHhUQioQlxhx8gfhSpVEpPi8VisZBkMin5fF5+ALPgzivet7o7AAAAAElFTkSuQmCC'); 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":["/static/42ada93f7fad74a8205a13a46dc4de57/a0b58/11_4_1.webp 230w","/static/42ada93f7fad74a8205a13a46dc4de57/b1a97/11_4_1.webp 307w"],"sizes":"(max-width: 307px) 100vw, 307px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/42ada93f7fad74a8205a13a46dc4de57/81c8e/11_4_1.png 230w","/static/42ada93f7fad74a8205a13a46dc4de57/4651d/11_4_1.png 307w"],"sizes":"(max-width: 307px) 100vw, 307px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/42ada93f7fad74a8205a13a46dc4de57/4651d/11_4_1.png","alt":"11 4 1","title":"11 4 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":"p","properties":{},"children":[{"type":"text","value":"Binary trees should at least theoretically be easy to handle recursively: if we want to perform some operation on every node in the tree, our algorithm simply needs to"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Process the current node"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Call itself on the child node on the left"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Call itself on the child node on the right"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 362px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/876ad26e1b0c7b303582f9e0f2cb0148/10600/11_4_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: 77.82608695652173%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAAAsTAAALEwEAmpwYAAABtklEQVQ4y4WU147CUAxE8/9/xQMvCCFEh9B7C703r45XvrrJAmvJCjjxeGauk+D1ekkyCa7dbleq1apMJhNX+y8DSYQB3m43CcNQTqeTXn3AbxG8Y0fs93upVCrS7/el0Wjof3/oR4ar1Uq2261sNhvZ7XYyn89lPB5LPp+X9XqtNRLpWDCdTsXvSWZwPp9jrGBULpcliiLHxoZR73Q6MhgMPkqPAS6XS2XRbDaVgQ9Icm84HEq9XpfH4/FeMqZbzGYzBWu1Wio7yfBwOGgd+dbnb4UyvF6vQiIRuRZIM1Bj6Afyk6AKiMHIYDVGo5EWGdDr9ZQtg/AWQGTaAFNiljnJsACIqzFCGoCYzz3++4A8h594iRXE8/n8ZXi5XGSxWGizNZgkaiy4rYhvB4B2SP5+xk75fr/rA6VSya2NSQSUwcViMQZSq9U0senP2thSM9lnBBgDaGS17PBYfJ6FsfkfHI9Hd0p4hdEkJ0gdG2i0PeQegwnsarfb+mryjDtlY4DByCLtZEl+o4QmXj17HalTY4A97742uVxO0um0ykqlUpLJZNxUP2jMZrP64aCnUCg4/2D4Ax3myJowab4fAAAAAElFTkSuQmCC'); 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":["/static/876ad26e1b0c7b303582f9e0f2cb0148/a0b58/11_4_2.webp 230w","/static/876ad26e1b0c7b303582f9e0f2cb0148/c2de8/11_4_2.webp 362w"],"sizes":"(max-width: 362px) 100vw, 362px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/876ad26e1b0c7b303582f9e0f2cb0148/81c8e/11_4_2.png 230w","/static/876ad26e1b0c7b303582f9e0f2cb0148/10600/11_4_2.png 362w"],"sizes":"(max-width: 362px) 100vw, 362px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/876ad26e1b0c7b303582f9e0f2cb0148/10600/11_4_2.png","alt":"11 4 2","title":"11 4 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":"As you can see from the image above, both the left and right \"subtrees\" are fully fledged binary trees themselves, and the only node left outside the recursive calls is the parent node, which is processed in step 1, before calling the function recursively. So, we can be sure that when the execution of the function finishes, each node has been visited exactly once."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An iterative version of a binary tree traversal would be much more complicated, as we would have to somehow keep track of all the nodes we have already visited. The same principles are true for all computational tree structures, not just binary ones."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A binary tree is easily modelled in Python code as well. We only need to write a class definition for a single node. It has a value attribute and attributes for the left and right child nodes:"}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Node"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The class represents a single node in a binary tree \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" left_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Node'"}]},{"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","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" right_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Node'"}]},{"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","boolean"]},"children":[{"type":"text","value":"None"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"value "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" value\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" left_child\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" right_child"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now let's assume we want to model the following tree:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 267px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/2d27ece550ee70e685bb54be04c9ea65/19e8f/11_4_3.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: 68.26086956521739%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAAsTAAALEwEAmpwYAAABfUlEQVQ4y4VTWcuDQAzc///sDyr0wRdrRVuqvbx6aA96gtArHxPIsm4t34LEI5lMZqKijvP5fDjebjfyPI+v+Xyuv8n3rqO6wKQgjmO63+/0er0oyzI6nU7/giqTkcT3+80xz3MKw5CKomCWAO7KN8GV2U3u5Xk0GvGo6/WakiTRY0tTu1YD2l1wAFBVVevdZrPhJr80/xoZAIvFgobDIaVpyu8wJtiIDMfjkXzf57zZbEZN03QzPJ/PbILE7Xb7ZYBoGEURy1DXNU0mk1aeBtzv9+wkOoIpDLF1lVzIAaZwHc1bDGWU5/NJ/X6fx0CEXqbjJiDAwAxMB4NByyS9h8vlkhOlcDwe0+PxaIGaTEAApyxLWq1WOk/hAXpMp9NWwfV6pV6vx3+L7aS9Ea7rskk8MoSFq9BPktEJI2FsMDX/EHt8EIJM0BwSKDDY7XZ6TYQdlvhwOLDb0szUU1yHQaiHBI7jkEIx2NhrEgQBXS4XXmQA/2IIIriwJTDoD1qJNDsmtBRAAAAAAElFTkSuQmCC'); 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":["/static/2d27ece550ee70e685bb54be04c9ea65/a0b58/11_4_3.webp 230w","/static/2d27ece550ee70e685bb54be04c9ea65/9156e/11_4_3.webp 267w"],"sizes":"(max-width: 267px) 100vw, 267px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/2d27ece550ee70e685bb54be04c9ea65/81c8e/11_4_3.png 230w","/static/2d27ece550ee70e685bb54be04c9ea65/19e8f/11_4_3.png 267w"],"sizes":"(max-width: 267px) 100vw, 267px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/2d27ece550ee70e685bb54be04c9ea65/19e8f/11_4_3.png","alt":"11 4 3","title":"11 4 3","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":"We could achieve this with the following code:"}]},{"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":"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 tree "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"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\n tree"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"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":"\n tree"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"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 tree"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"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\n tree"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"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":"text","value":"\n tree"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"recursive-binary-tree-algorithms","style":"position:relative;"},"children":[{"type":"text","value":"Recursive binary tree algorithms"},{"type":"element","tagName":"a","properties":{"href":"#recursive-binary-tree-algorithms","ariaLabel":"recursive binary tree algorithms 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":"First, let's take a look at an algorithm which prints out all the nodes in a binary tree one by one. In these following examples we will be working with the binary tree defined above."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The argument to the printing function is the root node of the binary tree. This is the node at the very top in our illustration above. All other nodes are "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"children"}]},{"type":"text","value":" to this node:"}]},{"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":"\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_nodes"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Node"},{"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":"root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"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":"if"}]},{"type":"text","value":" root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n print_nodes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child"},{"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":"if"}]},{"type":"text","value":" root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n print_nodes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function prints the value of the node passed as an argument, and then calls itself on the left and right child nodes, assuming the nodes are defined. This is a very simple algorithm, but it efficiently and reliably traverses all nodes in the tree, no matter the size of the tree. Crucially, no node is visited twice. Each value is printed only once."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we pass the root node "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tree"}]},{"type":"text","value":" of the binary tree illustrated above as an argument to the function, it prints out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n3\n5\n8\n4\n11"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see from the order of the nodesin the printout, the algorithm first moves down the \"left leg\" of the tree down to the very bottom, and from there traverses the other nodes in order."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Similarly, we can write an algorithm for calculating the sum of all the values stored in the nodes of the tree:"}]},{"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":"sum_of_nodes"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Node"},{"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 node_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"value\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n node_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" sum_of_nodes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child"},{"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":"if"}]},{"type":"text","value":" root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n node_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" sum_of_nodes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child"},{"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":" node_sum"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"node_sum"}]},{"type":"text","value":" is initialised to equal the value of the current node. The value in the variable is then augmented by recursive calls to the node sums of the left and right child trees (first making sure they exist, of course). This result is then returned"}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Greatest node","tmcname":"part11-16_greatest_node"},"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":"greatest_node(root: Node)"}]},{"type":"text","value":" which takes the root node of a binary tree as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return the value of the node with the greatest value within the tree. The tree should be traversed recursively."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hint: the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum_of_nodes"}]},{"type":"text","value":" in the example above may come in handy."}]},{"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":"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 tree "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"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\n tree"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"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":"\n tree"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"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 tree"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"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\n tree"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"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":"text","value":"\n tree"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Node"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"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":"greatest_node"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"tree"},{"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":"11"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"a-sorted-binary-tree","style":"position:relative;"},"children":[{"type":"text","value":"A sorted binary tree"},{"type":"element","tagName":"a","properties":{"href":"#a-sorted-binary-tree","ariaLabel":"a sorted binary tree 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":"A binary tree is especially useful when the nodes are sorted in a certain way. This makes finding nodes in the tree fast and efficient."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's take a look a tree which is sorted as follows: the left child of each node is smaller than the node itself, and the right child is correspondingly greater."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 307px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/42ada93f7fad74a8205a13a46dc4de57/4651d/11_4_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: 78.69565217391303%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAAAsTAAALEwEAmpwYAAABsElEQVQ4y3WU54rDQAyE/f5PlUD+JIT03ntvpHcdn0C+9Z5PYFxWO5rRaB2IF9/vV++fz0fa7bZ0u13pdDryer3CdcuJi8AHs+Tr9apgAHHn3c35DzRwF7nDjHg8HpJOp6XX60k2m5XNZhMy53L3uOCBL9WiXq/L7XYL35E9n89j2xMBdCvsdjsZDodSLBb12RhZjEYjLTQYDMJ1n0hgH5CICSQ2m82QnVvwcrlIo9GQ9XqtuX5fI4DP51Oq1arKqlQqcr/f/ySfz2ft6Ww2k1KppCR8o0JAAABarVZSq9UiJrjSDoeDFiWHZz8nMMfoC5IskH08Hv9tvhnnylbJzBjMuKyaycec7XYbC0qP2VMul2W/3/8yHI/HcjqddCwslsulOkrPYOqfIGI6ncpkMtFcJiMEZAPOsWiBjFwup98xIO6EULBQKChL1IRjgzy3d7BCZr/fD110Gb7fbzUFtynMflhikjJ0mwx1zMlkMhHn/PPNyCDZgsngG2uBm9xqtfQZdq7DlgMI7DDEcm0iMAaWkaNHhUQioQlxhx8gfhSpVEpPi8VisZBkMin5fF5+ALPgzivet7o7AAAAAElFTkSuQmCC'); 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":["/static/42ada93f7fad74a8205a13a46dc4de57/a0b58/11_4_1.webp 230w","/static/42ada93f7fad74a8205a13a46dc4de57/b1a97/11_4_1.webp 307w"],"sizes":"(max-width: 307px) 100vw, 307px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/42ada93f7fad74a8205a13a46dc4de57/81c8e/11_4_1.png 230w","/static/42ada93f7fad74a8205a13a46dc4de57/4651d/11_4_1.png 307w"],"sizes":"(max-width: 307px) 100vw, 307px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/42ada93f7fad74a8205a13a46dc4de57/4651d/11_4_1.png","alt":"11 4 1","title":"11 4 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":"p","properties":{},"children":[{"type":"text","value":"Now we can write a recursive algorithm for searching for nodes. The idea is very similar to the binary search from the previous section: if the current node is the node we are looking for, return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":". Else, continue recursively with either the left or the right child tree. If the node is not defined, return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"."}]},{"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":"find_node"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Node"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" 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","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" root "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"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","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" value "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" value "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"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":" find_node"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" 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":"return"}]},{"type":"text","value":" find_node"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"root"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_child"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bosses and subordinates","tmcname":"part11-17_bosses_and_subordinates"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Employee"}]},{"type":"text","value":" models an employee of a company:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Employee"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"subordinates "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_subordinate"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" employee"},{"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":"'Employee'"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"subordinates"},{"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":"employee"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"count_subordinates(employee: Employee)"}]},{"type":"text","value":" which recursively counts the number of subordinates each employee has."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"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 t1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Employee"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sally\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n t2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Employee"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n t3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Employee"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Matthew\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n t4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Employee"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n t5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Employee"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Adele\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n t6 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Employee"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Claire\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n t1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_subordinate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"t4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n t1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_subordinate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"t6"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n t4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_subordinate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"t2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n t4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_subordinate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"t3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n t4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_subordinate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"t5"},{"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":"count_subordinates"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"t1"},{"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":"count_subordinates"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"t4"},{"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":"count_subordinates"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"t5"},{"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\n0"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"revisiting-the-times-before-recursion","style":"position:relative;"},"children":[{"type":"text","value":"Revisiting the times before recursion"},{"type":"element","tagName":"a","properties":{"href":"#revisiting-the-times-before-recursion","ariaLabel":"revisiting the times before recursion 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":"Let's finish off this part of the material with a slightly larger exercise concentrating on object oriented programming principles. We do not recommend using recursion in this series of tasks, but list comprehension techniques will come in useful."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"OrderBook","tmcname":"part11-18_order_book"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will write two different classes, which will in turn form the backbone of the programming exercise which follows this one, where you will write an interactive application."}]},{"type":"element","tagName":"h2","properties":{"id":"task","style":"position:relative;"},"children":[{"type":"text","value":"Task"},{"type":"element","tagName":"a","properties":{"href":"#task","ariaLabel":"task 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":"Please write a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Task"}]},{"type":"text","value":" which models a single task in a software company's list of tasks. Tasks have"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a description"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"an estimate of the hours required for completing the task"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the name of the programmer assigned to the task"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a field for keeping track of whether the task is finished"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a unique identifier"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The class is used as follows:"}]},{"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":"t1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Task"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program hello world\""}]},{"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":"\"Eric\""}]},{"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":"3"}]},{"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":"t1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" t1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"description"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" t1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"programmer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" t1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"workload"},{"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":"t1"},{"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":"t1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"is_finished"},{"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":"\nt1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mark_finished"},{"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":"t1"},{"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":"t1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"is_finished"},{"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":"\nt2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Task"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program webstore\""}]},{"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":"\"Adele\""}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nt3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Task"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program mobile app for workload accounting\""}]},{"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":"\"Eric\""}]},{"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":"25"}]},{"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":"t2"},{"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":"t3"},{"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":"1 program hello world Eric 3\n1: program hello world (3 hours), programmer Eric NOT FINISHED\nFalse\n1: program hello world (3 hours), programmer Eric FINISHED\nTrue\n2: program webstore (10 hours), programmer Adele NOT FINISHED\n3: program mobile app for workload accounting (25 hours), programmer Eric NOT FINISHED"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some clarifications:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the state of the task (finished or not yet finished) can be checked with the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"is_finished(self)"}]},{"type":"text","value":" which returns a Boolean value"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a task is not finished when it is created"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a task is marked as finished by calling the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mark_finished(self)"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the id of a task is a running number which starts with 1. The id of the first task is 1, the id of the second is 2, and so forth."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint"}]},{"type":"text","value":": "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"id"}]},{"type":"text","value":" can be implemented as a "},{"type":"element","tagName":"a","properties":{"href":"/part-9/5-class-attributes#class-variables"},"children":[{"type":"text","value":"class variable"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"orderbook","style":"position:relative;"},"children":[{"type":"text","value":"OrderBook"},{"type":"element","tagName":"a","properties":{"href":"#orderbook","ariaLabel":"orderbook 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":"Please write a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"OrderBook"}]},{"type":"text","value":" which collects all the tasks ordered from the software company. The tasks should be modelled with the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Task"}]},{"type":"text","value":" you just wrote."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The basic version of an OrderBook is used as follows:"}]},{"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":"orders "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" OrderBook"},{"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":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_order"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program webstore\""}]},{"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":"\"Adele\""}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_order"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program mobile app for workload accounting\""}]},{"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":"\"Eric\""}]},{"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":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_order"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program app for practising mathematics\""}]},{"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":"\"Adele\""}]},{"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":"100"}]},{"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":" order "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" orders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"all_orders"},{"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 "},{"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":"order"},{"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":"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":" programmer "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" orders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"programmers"},{"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 "},{"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":"programmer"},{"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":"1: program webstore (10 hours), programmer Adele NOT FINISHED\n2: program mobile app for workload accounting (25 hours), programmer Eric NOT FINISHED\n3: program app for practising mathematics (100 hours), programmer Adele NOT FINISHED"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Adele\nEric"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"At this stage your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"OrderBook"}]},{"type":"text","value":" should provide three methods:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_order(self, description, programmer, workload)"}]},{"type":"text","value":" which adds a new order to the OrderBook. An OrderBook stores the orders internally as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Task"}]},{"type":"text","value":" objects. NB: the method should take exactly the arguments mentioned, or else the automated tests will not work correctly."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"all_orders(self)"}]},{"type":"text","value":" returns a list of all the tasks stored in the OrderBook"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"programmers(self)"}]},{"type":"text","value":" returns a list of the names of all the programmers with tasks stored in the OrderBook. The list should contain each programmer only once"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint:"}]},{"type":"text","value":" an easy method for removing duplicates is handling the list initially as a "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3.8/library/stdtypes.html#set","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"set"}]},{"type":"text","value":". A set is a collection of items where each unique item appears only once. A "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"set"}]},{"type":"text","value":" can be then converted back into a list, and we can then be sure each item is now unique:"}]},{"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":"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":"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":"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":"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":"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nmy_list2 "},{"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":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"set"}]},{"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","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":"\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_list2"},{"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":"[1, 1, 3, 6, 4, 1, 3]\n[1, 3, 4, 6]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"some-more-features-for-orderbook","style":"position:relative;"},"children":[{"type":"text","value":"Some more features for OrderBook"},{"type":"element","tagName":"a","properties":{"href":"#some-more-features-for-orderbook","ariaLabel":"some more features for orderbook 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":"Please write three more methods in your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"OrderBook"}]},{"type":"text","value":" class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"mark_finished(self, id: int)"}]},{"type":"text","value":" takes the id of the task as its argument and marks the relevant task as finished:"}]},{"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":"orders "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" OrderBook"},{"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":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_order"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program webstore\""}]},{"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":"\"Adele\""}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_order"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program mobile app for workload accounting\""}]},{"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":"\"Eric\""}]},{"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":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_order"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program app for practising mathematics\""}]},{"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":"\"Adele\""}]},{"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":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mark_finished"},{"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":"text","value":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mark_finished"},{"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" order "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" orders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"all_orders"},{"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 "},{"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":"order"},{"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":"1: program webstore (10 hours), programmer Adele FINISHED\n2: program mobile app for workload accounting (25 hours), programmer Eric FINISHED\n3: program app for practising mathematics (100 hours), programmer Adele NOT FINISHED"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If there is no task with the given id, the method should raise a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" exception. If you need a refresher on raising exceptions, please have a look at "},{"type":"element","tagName":"a","properties":{"href":"/part-6/3-errors#raising-exceptions"},"children":[{"type":"text","value":"part 6"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The methods "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"finished_orders(self)"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"unfinished_orders(self)"}]},{"type":"text","value":" work as expected: both return a list containing the relevant tasks from the OrderBook."}]},{"type":"element","tagName":"h2","properties":{"id":"finishing-touches-to-orderbook","style":"position:relative;"},"children":[{"type":"text","value":"Finishing touches to OrderBook"},{"type":"element","tagName":"a","properties":{"href":"#finishing-touches-to-orderbook","ariaLabel":"finishing touches to orderbook 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":"Please write one last method in your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"OrderBook"}]},{"type":"text","value":" class: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"status_of_programmer(self, programmer: str)"}]},{"type":"text","value":" which returns a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"tuple"}]},{"type":"text","value":". The tuple should contain the number of finished and unfinished tasks the programmer has assigned to them, along with the estimated hours in both categories."}]},{"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":"orders "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" OrderBook"},{"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":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_order"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program webstore\""}]},{"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":"\"Adele\""}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_order"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program mobile app for workload accounting\""}]},{"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":"\"Adele\""}]},{"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":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_order"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program app for practising mathematics\""}]},{"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":"\"Adele\""}]},{"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":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_order"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"program the next facebook\""}]},{"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":"\"Eric\""}]},{"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":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mark_finished"},{"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":"text","value":"\norders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mark_finished"},{"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\nstatus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" orders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"status_of_programmer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Adele\""}]},{"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":"status"},{"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":"(2, 1, 35, 100)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The first item in the tuple is the number of "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"finished"}]},{"type":"text","value":" tasks, while the second item is the number of "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"unfinished"}]},{"type":"text","value":" tasks. The third and fourth items are the sums of workload estimates for the finished and unfinished tasks, respectively."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If there is no programmer with the given name, the method should raise a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" exception."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Order book application","tmcname":"part11-19_order_book_application"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will create an interactive application for administering the tasks ordered from a software company. The implementation is completely up to you, but you may use the building blocks from the previous exercise in your application. The examples in the "},{"type":"element","tagName":"a","properties":{"href":"/part-10/4-application-development"},"children":[{"type":"text","value":"last section of part 10"}]},{"type":"text","value":" can also prove useful."}]},{"type":"element","tagName":"h2","properties":{"id":"without-error-handling","style":"position:relative;"},"children":[{"type":"text","value":"Without error handling"},{"type":"element","tagName":"a","properties":{"href":"#without-error-handling","ariaLabel":"without error handling 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 application should work "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"exactly"}]},{"type":"text","value":" as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"commands:\n0 exit\n1 add order\n2 list finished tasks\n3 list unfinished tasks\n4 mark task as finished\n5 programmers\n6 status of programmer"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ndescription: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"program the next facebook"}]},{"type":"text","value":"\nprogrammer and workload estimate: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jonah 1000"}]},{"type":"text","value":"\nadded!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ndescription: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"program mobile app for workload accounting"}]},{"type":"text","value":"\nprogrammer and workload estimate: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"eric 25"}]},{"type":"text","value":"\nadded!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ndescription: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"program an app for music theory revision"}]},{"type":"text","value":"\nprogrammer and workload estimate: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"nina 12"}]},{"type":"text","value":"\nadded!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ndescription: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"program the next twitter"}]},{"type":"text","value":"\nprogrammer and workload estimate: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jonah 55"}]},{"type":"text","value":"\nadded!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nno finished tasks"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n1: program the next facebook (1000 hours), programmer jonah NOT FINISHED\n2: program mobile app for workload accounting (25 hours), programmer eric NOT FINISHED\n3: program an app for music theory revision (12 hours), programmer nina NOT FINISHED\n4: program the next twitter (55 hours), programmer jonah NOT FINISHED"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nid: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nmarked as finished"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nid: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nmarked as finished"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n2: program mobile app for workload accounting (25 hours), programmer eric FINISHED\n4: program the next twitter (55 hours), programmer jonah FINISHED"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n1: program the next facebook (1000 hours), programmer jonah NOT FINISHED\n3: program an app for music theory revision (12 hours), programmer nina NOT FINISHED"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\njonah\neric\nnina"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nprogrammer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jonah"}]},{"type":"text","value":"\ntasks: finished 2 not finished 1, hours: done 55 scheduled 1000"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The first exercise point is granted for a working application when all user input is flawless."}]},{"type":"element","tagName":"h2","properties":{"id":"handling-errors-in-user-input","style":"position:relative;"},"children":[{"type":"text","value":"Handling errors in user input"},{"type":"element","tagName":"a","properties":{"href":"#handling-errors-in-user-input","ariaLabel":"handling errors in user input 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":"To gain the second exercise point for this exercise your application is expected to recover from erroneus user input. Any input which does not follow the specified format should produce an error message "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"erroneous input"}]},{"type":"text","value":", and result in yet another repeat of the loop asking for a new command:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ndescription: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"program mobile app for workload accounting"}]},{"type":"text","value":"\nprogrammer and workload estimate: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"eric xxx"}]},{"type":"text","value":"\nerroneous input"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\ndescription: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"program mobile app for workload accounting"}]},{"type":"text","value":"\nprogrammer and workload estimate: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"eric"}]},{"type":"text","value":"\nerroneous input"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nid: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1000000"}]},{"type":"text","value":"\nerroneous input"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nid: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"XXXX"}]},{"type":"text","value":"\nerroneous input"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nprogrammer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"unknownprogrammer"}]},{"type":"text","value":"\nerroneous input"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this part of the course."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"de061758-95fc-5a58-9c40-eb68a5629972"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will be familiar with binary trees and some recursive algorithms used to process them
    • \n

    The real advantages of recursion become evident when we come across problems where iterative solutions are difficult to write. Let's take a look at binary trees, for instance. A binary tree is a branched structure where we have nodes, and at each node the structure branches, at most, into two child branches with nodes of their own. A binary tree could then look like this (computer science is often considered a branch of the natural sciences, but our understanding of trees is a little topsy-turvy, as you'll notice):

    \n \n \n \n \n \n \"11\n \n \n

    Binary trees should at least theoretically be easy to handle recursively: if we want to perform some operation on every node in the tree, our algorithm simply needs to

      \n
    1. Process the current node
    2. \n
    3. Call itself on the child node on the left
    4. \n
    5. Call itself on the child node on the right
    6. \n
    \n \n \n \n \n \n \"11\n \n \n

    As you can see from the image above, both the left and right \"subtrees\" are fully fledged binary trees themselves, and the only node left outside the recursive calls is the parent node, which is processed in step 1, before calling the function recursively. So, we can be sure that when the execution of the function finishes, each node has been visited exactly once.

    An iterative version of a binary tree traversal would be much more complicated, as we would have to somehow keep track of all the nodes we have already visited. The same principles are true for all computational tree structures, not just binary ones.

    A binary tree is easily modelled in Python code as well. We only need to write a class definition for a single node. It has a value attribute and attributes for the left and right child nodes:

    \nclass Node:\n    \"\"\" The class represents a single node in a binary tree \"\"\"\n    def __init__(self, value, left_child:'Node' = None, right_child:'Node' = None):\n        self.value = value\n        self.left_child = left_child\n        self.right_child = right_child

    Now let's assume we want to model the following tree:

    \n \n \n \n \n \n \"11\n \n \n

    We could achieve this with the following code:

    if __name__ == \"__main__\":\n    tree = Node(2)\n\n    tree.left_child = Node(3)\n    tree.left_child.left_child = Node(5)\n    tree.left_child.right_child = Node(8)\n\n    tree.right_child = Node(4)\n    tree.right_child.right_child = Node(11)

    Recursive binary tree algorithms

    First, let's take a look at an algorithm which prints out all the nodes in a binary tree one by one. In these following examples we will be working with the binary tree defined above.

    The argument to the printing function is the root node of the binary tree. This is the node at the very top in our illustration above. All other nodes are children to this node:

    \ndef print_nodes(root: Node):\n    print(root.value)\n\n    if root.left_child is not None:\n        print_nodes(root.left_child)\n\n    if root.right_child is not None:\n        print_nodes(root.right_child)\n

    The function prints the value of the node passed as an argument, and then calls itself on the left and right child nodes, assuming the nodes are defined. This is a very simple algorithm, but it efficiently and reliably traverses all nodes in the tree, no matter the size of the tree. Crucially, no node is visited twice. Each value is printed only once.

    If we pass the root node tree of the binary tree illustrated above as an argument to the function, it prints out

    2\n3\n5\n8\n4\n11

    As you can see from the order of the nodesin the printout, the algorithm first moves down the \"left leg\" of the tree down to the very bottom, and from there traverses the other nodes in order.

    Similarly, we can write an algorithm for calculating the sum of all the values stored in the nodes of the tree:

    def sum_of_nodes(root: Node):\n    node_sum = root.value\n\n    if root.left_child is not None:\n        node_sum += sum_of_nodes(root.left_child)\n\n    if root.right_child is not None:\n        node_sum += sum_of_nodes(root.right_child)\n\n    return node_sum

    The variable node_sum is initialised to equal the value of the current node. The value in the variable is then augmented by recursive calls to the node sums of the left and right child trees (first making sure they exist, of course). This result is then returned

    Please write a function named greatest_node(root: Node) which takes the root node of a binary tree as its argument.

    The function should return the value of the node with the greatest value within the tree. The tree should be traversed recursively.

    Hint: the function sum_of_nodes in the example above may come in handy.

    An example of how the function should work:

    if __name__ == \"__main__\":\n    tree = Node(2)\n\n    tree.left_child = Node(3)\n    tree.left_child.left_child = Node(5)\n    tree.left_child.right_child = Node(8)\n\n    tree.right_child = Node(4)\n    tree.right_child.right_child = Node(11)\n\n    print(greatest_node(tree))

    11

    A sorted binary tree

    A binary tree is especially useful when the nodes are sorted in a certain way. This makes finding nodes in the tree fast and efficient.

    Let's take a look a tree which is sorted as follows: the left child of each node is smaller than the node itself, and the right child is correspondingly greater.

    \n \n \n \n \n \n \"11\n \n \n

    Now we can write a recursive algorithm for searching for nodes. The idea is very similar to the binary search from the previous section: if the current node is the node we are looking for, return True. Else, continue recursively with either the left or the right child tree. If the node is not defined, return False.

    def find_node(root: Node, value):\n    if root is None:\n        return False\n\n    if value == root.value:\n        return True\n\n    if value > root.value:\n        return find_node(root.right_child, value)\n\n    return find_node(root.left_child, value)

    The class Employee models an employee of a company:

    class Employee:\n    def __init__(self, name: str):\n        self.name = name\n        self.subordinates = []\n\n    def add_subordinate(self, employee: 'Employee'):\n        self.subordinates.append(employee)

    Please write a function named count_subordinates(employee: Employee) which recursively counts the number of subordinates each employee has.

    An example of the function in action:

    if __name__ == \"__main__\":\n    t1 = Employee(\"Sally\")\n    t2 = Employee(\"Eric\")\n    t3 = Employee(\"Matthew\")\n    t4 = Employee(\"Emily\")\n    t5 = Employee(\"Adele\")\n    t6 = Employee(\"Claire\")\n    t1.add_subordinate(t4)\n    t1.add_subordinate(t6)\n    t4.add_subordinate(t2)\n    t4.add_subordinate(t3)\n    t4.add_subordinate(t5)\n    print(count_subordinates(t1))\n    print(count_subordinates(t4))\n    print(count_subordinates(t5))

    5\n3\n0

    Revisiting the times before recursion

    Let's finish off this part of the material with a slightly larger exercise concentrating on object oriented programming principles. We do not recommend using recursion in this series of tasks, but list comprehension techniques will come in useful.

    In this exercise you will write two different classes, which will in turn form the backbone of the programming exercise which follows this one, where you will write an interactive application.

    Task

    Please write a class named Task which models a single task in a software company's list of tasks. Tasks have

      \n
    • a description
    • \n
    • an estimate of the hours required for completing the task
    • \n
    • the name of the programmer assigned to the task
    • \n
    • a field for keeping track of whether the task is finished
    • \n
    • a unique identifier
    • \n

    The class is used as follows:

    t1 = Task(\"program hello world\", \"Eric\", 3)\nprint(t1.id, t1.description, t1.programmer, t1.workload)\nprint(t1)\nprint(t1.is_finished())\nt1.mark_finished()\nprint(t1)\nprint(t1.is_finished())\nt2 = Task(\"program webstore\", \"Adele\", 10)\nt3 = Task(\"program mobile app for workload accounting\", \"Eric\", 25)\nprint(t2)\nprint(t3)

    1 program hello world Eric 3\n1: program hello world (3 hours), programmer Eric NOT FINISHED\nFalse\n1: program hello world (3 hours), programmer Eric FINISHED\nTrue\n2: program webstore (10 hours), programmer Adele NOT FINISHED\n3: program mobile app for workload accounting (25 hours), programmer Eric NOT FINISHED

    Some clarifications:

      \n
    • the state of the task (finished or not yet finished) can be checked with the function is_finished(self) which returns a Boolean value
    • \n
    • a task is not finished when it is created
    • \n
    • a task is marked as finished by calling the method mark_finished(self)
    • \n
    • the id of a task is a running number which starts with 1. The id of the first task is 1, the id of the second is 2, and so forth.
    • \n

    Hint: id can be implemented as a class variable.

    OrderBook

    Please write a class named OrderBook which collects all the tasks ordered from the software company. The tasks should be modelled with the class Task you just wrote.

    The basic version of an OrderBook is used as follows:

    orders = OrderBook()\norders.add_order(\"program webstore\", \"Adele\", 10)\norders.add_order(\"program mobile app for workload accounting\", \"Eric\", 25)\norders.add_order(\"program app for practising mathematics\", \"Adele\", 100)\n\nfor order in orders.all_orders():\n    print(order)\n\nprint()\n\nfor programmer in orders.programmers():\n    print(programmer)

    1: program webstore (10 hours), programmer Adele NOT FINISHED\n2: program mobile app for workload accounting (25 hours), programmer Eric NOT FINISHED\n3: program app for practising mathematics (100 hours), programmer Adele NOT FINISHED

    Adele\nEric

    At this stage your OrderBook should provide three methods:

      \n
    • add_order(self, description, programmer, workload) which adds a new order to the OrderBook. An OrderBook stores the orders internally as Task objects. NB: the method should take exactly the arguments mentioned, or else the automated tests will not work correctly.
    • \n
    • all_orders(self) returns a list of all the tasks stored in the OrderBook
    • \n
    • programmers(self) returns a list of the names of all the programmers with tasks stored in the OrderBook. The list should contain each programmer only once
    • \n

    Hint: an easy method for removing duplicates is handling the list initially as a set. A set is a collection of items where each unique item appears only once. A set can be then converted back into a list, and we can then be sure each item is now unique:

    my_list = [1,1,3,6,4,1,3]\nmy_list2 = list(set(my_list))\nprint(my_list)\nprint(my_list2)

    [1, 1, 3, 6, 4, 1, 3]\n[1, 3, 4, 6]

    Some more features for OrderBook

    Please write three more methods in your OrderBook class.

    The method mark_finished(self, id: int) takes the id of the task as its argument and marks the relevant task as finished:

    orders = OrderBook()\norders.add_order(\"program webstore\", \"Adele\", 10)\norders.add_order(\"program mobile app for workload accounting\", \"Eric\", 25)\norders.add_order(\"program app for practising mathematics\", \"Adele\", 100)\n\norders.mark_finished(1)\norders.mark_finished(2)\n\nfor order in orders.all_orders():\n    print(order)

    1: program webstore (10 hours), programmer Adele FINISHED\n2: program mobile app for workload accounting (25 hours), programmer Eric FINISHED\n3: program app for practising mathematics (100 hours), programmer Adele NOT FINISHED

    If there is no task with the given id, the method should raise a ValueError exception. If you need a refresher on raising exceptions, please have a look at part 6.

    The methods finished_orders(self) and unfinished_orders(self) work as expected: both return a list containing the relevant tasks from the OrderBook.

    Finishing touches to OrderBook

    Please write one last method in your OrderBook class: status_of_programmer(self, programmer: str) which returns a tuple. The tuple should contain the number of finished and unfinished tasks the programmer has assigned to them, along with the estimated hours in both categories.

    orders = OrderBook()\norders.add_order(\"program webstore\", \"Adele\", 10)\norders.add_order(\"program mobile app for workload accounting\", \"Adele\", 25)\norders.add_order(\"program app for practising mathematics\", \"Adele\", 100)\norders.add_order(\"program the next facebook\", \"Eric\", 1000)\n\norders.mark_finished(1)\norders.mark_finished(2)\n\nstatus = orders.status_of_programmer(\"Adele\")\nprint(status)

    (2, 1, 35, 100)

    The first item in the tuple is the number of finished tasks, while the second item is the number of unfinished tasks. The third and fourth items are the sums of workload estimates for the finished and unfinished tasks, respectively.

    If there is no programmer with the given name, the method should raise a ValueError exception.

    In this exercise you will create an interactive application for administering the tasks ordered from a software company. The implementation is completely up to you, but you may use the building blocks from the previous exercise in your application. The examples in the last section of part 10 can also prove useful.

    Without error handling

    The application should work exactly as follows:

    commands:\n0 exit\n1 add order\n2 list finished tasks\n3 list unfinished tasks\n4 mark task as finished\n5 programmers\n6 status of programmer

    command: 1\ndescription: program the next facebook\nprogrammer and workload estimate: jonah 1000\nadded!

    command: 1\ndescription: program mobile app for workload accounting\nprogrammer and workload estimate: eric 25\nadded!

    command: 1\ndescription: program an app for music theory revision\nprogrammer and workload estimate: nina 12\nadded!

    command: 1\ndescription: program the next twitter\nprogrammer and workload estimate: jonah 55\nadded!

    command: 2\nno finished tasks

    command: 3\n1: program the next facebook (1000 hours), programmer jonah NOT FINISHED\n2: program mobile app for workload accounting (25 hours), programmer eric NOT FINISHED\n3: program an app for music theory revision (12 hours), programmer nina NOT FINISHED\n4: program the next twitter (55 hours), programmer jonah NOT FINISHED

    command: 4\nid: 2\nmarked as finished

    command: 4\nid: 4\nmarked as finished

    command: 2\n2: program mobile app for workload accounting (25 hours), programmer eric FINISHED\n4: program the next twitter (55 hours), programmer jonah FINISHED

    command: 3\n1: program the next facebook (1000 hours), programmer jonah NOT FINISHED\n3: program an app for music theory revision (12 hours), programmer nina NOT FINISHED

    command: 5\njonah\neric\nnina

    command: 6\nprogrammer: jonah\ntasks: finished 2 not finished 1, hours: done 55 scheduled 1000

    The first exercise point is granted for a working application when all user input is flawless.

    Handling errors in user input

    To gain the second exercise point for this exercise your application is expected to recover from erroneus user input. Any input which does not follow the specified format should produce an error message erroneous input, and result in yet another repeat of the loop asking for a new command:

    command: 1\ndescription: program mobile app for workload accounting\nprogrammer and workload estimate: eric xxx\nerroneous input

    command: 1\ndescription: program mobile app for workload accounting\nprogrammer and workload estimate: eric\nerroneous input

    command: 4\nid: 1000000\nerroneous input

    command: 4\nid: XXXX\nerroneous input

    command: 6\nprogrammer: unknownprogrammer\nerroneous input

    Please respond to a quick questionnaire on this part of the course.

    ","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-11/4-more-recursion-examples.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-11/page-data.json b/page-data/part-11/page-data.json index 149af71b4..2a730b271 100644 --- a/page-data/part-11/page-data.json +++ b/page-data/part-11/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/part-11", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-11","title":"Part 11"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-11/index.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-11","title":"Part 11"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-11/index.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-12/1-functions-as-arguments/page-data.json b/page-data/part-12/1-functions-as-arguments/page-data.json index bb7351db5..ef09a9101 100644 --- a/page-data/part-12/1-functions-as-arguments/page-data.json +++ b/page-data/part-12/1-functions-as-arguments/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-12/1-functions-as-arguments", - "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 able to sort lists according to different criteria"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what a lambda expression is"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use lambda expression with other Python functions"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how a function is passed as an argument to another function"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We are already familiar with the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":" and the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":", which are used to sort lists into their natural order. For numbers and strings this usually works just fine. For anything more complicated than that, however, what Python deems to be the natural order of items is not always what was intended by us as programmers."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, a list of tuples is, by default, sorted based on the first item of each tuple:"}]},{"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":"products "},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banana\""}]},{"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":"5.95"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apple\""}]},{"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":"3.95"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"orange\""}]},{"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":"4.50"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"watermelon\""}]},{"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":"4.95"}]},{"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\nproducts"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"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":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" products"},{"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":"product"},{"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":"('apple', 3.95)\n('banana', 5.95)\n('orange', 4.5)\n('watermelon', 4.95)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"But what if we wanted to sort the list based on the price?"}]},{"type":"element","tagName":"h2","properties":{"id":"functions-as-arguments","style":"position:relative;"},"children":[{"type":"text","value":"Functions as arguments"},{"type":"element","tagName":"a","properties":{"href":"#functions-as-arguments","ariaLabel":"functions as arguments 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":"A sorting method or function usually accepts an optional second argument which allows you to bypass the default sorting criteria. This second argument is a function which defines how the value of each item on the list is determined. As the list is sorted, Python calls this function when it compares the items to each other."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's take a look at an example:"}]},{"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":"order_by_price"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"item"},{"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":"tuple"}]},{"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":"# Return the price, which is the second item within the tuple"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" item"},{"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":"text","value":"\n\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 products "},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banana\""}]},{"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":"5.95"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apple\""}]},{"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":"3.95"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"orange\""}]},{"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":"4.50"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"watermelon\""}]},{"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":"4.95"}]},{"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","comment"]},"children":[{"type":"text","value":"# Use the function order_by_price for sorting"}]},{"type":"text","value":"\n products"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"order_by_price"},{"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":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" products"},{"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":"product"},{"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":"('apple', 3.95)\n('orange', 4.5)\n('watermelon', 4.95)\n('banana', 5.95)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now the list is sorted based on the prices of the items, but what really happens in the program?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"order_by_price"}]},{"type":"text","value":" is actually pretty simple. It takes one item as its argument and returns a value for that item. More specifically, it returns the second item in the tuple, which represents the price. But then we have this line of code, where the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":" method is called:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"products.sort(key=order_by_price)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":" method is called with a function as its argument. This is not a reference to the return value of the function, but a reference to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"the function itself"}]},{"type":"text","value":". The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":" method calls this function multiple times, using each item on the list as the argument in turn."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we include an extra print statement in the function definition of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"order_by_price"}]},{"type":"text","value":", we can verify that the function does indeed get called once per each item on the 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":"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":"order_by_price"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"item"},{"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":"tuple"}]},{"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":"# Print the item"}]},{"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\"Function call: order_by_price("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"item"},{"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\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Return the price, which is the second item within the tuple"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" item"},{"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":"text","value":"\n\n\nproducts "},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banana\""}]},{"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":"5.95"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apple\""}]},{"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":"3.95"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"orange\""}]},{"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":"4.50"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"watermelon\""}]},{"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":"4.95"}]},{"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","comment"]},"children":[{"type":"text","value":"# Use the function order_by_price for sorting"}]},{"type":"text","value":"\nproducts"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"order_by_price"},{"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":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" products"},{"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":"product"},{"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":"Function call: order_by_price(('banana', 5.95))\nFunction call: order_by_price(('apple', 3.95))\nFunction call: order_by_price(('orange', 4.5))\nFunction call: order_by_price(('watermelon', 4.95))\n('apple', 3.95)\n('orange', 4.5)\n('watermelon', 4.95)\n('banana', 5.95)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The order can be "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reversed"}]},{"type":"text","value":" with another keyword argument; "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reverse"}]},{"type":"text","value":", which is available with both the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":" method and the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"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":"text","value":"products"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"order_by_price"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" reverse"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nt2 "},{"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":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"products"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"order_by_price"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" reverse"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"a-function-definition-within-a-function-definition","style":"position:relative;"},"children":[{"type":"text","value":"A function definition within a function definition"},{"type":"element","tagName":"a","properties":{"href":"#a-function-definition-within-a-function-definition","ariaLabel":"a function definition within a function definition 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 could also include a named function for this new price-based sort functionality we created. Let's add a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort_by_price"}]},{"type":"text","value":":"}]},{"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":"order_by_price"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"item"},{"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":"tuple"}]},{"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":" item"},{"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":"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":"sort_by_price"}]},{"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":"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":"# use the order_by_price function here"}]},{"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","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"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":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"order_by_price"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nproducts "},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banana\""}]},{"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":"5.95"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apple\""}]},{"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":"3.95"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"orange\""}]},{"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":"4.50"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"watermelon\""}]},{"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":"4.95"}]},{"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":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sort_by_price"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"products"},{"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":"product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we know that the helper function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"order_by_price"}]},{"type":"text","value":" is not used anywhere outside the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort_by_price"}]},{"type":"text","value":" function, we can place the former function definition within the latter function definition:"}]},{"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":"sort_by_price"}]},{"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":"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":"# helper function defined within the function"}]},{"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":"order_by_price"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"item"},{"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":"tuple"}]},{"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":" item"},{"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":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"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":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"order_by_price"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sort by remaining stock","tmcname":"part12-01_remaining_stock"},"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":"sort_by_remaining_stock(items: list)"}]},{"type":"text","value":". The function takes a list of tuples as its argument. The tuples consist of the name, price and remaining stock of a product. The function should return a new list, where the items are sorted according to the stock remaining, lowest value first. The original list should not be changed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should work as follows:"}]},{"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":"products "},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banana\""}]},{"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":"5.95"}]},{"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":"12"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apple\""}]},{"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":"3.95"}]},{"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":"3"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"orange\""}]},{"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":"4.50"}]},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"watermelon\""}]},{"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":"4.95"}]},{"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":"22"}]},{"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":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sort_by_remaining_stock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"products"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"product"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"product"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" pcs\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\norange 2 pcs\napple 3 pcs\nbanana 12 pcs\nwatermelon 22 pcs\n"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sort by number of seasons","tmcname":"part12-02_seasons"},"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":"sort_by_seasons(items: list)"}]},{"type":"text","value":" which takes a list of dictionaries as its argument. Each dictionary contains the information of a single TV show. The function should sort this list by the number of seasons each show has, in ascending order. The function should not change the original list, but return a new list instead."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should work as follows:"}]},{"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":"shows "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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":"\"Dexter\""}]},{"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":"\"rating\""}]},{"type":"text","value":" "},{"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":"8.6"}]},{"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":"\"seasons\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":" "},{"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":"\"name\""}]},{"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":"\"Friends\""}]},{"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":"\"rating\""}]},{"type":"text","value":" "},{"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":"8.9"}]},{"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":"\"seasons\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":" "},{"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":"\"name\""}]},{"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":"\"Simpsons\""}]},{"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":"\"rating\""}]},{"type":"text","value":" "},{"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":"8.7"}]},{"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":"\"seasons\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"32"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"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":" show "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sort_by_seasons"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"shows"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"show"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'name'"}]},{"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":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"show"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'seasons'"}]},{"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":" seasons\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\nDexter 9 seasons\nFriends 10 seasons\nSimpsons 32 seasons\n"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sort by ratings","tmcname":"part12-03_ratings"},"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":"sort_by_ratings(items: list)"}]},{"type":"text","value":" which takes a list of dictionaries as its argument. The structure of the dictionaries is identical to the previous exercise. This function should sort the dictionaries in "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"descending order based on the shows' ratings"}]},{"type":"text","value":". The function should not change the original list, but return a new list instead."}]},{"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":"shows "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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":"\"Dexter\""}]},{"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":"\"rating\""}]},{"type":"text","value":" "},{"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":"8.6"}]},{"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":"\"seasons\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":" "},{"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":"\"name\""}]},{"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":"\"Friends\""}]},{"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":"\"rating\""}]},{"type":"text","value":" "},{"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":"8.9"}]},{"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":"\"seasons\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":" "},{"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":"\"name\""}]},{"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":"\"Simpsons\""}]},{"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":"\"rating\""}]},{"type":"text","value":" "},{"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":"8.7"}]},{"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":"\"seasons\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"32"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"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":"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":"\"Rating according to IMDB\""}]},{"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":" show "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sort_by_ratings"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"shows"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"show"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'name'"}]},{"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":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"show"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'rating'"}]},{"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":"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":"Rating according to IMDB\nFriends 8.9\nSimpsons 8.7\nDexter 8.6"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"sorting-collections-of-your-own-objects","style":"position:relative;"},"children":[{"type":"text","value":"Sorting collections of your own objects"},{"type":"element","tagName":"a","properties":{"href":"#sorting-collections-of-your-own-objects","ariaLabel":"sorting collections of your own objects 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":"Using the same principle, let's write a program which sorts a list of objects from our own "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" class in two different ways:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The class models a single student \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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":"id"}]},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" credits"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"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":"id"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" credits\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" cr.\""}]}]},{"type":"text","value":"\n\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":"by_id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Student"},{"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":" item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"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":"by_credits"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Student"},{"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":" item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits\n\n\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 o1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Archie\""}]},{"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":"\"a123\""}]},{"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":"220"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n o2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Marvin\""}]},{"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":"\"m321\""}]},{"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":"210"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n o3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anna\""}]},{"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":"\"a999\""}]},{"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":"131"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n students "},{"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":"text","value":"o1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" o2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" o3"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sort by id:\""}]},{"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":" student "},{"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":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"by_id"},{"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":"student"},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sort by credits:\""}]},{"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":" student "},{"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":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"by_credits"},{"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":"student"},{"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":"Sort by id:\nArchie (a123), 220 cr.\nAnna (a999), 131 cr.\nMarvin (m321), 210 cr."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sort by credits:\nAnna (a999), 131 cr.\nMarvin (m321), 210 cr.\nArchie (a123), 220 cr."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, sorting by different criteria works exactly as intended. If the functions "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"by_id"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"by_credits"}]},{"type":"text","value":" are not needed elsewhere, there are ways of making the implementation simpler. We will return to this topic after these exercises."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"ClimbingRoute","tmcname":"part12-04_climbing_route"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the class definition for a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ClimbingRoute"}]},{"type":"text","value":". It works as follows:"}]},{"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":"route1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Edge\""}]},{"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":"38"}]},{"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":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nroute2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Smooth operator\""}]},{"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":"11"}]},{"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":"\"7A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nroute3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Synchro\""}]},{"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":"14"}]},{"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":"\"8C+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\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":"route1"},{"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":"route2"},{"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":"route3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" route3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" route3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"grade"},{"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":"Edge, length 38 metres, grade 6A+\nSmooth operator, length 11 metres, grade 7A\nSynchro 14 8C+"}]}]},{"type":"element","tagName":"h2","properties":{"id":"sort-by-length","style":"position:relative;"},"children":[{"type":"text","value":"Sort by length"},{"type":"element","tagName":"a","properties":{"href":"#sort-by-length","ariaLabel":"sort by length 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort_by_length(routes: list)"}]},{"type":"text","value":" which returns a new list of routes, sorted by length from longest to shortest."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should work as follows:"}]},{"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":"r1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Edge\""}]},{"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":"38"}]},{"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":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Smooth operator\""}]},{"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":"11"}]},{"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":"\"7A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Synchro\""}]},{"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":"14"}]},{"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":"\"8C+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Small steps\""}]},{"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":"12"}]},{"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":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nroutes "},{"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":"text","value":"r1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r4"},{"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":" route "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sort_by_length"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"routes"},{"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":"route"},{"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":"Edge, length 38 metres, grade 6A+\nSynchro, length 14 metres, grade 8C+\nSmall steps, length 12 metres, grade 6A+\nSmooth operator, length 11 metres, grade 7A"}]}]},{"type":"element","tagName":"h2","properties":{"id":"sort-by-difficulty","style":"position:relative;"},"children":[{"type":"text","value":"Sort by difficulty"},{"type":"element","tagName":"a","properties":{"href":"#sort-by-difficulty","ariaLabel":"sort by difficulty 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort_by_difficulty(routes: list)"}]},{"type":"text","value":" which returns a new list of routes, sorted by difficulty, i.e. grade, from hardest to easiest. For routes with the same grade, the longer one is more difficult. The scale of climbing route grades is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"4, 4+, 5, 5+, 6A, 6A+, ..."}]},{"type":"text","value":", which in practice works out as the alphabetical order for strings."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should work as follows:"}]},{"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":"r1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Edge\""}]},{"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":"38"}]},{"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":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Smooth operator\""}]},{"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":"11"}]},{"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":"\"7A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Synchro\""}]},{"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":"14"}]},{"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":"\"8C+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Small steps\""}]},{"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":"12"}]},{"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":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nroutes "},{"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":"text","value":"r1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r4"},{"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":" route "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sort_by_difficulty"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"routes"},{"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":"route"},{"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":"Synchro, length 14 metres, grade 8C+\nSmooth operator, length 11 metres, grade 7A\nEdge, length 38 metres, grade 6A+\nSmall steps, length 12 metres, grade 6A+"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint:"}]},{"type":"text","value":" if the order is based on a list or a tuple, by default Python sorts the items first based on the first item, next based on the second item, and so forth:"}]},{"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","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"a\""}]},{"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":"4"}]},{"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","string"]},"children":[{"type":"text","value":"\"a\""}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"b\""}]},{"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":"30"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"b\""}]},{"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"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","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[('a', 2), ('a', 4), ('b', 0), ('b', 30)]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Climbing areas","tmcname":"part12-05_climbing_areas"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In addition to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ClimbingRoute"}]},{"type":"text","value":" from the previous exercise, the exercise template contains the class definition for a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ClimbingArea"}]},{"type":"text","value":"."}]},{"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":"ca1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingArea"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olhava\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nca1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Edge\""}]},{"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":"38"}]},{"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":"\"6A+\""}]},{"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":"\nca1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Great cut\""}]},{"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":"36"}]},{"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":"\"6B\""}]},{"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":"\nca1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Swedish route\""}]},{"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":"42"}]},{"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":"\"5+\""}]},{"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\nca2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingArea"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nummi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nca2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Synchro\""}]},{"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":"14"}]},{"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":"\"8C+\""}]},{"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\nca3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingArea"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nalkkila slab\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nca3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Small steps\""}]},{"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":"12"}]},{"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":"\"6A+\""}]},{"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":"\nca3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Smooth operator\""}]},{"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":"11"}]},{"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":"\"7A\""}]},{"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":"\nca3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Piggy not likey\""}]},{"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":"12"}]},{"type":"text","value":" "},{"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":"\"6B+\""}]},{"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":"\nca3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Orchard\""}]},{"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":"8"}]},{"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":"\"6A\""}]},{"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":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ca1"},{"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":"ca3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ca3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"routes"},{"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"},{"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":"ca3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hardest_route"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Olhava, 3 routes, hardest 6B\nNalkkila slab 4\nSmooth operator, length 9 metres, grade 7A"}]}]},{"type":"element","tagName":"h2","properties":{"id":"sort-by-number-of-routes","style":"position:relative;"},"children":[{"type":"text","value":"Sort by number of routes"},{"type":"element","tagName":"a","properties":{"href":"#sort-by-number-of-routes","ariaLabel":"sort by number of routes 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort_by_number_of_routes"}]},{"type":"text","value":" which sorts climbing areas in ascending order based on the number of routes they each have."}]},{"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":"# ca1, ca2 and ca3 declared as above"}]},{"type":"text","value":"\nareas "},{"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":"text","value":"ca1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ca2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ca3"},{"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":" area "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sort_by_number_of_routes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"areas"},{"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":"area"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nummi, 1 routes, hardest 8C+\nOlhava, 3 routes, hardest 6B\nNalkkila slab, 4 routes, hardest 7A"}]}]},{"type":"element","tagName":"h2","properties":{"id":"sort-by-the-most-difficult-route","style":"position:relative;"},"children":[{"type":"text","value":"Sort by the most difficult route"},{"type":"element","tagName":"a","properties":{"href":"#sort-by-the-most-difficult-route","ariaLabel":"sort by the most difficult route 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort_by_most_difficult"}]},{"type":"text","value":" which sorts climbing areas in "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"descending"}]},{"type":"text","value":" order based on the most difficult route in each area."}]},{"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":"# ca1, ca2 and ca3 declared as above"}]},{"type":"text","value":"\nareas "},{"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":"text","value":"ca1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ca2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ca3"},{"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":" area "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sort_by_most_difficult"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"areas"},{"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":"area"},{"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":"Nummi, 1 routes, hardest 8C+\nNalkkila slab, 4 routes, hardest 7A\nOlhava, 3 routes, hardest 6B"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"lambda-expressions","style":"position:relative;"},"children":[{"type":"text","value":"Lambda expressions"},{"type":"element","tagName":"a","properties":{"href":"#lambda-expressions","ariaLabel":"lambda expressions 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 have mostly worked with functions from the viewpoint of modularity. It is true that functions play an important role in managing the complexity of your programs and avoiding code repetition. Functions are usually written so that they can be used many times."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"But sometimes you need something resembling a function that you will use just once. Lambda expressions allow you to create small, anonymous functions which are created (and discarded) as they are needed in the code. The general syntax is as follows:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lambda : "}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sorting a list of tuples by the second item in each tuple would look like this implemented with a lambda expression:"}]},{"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":"products "},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banana\""}]},{"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":"5.95"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apple\""}]},{"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":"3.95"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"orange\""}]},{"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":"4.50"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"watermelon\""}]},{"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":"4.95"}]},{"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","comment"]},"children":[{"type":"text","value":"# Function is created \"on the fly\" with a lambda expression:"}]},{"type":"text","value":"\nproducts"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" item"},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" products"},{"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":"product"},{"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":"('apple', 3.95)\n('orange', 4.5)\n('watermelon', 4.95)\n('banana', 5.95)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The expression"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lambda item: item[1]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"is equivalent to the function definition"}]},{"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":"price"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"item"},{"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":" item"},{"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":"p","properties":{},"children":[{"type":"text","value":"except for the fact that a lambda function doesn't have a name. This is why lambda functions are called anonymous functions."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In every other respect a lambda function is no different from any other function, and they can be used in all the same contexts as any equivalent named function. For example, the following program sorts a list of strings alphabetically by the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"last"}]},{"type":"text","value":" character in each 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":"strings "},{"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":"\"Mickey\""}]},{"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":"\"Mack\""}]},{"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":"\"Marvin\""}]},{"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":"\"Minnie\""}]},{"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":"\"Merl\""}]},{"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":" word "},{"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":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strings"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" word"},{"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":"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":"word"},{"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":"Minnie\nMack\nMerl\nMarvin\nMickey"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We can also combine list comprehensions, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"join"}]},{"type":"text","value":" method and lambda expressions. For example, we could sort strings based on just the vowels in them, ignoring all other characters:"}]},{"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":"strings "},{"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":"\"Mickey\""}]},{"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":"\"Mack\""}]},{"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":"\"Marvin\""}]},{"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":"\"Minnie\""}]},{"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":"\"Merl\""}]},{"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":" word "},{"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":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strings"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" word"},{"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"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":"c "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" c "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" c "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"aeiou\""}]},{"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","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":"word"},{"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":"Mack\nMarvin\nMerl\nMickey\nMinnie"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Anonymous functions can also be used with other built in Python functions, not just those used for sorting. For example, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"min"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"max"}]},{"type":"text","value":" functions also take a keyword argument named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"key"}]},{"type":"text","value":". It is used as the criteria for comparing the items when selecting the minimum or maximum value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example we are dealing with audio recordings. First we select the oldest recording, and then the longest:"}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Recording"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The class models a single audio recording \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" performer"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" year"},{"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":"text","value":" runtime"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"performer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" performer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" year\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"runtime "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" runtime\n\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"performer"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"runtime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" min.\""}]}]},{"type":"text","value":"\n\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 r1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Recording"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nevermind\""}]},{"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":"\"Nirvana\""}]},{"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":"1991"}]},{"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":"43"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n r2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Recording"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Let It Be\""}]},{"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":"\"Beatles\""}]},{"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":"1969"}]},{"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":"35"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n r3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Recording"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Joshua Tree\""}]},{"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":"\"U2\""}]},{"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":"1986"}]},{"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":"50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n recordings "},{"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":"text","value":"r1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The oldest recording:\""}]},{"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","builtin"]},"children":[{"type":"text","value":"min"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"recordings"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" rec"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" rec"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year"},{"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":"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 longest recording:\""}]},{"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","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"recordings"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" rec"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" rec"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"runtime"},{"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":"The oldest recording:\nLet It Be (Beatles), 1969. 35 min.\nThe longest recording:\nU2 (Joshua Tree), 1986. 50 min."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"BallPlayers","tmcname":"part12-06_ballplayers"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the definition for a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BallPlayer"}]},{"type":"text","value":". It has the following public attributes:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"name"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"shirt number "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"scored goals "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"goals"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"assists completed "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"assists"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"minutes played "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"minutes"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement the following functions. NB: each function has a different type of return value."}]},{"type":"element","tagName":"h2","properties":{"id":"most-goals","style":"position:relative;"},"children":[{"type":"text","value":"Most goals"},{"type":"element","tagName":"a","properties":{"href":"#most-goals","ariaLabel":"most goals 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"most_goals"}]},{"type":"text","value":" which takes a list of ball players as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return the name of the player who scored the most goals, in string format."}]},{"type":"element","tagName":"h2","properties":{"id":"most-points","style":"position:relative;"},"children":[{"type":"text","value":"Most points"},{"type":"element","tagName":"a","properties":{"href":"#most-points","ariaLabel":"most points 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"most_points"}]},{"type":"text","value":", which takes a list of ball players as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return a tuple containing the name and shirt number of the player who has scored the most points. The total number of points is the number of goals and the number of assists combined."}]},{"type":"element","tagName":"h2","properties":{"id":"least-minutes","style":"position:relative;"},"children":[{"type":"text","value":"Least minutes"},{"type":"element","tagName":"a","properties":{"href":"#least-minutes","ariaLabel":"least minutes 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"least_minutes"}]},{"type":"text","value":", which takes a list of ball players as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BallPlayer"}]},{"type":"text","value":" object which has the smallest value of minutes played."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can test your functions with the following program:"}]},{"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":"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 player1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BallPlayer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Archie Bonkers\""}]},{"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":"13"}]},{"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":"5"}]},{"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":"12"}]},{"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":"46"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n player2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BallPlayer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Speedy Tickets\""}]},{"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":"7"}]},{"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":"2"}]},{"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":"26"}]},{"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":"55"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n player3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BallPlayer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Cruella De Hill\""}]},{"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":"9"}]},{"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":"1"}]},{"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":"32"}]},{"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":"26"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n player4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BallPlayer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Devilled Tasmanian\""}]},{"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":"12"}]},{"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":"1"}]},{"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":"11"}]},{"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":"41"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n player5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BallPlayer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Donald Quack\""}]},{"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":"4"}]},{"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":"3"}]},{"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":"9"}]},{"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":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n \n team "},{"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":"text","value":"player1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player5"},{"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":"most_goals"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"team"},{"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":"most_points"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"team"},{"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":"least_minutes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"team"},{"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":"This should print out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Archie Bonkers\n('Cruella De Hill', 9)\nBallPlayer(name=Donald Quack, number=4, goals=3, assists=9, minutes=12)"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"functions-as-arguments-within-your-own-functions","style":"position:relative;"},"children":[{"type":"text","value":"Functions as arguments within your own functions"},{"type":"element","tagName":"a","properties":{"href":"#functions-as-arguments-within-your-own-functions","ariaLabel":"functions as arguments within your own 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 established above that it is possible to pass a reference to a function as an argument to another function. To wrap this section up, let's write our very own function which takes a function as its argument."}]},{"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":"# the type hint \"callable\" refers to a function"}]},{"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":"perform_operation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"operation"},{"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":"callable"}]},{"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":"# Call the function which was passed as an argument"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" operation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"5"}]},{"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":"my_sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"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":"text","value":" b"},{"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":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\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":"my_product"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"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":"text","value":" b"},{"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":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" b\n\n\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","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"perform_operation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_sum"},{"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":"perform_operation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_product"},{"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":"perform_operation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" y"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"15\n50\n5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The value returned by the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"perform_operation"}]},{"type":"text","value":" depends on which function was passed as an argument. Any function which accepts two arguments would do, no matter whether it is anonymous or named."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Passing references to functions as arguments to other functions might not be something you will end up doing on a daily basis in your programming career, but it can be a useful technique. This following program selects some lines from one file and writes them to another file. The way the lines are selected is determined by a function which returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" only if the lines should be copied:"}]},{"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":"copy_lines"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"source_file"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" target_file"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" criterion"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"source_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" source"},{"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":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target_file"},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" target"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" source"},{"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":"# Remove any whitespace from beginning and end of line"}]},{"type":"text","value":"\n line "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strip"},{"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":"if"}]},{"type":"text","value":" criterion"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"line"},{"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 target"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"line "},{"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":"\"\\n\""}]},{"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":"# Some examples"}]},{"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":"# If the third parameter is not given, copy all lines"}]},{"type":"text","value":"\n copy_lines"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"first.txt\""}]},{"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":"\"second.txt\""}]},{"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":"# Copy all non-empty lines"}]},{"type":"text","value":"\n copy_lines"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"first.txt\""}]},{"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":"\"second.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" line"},{"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":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"line"},{"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":"0"}]},{"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":"# Copy all lines which contain the word \"Python\""}]},{"type":"text","value":"\n copy_lines"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"first.txt\""}]},{"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":"\"second.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" line"},{"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":"\"Python\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" line"},{"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":"# Copy all lines which do not end in a full stop"}]},{"type":"text","value":"\n copy_lines"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"first.txt\""}]},{"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":"\"second.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" line"},{"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":" "},{"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":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function definition contains a default value for the keyword parameter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"criterion"}]},{"type":"text","value":": "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lambda x: True"}]},{"type":"text","value":". This anonymous function always returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" regardless of the input. So, the default behaviour is to copy all lines. As usual, if a value is given for a parameter with a default value, the new value replaces the default value."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Product search","tmcname":"part12-07_product_search"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This exercise deals with products which are stored in tuples. The examples all assume a variable named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"products"}]},{"type":"text","value":", which is assigned the following value:"}]},{"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":"products "},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banana\""}]},{"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":"5.95"}]},{"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":"12"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apple\""}]},{"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":"3.95"}]},{"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":"3"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"orange\""}]},{"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":"4.50"}]},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"watermelon\""}]},{"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":"4.95"}]},{"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":"22"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"kale\""}]},{"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":"0.99"}]},{"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":"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":"Each tuple contains three items: name, price and amount."}]},{"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":"search(products: list, criterion: callable)"}]},{"type":"text","value":". The second argument to the function is a function itself, and it should be able to process a tuple as defined above, and return a Boolean value. The search function should return a new list containing those tuples from the original which fulfil the criterion."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we wanted to include only products whose price was under 4 euros, we could use the following criterion 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"price_under_4_euros"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"product"},{"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":" product"},{"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":"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":"4"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" if the second item in the tuple is less than four in value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"search"}]},{"type":"text","value":" function in use:"}]},{"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":"for"}]},{"type":"text","value":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"products"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" price_under_4_euros"},{"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":"product"},{"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":"('apple', 3.95, 3)\n('kale', 0.99, 1)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The criterion function can also be a lambda function. If we wanted to search for only those products whose amount was at least 11, we could write the following:"}]},{"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":"for"}]},{"type":"text","value":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"products"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" t"},{"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":"10"}]},{"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":"product"},{"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":"('banana', 5.95, 12)\n('watermelon', 4.95, 22)"}]}]}]}]},"html":"

    After this section

      \n
    • You will be able to sort lists according to different criteria
    • \n
    • You will know what a lambda expression is
    • \n
    • You will be able to use lambda expression with other Python functions
    • \n
    • You will know how a function is passed as an argument to another function
    • \n

    We are already familiar with the method sort and the function sorted, which are used to sort lists into their natural order. For numbers and strings this usually works just fine. For anything more complicated than that, however, what Python deems to be the natural order of items is not always what was intended by us as programmers.

    For example, a list of tuples is, by default, sorted based on the first item of each tuple:

    products = [(\"banana\", 5.95), (\"apple\", 3.95), (\"orange\", 4.50), (\"watermelon\", 4.95)]\n\nproducts.sort()\n\nfor product in products:\n    print(product)

    ('apple', 3.95)\n('banana', 5.95)\n('orange', 4.5)\n('watermelon', 4.95)

    But what if we wanted to sort the list based on the price?

    Functions as arguments

    A sorting method or function usually accepts an optional second argument which allows you to bypass the default sorting criteria. This second argument is a function which defines how the value of each item on the list is determined. As the list is sorted, Python calls this function when it compares the items to each other.

    Let's take a look at an example:

    def order_by_price(item: tuple):\n    # Return the price, which is the second item within the tuple\n    return item[1]\n\nif __name__ == \"__main__\":\n    products = [(\"banana\", 5.95), (\"apple\", 3.95), (\"orange\", 4.50), (\"watermelon\", 4.95)]\n\n    # Use the function order_by_price for sorting\n    products.sort(key=order_by_price)\n\n    for product in products:\n        print(product)

    ('apple', 3.95)\n('orange', 4.5)\n('watermelon', 4.95)\n('banana', 5.95)

    Now the list is sorted based on the prices of the items, but what really happens in the program?

    The function order_by_price is actually pretty simple. It takes one item as its argument and returns a value for that item. More specifically, it returns the second item in the tuple, which represents the price. But then we have this line of code, where the sort method is called:

    products.sort(key=order_by_price)

    Here the sort method is called with a function as its argument. This is not a reference to the return value of the function, but a reference to the function itself. The sort method calls this function multiple times, using each item on the list as the argument in turn.

    If we include an extra print statement in the function definition of order_by_price, we can verify that the function does indeed get called once per each item on the list:

    def order_by_price(item: tuple):\n    # Print the item\n    print(f\"Function call: order_by_price({item})\")\n\n    # Return the price, which is the second item within the tuple\n    return item[1]\n\n\nproducts = [(\"banana\", 5.95), (\"apple\", 3.95), (\"orange\", 4.50), (\"watermelon\", 4.95)]\n\n# Use the function order_by_price for sorting\nproducts.sort(key=order_by_price)\n\nfor product in products:\n    print(product)

    Function call: order_by_price(('banana', 5.95))\nFunction call: order_by_price(('apple', 3.95))\nFunction call: order_by_price(('orange', 4.5))\nFunction call: order_by_price(('watermelon', 4.95))\n('apple', 3.95)\n('orange', 4.5)\n('watermelon', 4.95)\n('banana', 5.95)

    The order can be reversed with another keyword argument; reverse, which is available with both the sort method and the sorted function:

    products.sort(key=order_by_price, reverse=True)\n\nt2 = sorted(products, key=order_by_price, reverse=True)

    A function definition within a function definition

    We could also include a named function for this new price-based sort functionality we created. Let's add a function named sort_by_price:

    def order_by_price(item: tuple):\n    return item[1]\n\ndef sort_by_price(items: list):\n    # use the order_by_price function here\n    return sorted(items, key=order_by_price)\n\nproducts = [(\"banana\", 5.95), (\"apple\", 3.95), (\"orange\", 4.50), (\"watermelon\", 4.95)]\n\nfor product in sort_by_price(products):\n    print(product)

    If we know that the helper function order_by_price is not used anywhere outside the sort_by_price function, we can place the former function definition within the latter function definition:

    def sort_by_price(items: list):\n    # helper function defined within the function\n    def order_by_price(item: tuple):\n        return item[1]\n\n    return sorted(items, key=order_by_price)

    Please write a function named sort_by_remaining_stock(items: list). The function takes a list of tuples as its argument. The tuples consist of the name, price and remaining stock of a product. The function should return a new list, where the items are sorted according to the stock remaining, lowest value first. The original list should not be changed.

    The function should work as follows:

    products = [(\"banana\", 5.95, 12), (\"apple\", 3.95, 3), (\"orange\", 4.50, 2), (\"watermelon\", 4.95, 22)]\n\nfor product in sort_by_remaining_stock(products):\n    print(f\"{product[0]} {product[2]} pcs\")
    \norange 2 pcs\napple 3 pcs\nbanana 12 pcs\nwatermelon 22 pcs\n

    Please write a function named sort_by_seasons(items: list) which takes a list of dictionaries as its argument. Each dictionary contains the information of a single TV show. The function should sort this list by the number of seasons each show has, in ascending order. The function should not change the original list, but return a new list instead.

    The function should work as follows:

    shows = [{ \"name\": \"Dexter\", \"rating\" : 8.6, \"seasons\":9 }, { \"name\": \"Friends\", \"rating\" : 8.9, \"seasons\":10 },  { \"name\": \"Simpsons\", \"rating\" : 8.7, \"seasons\":32 }  ]\n\nfor show in sort_by_seasons(shows):\n    print(f\"{show['name']} {show['seasons']} seasons\")
    \nDexter 9 seasons\nFriends 10 seasons\nSimpsons 32 seasons\n

    Please write a function named sort_by_ratings(items: list) which takes a list of dictionaries as its argument. The structure of the dictionaries is identical to the previous exercise. This function should sort the dictionaries in descending order based on the shows' ratings. The function should not change the original list, but return a new list instead.

    shows = [{ \"name\": \"Dexter\", \"rating\" : 8.6, \"seasons\":9 }, { \"name\": \"Friends\", \"rating\" : 8.9, \"seasons\":10 },  { \"name\": \"Simpsons\", \"rating\" : 8.7, \"seasons\":32 }  ]\n\nprint(\"Rating according to IMDB\")\nfor show in sort_by_ratings(shows):\n    print(f\"{show['name']}  {show['rating']}\")

    Rating according to IMDB\nFriends 8.9\nSimpsons 8.7\nDexter 8.6

    Sorting collections of your own objects

    Using the same principle, let's write a program which sorts a list of objects from our own Student class in two different ways:

    class Student:\n    \"\"\" The class models a single student \"\"\"\n    def __init__(self, name: str, id: str, credits: int):\n        self.name = name\n        self.id = id\n        self.credits = credits\n\n    def __str__(self):\n        return f\"{self.name} ({self.id}), {self.credits} cr.\"\n\n\ndef by_id(item: Student):\n    return item.id\n\ndef by_credits(item: Student):\n    return item.credits\n\n\nif __name__ == \"__main__\":\n    o1 = Student(\"Archie\", \"a123\", 220)\n    o2 = Student(\"Marvin\", \"m321\", 210)\n    o3 = Student(\"Anna\", \"a999\", 131)\n\n    students = [o1, o2, o3]\n\n    print(\"Sort by id:\")\n    for student in sorted(students, key=by_id):\n        print(student)\n\n    print()\n\n    print(\"Sort by credits:\")\n    for student in sorted(students, key=by_credits):\n        print(student)

    Sort by id:\nArchie (a123), 220 cr.\nAnna (a999), 131 cr.\nMarvin (m321), 210 cr.

    Sort by credits:\nAnna (a999), 131 cr.\nMarvin (m321), 210 cr.\nArchie (a123), 220 cr.

    As you can see above, sorting by different criteria works exactly as intended. If the functions by_id and by_credits are not needed elsewhere, there are ways of making the implementation simpler. We will return to this topic after these exercises.

    The exercise template contains the class definition for a ClimbingRoute. It works as follows:

    route1 = ClimbingRoute(\"Edge\", 38, \"6A+\")\nroute2 = ClimbingRoute(\"Smooth operator\", 11, \"7A\")\nroute3 = ClimbingRoute(\"Synchro\", 14, \"8C+\")\n\n\nprint(route1)\nprint(route2)\nprint(route3.name, route3.length, route3.grade)

    Edge, length 38 metres, grade 6A+\nSmooth operator, length 11 metres, grade 7A\nSynchro 14 8C+

    Sort by length

    Please write a function named sort_by_length(routes: list) which returns a new list of routes, sorted by length from longest to shortest.

    The function should work as follows:

    r1 = ClimbingRoute(\"Edge\", 38, \"6A+\")\nr2 = ClimbingRoute(\"Smooth operator\", 11, \"7A\")\nr3 = ClimbingRoute(\"Synchro\", 14, \"8C+\")\nr4 = ClimbingRoute(\"Small steps\", 12, \"6A+\")\n\nroutes = [r1, r2, r3, r4]\n\nfor route in sort_by_length(routes):\n    print(route)

    Edge, length 38 metres, grade 6A+\nSynchro, length 14 metres, grade 8C+\nSmall steps, length 12 metres, grade 6A+\nSmooth operator, length 11 metres, grade 7A

    Sort by difficulty

    Please write a function named sort_by_difficulty(routes: list) which returns a new list of routes, sorted by difficulty, i.e. grade, from hardest to easiest. For routes with the same grade, the longer one is more difficult. The scale of climbing route grades is 4, 4+, 5, 5+, 6A, 6A+, ..., which in practice works out as the alphabetical order for strings.

    The function should work as follows:

    r1 = ClimbingRoute(\"Edge\", 38, \"6A+\")\nr2 = ClimbingRoute(\"Smooth operator\", 11, \"7A\")\nr3 = ClimbingRoute(\"Synchro\", 14, \"8C+\")\nr4 = ClimbingRoute(\"Small steps\", 12, \"6A+\")\n\nroutes = [r1, r2, r3, r4]\nfor route in sort_by_difficulty(routes):\n    print(route)

    Synchro, length 14 metres, grade 8C+\nSmooth operator, length 11 metres, grade 7A\nEdge, length 38 metres, grade 6A+\nSmall steps, length 12 metres, grade 6A+

    Hint: if the order is based on a list or a tuple, by default Python sorts the items first based on the first item, next based on the second item, and so forth:

    my_list = [(\"a\", 4),(\"a\", 2),(\"b\", 30), (\"b\", 0) ]\nprint(sorted(my_list))

    [('a', 2), ('a', 4), ('b', 0), ('b', 30)]

    In addition to the ClimbingRoute from the previous exercise, the exercise template contains the class definition for a ClimbingArea.

    ca1 = ClimbingArea(\"Olhava\")\nca1.add_route(ClimbingRoute(\"Edge\", 38, \"6A+\"))\nca1.add_route(ClimbingRoute(\"Great cut\", 36, \"6B\"))\nca1.add_route(ClimbingRoute(\"Swedish route\", 42, \"5+\"))\n\nca2 = ClimbingArea(\"Nummi\")\nca2.add_route(ClimbingRoute(\"Synchro\", 14, \"8C+\"))\n\nca3 = ClimbingArea(\"Nalkkila slab\")\nca3.add_route(ClimbingRoute(\"Small steps\", 12, \"6A+\"))\nca3.add_route(ClimbingRoute(\"Smooth operator\", 11, \"7A\"))\nca3.add_route(ClimbingRoute(\"Piggy not likey\", 12 , \"6B+\"))\nca3.add_route(ClimbingRoute(\"Orchard\", 8, \"6A\"))\n\nprint(ca1)\nprint(ca3.name, ca3.routes())\nprint(ca3.hardest_route())

    Olhava, 3 routes, hardest 6B\nNalkkila slab 4\nSmooth operator, length 9 metres, grade 7A

    Sort by number of routes

    Please write a function named sort_by_number_of_routes which sorts climbing areas in ascending order based on the number of routes they each have.

    # ca1, ca2 and ca3 declared as above\nareas = [ca1, ca2, ca3]\nfor area in sort_by_number_of_routes(areas):\n    print(area)\n

    Nummi, 1 routes, hardest 8C+\nOlhava, 3 routes, hardest 6B\nNalkkila slab, 4 routes, hardest 7A

    Sort by the most difficult route

    Please write a function named sort_by_most_difficult which sorts climbing areas in descending order based on the most difficult route in each area.

    # ca1, ca2 and ca3 declared as above\nareas = [ca1, ca2, ca3]\nfor area in sort_by_most_difficult(areas):\n    print(area)

    Nummi, 1 routes, hardest 8C+\nNalkkila slab, 4 routes, hardest 7A\nOlhava, 3 routes, hardest 6B

    Lambda expressions

    We have mostly worked with functions from the viewpoint of modularity. It is true that functions play an important role in managing the complexity of your programs and avoiding code repetition. Functions are usually written so that they can be used many times.

    But sometimes you need something resembling a function that you will use just once. Lambda expressions allow you to create small, anonymous functions which are created (and discarded) as they are needed in the code. The general syntax is as follows:

    lambda <parameters> : <expression>

    Sorting a list of tuples by the second item in each tuple would look like this implemented with a lambda expression:

    products = [(\"banana\", 5.95), (\"apple\", 3.95), (\"orange\", 4.50), (\"watermelon\", 4.95)]\n\n# Function is created \"on the fly\" with a lambda expression:\nproducts.sort(key=lambda item: item[1])\n\nfor product in products:\n    print(product)

    ('apple', 3.95)\n('orange', 4.5)\n('watermelon', 4.95)\n('banana', 5.95)

    The expression

    lambda item: item[1]

    is equivalent to the function definition

    def price(item):\n    return item[1]

    except for the fact that a lambda function doesn't have a name. This is why lambda functions are called anonymous functions.

    In every other respect a lambda function is no different from any other function, and they can be used in all the same contexts as any equivalent named function. For example, the following program sorts a list of strings alphabetically by the last character in each string:

    strings = [\"Mickey\", \"Mack\", \"Marvin\", \"Minnie\", \"Merl\"]\n\nfor word in sorted(strings, key=lambda word: word[-1]):\n    print(word)

    Minnie\nMack\nMerl\nMarvin\nMickey

    We can also combine list comprehensions, the join method and lambda expressions. For example, we could sort strings based on just the vowels in them, ignoring all other characters:

    strings = [\"Mickey\", \"Mack\", \"Marvin\", \"Minnie\", \"Merl\"]\n\nfor word in sorted(strings, key=lambda word: \"\".join([c for c in word if c in \"aeiou\"])):\n    print(word)

    Mack\nMarvin\nMerl\nMickey\nMinnie

    Anonymous functions can also be used with other built in Python functions, not just those used for sorting. For example, the min and max functions also take a keyword argument named key. It is used as the criteria for comparing the items when selecting the minimum or maximum value.

    In the following example we are dealing with audio recordings. First we select the oldest recording, and then the longest:

    \nclass Recording:\n    \"\"\" The class models a single audio recording \"\"\"\n    def __init__(self, name: str, performer: str, year: int, runtime: int):\n        self.name = name\n        self.performer = performer\n        self.year = year\n        self.runtime = runtime\n\n\n    def __str__(self):\n        return f\"{self.name} ({self.performer}), {self.year}. {self.runtime} min.\"\n\nif __name__ == \"__main__\":\n    r1 = Recording(\"Nevermind\", \"Nirvana\", 1991, 43)\n    r2 = Recording(\"Let It Be\", \"Beatles\", 1969, 35)\n    r3 = Recording(\"Joshua Tree\", \"U2\", 1986, 50)\n\n    recordings = [r1, r2, r3]\n\n\n    print(\"The oldest recording:\")\n    print(min(recordings, key=lambda rec: rec.year))\n\n    print(\"The longest recording:\")\n    print(max(recordings, key=lambda rec: rec.runtime))

    The oldest recording:\nLet It Be (Beatles), 1969. 35 min.\nThe longest recording:\nU2 (Joshua Tree), 1986. 50 min.

    The exercise template contains the definition for a class named BallPlayer. It has the following public attributes:

      \n
    • name
    • \n
    • shirt number number
    • \n
    • scored goals goals
    • \n
    • assists completed assists
    • \n
    • minutes played minutes
    • \n

    Please implement the following functions. NB: each function has a different type of return value.

    Most goals

    Please write a function named most_goals which takes a list of ball players as its argument.

    The function should return the name of the player who scored the most goals, in string format.

    Most points

    Please write a function named most_points, which takes a list of ball players as its argument.

    The function should return a tuple containing the name and shirt number of the player who has scored the most points. The total number of points is the number of goals and the number of assists combined.

    Least minutes

    Please write a function named least_minutes, which takes a list of ball players as its argument.

    The function should return the BallPlayer object which has the smallest value of minutes played.

    You can test your functions with the following program:

    if __name__ == \"__main__\":\n    player1 = BallPlayer(\"Archie Bonkers\", 13, 5, 12, 46)\n    player2 = BallPlayer(\"Speedy Tickets\", 7, 2, 26, 55)\n    player3 = BallPlayer(\"Cruella De Hill\", 9, 1, 32, 26)\n    player4 = BallPlayer(\"Devilled Tasmanian\", 12, 1, 11, 41)\n    player5 = BallPlayer(\"Donald Quack\", 4, 3, 9, 12)\n    \n    team = [player1, player2, player3, player4, player5]\n    print(most_goals(team))\n    print(most_points(team))\n    print(least_minutes(team))

    This should print out:

    Archie Bonkers\n('Cruella De Hill', 9)\nBallPlayer(name=Donald Quack, number=4, goals=3, assists=9, minutes=12)

    Functions as arguments within your own functions

    We established above that it is possible to pass a reference to a function as an argument to another function. To wrap this section up, let's write our very own function which takes a function as its argument.

    # the type hint \"callable\" refers to a function\ndef perform_operation(operation: callable):\n    # Call the function which was passed as an argument\n    return operation(10, 5)\n\ndef my_sum(a: int, b: int):\n    return a + b\n\ndef my_product(a: int, b: int):\n    return a * b\n\n\nif __name__ == \"__main__\":\n    print(perform_operation(my_sum))\n    print(perform_operation(my_product))\n    print(perform_operation(lambda x,y: x - y))\n

    15\n50\n5

    The value returned by the function perform_operation depends on which function was passed as an argument. Any function which accepts two arguments would do, no matter whether it is anonymous or named.

    Passing references to functions as arguments to other functions might not be something you will end up doing on a daily basis in your programming career, but it can be a useful technique. This following program selects some lines from one file and writes them to another file. The way the lines are selected is determined by a function which returns True only if the lines should be copied:

    def copy_lines(source_file: str, target_file: str, criterion= lambda x: True):\n    with open(source_file) as source, open(target_file, \"w\") as target:\n        for line in source:\n            # Remove any whitespace from beginning and end of line\n            line = line.strip()\n\n            if criterion(line):\n                target.write(line + \"\\n\")\n\n# Some examples\nif __name__ == \"__main__\":\n    # If the third parameter is not given, copy all lines\n    copy_lines(\"first.txt\", \"second.txt\")\n\n    # Copy all non-empty lines\n    copy_lines(\"first.txt\", \"second.txt\", lambda line: len(line) > 0)\n\n    # Copy all lines which contain the word \"Python\"\n    copy_lines(\"first.txt\", \"second.txt\", lambda line: \"Python\" in line)\n\n    # Copy all lines which do not end in a full stop\n    copy_lines(\"first.txt\", \"second.txt\", lambda line: line[-1] != \".\")

    The function definition contains a default value for the keyword parameter criterion: lambda x: True. This anonymous function always returns True regardless of the input. So, the default behaviour is to copy all lines. As usual, if a value is given for a parameter with a default value, the new value replaces the default value.

    This exercise deals with products which are stored in tuples. The examples all assume a variable named products, which is assigned the following value:

    products = [(\"banana\", 5.95, 12), (\"apple\", 3.95, 3), (\"orange\", 4.50, 2), (\"watermelon\", 4.95, 22), (\"kale\", 0.99, 1)]

    Each tuple contains three items: name, price and amount.

    Please write a function named search(products: list, criterion: callable). The second argument to the function is a function itself, and it should be able to process a tuple as defined above, and return a Boolean value. The search function should return a new list containing those tuples from the original which fulfil the criterion.

    If we wanted to include only products whose price was under 4 euros, we could use the following criterion function:

    def price_under_4_euros(product):\n    return product[1] < 4

    The function returns True if the second item in the tuple is less than four in value.

    An example of the search function in use:

    for product in search(products, price_under_4_euros):\n    print(product)

    ('apple', 3.95, 3)\n('kale', 0.99, 1)

    The criterion function can also be a lambda function. If we wanted to search for only those products whose amount was at least 11, we could write the following:

    for product in search(products, lambda t: t[2]>10):\n    print(product)

    ('banana', 5.95, 12)\n('watermelon', 4.95, 22)

    ","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-12/1-functions-as-arguments.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 able to sort lists according to different criteria"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what a lambda expression is"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use lambda expression with other Python functions"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how a function is passed as an argument to another function"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We are already familiar with the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":" and the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":", which are used to sort lists into their natural order. For numbers and strings this usually works just fine. For anything more complicated than that, however, what Python deems to be the natural order of items is not always what was intended by us as programmers."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, a list of tuples is, by default, sorted based on the first item of each tuple:"}]},{"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":"products "},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banana\""}]},{"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":"5.95"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apple\""}]},{"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":"3.95"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"orange\""}]},{"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":"4.50"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"watermelon\""}]},{"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":"4.95"}]},{"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\nproducts"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"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":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" products"},{"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":"product"},{"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":"('apple', 3.95)\n('banana', 5.95)\n('orange', 4.5)\n('watermelon', 4.95)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"But what if we wanted to sort the list based on the price?"}]},{"type":"element","tagName":"h2","properties":{"id":"functions-as-arguments","style":"position:relative;"},"children":[{"type":"text","value":"Functions as arguments"},{"type":"element","tagName":"a","properties":{"href":"#functions-as-arguments","ariaLabel":"functions as arguments 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":"A sorting method or function usually accepts an optional second argument which allows you to bypass the default sorting criteria. This second argument is a function which defines how the value of each item on the list is determined. As the list is sorted, Python calls this function when it compares the items to each other."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's take a look at an example:"}]},{"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":"order_by_price"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"item"},{"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":"tuple"}]},{"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":"# Return the price, which is the second item within the tuple"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" item"},{"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":"text","value":"\n\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 products "},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banana\""}]},{"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":"5.95"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apple\""}]},{"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":"3.95"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"orange\""}]},{"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":"4.50"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"watermelon\""}]},{"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":"4.95"}]},{"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","comment"]},"children":[{"type":"text","value":"# Use the function order_by_price for sorting"}]},{"type":"text","value":"\n products"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"order_by_price"},{"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":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" products"},{"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":"product"},{"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":"('apple', 3.95)\n('orange', 4.5)\n('watermelon', 4.95)\n('banana', 5.95)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now the list is sorted based on the prices of the items, but what really happens in the program?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"order_by_price"}]},{"type":"text","value":" is actually pretty simple. It takes one item as its argument and returns a value for that item. More specifically, it returns the second item in the tuple, which represents the price. But then we have this line of code, where the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":" method is called:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"products.sort(key=order_by_price)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":" method is called with a function as its argument. This is not a reference to the return value of the function, but a reference to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"the function itself"}]},{"type":"text","value":". The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":" method calls this function multiple times, using each item on the list as the argument in turn."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we include an extra print statement in the function definition of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"order_by_price"}]},{"type":"text","value":", we can verify that the function does indeed get called once per each item on the 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":"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":"order_by_price"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"item"},{"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":"tuple"}]},{"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":"# Print the item"}]},{"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\"Function call: order_by_price("}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"item"},{"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\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Return the price, which is the second item within the tuple"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" item"},{"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":"text","value":"\n\n\nproducts "},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banana\""}]},{"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":"5.95"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apple\""}]},{"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":"3.95"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"orange\""}]},{"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":"4.50"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"watermelon\""}]},{"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":"4.95"}]},{"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","comment"]},"children":[{"type":"text","value":"# Use the function order_by_price for sorting"}]},{"type":"text","value":"\nproducts"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"order_by_price"},{"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":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" products"},{"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":"product"},{"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":"Function call: order_by_price(('banana', 5.95))\nFunction call: order_by_price(('apple', 3.95))\nFunction call: order_by_price(('orange', 4.5))\nFunction call: order_by_price(('watermelon', 4.95))\n('apple', 3.95)\n('orange', 4.5)\n('watermelon', 4.95)\n('banana', 5.95)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The order can be "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reversed"}]},{"type":"text","value":" with another keyword argument; "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reverse"}]},{"type":"text","value":", which is available with both the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":" method and the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"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":"text","value":"products"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"order_by_price"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" reverse"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nt2 "},{"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":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"products"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"order_by_price"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" reverse"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"a-function-definition-within-a-function-definition","style":"position:relative;"},"children":[{"type":"text","value":"A function definition within a function definition"},{"type":"element","tagName":"a","properties":{"href":"#a-function-definition-within-a-function-definition","ariaLabel":"a function definition within a function definition 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 could also include a named function for this new price-based sort functionality we created. Let's add a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort_by_price"}]},{"type":"text","value":":"}]},{"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":"order_by_price"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"item"},{"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":"tuple"}]},{"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":" item"},{"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":"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":"sort_by_price"}]},{"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":"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":"# use the order_by_price function here"}]},{"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","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"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":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"order_by_price"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nproducts "},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banana\""}]},{"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":"5.95"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apple\""}]},{"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":"3.95"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"orange\""}]},{"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":"4.50"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"watermelon\""}]},{"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":"4.95"}]},{"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":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sort_by_price"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"products"},{"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":"product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we know that the helper function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"order_by_price"}]},{"type":"text","value":" is not used anywhere outside the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort_by_price"}]},{"type":"text","value":" function, we can place the former function definition within the latter function definition:"}]},{"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":"sort_by_price"}]},{"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":"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":"# helper function defined within the function"}]},{"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":"order_by_price"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"item"},{"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":"tuple"}]},{"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":" item"},{"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":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"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":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"order_by_price"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sort by remaining stock","tmcname":"part12-01_remaining_stock"},"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":"sort_by_remaining_stock(items: list)"}]},{"type":"text","value":". The function takes a list of tuples as its argument. The tuples consist of the name, price and remaining stock of a product. The function should return a new list, where the items are sorted according to the stock remaining, lowest value first. The original list should not be changed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should work as follows:"}]},{"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":"products "},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banana\""}]},{"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":"5.95"}]},{"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":"12"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apple\""}]},{"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":"3.95"}]},{"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":"3"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"orange\""}]},{"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":"4.50"}]},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"watermelon\""}]},{"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":"4.95"}]},{"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":"22"}]},{"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":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sort_by_remaining_stock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"products"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"product"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"product"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" pcs\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\norange 2 pcs\napple 3 pcs\nbanana 12 pcs\nwatermelon 22 pcs\n"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sort by number of seasons","tmcname":"part12-02_seasons"},"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":"sort_by_seasons(items: list)"}]},{"type":"text","value":" which takes a list of dictionaries as its argument. Each dictionary contains the information of a single TV show. The function should sort this list by the number of seasons each show has, in ascending order. The function should not change the original list, but return a new list instead."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should work as follows:"}]},{"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":"shows "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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":"\"Dexter\""}]},{"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":"\"rating\""}]},{"type":"text","value":" "},{"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":"8.6"}]},{"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":"\"seasons\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":" "},{"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":"\"name\""}]},{"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":"\"Friends\""}]},{"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":"\"rating\""}]},{"type":"text","value":" "},{"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":"8.9"}]},{"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":"\"seasons\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":" "},{"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":"\"name\""}]},{"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":"\"Simpsons\""}]},{"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":"\"rating\""}]},{"type":"text","value":" "},{"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":"8.7"}]},{"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":"\"seasons\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"32"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"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":" show "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sort_by_seasons"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"shows"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"show"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'name'"}]},{"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":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"show"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'seasons'"}]},{"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":" seasons\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\nDexter 9 seasons\nFriends 10 seasons\nSimpsons 32 seasons\n"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sort by ratings","tmcname":"part12-03_ratings"},"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":"sort_by_ratings(items: list)"}]},{"type":"text","value":" which takes a list of dictionaries as its argument. The structure of the dictionaries is identical to the previous exercise. This function should sort the dictionaries in "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"descending order based on the shows' ratings"}]},{"type":"text","value":". The function should not change the original list, but return a new list instead."}]},{"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":"shows "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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":"\"Dexter\""}]},{"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":"\"rating\""}]},{"type":"text","value":" "},{"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":"8.6"}]},{"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":"\"seasons\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":" "},{"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":"\"name\""}]},{"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":"\"Friends\""}]},{"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":"\"rating\""}]},{"type":"text","value":" "},{"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":"8.9"}]},{"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":"\"seasons\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":" "},{"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":"\"name\""}]},{"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":"\"Simpsons\""}]},{"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":"\"rating\""}]},{"type":"text","value":" "},{"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":"8.7"}]},{"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":"\"seasons\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"32"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"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":"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":"\"Rating according to IMDB\""}]},{"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":" show "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sort_by_ratings"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"shows"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"show"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'name'"}]},{"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":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"show"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'rating'"}]},{"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":"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":"Rating according to IMDB\nFriends 8.9\nSimpsons 8.7\nDexter 8.6"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"sorting-collections-of-your-own-objects","style":"position:relative;"},"children":[{"type":"text","value":"Sorting collections of your own objects"},{"type":"element","tagName":"a","properties":{"href":"#sorting-collections-of-your-own-objects","ariaLabel":"sorting collections of your own objects 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":"Using the same principle, let's write a program which sorts a list of objects from our own "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" class in two different ways:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The class models a single student \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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":"id"}]},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" credits"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"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":"id"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" credits\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" cr.\""}]}]},{"type":"text","value":"\n\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":"by_id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Student"},{"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":" item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"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":"by_credits"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Student"},{"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":" item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits\n\n\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 o1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Archie\""}]},{"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":"\"a123\""}]},{"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":"220"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n o2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Marvin\""}]},{"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":"\"m321\""}]},{"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":"210"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n o3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anna\""}]},{"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":"\"a999\""}]},{"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":"131"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n students "},{"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":"text","value":"o1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" o2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" o3"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sort by id:\""}]},{"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":" student "},{"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":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"by_id"},{"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":"student"},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sort by credits:\""}]},{"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":" student "},{"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":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"by_credits"},{"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":"student"},{"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":"Sort by id:\nArchie (a123), 220 cr.\nAnna (a999), 131 cr.\nMarvin (m321), 210 cr."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sort by credits:\nAnna (a999), 131 cr.\nMarvin (m321), 210 cr.\nArchie (a123), 220 cr."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, sorting by different criteria works exactly as intended. If the functions "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"by_id"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"by_credits"}]},{"type":"text","value":" are not needed elsewhere, there are ways of making the implementation simpler. We will return to this topic after these exercises."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"ClimbingRoute","tmcname":"part12-04_climbing_route"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the class definition for a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ClimbingRoute"}]},{"type":"text","value":". It works as follows:"}]},{"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":"route1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Edge\""}]},{"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":"38"}]},{"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":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nroute2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Smooth operator\""}]},{"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":"11"}]},{"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":"\"7A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nroute3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Synchro\""}]},{"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":"14"}]},{"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":"\"8C+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\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":"route1"},{"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":"route2"},{"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":"route3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" route3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" route3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"grade"},{"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":"Edge, length 38 metres, grade 6A+\nSmooth operator, length 11 metres, grade 7A\nSynchro 14 8C+"}]}]},{"type":"element","tagName":"h2","properties":{"id":"sort-by-length","style":"position:relative;"},"children":[{"type":"text","value":"Sort by length"},{"type":"element","tagName":"a","properties":{"href":"#sort-by-length","ariaLabel":"sort by length 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort_by_length(routes: list)"}]},{"type":"text","value":" which returns a new list of routes, sorted by length from longest to shortest."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should work as follows:"}]},{"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":"r1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Edge\""}]},{"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":"38"}]},{"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":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Smooth operator\""}]},{"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":"11"}]},{"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":"\"7A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Synchro\""}]},{"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":"14"}]},{"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":"\"8C+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Small steps\""}]},{"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":"12"}]},{"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":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nroutes "},{"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":"text","value":"r1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r4"},{"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":" route "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sort_by_length"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"routes"},{"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":"route"},{"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":"Edge, length 38 metres, grade 6A+\nSynchro, length 14 metres, grade 8C+\nSmall steps, length 12 metres, grade 6A+\nSmooth operator, length 11 metres, grade 7A"}]}]},{"type":"element","tagName":"h2","properties":{"id":"sort-by-difficulty","style":"position:relative;"},"children":[{"type":"text","value":"Sort by difficulty"},{"type":"element","tagName":"a","properties":{"href":"#sort-by-difficulty","ariaLabel":"sort by difficulty 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort_by_difficulty(routes: list)"}]},{"type":"text","value":" which returns a new list of routes, sorted by difficulty, i.e. grade, from hardest to easiest. For routes with the same grade, the longer one is more difficult. The scale of climbing route grades is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"4, 4+, 5, 5+, 6A, 6A+, ..."}]},{"type":"text","value":", which in practice works out as the alphabetical order for strings."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should work as follows:"}]},{"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":"r1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Edge\""}]},{"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":"38"}]},{"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":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Smooth operator\""}]},{"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":"11"}]},{"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":"\"7A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Synchro\""}]},{"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":"14"}]},{"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":"\"8C+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nr4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Small steps\""}]},{"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":"12"}]},{"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":"\"6A+\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nroutes "},{"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":"text","value":"r1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r4"},{"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":" route "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sort_by_difficulty"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"routes"},{"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":"route"},{"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":"Synchro, length 14 metres, grade 8C+\nSmooth operator, length 11 metres, grade 7A\nEdge, length 38 metres, grade 6A+\nSmall steps, length 12 metres, grade 6A+"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint:"}]},{"type":"text","value":" if the order is based on a list or a tuple, by default Python sorts the items first based on the first item, next based on the second item, and so forth:"}]},{"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","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"a\""}]},{"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":"4"}]},{"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","string"]},"children":[{"type":"text","value":"\"a\""}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"b\""}]},{"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":"30"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"b\""}]},{"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"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","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[('a', 2), ('a', 4), ('b', 0), ('b', 30)]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Climbing areas","tmcname":"part12-05_climbing_areas"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In addition to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ClimbingRoute"}]},{"type":"text","value":" from the previous exercise, the exercise template contains the class definition for a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ClimbingArea"}]},{"type":"text","value":"."}]},{"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":"ca1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingArea"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olhava\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nca1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Edge\""}]},{"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":"38"}]},{"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":"\"6A+\""}]},{"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":"\nca1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Great cut\""}]},{"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":"36"}]},{"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":"\"6B\""}]},{"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":"\nca1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Swedish route\""}]},{"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":"42"}]},{"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":"\"5+\""}]},{"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\nca2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingArea"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nummi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nca2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Synchro\""}]},{"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":"14"}]},{"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":"\"8C+\""}]},{"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\nca3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ClimbingArea"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nalkkila slab\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nca3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Small steps\""}]},{"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":"12"}]},{"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":"\"6A+\""}]},{"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":"\nca3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Smooth operator\""}]},{"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":"11"}]},{"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":"\"7A\""}]},{"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":"\nca3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Piggy not likey\""}]},{"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":"12"}]},{"type":"text","value":" "},{"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":"\"6B+\""}]},{"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":"\nca3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_route"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ClimbingRoute"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Orchard\""}]},{"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":"8"}]},{"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":"\"6A\""}]},{"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":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ca1"},{"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":"ca3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ca3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"routes"},{"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"},{"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":"ca3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"hardest_route"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Olhava, 3 routes, hardest 6B\nNalkkila slab 4\nSmooth operator, length 9 metres, grade 7A"}]}]},{"type":"element","tagName":"h2","properties":{"id":"sort-by-number-of-routes","style":"position:relative;"},"children":[{"type":"text","value":"Sort by number of routes"},{"type":"element","tagName":"a","properties":{"href":"#sort-by-number-of-routes","ariaLabel":"sort by number of routes 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort_by_number_of_routes"}]},{"type":"text","value":" which sorts climbing areas in ascending order based on the number of routes they each have."}]},{"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":"# ca1, ca2 and ca3 declared as above"}]},{"type":"text","value":"\nareas "},{"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":"text","value":"ca1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ca2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ca3"},{"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":" area "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sort_by_number_of_routes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"areas"},{"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":"area"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nummi, 1 routes, hardest 8C+\nOlhava, 3 routes, hardest 6B\nNalkkila slab, 4 routes, hardest 7A"}]}]},{"type":"element","tagName":"h2","properties":{"id":"sort-by-the-most-difficult-route","style":"position:relative;"},"children":[{"type":"text","value":"Sort by the most difficult route"},{"type":"element","tagName":"a","properties":{"href":"#sort-by-the-most-difficult-route","ariaLabel":"sort by the most difficult route 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort_by_most_difficult"}]},{"type":"text","value":" which sorts climbing areas in "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"descending"}]},{"type":"text","value":" order based on the most difficult route in each area."}]},{"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":"# ca1, ca2 and ca3 declared as above"}]},{"type":"text","value":"\nareas "},{"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":"text","value":"ca1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ca2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" ca3"},{"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":" area "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sort_by_most_difficult"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"areas"},{"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":"area"},{"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":"Nummi, 1 routes, hardest 8C+\nNalkkila slab, 4 routes, hardest 7A\nOlhava, 3 routes, hardest 6B"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"lambda-expressions","style":"position:relative;"},"children":[{"type":"text","value":"Lambda expressions"},{"type":"element","tagName":"a","properties":{"href":"#lambda-expressions","ariaLabel":"lambda expressions 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 have mostly worked with functions from the viewpoint of modularity. It is true that functions play an important role in managing the complexity of your programs and avoiding code repetition. Functions are usually written so that they can be used many times."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"But sometimes you need something resembling a function that you will use just once. Lambda expressions allow you to create small, anonymous functions which are created (and discarded) as they are needed in the code. The general syntax is as follows:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lambda : "}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sorting a list of tuples by the second item in each tuple would look like this implemented with a lambda expression:"}]},{"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":"products "},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banana\""}]},{"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":"5.95"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apple\""}]},{"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":"3.95"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"orange\""}]},{"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":"4.50"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"watermelon\""}]},{"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":"4.95"}]},{"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","comment"]},"children":[{"type":"text","value":"# Function is created \"on the fly\" with a lambda expression:"}]},{"type":"text","value":"\nproducts"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" item"},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" products"},{"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":"product"},{"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":"('apple', 3.95)\n('orange', 4.5)\n('watermelon', 4.95)\n('banana', 5.95)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The expression"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lambda item: item[1]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"is equivalent to the function definition"}]},{"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":"price"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"item"},{"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":" item"},{"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":"p","properties":{},"children":[{"type":"text","value":"except for the fact that a lambda function doesn't have a name. This is why lambda functions are called anonymous functions."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In every other respect a lambda function is no different from any other function, and they can be used in all the same contexts as any equivalent named function. For example, the following program sorts a list of strings alphabetically by the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"last"}]},{"type":"text","value":" character in each 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":"strings "},{"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":"\"Mickey\""}]},{"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":"\"Mack\""}]},{"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":"\"Marvin\""}]},{"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":"\"Minnie\""}]},{"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":"\"Merl\""}]},{"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":" word "},{"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":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strings"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" word"},{"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":"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":"word"},{"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":"Minnie\nMack\nMerl\nMarvin\nMickey"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We can also combine list comprehensions, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"join"}]},{"type":"text","value":" method and lambda expressions. For example, we could sort strings based on just the vowels in them, ignoring all other characters:"}]},{"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":"strings "},{"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":"\"Mickey\""}]},{"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":"\"Mack\""}]},{"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":"\"Marvin\""}]},{"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":"\"Minnie\""}]},{"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":"\"Merl\""}]},{"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":" word "},{"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":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strings"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" word"},{"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"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":"c "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" c "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" c "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"aeiou\""}]},{"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","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":"word"},{"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":"Mack\nMarvin\nMerl\nMickey\nMinnie"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Anonymous functions can also be used with other built in Python functions, not just those used for sorting. For example, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"min"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"max"}]},{"type":"text","value":" functions also take a keyword argument named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"key"}]},{"type":"text","value":". It is used as the criteria for comparing the items when selecting the minimum or maximum value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example we are dealing with audio recordings. First we select the oldest recording, and then the longest:"}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Recording"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The class models a single audio recording \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" performer"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" year"},{"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":"text","value":" runtime"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"performer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" performer\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" year\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"runtime "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" runtime\n\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"performer"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"runtime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" min.\""}]}]},{"type":"text","value":"\n\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 r1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Recording"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nevermind\""}]},{"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":"\"Nirvana\""}]},{"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":"1991"}]},{"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":"43"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n r2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Recording"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Let It Be\""}]},{"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":"\"Beatles\""}]},{"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":"1969"}]},{"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":"35"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n r3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Recording"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Joshua Tree\""}]},{"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":"\"U2\""}]},{"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":"1986"}]},{"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":"50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n recordings "},{"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":"text","value":"r1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" r3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The oldest recording:\""}]},{"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","builtin"]},"children":[{"type":"text","value":"min"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"recordings"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" rec"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" rec"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year"},{"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":"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 longest recording:\""}]},{"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","builtin"]},"children":[{"type":"text","value":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"recordings"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" rec"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" rec"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"runtime"},{"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":"The oldest recording:\nLet It Be (Beatles), 1969. 35 min.\nThe longest recording:\nU2 (Joshua Tree), 1986. 50 min."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"BallPlayers","tmcname":"part12-06_ballplayers"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the definition for a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BallPlayer"}]},{"type":"text","value":". It has the following public attributes:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"name"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"shirt number "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"scored goals "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"goals"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"assists completed "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"assists"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"minutes played "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"minutes"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement the following functions. NB: each function has a different type of return value."}]},{"type":"element","tagName":"h2","properties":{"id":"most-goals","style":"position:relative;"},"children":[{"type":"text","value":"Most goals"},{"type":"element","tagName":"a","properties":{"href":"#most-goals","ariaLabel":"most goals 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"most_goals"}]},{"type":"text","value":" which takes a list of ball players as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return the name of the player who scored the most goals, in string format."}]},{"type":"element","tagName":"h2","properties":{"id":"most-points","style":"position:relative;"},"children":[{"type":"text","value":"Most points"},{"type":"element","tagName":"a","properties":{"href":"#most-points","ariaLabel":"most points 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"most_points"}]},{"type":"text","value":", which takes a list of ball players as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return a tuple containing the name and shirt number of the player who has scored the most points. The total number of points is the number of goals and the number of assists combined."}]},{"type":"element","tagName":"h2","properties":{"id":"least-minutes","style":"position:relative;"},"children":[{"type":"text","value":"Least minutes"},{"type":"element","tagName":"a","properties":{"href":"#least-minutes","ariaLabel":"least minutes 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"least_minutes"}]},{"type":"text","value":", which takes a list of ball players as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BallPlayer"}]},{"type":"text","value":" object which has the smallest value of minutes played."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can test your functions with the following program:"}]},{"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":"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 player1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BallPlayer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Archie Bonkers\""}]},{"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":"13"}]},{"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":"5"}]},{"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":"12"}]},{"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":"46"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n player2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BallPlayer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Speedy Tickets\""}]},{"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":"7"}]},{"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":"2"}]},{"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":"26"}]},{"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":"55"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n player3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BallPlayer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Cruella De Hill\""}]},{"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":"9"}]},{"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":"1"}]},{"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":"32"}]},{"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":"26"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n player4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BallPlayer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Devilled Tasmanian\""}]},{"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":"12"}]},{"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":"1"}]},{"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":"11"}]},{"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":"41"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n player5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BallPlayer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Donald Quack\""}]},{"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":"4"}]},{"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":"3"}]},{"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":"9"}]},{"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":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n \n team "},{"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":"text","value":"player1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player5"},{"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":"most_goals"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"team"},{"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":"most_points"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"team"},{"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":"least_minutes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"team"},{"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":"This should print out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Archie Bonkers\n('Cruella De Hill', 9)\nBallPlayer(name=Donald Quack, number=4, goals=3, assists=9, minutes=12)"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"functions-as-arguments-within-your-own-functions","style":"position:relative;"},"children":[{"type":"text","value":"Functions as arguments within your own functions"},{"type":"element","tagName":"a","properties":{"href":"#functions-as-arguments-within-your-own-functions","ariaLabel":"functions as arguments within your own 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 established above that it is possible to pass a reference to a function as an argument to another function. To wrap this section up, let's write our very own function which takes a function as its argument."}]},{"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":"# the type hint \"callable\" refers to a function"}]},{"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":"perform_operation"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"operation"},{"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":"callable"}]},{"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":"# Call the function which was passed as an argument"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" operation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"5"}]},{"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":"my_sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"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":"text","value":" b"},{"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":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\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":"my_product"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"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":"text","value":" b"},{"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":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" b\n\n\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","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"perform_operation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_sum"},{"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":"perform_operation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_product"},{"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":"perform_operation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" y"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"15\n50\n5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The value returned by the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"perform_operation"}]},{"type":"text","value":" depends on which function was passed as an argument. Any function which accepts two arguments would do, no matter whether it is anonymous or named."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Passing references to functions as arguments to other functions might not be something you will end up doing on a daily basis in your programming career, but it can be a useful technique. This following program selects some lines from one file and writes them to another file. The way the lines are selected is determined by a function which returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" only if the lines should be copied:"}]},{"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":"copy_lines"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"source_file"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" target_file"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" criterion"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"source_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" source"},{"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":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target_file"},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" target"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" source"},{"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":"# Remove any whitespace from beginning and end of line"}]},{"type":"text","value":"\n line "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strip"},{"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":"if"}]},{"type":"text","value":" criterion"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"line"},{"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 target"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"line "},{"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":"\"\\n\""}]},{"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":"# Some examples"}]},{"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":"# If the third parameter is not given, copy all lines"}]},{"type":"text","value":"\n copy_lines"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"first.txt\""}]},{"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":"\"second.txt\""}]},{"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":"# Copy all non-empty lines"}]},{"type":"text","value":"\n copy_lines"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"first.txt\""}]},{"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":"\"second.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" line"},{"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":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"line"},{"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":"0"}]},{"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":"# Copy all lines which contain the word \"Python\""}]},{"type":"text","value":"\n copy_lines"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"first.txt\""}]},{"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":"\"second.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" line"},{"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":"\"Python\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" line"},{"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":"# Copy all lines which do not end in a full stop"}]},{"type":"text","value":"\n copy_lines"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"first.txt\""}]},{"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":"\"second.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" line"},{"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":" "},{"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":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function definition contains a default value for the keyword parameter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"criterion"}]},{"type":"text","value":": "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lambda x: True"}]},{"type":"text","value":". This anonymous function always returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" regardless of the input. So, the default behaviour is to copy all lines. As usual, if a value is given for a parameter with a default value, the new value replaces the default value."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Product search","tmcname":"part12-07_product_search"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This exercise deals with products which are stored in tuples. The examples all assume a variable named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"products"}]},{"type":"text","value":", which is assigned the following value:"}]},{"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":"products "},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banana\""}]},{"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":"5.95"}]},{"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":"12"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apple\""}]},{"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":"3.95"}]},{"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":"3"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"orange\""}]},{"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":"4.50"}]},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"watermelon\""}]},{"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":"4.95"}]},{"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":"22"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"kale\""}]},{"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":"0.99"}]},{"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":"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":"Each tuple contains three items: name, price and amount."}]},{"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":"search(products: list, criterion: callable)"}]},{"type":"text","value":". The second argument to the function is a function itself, and it should be able to process a tuple as defined above, and return a Boolean value. The search function should return a new list containing those tuples from the original which fulfil the criterion."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we wanted to include only products whose price was under 4 euros, we could use the following criterion 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"price_under_4_euros"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"product"},{"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":" product"},{"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":"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":"4"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" if the second item in the tuple is less than four in value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"search"}]},{"type":"text","value":" function in use:"}]},{"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":"for"}]},{"type":"text","value":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"products"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" price_under_4_euros"},{"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":"product"},{"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":"('apple', 3.95, 3)\n('kale', 0.99, 1)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The criterion function can also be a lambda function. If we wanted to search for only those products whose amount was at least 11, we could write the following:"}]},{"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":"for"}]},{"type":"text","value":" product "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"products"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" t"},{"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":"10"}]},{"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":"product"},{"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":"('banana', 5.95, 12)\n('watermelon', 4.95, 22)"}]}]}]}]},"html":"

    After this section

      \n
    • You will be able to sort lists according to different criteria
    • \n
    • You will know what a lambda expression is
    • \n
    • You will be able to use lambda expression with other Python functions
    • \n
    • You will know how a function is passed as an argument to another function
    • \n

    We are already familiar with the method sort and the function sorted, which are used to sort lists into their natural order. For numbers and strings this usually works just fine. For anything more complicated than that, however, what Python deems to be the natural order of items is not always what was intended by us as programmers.

    For example, a list of tuples is, by default, sorted based on the first item of each tuple:

    products = [(\"banana\", 5.95), (\"apple\", 3.95), (\"orange\", 4.50), (\"watermelon\", 4.95)]\n\nproducts.sort()\n\nfor product in products:\n    print(product)

    ('apple', 3.95)\n('banana', 5.95)\n('orange', 4.5)\n('watermelon', 4.95)

    But what if we wanted to sort the list based on the price?

    Functions as arguments

    A sorting method or function usually accepts an optional second argument which allows you to bypass the default sorting criteria. This second argument is a function which defines how the value of each item on the list is determined. As the list is sorted, Python calls this function when it compares the items to each other.

    Let's take a look at an example:

    def order_by_price(item: tuple):\n    # Return the price, which is the second item within the tuple\n    return item[1]\n\nif __name__ == \"__main__\":\n    products = [(\"banana\", 5.95), (\"apple\", 3.95), (\"orange\", 4.50), (\"watermelon\", 4.95)]\n\n    # Use the function order_by_price for sorting\n    products.sort(key=order_by_price)\n\n    for product in products:\n        print(product)

    ('apple', 3.95)\n('orange', 4.5)\n('watermelon', 4.95)\n('banana', 5.95)

    Now the list is sorted based on the prices of the items, but what really happens in the program?

    The function order_by_price is actually pretty simple. It takes one item as its argument and returns a value for that item. More specifically, it returns the second item in the tuple, which represents the price. But then we have this line of code, where the sort method is called:

    products.sort(key=order_by_price)

    Here the sort method is called with a function as its argument. This is not a reference to the return value of the function, but a reference to the function itself. The sort method calls this function multiple times, using each item on the list as the argument in turn.

    If we include an extra print statement in the function definition of order_by_price, we can verify that the function does indeed get called once per each item on the list:

    def order_by_price(item: tuple):\n    # Print the item\n    print(f\"Function call: order_by_price({item})\")\n\n    # Return the price, which is the second item within the tuple\n    return item[1]\n\n\nproducts = [(\"banana\", 5.95), (\"apple\", 3.95), (\"orange\", 4.50), (\"watermelon\", 4.95)]\n\n# Use the function order_by_price for sorting\nproducts.sort(key=order_by_price)\n\nfor product in products:\n    print(product)

    Function call: order_by_price(('banana', 5.95))\nFunction call: order_by_price(('apple', 3.95))\nFunction call: order_by_price(('orange', 4.5))\nFunction call: order_by_price(('watermelon', 4.95))\n('apple', 3.95)\n('orange', 4.5)\n('watermelon', 4.95)\n('banana', 5.95)

    The order can be reversed with another keyword argument; reverse, which is available with both the sort method and the sorted function:

    products.sort(key=order_by_price, reverse=True)\n\nt2 = sorted(products, key=order_by_price, reverse=True)

    A function definition within a function definition

    We could also include a named function for this new price-based sort functionality we created. Let's add a function named sort_by_price:

    def order_by_price(item: tuple):\n    return item[1]\n\ndef sort_by_price(items: list):\n    # use the order_by_price function here\n    return sorted(items, key=order_by_price)\n\nproducts = [(\"banana\", 5.95), (\"apple\", 3.95), (\"orange\", 4.50), (\"watermelon\", 4.95)]\n\nfor product in sort_by_price(products):\n    print(product)

    If we know that the helper function order_by_price is not used anywhere outside the sort_by_price function, we can place the former function definition within the latter function definition:

    def sort_by_price(items: list):\n    # helper function defined within the function\n    def order_by_price(item: tuple):\n        return item[1]\n\n    return sorted(items, key=order_by_price)

    Please write a function named sort_by_remaining_stock(items: list). The function takes a list of tuples as its argument. The tuples consist of the name, price and remaining stock of a product. The function should return a new list, where the items are sorted according to the stock remaining, lowest value first. The original list should not be changed.

    The function should work as follows:

    products = [(\"banana\", 5.95, 12), (\"apple\", 3.95, 3), (\"orange\", 4.50, 2), (\"watermelon\", 4.95, 22)]\n\nfor product in sort_by_remaining_stock(products):\n    print(f\"{product[0]} {product[2]} pcs\")
    \norange 2 pcs\napple 3 pcs\nbanana 12 pcs\nwatermelon 22 pcs\n

    Please write a function named sort_by_seasons(items: list) which takes a list of dictionaries as its argument. Each dictionary contains the information of a single TV show. The function should sort this list by the number of seasons each show has, in ascending order. The function should not change the original list, but return a new list instead.

    The function should work as follows:

    shows = [{ \"name\": \"Dexter\", \"rating\" : 8.6, \"seasons\":9 }, { \"name\": \"Friends\", \"rating\" : 8.9, \"seasons\":10 },  { \"name\": \"Simpsons\", \"rating\" : 8.7, \"seasons\":32 }  ]\n\nfor show in sort_by_seasons(shows):\n    print(f\"{show['name']} {show['seasons']} seasons\")
    \nDexter 9 seasons\nFriends 10 seasons\nSimpsons 32 seasons\n

    Please write a function named sort_by_ratings(items: list) which takes a list of dictionaries as its argument. The structure of the dictionaries is identical to the previous exercise. This function should sort the dictionaries in descending order based on the shows' ratings. The function should not change the original list, but return a new list instead.

    shows = [{ \"name\": \"Dexter\", \"rating\" : 8.6, \"seasons\":9 }, { \"name\": \"Friends\", \"rating\" : 8.9, \"seasons\":10 },  { \"name\": \"Simpsons\", \"rating\" : 8.7, \"seasons\":32 }  ]\n\nprint(\"Rating according to IMDB\")\nfor show in sort_by_ratings(shows):\n    print(f\"{show['name']}  {show['rating']}\")

    Rating according to IMDB\nFriends 8.9\nSimpsons 8.7\nDexter 8.6

    Sorting collections of your own objects

    Using the same principle, let's write a program which sorts a list of objects from our own Student class in two different ways:

    class Student:\n    \"\"\" The class models a single student \"\"\"\n    def __init__(self, name: str, id: str, credits: int):\n        self.name = name\n        self.id = id\n        self.credits = credits\n\n    def __str__(self):\n        return f\"{self.name} ({self.id}), {self.credits} cr.\"\n\n\ndef by_id(item: Student):\n    return item.id\n\ndef by_credits(item: Student):\n    return item.credits\n\n\nif __name__ == \"__main__\":\n    o1 = Student(\"Archie\", \"a123\", 220)\n    o2 = Student(\"Marvin\", \"m321\", 210)\n    o3 = Student(\"Anna\", \"a999\", 131)\n\n    students = [o1, o2, o3]\n\n    print(\"Sort by id:\")\n    for student in sorted(students, key=by_id):\n        print(student)\n\n    print()\n\n    print(\"Sort by credits:\")\n    for student in sorted(students, key=by_credits):\n        print(student)

    Sort by id:\nArchie (a123), 220 cr.\nAnna (a999), 131 cr.\nMarvin (m321), 210 cr.

    Sort by credits:\nAnna (a999), 131 cr.\nMarvin (m321), 210 cr.\nArchie (a123), 220 cr.

    As you can see above, sorting by different criteria works exactly as intended. If the functions by_id and by_credits are not needed elsewhere, there are ways of making the implementation simpler. We will return to this topic after these exercises.

    The exercise template contains the class definition for a ClimbingRoute. It works as follows:

    route1 = ClimbingRoute(\"Edge\", 38, \"6A+\")\nroute2 = ClimbingRoute(\"Smooth operator\", 11, \"7A\")\nroute3 = ClimbingRoute(\"Synchro\", 14, \"8C+\")\n\n\nprint(route1)\nprint(route2)\nprint(route3.name, route3.length, route3.grade)

    Edge, length 38 metres, grade 6A+\nSmooth operator, length 11 metres, grade 7A\nSynchro 14 8C+

    Sort by length

    Please write a function named sort_by_length(routes: list) which returns a new list of routes, sorted by length from longest to shortest.

    The function should work as follows:

    r1 = ClimbingRoute(\"Edge\", 38, \"6A+\")\nr2 = ClimbingRoute(\"Smooth operator\", 11, \"7A\")\nr3 = ClimbingRoute(\"Synchro\", 14, \"8C+\")\nr4 = ClimbingRoute(\"Small steps\", 12, \"6A+\")\n\nroutes = [r1, r2, r3, r4]\n\nfor route in sort_by_length(routes):\n    print(route)

    Edge, length 38 metres, grade 6A+\nSynchro, length 14 metres, grade 8C+\nSmall steps, length 12 metres, grade 6A+\nSmooth operator, length 11 metres, grade 7A

    Sort by difficulty

    Please write a function named sort_by_difficulty(routes: list) which returns a new list of routes, sorted by difficulty, i.e. grade, from hardest to easiest. For routes with the same grade, the longer one is more difficult. The scale of climbing route grades is 4, 4+, 5, 5+, 6A, 6A+, ..., which in practice works out as the alphabetical order for strings.

    The function should work as follows:

    r1 = ClimbingRoute(\"Edge\", 38, \"6A+\")\nr2 = ClimbingRoute(\"Smooth operator\", 11, \"7A\")\nr3 = ClimbingRoute(\"Synchro\", 14, \"8C+\")\nr4 = ClimbingRoute(\"Small steps\", 12, \"6A+\")\n\nroutes = [r1, r2, r3, r4]\nfor route in sort_by_difficulty(routes):\n    print(route)

    Synchro, length 14 metres, grade 8C+\nSmooth operator, length 11 metres, grade 7A\nEdge, length 38 metres, grade 6A+\nSmall steps, length 12 metres, grade 6A+

    Hint: if the order is based on a list or a tuple, by default Python sorts the items first based on the first item, next based on the second item, and so forth:

    my_list = [(\"a\", 4),(\"a\", 2),(\"b\", 30), (\"b\", 0) ]\nprint(sorted(my_list))

    [('a', 2), ('a', 4), ('b', 0), ('b', 30)]

    In addition to the ClimbingRoute from the previous exercise, the exercise template contains the class definition for a ClimbingArea.

    ca1 = ClimbingArea(\"Olhava\")\nca1.add_route(ClimbingRoute(\"Edge\", 38, \"6A+\"))\nca1.add_route(ClimbingRoute(\"Great cut\", 36, \"6B\"))\nca1.add_route(ClimbingRoute(\"Swedish route\", 42, \"5+\"))\n\nca2 = ClimbingArea(\"Nummi\")\nca2.add_route(ClimbingRoute(\"Synchro\", 14, \"8C+\"))\n\nca3 = ClimbingArea(\"Nalkkila slab\")\nca3.add_route(ClimbingRoute(\"Small steps\", 12, \"6A+\"))\nca3.add_route(ClimbingRoute(\"Smooth operator\", 11, \"7A\"))\nca3.add_route(ClimbingRoute(\"Piggy not likey\", 12 , \"6B+\"))\nca3.add_route(ClimbingRoute(\"Orchard\", 8, \"6A\"))\n\nprint(ca1)\nprint(ca3.name, ca3.routes())\nprint(ca3.hardest_route())

    Olhava, 3 routes, hardest 6B\nNalkkila slab 4\nSmooth operator, length 9 metres, grade 7A

    Sort by number of routes

    Please write a function named sort_by_number_of_routes which sorts climbing areas in ascending order based on the number of routes they each have.

    # ca1, ca2 and ca3 declared as above\nareas = [ca1, ca2, ca3]\nfor area in sort_by_number_of_routes(areas):\n    print(area)\n

    Nummi, 1 routes, hardest 8C+\nOlhava, 3 routes, hardest 6B\nNalkkila slab, 4 routes, hardest 7A

    Sort by the most difficult route

    Please write a function named sort_by_most_difficult which sorts climbing areas in descending order based on the most difficult route in each area.

    # ca1, ca2 and ca3 declared as above\nareas = [ca1, ca2, ca3]\nfor area in sort_by_most_difficult(areas):\n    print(area)

    Nummi, 1 routes, hardest 8C+\nNalkkila slab, 4 routes, hardest 7A\nOlhava, 3 routes, hardest 6B

    Lambda expressions

    We have mostly worked with functions from the viewpoint of modularity. It is true that functions play an important role in managing the complexity of your programs and avoiding code repetition. Functions are usually written so that they can be used many times.

    But sometimes you need something resembling a function that you will use just once. Lambda expressions allow you to create small, anonymous functions which are created (and discarded) as they are needed in the code. The general syntax is as follows:

    lambda <parameters> : <expression>

    Sorting a list of tuples by the second item in each tuple would look like this implemented with a lambda expression:

    products = [(\"banana\", 5.95), (\"apple\", 3.95), (\"orange\", 4.50), (\"watermelon\", 4.95)]\n\n# Function is created \"on the fly\" with a lambda expression:\nproducts.sort(key=lambda item: item[1])\n\nfor product in products:\n    print(product)

    ('apple', 3.95)\n('orange', 4.5)\n('watermelon', 4.95)\n('banana', 5.95)

    The expression

    lambda item: item[1]

    is equivalent to the function definition

    def price(item):\n    return item[1]

    except for the fact that a lambda function doesn't have a name. This is why lambda functions are called anonymous functions.

    In every other respect a lambda function is no different from any other function, and they can be used in all the same contexts as any equivalent named function. For example, the following program sorts a list of strings alphabetically by the last character in each string:

    strings = [\"Mickey\", \"Mack\", \"Marvin\", \"Minnie\", \"Merl\"]\n\nfor word in sorted(strings, key=lambda word: word[-1]):\n    print(word)

    Minnie\nMack\nMerl\nMarvin\nMickey

    We can also combine list comprehensions, the join method and lambda expressions. For example, we could sort strings based on just the vowels in them, ignoring all other characters:

    strings = [\"Mickey\", \"Mack\", \"Marvin\", \"Minnie\", \"Merl\"]\n\nfor word in sorted(strings, key=lambda word: \"\".join([c for c in word if c in \"aeiou\"])):\n    print(word)

    Mack\nMarvin\nMerl\nMickey\nMinnie

    Anonymous functions can also be used with other built in Python functions, not just those used for sorting. For example, the min and max functions also take a keyword argument named key. It is used as the criteria for comparing the items when selecting the minimum or maximum value.

    In the following example we are dealing with audio recordings. First we select the oldest recording, and then the longest:

    \nclass Recording:\n    \"\"\" The class models a single audio recording \"\"\"\n    def __init__(self, name: str, performer: str, year: int, runtime: int):\n        self.name = name\n        self.performer = performer\n        self.year = year\n        self.runtime = runtime\n\n\n    def __str__(self):\n        return f\"{self.name} ({self.performer}), {self.year}. {self.runtime} min.\"\n\nif __name__ == \"__main__\":\n    r1 = Recording(\"Nevermind\", \"Nirvana\", 1991, 43)\n    r2 = Recording(\"Let It Be\", \"Beatles\", 1969, 35)\n    r3 = Recording(\"Joshua Tree\", \"U2\", 1986, 50)\n\n    recordings = [r1, r2, r3]\n\n\n    print(\"The oldest recording:\")\n    print(min(recordings, key=lambda rec: rec.year))\n\n    print(\"The longest recording:\")\n    print(max(recordings, key=lambda rec: rec.runtime))

    The oldest recording:\nLet It Be (Beatles), 1969. 35 min.\nThe longest recording:\nU2 (Joshua Tree), 1986. 50 min.

    The exercise template contains the definition for a class named BallPlayer. It has the following public attributes:

      \n
    • name
    • \n
    • shirt number number
    • \n
    • scored goals goals
    • \n
    • assists completed assists
    • \n
    • minutes played minutes
    • \n

    Please implement the following functions. NB: each function has a different type of return value.

    Most goals

    Please write a function named most_goals which takes a list of ball players as its argument.

    The function should return the name of the player who scored the most goals, in string format.

    Most points

    Please write a function named most_points, which takes a list of ball players as its argument.

    The function should return a tuple containing the name and shirt number of the player who has scored the most points. The total number of points is the number of goals and the number of assists combined.

    Least minutes

    Please write a function named least_minutes, which takes a list of ball players as its argument.

    The function should return the BallPlayer object which has the smallest value of minutes played.

    You can test your functions with the following program:

    if __name__ == \"__main__\":\n    player1 = BallPlayer(\"Archie Bonkers\", 13, 5, 12, 46)\n    player2 = BallPlayer(\"Speedy Tickets\", 7, 2, 26, 55)\n    player3 = BallPlayer(\"Cruella De Hill\", 9, 1, 32, 26)\n    player4 = BallPlayer(\"Devilled Tasmanian\", 12, 1, 11, 41)\n    player5 = BallPlayer(\"Donald Quack\", 4, 3, 9, 12)\n    \n    team = [player1, player2, player3, player4, player5]\n    print(most_goals(team))\n    print(most_points(team))\n    print(least_minutes(team))

    This should print out:

    Archie Bonkers\n('Cruella De Hill', 9)\nBallPlayer(name=Donald Quack, number=4, goals=3, assists=9, minutes=12)

    Functions as arguments within your own functions

    We established above that it is possible to pass a reference to a function as an argument to another function. To wrap this section up, let's write our very own function which takes a function as its argument.

    # the type hint \"callable\" refers to a function\ndef perform_operation(operation: callable):\n    # Call the function which was passed as an argument\n    return operation(10, 5)\n\ndef my_sum(a: int, b: int):\n    return a + b\n\ndef my_product(a: int, b: int):\n    return a * b\n\n\nif __name__ == \"__main__\":\n    print(perform_operation(my_sum))\n    print(perform_operation(my_product))\n    print(perform_operation(lambda x,y: x - y))\n

    15\n50\n5

    The value returned by the function perform_operation depends on which function was passed as an argument. Any function which accepts two arguments would do, no matter whether it is anonymous or named.

    Passing references to functions as arguments to other functions might not be something you will end up doing on a daily basis in your programming career, but it can be a useful technique. This following program selects some lines from one file and writes them to another file. The way the lines are selected is determined by a function which returns True only if the lines should be copied:

    def copy_lines(source_file: str, target_file: str, criterion= lambda x: True):\n    with open(source_file) as source, open(target_file, \"w\") as target:\n        for line in source:\n            # Remove any whitespace from beginning and end of line\n            line = line.strip()\n\n            if criterion(line):\n                target.write(line + \"\\n\")\n\n# Some examples\nif __name__ == \"__main__\":\n    # If the third parameter is not given, copy all lines\n    copy_lines(\"first.txt\", \"second.txt\")\n\n    # Copy all non-empty lines\n    copy_lines(\"first.txt\", \"second.txt\", lambda line: len(line) > 0)\n\n    # Copy all lines which contain the word \"Python\"\n    copy_lines(\"first.txt\", \"second.txt\", lambda line: \"Python\" in line)\n\n    # Copy all lines which do not end in a full stop\n    copy_lines(\"first.txt\", \"second.txt\", lambda line: line[-1] != \".\")

    The function definition contains a default value for the keyword parameter criterion: lambda x: True. This anonymous function always returns True regardless of the input. So, the default behaviour is to copy all lines. As usual, if a value is given for a parameter with a default value, the new value replaces the default value.

    This exercise deals with products which are stored in tuples. The examples all assume a variable named products, which is assigned the following value:

    products = [(\"banana\", 5.95, 12), (\"apple\", 3.95, 3), (\"orange\", 4.50, 2), (\"watermelon\", 4.95, 22), (\"kale\", 0.99, 1)]

    Each tuple contains three items: name, price and amount.

    Please write a function named search(products: list, criterion: callable). The second argument to the function is a function itself, and it should be able to process a tuple as defined above, and return a Boolean value. The search function should return a new list containing those tuples from the original which fulfil the criterion.

    If we wanted to include only products whose price was under 4 euros, we could use the following criterion function:

    def price_under_4_euros(product):\n    return product[1] < 4

    The function returns True if the second item in the tuple is less than four in value.

    An example of the search function in use:

    for product in search(products, price_under_4_euros):\n    print(product)

    ('apple', 3.95, 3)\n('kale', 0.99, 1)

    The criterion function can also be a lambda function. If we wanted to search for only those products whose amount was at least 11, we could write the following:

    for product in search(products, lambda t: t[2]>10):\n    print(product)

    ('banana', 5.95, 12)\n('watermelon', 4.95, 22)

    ","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-12/1-functions-as-arguments.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-12/2-generators/page-data.json b/page-data/part-12/2-generators/page-data.json index 744a7153b..0a641b0ad 100644 --- a/page-data/part-12/2-generators/page-data.json +++ b/page-data/part-12/2-generators/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-12/2-generators", - "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 know what a Python generator is"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be familiar with the keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yield"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to write your own generator functions"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We have already come across situations where we're dealing with a series of items, and we'd need the next item(s) in the series, but we wouldn't necessarily want to formulate the entire series up to that point each time a new item is required. Some recursive series, such as the Fibonacci number, are a good example of such a situation. If each function call recursively generates the entire series up to the desired point, we end up generating the beginning of the series many times over."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"generators"}]},{"type":"text","value":" are a way of producing just the next item in a series when it is needed, essentially running the generation process for the series only once (for a given execution of a program). They work mostly like normal functions, as they can be called and will return values, but the value a generator function returns differs from a normal function. A normal function should return the same value every time, given the same arguments. A generator function, on the other hand, should remember its current state and return the next item in the series, which may be different from the previous item."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Just as there are many ways of solving most any programming problem, there are many ways of achieving a functionality similar to generators, but generators can help make the program easier to understand, and can in certain situations save memory or other computational resources."}]},{"type":"element","tagName":"h2","properties":{"id":"the-keyword-yield","style":"position:relative;"},"children":[{"type":"text","value":"The keyword yield"},{"type":"element","tagName":"a","properties":{"href":"#the-keyword-yield","ariaLabel":"the keyword yield 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":"A generator function must contain the keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yield"}]},{"type":"text","value":", which marks out the value which the function returns. Let's take a look at a function which generates integer numbers, starting from zero and ending at a pre-determined maximum value:"}]},{"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":"\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":"counter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"max_value"},{"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 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":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" max_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":"yield"}]},{"type":"text","value":" number\n 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":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"counter"}]},{"type":"text","value":" function can be passed as an argument to the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"next()"}]},{"type":"text","value":":"}]},{"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":"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 numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" counter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"\"First 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","builtin"]},"children":[{"type":"text","value":"next"}]},{"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":"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":"\"Second 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","builtin"]},"children":[{"type":"text","value":"next"}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First value:\n0\nSecond value:\n1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see from the example above, the keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yield"}]},{"type":"text","value":" is similar to the keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":": both are used to define a return value. The difference is that "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yield"}]},{"type":"text","value":" doesn't \"close\" the function in the same sense as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":". A generator function with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yield"}]},{"type":"text","value":" keyword keeps track of its state, and the next time it is called, it will continue from the same state."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This generator also requires a maximum value, which was "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":" in the example above. When the generator runs out of values, it will raise a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"StopIteration"}]},{"type":"text","value":" exception:"}]},{"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":"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":"# creates a generator with maximum value 1"}]},{"type":"text","value":"\n numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" counter"},{"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":"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","builtin"]},"children":[{"type":"text","value":"next"}]},{"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":"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","builtin"]},"children":[{"type":"text","value":"next"}]},{"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":"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","builtin"]},"children":[{"type":"text","value":"next"}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"0\n1\nTraceback (most recent call last):\nFile \"generator_example.py\", line 11, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nprint(next(numbers))\nStopIteration"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exception can be caught with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":" - "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" block:"}]},{"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":"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 numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" counter"},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"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","builtin"]},"children":[{"type":"text","value":"next"}]},{"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":"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","builtin"]},"children":[{"type":"text","value":"next"}]},{"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":"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","builtin"]},"children":[{"type":"text","value":"next"}]},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" StopIteration"},{"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":"\"ran out of numbers\""}]},{"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\n1\nran out of numbers"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Traversing through all the items in a generator is easily done with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop:"}]},{"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":"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 numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" counter"},{"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 "},{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0\n1\n2\n3\n4\n5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Generators do not have to have a defined maximum value or termination point. They can generate values infinitely (within other computational and physical constraints, naturally)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pay mind, though: traversing a generator with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop only works if the generator terminates at some point. If the generator is built on an infinite loop, trying to traverse it with a simple "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop will cause an endless execution, just like a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop with no end or break condition would."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Even numbers","tmcname":"part12-08_even_numbers"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a generator function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"even_numbers(beginning: int, maximum: int)"}]},{"type":"text","value":" which takes two integers as its arguments. The function should produce even numbers starting from "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"beginning"}]},{"type":"text","value":" and ending with, at most, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"maximum"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Two examples of how the function works:"}]},{"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":"numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" even_numbers"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n4\n6\n8\n10"}]}]},{"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":"numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" even_numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"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":"21"}]},{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"12\n14\n16\n18\n20"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Prime numbers","tmcname":"part12-09_prime_numbers"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A prime number is a number which is divisible only by itself and the number 1. By convention prime numbers are defined as positive integers from the number 2 upwards. The first six prime numbers are 2, 3, 5, 7, 11 and 13."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a generator function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"prime_numbers()"}]},{"type":"text","value":" which creates a new generator. The generator should return new prime numbers, one by one in sequence, from 2 onwards. NB: this generator never terminates. It will generate numbers for as long as they are needed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"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":"numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" prime_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":"text","value":"\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":"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":"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","builtin"]},"children":[{"type":"text","value":"next"}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n3\n5\n7\n11\n13\n17\n19"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint:"}]},{"type":"text","value":" you can use a loop to check if a number is a prime number. If we are checking the number "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":", the loop would go through the numbers "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x-1"}]},{"type":"text","value":". If "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" is divisible by any one of these, it is not a prime number."}]}]},{"type":"element","tagName":"h2","properties":{"id":"generator-comprehensions","style":"position:relative;"},"children":[{"type":"text","value":"Generator comprehensions"},{"type":"element","tagName":"a","properties":{"href":"#generator-comprehensions","ariaLabel":"generator comprehensions 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":"You do not necessarily need a function definition to create a generator. We can use a structure similar to a list comprehension instead. This time we use "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"round"}]},{"type":"text","value":" brackets to signify a generator instead of a list or a dictionary:"}]},{"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":"# This generator returns squares of integers"}]},{"type":"text","value":"\nsquares "},{"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":"text","value":"x "},{"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":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" x "},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"64"}]},{"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":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"squares"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the printout of a generator object isn't too informative"}]},{"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":"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","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","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"squares"},{"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":" at 0x000002B4224EBFC0>\n1\n4\n9\n16\n25"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example we print out substrings of the English alphabet, each three characters long. This prints out the first 10 items in the generator:"}]},{"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":"substrings "},{"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":"\"abcdefghijklmnopqrstuvwxyz\""}]},{"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":"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"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","comment"]},"children":[{"type":"text","value":"# print out first 10 substrings"}]},{"type":"text","value":"\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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"substrings"},{"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":"abc\nbcd\ncde\ndef\nefg\nfgh\nghi\nhij\nijk\njkl"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Random words","tmcname":"part12-10_random_words"},"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":"word_generator(characters: str, length: int, amount: int)"}]},{"type":"text","value":" which returns a new generator for generating random words based on the parameters given."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A random word is generated by selecting from the string named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"characters"}]},{"type":"text","value":" as many characters as is indicated by the argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"length"}]},{"type":"text","value":". The same character can appear many times in a random word."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The generator returns as many words as specified by the argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"amount"}]},{"type":"text","value":" before terminating."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example run of the word generator:"}]},{"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":"wordgen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" word_generator"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"abcdefg\""}]},{"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":"3"}]},{"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":"5"}]},{"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":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" wordgen"},{"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":"word"},{"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":"dbf\nbaf\nead\nfga\nccc"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: it is up to you how you implement this function. You may use a \"traditional\" generator or a generator comprehension equally well."}]}]}]},"html":"

    After this section

      \n
    • You will know what a Python generator is
    • \n
    • You will be familiar with the keyword yield
    • \n
    • You will be able to write your own generator functions
    • \n

    We have already come across situations where we're dealing with a series of items, and we'd need the next item(s) in the series, but we wouldn't necessarily want to formulate the entire series up to that point each time a new item is required. Some recursive series, such as the Fibonacci number, are a good example of such a situation. If each function call recursively generates the entire series up to the desired point, we end up generating the beginning of the series many times over.

    Python generators are a way of producing just the next item in a series when it is needed, essentially running the generation process for the series only once (for a given execution of a program). They work mostly like normal functions, as they can be called and will return values, but the value a generator function returns differs from a normal function. A normal function should return the same value every time, given the same arguments. A generator function, on the other hand, should remember its current state and return the next item in the series, which may be different from the previous item.

    Just as there are many ways of solving most any programming problem, there are many ways of achieving a functionality similar to generators, but generators can help make the program easier to understand, and can in certain situations save memory or other computational resources.

    The keyword yield

    A generator function must contain the keyword yield, which marks out the value which the function returns. Let's take a look at a function which generates integer numbers, starting from zero and ending at a pre-determined maximum value:

    \ndef counter(max_value: int):\n    number = 0\n    while number <= max_value:\n        yield number\n        number += 1\n

    Now the counter function can be passed as an argument to the function next():

    if __name__ == \"__main__\":\n    numbers = counter(10)\n    print(\"First value:\")\n    print(next(numbers))\n    print(\"Second value:\")\n    print(next(numbers))

    First value:\n0\nSecond value:\n1

    As you can see from the example above, the keyword yield is similar to the keyword return: both are used to define a return value. The difference is that yield doesn't \"close\" the function in the same sense as return. A generator function with the yield keyword keeps track of its state, and the next time it is called, it will continue from the same state.

    This generator also requires a maximum value, which was 10 in the example above. When the generator runs out of values, it will raise a StopIteration exception:

    if __name__ == \"__main__\":\n    # creates a generator with maximum value 1\n    numbers = counter(1)\n    print(next(numbers))\n    print(next(numbers))\n    print(next(numbers))
    0\n1\nTraceback (most recent call last):\nFile \"generator_example.py\", line 11, in \nprint(next(numbers))\nStopIteration

    The exception can be caught with a try - except block:

    if __name__ == \"__main__\":\n    numbers = counter(1)\n    try:\n        print(next(numbers))\n        print(next(numbers))\n        print(next(numbers))\n    except StopIteration:\n        print(\"ran out of numbers\")

    0\n1\nran out of numbers

    Traversing through all the items in a generator is easily done with a for loop:

    if __name__ == \"__main__\":\n    numbers = counter(5)\n    for number in numbers:\n        print(number)

    0\n1\n2\n3\n4\n5

    Generators do not have to have a defined maximum value or termination point. They can generate values infinitely (within other computational and physical constraints, naturally).

    Pay mind, though: traversing a generator with a for loop only works if the generator terminates at some point. If the generator is built on an infinite loop, trying to traverse it with a simple for loop will cause an endless execution, just like a while loop with no end or break condition would.

    Please write a generator function named even_numbers(beginning: int, maximum: int) which takes two integers as its arguments. The function should produce even numbers starting from beginning and ending with, at most, maximum.

    Two examples of how the function works:

    numbers = even_numbers(2, 10)\nfor number in numbers:\n    print(number)

    2\n4\n6\n8\n10

    numbers = even_numbers(11, 21)\nfor number in numbers:\n    print(number)

    12\n14\n16\n18\n20

    A prime number is a number which is divisible only by itself and the number 1. By convention prime numbers are defined as positive integers from the number 2 upwards. The first six prime numbers are 2, 3, 5, 7, 11 and 13.

    Please write a generator function prime_numbers() which creates a new generator. The generator should return new prime numbers, one by one in sequence, from 2 onwards. NB: this generator never terminates. It will generate numbers for as long as they are needed.

    For example:

    numbers = prime_numbers()\nfor i in range(8):\n    print(next(numbers))

    2\n3\n5\n7\n11\n13\n17\n19

    Hint: you can use a loop to check if a number is a prime number. If we are checking the number x, the loop would go through the numbers 2 to x-1. If x is divisible by any one of these, it is not a prime number.

    Generator comprehensions

    You do not necessarily need a function definition to create a generator. We can use a structure similar to a list comprehension instead. This time we use round brackets to signify a generator instead of a list or a dictionary:

    # This generator returns squares of integers\nsquares = (x ** 2 for x in range(1, 64))\n\nprint(squares) # the printout of a generator object isn't too informative\n\nfor i in range(5):\n    print(next(squares))

    <generator object <genexpr> at 0x000002B4224EBFC0>\n1\n4\n9\n16\n25

    In the following example we print out substrings of the English alphabet, each three characters long. This prints out the first 10 items in the generator:

    substrings = (\"abcdefghijklmnopqrstuvwxyz\"[i : i + 3] for i in range(24))\n\n# print out first 10 substrings\nfor i in range(10):\n    print(next(substrings))

    abc\nbcd\ncde\ndef\nefg\nfgh\nghi\nhij\nijk\njkl

    Please write a function named word_generator(characters: str, length: int, amount: int) which returns a new generator for generating random words based on the parameters given.

    A random word is generated by selecting from the string named characters as many characters as is indicated by the argument length. The same character can appear many times in a random word.

    The generator returns as many words as specified by the argument amount before terminating.

    An example run of the word generator:

    wordgen = word_generator(\"abcdefg\", 3, 5)\nfor word in wordgen:\n    print(word)

    dbf\nbaf\nead\nfga\nccc

    NB: it is up to you how you implement this function. You may use a \"traditional\" generator or a generator comprehension equally well.

    ","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-12/2-generators.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know what a Python generator is"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be familiar with the keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yield"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to write your own generator functions"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We have already come across situations where we're dealing with a series of items, and we'd need the next item(s) in the series, but we wouldn't necessarily want to formulate the entire series up to that point each time a new item is required. Some recursive series, such as the Fibonacci number, are a good example of such a situation. If each function call recursively generates the entire series up to the desired point, we end up generating the beginning of the series many times over."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"generators"}]},{"type":"text","value":" are a way of producing just the next item in a series when it is needed, essentially running the generation process for the series only once (for a given execution of a program). They work mostly like normal functions, as they can be called and will return values, but the value a generator function returns differs from a normal function. A normal function should return the same value every time, given the same arguments. A generator function, on the other hand, should remember its current state and return the next item in the series, which may be different from the previous item."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Just as there are many ways of solving most any programming problem, there are many ways of achieving a functionality similar to generators, but generators can help make the program easier to understand, and can in certain situations save memory or other computational resources."}]},{"type":"element","tagName":"h2","properties":{"id":"the-keyword-yield","style":"position:relative;"},"children":[{"type":"text","value":"The keyword yield"},{"type":"element","tagName":"a","properties":{"href":"#the-keyword-yield","ariaLabel":"the keyword yield 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":"A generator function must contain the keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yield"}]},{"type":"text","value":", which marks out the value which the function returns. Let's take a look at a function which generates integer numbers, starting from zero and ending at a pre-determined maximum value:"}]},{"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":"\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":"counter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"max_value"},{"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 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":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" max_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":"yield"}]},{"type":"text","value":" number\n 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":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"counter"}]},{"type":"text","value":" function can be passed as an argument to the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"next()"}]},{"type":"text","value":":"}]},{"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":"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 numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" counter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"\"First 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","builtin"]},"children":[{"type":"text","value":"next"}]},{"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":"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":"\"Second 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","builtin"]},"children":[{"type":"text","value":"next"}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First value:\n0\nSecond value:\n1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see from the example above, the keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yield"}]},{"type":"text","value":" is similar to the keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":": both are used to define a return value. The difference is that "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yield"}]},{"type":"text","value":" doesn't \"close\" the function in the same sense as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":". A generator function with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yield"}]},{"type":"text","value":" keyword keeps track of its state, and the next time it is called, it will continue from the same state."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This generator also requires a maximum value, which was "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":" in the example above. When the generator runs out of values, it will raise a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"StopIteration"}]},{"type":"text","value":" exception:"}]},{"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":"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":"# creates a generator with maximum value 1"}]},{"type":"text","value":"\n numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" counter"},{"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":"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","builtin"]},"children":[{"type":"text","value":"next"}]},{"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":"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","builtin"]},"children":[{"type":"text","value":"next"}]},{"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":"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","builtin"]},"children":[{"type":"text","value":"next"}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"0\n1\nTraceback (most recent call last):\nFile \"generator_example.py\", line 11, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nprint(next(numbers))\nStopIteration"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exception can be caught with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":" - "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" block:"}]},{"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":"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 numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" counter"},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"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","builtin"]},"children":[{"type":"text","value":"next"}]},{"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":"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","builtin"]},"children":[{"type":"text","value":"next"}]},{"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":"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","builtin"]},"children":[{"type":"text","value":"next"}]},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" StopIteration"},{"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":"\"ran out of numbers\""}]},{"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\n1\nran out of numbers"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Traversing through all the items in a generator is easily done with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop:"}]},{"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":"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 numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" counter"},{"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 "},{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0\n1\n2\n3\n4\n5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Generators do not have to have a defined maximum value or termination point. They can generate values infinitely (within other computational and physical constraints, naturally)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pay mind, though: traversing a generator with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop only works if the generator terminates at some point. If the generator is built on an infinite loop, trying to traverse it with a simple "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop will cause an endless execution, just like a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop with no end or break condition would."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Even numbers","tmcname":"part12-08_even_numbers"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a generator function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"even_numbers(beginning: int, maximum: int)"}]},{"type":"text","value":" which takes two integers as its arguments. The function should produce even numbers starting from "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"beginning"}]},{"type":"text","value":" and ending with, at most, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"maximum"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Two examples of how the function works:"}]},{"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":"numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" even_numbers"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n4\n6\n8\n10"}]}]},{"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":"numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" even_numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"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":"21"}]},{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"12\n14\n16\n18\n20"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Prime numbers","tmcname":"part12-09_prime_numbers"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A prime number is a number which is divisible only by itself and the number 1. By convention prime numbers are defined as positive integers from the number 2 upwards. The first six prime numbers are 2, 3, 5, 7, 11 and 13."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a generator function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"prime_numbers()"}]},{"type":"text","value":" which creates a new generator. The generator should return new prime numbers, one by one in sequence, from 2 onwards. NB: this generator never terminates. It will generate numbers for as long as they are needed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"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":"numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" prime_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":"text","value":"\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":"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":"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","builtin"]},"children":[{"type":"text","value":"next"}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n3\n5\n7\n11\n13\n17\n19"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint:"}]},{"type":"text","value":" you can use a loop to check if a number is a prime number. If we are checking the number "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":", the loop would go through the numbers "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x-1"}]},{"type":"text","value":". If "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" is divisible by any one of these, it is not a prime number."}]}]},{"type":"element","tagName":"h2","properties":{"id":"generator-comprehensions","style":"position:relative;"},"children":[{"type":"text","value":"Generator comprehensions"},{"type":"element","tagName":"a","properties":{"href":"#generator-comprehensions","ariaLabel":"generator comprehensions 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":"You do not necessarily need a function definition to create a generator. We can use a structure similar to a list comprehension instead. This time we use "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"round"}]},{"type":"text","value":" brackets to signify a generator instead of a list or a dictionary:"}]},{"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":"# This generator returns squares of integers"}]},{"type":"text","value":"\nsquares "},{"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":"text","value":"x "},{"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":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" x "},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"64"}]},{"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":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"squares"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the printout of a generator object isn't too informative"}]},{"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":"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","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","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"squares"},{"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":" at 0x000002B4224EBFC0>\n1\n4\n9\n16\n25"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example we print out substrings of the English alphabet, each three characters long. This prints out the first 10 items in the generator:"}]},{"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":"substrings "},{"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":"\"abcdefghijklmnopqrstuvwxyz\""}]},{"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":"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"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","comment"]},"children":[{"type":"text","value":"# print out first 10 substrings"}]},{"type":"text","value":"\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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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","builtin"]},"children":[{"type":"text","value":"next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"substrings"},{"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":"abc\nbcd\ncde\ndef\nefg\nfgh\nghi\nhij\nijk\njkl"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Random words","tmcname":"part12-10_random_words"},"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":"word_generator(characters: str, length: int, amount: int)"}]},{"type":"text","value":" which returns a new generator for generating random words based on the parameters given."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A random word is generated by selecting from the string named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"characters"}]},{"type":"text","value":" as many characters as is indicated by the argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"length"}]},{"type":"text","value":". The same character can appear many times in a random word."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The generator returns as many words as specified by the argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"amount"}]},{"type":"text","value":" before terminating."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example run of the word generator:"}]},{"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":"wordgen "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" word_generator"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"abcdefg\""}]},{"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":"3"}]},{"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":"5"}]},{"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":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" wordgen"},{"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":"word"},{"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":"dbf\nbaf\nead\nfga\nccc"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: it is up to you how you implement this function. You may use a \"traditional\" generator or a generator comprehension equally well."}]}]}]},"html":"

    After this section

      \n
    • You will know what a Python generator is
    • \n
    • You will be familiar with the keyword yield
    • \n
    • You will be able to write your own generator functions
    • \n

    We have already come across situations where we're dealing with a series of items, and we'd need the next item(s) in the series, but we wouldn't necessarily want to formulate the entire series up to that point each time a new item is required. Some recursive series, such as the Fibonacci number, are a good example of such a situation. If each function call recursively generates the entire series up to the desired point, we end up generating the beginning of the series many times over.

    Python generators are a way of producing just the next item in a series when it is needed, essentially running the generation process for the series only once (for a given execution of a program). They work mostly like normal functions, as they can be called and will return values, but the value a generator function returns differs from a normal function. A normal function should return the same value every time, given the same arguments. A generator function, on the other hand, should remember its current state and return the next item in the series, which may be different from the previous item.

    Just as there are many ways of solving most any programming problem, there are many ways of achieving a functionality similar to generators, but generators can help make the program easier to understand, and can in certain situations save memory or other computational resources.

    The keyword yield

    A generator function must contain the keyword yield, which marks out the value which the function returns. Let's take a look at a function which generates integer numbers, starting from zero and ending at a pre-determined maximum value:

    \ndef counter(max_value: int):\n    number = 0\n    while number <= max_value:\n        yield number\n        number += 1\n

    Now the counter function can be passed as an argument to the function next():

    if __name__ == \"__main__\":\n    numbers = counter(10)\n    print(\"First value:\")\n    print(next(numbers))\n    print(\"Second value:\")\n    print(next(numbers))

    First value:\n0\nSecond value:\n1

    As you can see from the example above, the keyword yield is similar to the keyword return: both are used to define a return value. The difference is that yield doesn't \"close\" the function in the same sense as return. A generator function with the yield keyword keeps track of its state, and the next time it is called, it will continue from the same state.

    This generator also requires a maximum value, which was 10 in the example above. When the generator runs out of values, it will raise a StopIteration exception:

    if __name__ == \"__main__\":\n    # creates a generator with maximum value 1\n    numbers = counter(1)\n    print(next(numbers))\n    print(next(numbers))\n    print(next(numbers))
    0\n1\nTraceback (most recent call last):\nFile \"generator_example.py\", line 11, in \nprint(next(numbers))\nStopIteration

    The exception can be caught with a try - except block:

    if __name__ == \"__main__\":\n    numbers = counter(1)\n    try:\n        print(next(numbers))\n        print(next(numbers))\n        print(next(numbers))\n    except StopIteration:\n        print(\"ran out of numbers\")

    0\n1\nran out of numbers

    Traversing through all the items in a generator is easily done with a for loop:

    if __name__ == \"__main__\":\n    numbers = counter(5)\n    for number in numbers:\n        print(number)

    0\n1\n2\n3\n4\n5

    Generators do not have to have a defined maximum value or termination point. They can generate values infinitely (within other computational and physical constraints, naturally).

    Pay mind, though: traversing a generator with a for loop only works if the generator terminates at some point. If the generator is built on an infinite loop, trying to traverse it with a simple for loop will cause an endless execution, just like a while loop with no end or break condition would.

    Please write a generator function named even_numbers(beginning: int, maximum: int) which takes two integers as its arguments. The function should produce even numbers starting from beginning and ending with, at most, maximum.

    Two examples of how the function works:

    numbers = even_numbers(2, 10)\nfor number in numbers:\n    print(number)

    2\n4\n6\n8\n10

    numbers = even_numbers(11, 21)\nfor number in numbers:\n    print(number)

    12\n14\n16\n18\n20

    A prime number is a number which is divisible only by itself and the number 1. By convention prime numbers are defined as positive integers from the number 2 upwards. The first six prime numbers are 2, 3, 5, 7, 11 and 13.

    Please write a generator function prime_numbers() which creates a new generator. The generator should return new prime numbers, one by one in sequence, from 2 onwards. NB: this generator never terminates. It will generate numbers for as long as they are needed.

    For example:

    numbers = prime_numbers()\nfor i in range(8):\n    print(next(numbers))

    2\n3\n5\n7\n11\n13\n17\n19

    Hint: you can use a loop to check if a number is a prime number. If we are checking the number x, the loop would go through the numbers 2 to x-1. If x is divisible by any one of these, it is not a prime number.

    Generator comprehensions

    You do not necessarily need a function definition to create a generator. We can use a structure similar to a list comprehension instead. This time we use round brackets to signify a generator instead of a list or a dictionary:

    # This generator returns squares of integers\nsquares = (x ** 2 for x in range(1, 64))\n\nprint(squares) # the printout of a generator object isn't too informative\n\nfor i in range(5):\n    print(next(squares))

    <generator object <genexpr> at 0x000002B4224EBFC0>\n1\n4\n9\n16\n25

    In the following example we print out substrings of the English alphabet, each three characters long. This prints out the first 10 items in the generator:

    substrings = (\"abcdefghijklmnopqrstuvwxyz\"[i : i + 3] for i in range(24))\n\n# print out first 10 substrings\nfor i in range(10):\n    print(next(substrings))

    abc\nbcd\ncde\ndef\nefg\nfgh\nghi\nhij\nijk\njkl

    Please write a function named word_generator(characters: str, length: int, amount: int) which returns a new generator for generating random words based on the parameters given.

    A random word is generated by selecting from the string named characters as many characters as is indicated by the argument length. The same character can appear many times in a random word.

    The generator returns as many words as specified by the argument amount before terminating.

    An example run of the word generator:

    wordgen = word_generator(\"abcdefg\", 3, 5)\nfor word in wordgen:\n    print(word)

    dbf\nbaf\nead\nfga\nccc

    NB: it is up to you how you implement this function. You may use a \"traditional\" generator or a generator comprehension equally well.

    ","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-12/2-generators.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-12/3-functional-programming/page-data.json b/page-data/part-12/3-functional-programming/page-data.json index 3887e3acf..cdfe1d141 100644 --- a/page-data/part-12/3-functional-programming/page-data.json +++ b/page-data/part-12/3-functional-programming/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-12/3-functional-programming", - "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 know what functional programming means"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to make use of the functions "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" in your own programs"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Functional programming refers to a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"programming paradigm"}]},{"type":"text","value":" which avoids changes in program state as much as possible. Variables are generally avoided. Instead, chains of function calls form the backbone of the program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lambda expressions and different types of comprehensions are common techniques in the functional programming style, as they let you process data without storing it in variables, so that the state of the program does not change. For example, a lambda expression is for all intents and purposes a function, but we do not need to store a named reference to it anywhere."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As mentioned above, functional programming is a programming paradigm, or a style of programming. There are many different programming paradigms, and we've already come across some of them:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"imperative programming, where the program consists of a sequence of commands which is executed in order"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"procedural programming, where the program is grouped into procedures or sub-programs"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"object-oriented programming, where the program and its state is stored in objects defined in classes."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are differing opinions on the divisions between the different paradigms; for example, some maintain that imperative and procedural programming mean the same thing, while others place imperative programming as an umbrella term which covers both procedural and object-oriented programming. Th terminology and divisions are not that important, and neither is strictly sticking to one or the other paradigm, but it is important to understand that such different approaches exist, as they affect the choices programmers make."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Many programming languages are designed with one or the other programming paradigm in mind, but Python is a rather versatile programming language, and allows for following several different programming paradigms, even within a single program. This lets us choose the most efficient and clear method for solving each problem."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at some functional programming tools provided by Python."}]},{"type":"element","tagName":"h2","properties":{"id":"map","style":"position:relative;"},"children":[{"type":"text","value":"map"},{"type":"element","tagName":"a","properties":{"href":"#map","ariaLabel":"map 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 "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function executes some operation on each item in an iterable series. This sounds a lot like the effect a comprehension has, but the syntax is different."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's assume we have list of strings which we want to convert into a list of integers:"}]},{"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":"str_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":"\"123\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"-10\""}]},{"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":"\"23\""}]},{"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":"\"98\""}]},{"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":"\"0\""}]},{"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":"\"-110\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nintegers "},{"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":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" x "},{"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":"text","value":"x"},{"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":" str_list"},{"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":"integers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# this tells us the type of object we're dealing with"}]},{"type":"text","value":"\n\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":" integers"},{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"\n123\n-10\n23\n98\n0\n-110"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The general syntax for the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function is"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map(, )"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"where "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" is the operation we want to execute on each item in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"series"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function returns an object of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":", which is iterable, and can be converted into 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":"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":"capitalize"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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 "},{"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":"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 first "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" first"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"upper"},{"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":" first "},{"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":"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":"text","value":"\n\ntest_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":"\"first\""}]},{"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":"\"second\""}]},{"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":"\"third\""}]},{"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":"\"fourth\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\ncapitalized "},{"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":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"capitalize"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" test_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ncapitalized_list "},{"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":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"capitalized"},{"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":"capitalized_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":"['First', 'Second', 'Third', 'Fourth']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see from the examples above, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function accepts both an anonymous lambda function and a named function defined with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We could achieve the same result with a list comprehension:"}]},{"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":"capitalize"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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 "},{"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":"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 first "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" first"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"upper"},{"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":" first "},{"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":"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":"text","value":"\n\ntest_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":"\"first\""}]},{"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":"\"second\""}]},{"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":"\"third\""}]},{"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":"\"fourth\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\ncapitalized_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":"text","value":"capitalize"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" test_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":"capitalized_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"...or we could go through the original list with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop and save the processed items in a new list with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"append"}]},{"type":"text","value":" method. Typically, in programming there are many different solutions to each problem. There are rarely any absolutely right or wrong answers. Knowing many different approaches helps you choose the most appropriate one for each situation, or one that best suits your own tastes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is worth pointing out that the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function does not return a list, but an "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"iterator"}]},{"type":"text","value":" object of type map. An iterator behaves in many ways like a list, but there are exceptions, as can be seen in the following example:"}]},{"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":"capitalize"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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 "},{"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":"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 first "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" first"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"upper"},{"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":" first "},{"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":"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":"text","value":"\n\ntest_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":"\"first\""}]},{"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":"\"second\""}]},{"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":"\"third\""}]},{"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":"\"fourth\""}]},{"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":"# store the return value from the map function"}]},{"type":"text","value":"\ncapitalized "},{"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":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"capitalize"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" test_list"},{"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":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" capitalized"},{"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":"word"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"print the same again:\""}]},{"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":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" capitalized"},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This would print out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First\nSecond\nThird\nFourth\nprint the same again:"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Above we tried to print out the contents of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" iterator twice, but the second attempt produced no printout. The reason is that "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" is an iterator; passing through it with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop \"depletes\" it, much like a generator is depleted once its maximum value is reached. Once the items in the iterator have been traversed with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop, there is nothing left to go through."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you need to go through the contents of a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" iterator more than once, you could, for example, convert the map into 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":"test_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":"\"first\""}]},{"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":"\"second\""}]},{"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":"\"third\""}]},{"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":"\"fourth\""}]},{"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":"# convert the return value of the map function into a list"}]},{"type":"text","value":"\ncapitalized "},{"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":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"capitalize"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" test_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\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" capitalized"},{"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":"word"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"print the same again:\""}]},{"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":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" capitalized"},{"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":"word"},{"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":"First\nSecond\nThird\nFourth\nprint the same again:\nFirst\nSecond\nThird\nFourth"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-map-function-and-your-own-classes","style":"position:relative;"},"children":[{"type":"text","value":"The map function and your own classes"},{"type":"element","tagName":"a","properties":{"href":"#the-map-function-and-your-own-classes","ariaLabel":"the map function and your own classes 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":"You can naturally also process instances of your own classes with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function. There are no special gimmicks involved, as you can see in the example below:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" account_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__account_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" account_number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\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":"deposit"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" amount"},{"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":"float"}]},{"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":"if"}]},{"type":"text","value":" amount "},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" amount\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":"get_balance"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance\n\na1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123456\""}]},{"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":"\"Randy Riches\""}]},{"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":"5000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12321\""}]},{"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 Pauper\""}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"223344\""}]},{"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":"\"Mary Millionaire \""}]},{"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":"1000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\naccounts "},{"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":"text","value":"a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nclients "},{"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":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" accounts"},{"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":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" clients"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbalances "},{"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":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_balance"},{"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":" accounts"},{"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":" balance "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" balances"},{"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":"balance"},{"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":"Randy Riches\nPaul Pauper\nMary Millionaire\n5000\n1\n1000000"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here we first collect the names of the account holders with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function. An anonymous lambda function is used to retrieve the value of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" attribute from each BankAccount object:"}]},{"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":"clients "},{"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":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" accounts"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Similarly, the balance of each BankAccount is collected. The lambda function looks a bit different, because the balance is retrieved with a method call, not from the attribute directly:"}]},{"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":"balances "},{"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":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_balance"},{"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":" accounts"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Attempted courses","tmcname":"part12-11_attempted_courses"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the class definition for a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CourseAttempt"}]},{"type":"text","value":". It works 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":"text","value":"attempt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Introduction to Programming\""}]},{"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":"5"}]},{"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":"attempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"student_name"},{"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":"attempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"course_name"},{"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":"attempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"grade"},{"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":"attempt"},{"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":"Peter Python\nIntroduction to Programming\n5\nPeter Python, grade for the course Introduction to Programming 5"}]}]},{"type":"element","tagName":"h2","properties":{"id":"names-of-students","style":"position:relative;"},"children":[{"type":"text","value":"Names of students"},{"type":"element","tagName":"a","properties":{"href":"#names-of-students","ariaLabel":"names of students 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"names_of_students(attempts: list)"}]},{"type":"text","value":" which takes a list of CourseAttempt objects as its argument. The function should return a new list with the names of the students who have attempted the course."}]},{"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":"s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Introduction to Programming\""}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olivia C. Objective\""}]},{"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":"\"Introduction to Programming\""}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Advanced Course in Programming\""}]},{"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":"2"}]},{"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":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" names_of_students"},{"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":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"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 "},{"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"},{"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":"Peter Python\nOlivia C. Objective\nPeter Python"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement the function using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function."}]},{"type":"element","tagName":"h2","properties":{"id":"courses","style":"position:relative;"},"children":[{"type":"text","value":"Courses"},{"type":"element","tagName":"a","properties":{"href":"#courses","ariaLabel":"courses 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"course_names(attempts: list)"}]},{"type":"text","value":" which takes a list of CourseAttempt objects as its argument. The function should return a new list containing the names of the courses on the original list in alphabetical order. Each course name should appear only once on the 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":"s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Introduction to Programming\""}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olivia C. Objective\""}]},{"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":"\"Introduction to Programming\""}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Advanced Course in Programming\""}]},{"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":"2"}]},{"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":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" course_names"},{"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":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"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 "},{"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"},{"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":"Advanced Course in Programming\nIntroduction to Programming"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement the function using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function. That alone will likely not be enough, however. You will need something else, too, to make sure the course names are unique."}]}]},{"type":"element","tagName":"h2","properties":{"id":"filter","style":"position:relative;"},"children":[{"type":"text","value":"filter"},{"type":"element","tagName":"a","properties":{"href":"#filter","ariaLabel":"filter 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 built-in Python function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" is similar to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function, but, as the name implies, it doesn't take all the items from the source. Instead, it filters them with a criterion function, which is passed as an argument. If the criterion function returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":", the item is selected."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's look at an example using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":":"}]},{"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":"integers "},{"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"10"}]},{"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":"14"}]},{"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":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\neven_numbers "},{"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":"filter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"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":"2"}]},{"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" integers"},{"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":" number "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" even_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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n6\n4\n10\n14"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It might make the above example a bit clearer if we used a named function instead:"}]},{"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":"is_it_even"}]},{"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":" "},{"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":"if"}]},{"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":"2"}]},{"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":"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":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\nintegers "},{"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"10"}]},{"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":"14"}]},{"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":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\neven_numbers "},{"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":"filter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"is_it_even"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" integers"},{"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":" number "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" even_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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"These two programs are functionally completely identical. It is mostly a matter of opinion which you consider the better approach."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at another filtering example. This program models fishes, and selects only those which weigh at least 1000 grams:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Fish"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The class models a fish of a certain species and weight \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" species"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" weight"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"species "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" species\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weight "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" weight\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":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"species"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weight"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" g.)\""}]}]},{"type":"text","value":"\n\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 f1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pike\""}]},{"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":"1870"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n f2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Perch\""}]},{"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":"763"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n f3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pike\""}]},{"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":"3410"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n f4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Cod\""}]},{"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":"2449"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n f5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Roach\""}]},{"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":"210"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n fishes "},{"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":"text","value":"f1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n over_a_kilo "},{"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":"filter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" fish "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weight "},{"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":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fishes"},{"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":" fish "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" over_a_kilo"},{"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":"fish"},{"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":"Pike (1870 g.)\nPike (3410 g.)\nCod (2449 g.)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We could just as well use a list comprehension and achieve the same result:"}]},{"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":"over_a_kilo "},{"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":"text","value":"fish "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" fish "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fishes "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weight "},{"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":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-return-value-of-filter-is-an-iterator","style":"position:relative;"},"children":[{"type":"text","value":"The return value of filter is an iterator"},{"type":"element","tagName":"a","properties":{"href":"#the-return-value-of-filter-is-an-iterator","ariaLabel":"the return value of filter is an iterator 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 "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" function resembles the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function in also that it returns an "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"iterator"}]},{"type":"text","value":". There are situations where you should be especially careful with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" as iterators can only be traversed once. So, trying to print out the collection of large fishes twice will not work quite as straightforwardly as you might think:"}]},{"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":"f1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pike\""}]},{"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":"1870"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nf2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Perch\""}]},{"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":"763"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nf3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pike\""}]},{"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":"3410"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nf4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Cod\""}]},{"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":"2449"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nf5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Roach\""}]},{"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":"210"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nfishes "},{"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":"text","value":"f1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nover_a_kilo "},{"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":"filter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" fish "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weight "},{"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":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fishes"},{"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":" fish "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" over_a_kilo"},{"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":"fish"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"print the same again:\""}]},{"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":" Fish "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" over_a_kilo"},{"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":"Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This would print out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pike (1870 g.)\nPike (3410 g.)\nCod (2449 g.)\nprint the same again:"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you need to go through the contents of a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" iterator more than once, you could convert the result into 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":"fishes "},{"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":"text","value":"f1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f5"},{"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":"# convert the return value of the filter function into a list"}]},{"type":"text","value":"\nover_a_kilo "},{"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":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"filter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" fish "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weight "},{"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":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fishes"},{"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":"Filtering attempts","tmcname":"part12-12_filtering_attempts"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise we will continue with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CourseAttempt"}]},{"type":"text","value":" class."}]},{"type":"element","tagName":"h2","properties":{"id":"accepted-attempts","style":"position:relative;"},"children":[{"type":"text","value":"Accepted attempts"},{"type":"element","tagName":"a","properties":{"href":"#accepted-attempts","ariaLabel":"accepted attempts 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"accepted(attempts: list)"}]},{"type":"text","value":" which takes a list of CourseAttempt objects as its argument. The function should return a new list of CourseAttempt objects, including only those items from the original list whose grade is at least 1."}]},{"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":"s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Introduction to Programming\""}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olivia C. Objective\""}]},{"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":"\"Introduction to Programming\""}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Advanced Course in Programming\""}]},{"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":"0"}]},{"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":" attempt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" accepted"},{"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":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"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 "},{"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":"attempt"},{"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":"Peter Python, grade for the course Introduction to Programming 3\nOlivia C. Objective grade for the course Introduction to Programming 5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement the function using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" function."}]},{"type":"element","tagName":"h2","properties":{"id":"attempts-with-grade","style":"position:relative;"},"children":[{"type":"text","value":"Attempts with grade"},{"type":"element","tagName":"a","properties":{"href":"#attempts-with-grade","ariaLabel":"attempts with grade 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"attempts_with_grade(attempts: list, grade: int)"}]},{"type":"text","value":" which takes a list of CourseAttempt objects and an integer as its arguments. The function should return a new list containing only those CourseAttempt objects from the original list whose grade matches the second argument."}]},{"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":"s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Introduction to Programming\""}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olivia C. Objective\""}]},{"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":"\"Introduction to Programming\""}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Introduction to AI\""}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olivia C. Objective\""}]},{"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":"\"Data Structures and Algorithms\""}]},{"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":"3"}]},{"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":" attempt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" attempts_with_grade"},{"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":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s4"},{"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":" "},{"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","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":"attempt"},{"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":"Peter Python, grade for the course Introduction to Programming 3\nPeter Python, grade for the course Introduction to AI 3\nOlivia C. Objective, grade for the course Data Structures and Algorithms 3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement the function using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" function."}]},{"type":"element","tagName":"h2","properties":{"id":"students-who-passed-the-course","style":"position:relative;"},"children":[{"type":"text","value":"Students who passed the course"},{"type":"element","tagName":"a","properties":{"href":"#students-who-passed-the-course","ariaLabel":"students who passed the course 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"passed_students(attempts: list, course: str)"}]},{"type":"text","value":" which takes a list of CourseAttempt objects and a course name as its arguments. The function should return an "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"alphabetically ordered"}]},{"type":"text","value":" list of names of those students who passed the course, i.e. their grade for the given course was higher than 0."}]},{"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":"s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Introduction to Programming\""}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olivia C. Objective\""}]},{"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":"\"Introduction to AI\""}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Introduction to AI\""}]},{"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jack Java\""}]},{"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":"\"Introduction to AI\""}]},{"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":"3"}]},{"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":" attempt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" passed_students"},{"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":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s4"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduction to AI\""}]},{"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":"attempt"},{"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":"Jack Java\nOlivia C. Objective"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement the function using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" functions."}]}]},{"type":"element","tagName":"h2","properties":{"id":"reduce","style":"position:relative;"},"children":[{"type":"text","value":"reduce"},{"type":"element","tagName":"a","properties":{"href":"#reduce","ariaLabel":"reduce 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":"A third cornerstone function in this introduction to functional programming principles is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":", from the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"functools"}]},{"type":"text","value":" module. As the name implies, its purpose is to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" the items in a series into a single value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" function starts with an operation and an initial value. It performs the given operation on each item in the series in turn, so that the value changes at each step. Once all items have been processed, the resulting value is returned."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We have done summation of lists of integers in different ways before, but here we have an example with the help of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" function. Notice the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement; in Python versions 3 and higher it is necessary to access the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" function. In older Python versions the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement was not needed, so you may come across examples without it online."}]},{"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":"from"}]},{"type":"text","value":" functools "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"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":"2"}]},{"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":"3"}]},{"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":"1"}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nsum_of_numbers "},{"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":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" reduced_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" reduced_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" item"},{"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":" "},{"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\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":"sum_of_numbers"},{"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":"11"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's take a closer look at what's happening here. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" function takes three arguments: a function, a series of items, and an initial value. In this case, the series is a list of integers, and as we are calculating a sum, a suitable initial value is zero."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The first argument is a function, which represents the operation we want to perform on each item. Here the function is an anonymous lambda 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","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" reduced_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" reduced_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" item"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This function takes two arguments: the current reduced value and the item whose turn it is to be processed. These are used to calculate a new value for the reduced value. In this case the new value is the sum of the old value and the current item."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It may be easier to comprehend what the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" function actually does if we use a normal named function instead of a lambda function. That way we can also include helpful printouts:"}]},{"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":"from"}]},{"type":"text","value":" functools "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"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":"2"}]},{"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":"3"}]},{"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":"1"}]},{"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":"5"}]},{"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":"# a helper function for reduce, adds one value to the current reduced sum"}]},{"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":"sum_helper"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"reduced_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" item"},{"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\"the reduced sum is now "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"reduced_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", next item 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":"item"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the new reduced sum is the old sum + the next item"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" reduced_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" item\n\nsum_of_numbers "},{"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":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sum_helper"},{"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":" "},{"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\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":"sum_of_numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program prints out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"the reduced sum is now 0, next item is 2\nthe reduced sum is now 2, next item is 3\nthe reduced sum is now 5, next item is 1\nthe reduced sum is now 6, next item is 5\n11"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First, the function takes care of the item with value 2. To begin with, the reduced sum is 0, which is the initial value passed to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" function. The function calculates and returns the sum of these two: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0 + 2 = 2"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is the value stored in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduced_sum"}]},{"type":"text","value":" as the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" function processes the next item on the list, with value 3. The function calculates and returns the sum of these two: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 + 3 = 5"}]},{"type":"text","value":". This result is then used when processing the next item, and so forth, and so forth."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, summation is simple, as there is even the built-in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" function for this purpose. But how about multiplication? Only minor changes are needed to create a reduced product:"}]},{"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":"from"}]},{"type":"text","value":" functools "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"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":"2"}]},{"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":"2"}]},{"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":"4"}]},{"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":"3"}]},{"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":"5"}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nproduct_of_list "},{"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":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" product "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" item"},{"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":" "},{"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\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":"product_of_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":"480"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As we are dealing with multiplication the initial value is not zero. Instead, we use 1. What would happen if we used 0 as the initial value?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Above we have dealt largely with integers, but "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" can all handle a collection of objects of any type."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As an example, let's generate a sum total of the balances of all accounts in a bank, with the help of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" account_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__account_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" account_number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\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":"deposit"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" amount"},{"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":"float"}]},{"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":"if"}]},{"type":"text","value":" amount "},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" amount\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":"get_balance"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance\n\na1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123456\""}]},{"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":"\"Randy Riches\""}]},{"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":"5000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12321\""}]},{"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 Pauper\""}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"223344\""}]},{"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":"\"Mary Millionaire \""}]},{"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":"1000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\naccounts "},{"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":"text","value":"a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a3"},{"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":"from"}]},{"type":"text","value":" functools "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"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":"balance_sum_helper"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"balance_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" account"},{"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":" balance_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_balance"},{"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\nbalances_total "},{"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":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"balance_sum_helper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" accounts"},{"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":"0"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The total of the bank's balances:\""}]},{"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":"balances_total"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program would print out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The total of the bank's balances:\n1005001"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balance_sum_helper"}]},{"type":"text","value":" function grabs the balance of each bank account, with the method dedicated for the purpose in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" class definition:"}]},{"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":"balance_sum_helper"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"balance_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" account"},{"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":" balance_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_balance"},{"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":"text-box","properties":{"variant":"hint","name":"Reduce without an initial value"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You do not need to always pass a third argument to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" function. For instance, summation would work just as well "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"without"}]},{"type":"text","value":" the initial value:"}]},{"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":"2"}]},{"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":"3"}]},{"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":"1"}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nsum_of_numbers "},{"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":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" reduced_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" reduced_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" item"},{"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\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":"sum_of_numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the initial value is left out, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" takes the first item in the list as the initial value and starts reducing from the second item onwards."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" if the items in the series are of a different type than the intended reduced result, the thrd argument is mandatory. The example with the bank accounts would not work without the initial value. That is, trying 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":"text","value":"balances_total "},{"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":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"balance_sum_helper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" accounts"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"would produce an error:"}]},{"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":"TypeError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" unsupported operand "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"s"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"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":"'BankAccount'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'int'"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the above case, when "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" tries to execute the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balance_sum_helper"}]},{"type":"text","value":" function for the first time, the arguments it uses are the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"two first items in the list"}]},{"type":"text","value":", which are both of type BankAccount. Specifically, the value assigned to the parameter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balance_sum"}]},{"type":"text","value":" is the first item in the list. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balance_sum_helper"}]},{"type":"text","value":" function tries to add an integer value to it, but adding an integer directly to a BankAccount object is not a supported operation."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Study credits","tmcname":"part12-13_credits"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise we will work with a slightly modified version of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CourseAttempt"}]},{"type":"text","value":" class. The name of the student is omitted, but the number of credits is included. The class works as follows:"}]},{"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":"attempt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Data Structures and Algorithms\""}]},{"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":"3"}]},{"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":"10"}]},{"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":"attempt"},{"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":"attempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"course_name"},{"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":"attempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits"},{"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":"attempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"grade"},{"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":"Data Structures and Algorithms (10 cr) grade 3\nData Structures and Algorithms\n10\n3"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-sum-of-all-credits","style":"position:relative;"},"children":[{"type":"text","value":"The sum of all credits"},{"type":"element","tagName":"a","properties":{"href":"#the-sum-of-all-credits","ariaLabel":"the sum of all credits 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":"Please implement a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum_of_all_credits"}]},{"type":"text","value":" which takes a list of course attempts as its argument. The function sums up the total number of study credits covered by the courses. It should work 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":"text","value":"s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduction to Programming\""}]},{"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":"5"}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Advanced Course in Programming\""}]},{"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":"4"}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Data Structures and Algorithms\""}]},{"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":"3"}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncredit_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sum_of_all_credits"},{"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":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"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":"credit_sum"},{"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":"20"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement the function using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" function."}]},{"type":"element","tagName":"h2","properties":{"id":"the-sum-of-passed-credits","style":"position:relative;"},"children":[{"type":"text","value":"The sum of passed credits"},{"type":"element","tagName":"a","properties":{"href":"#the-sum-of-passed-credits","ariaLabel":"the sum of passed credits 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":"Please implement a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum_of_passed_credits"}]},{"type":"text","value":" which takes a list of course attempts as its argument. The function sums up the credits for the course attempts with grade 1 or above. It should work 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":"text","value":"s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduction to Programming\""}]},{"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":"5"}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Advanced Course in Programming\""}]},{"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":"0"}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Data Structures and Algorithms\""}]},{"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":"3"}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncredit_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sum_of_passed_credits"},{"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":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"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":"credit_sum"},{"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":"15"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement the function using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" functions."}]},{"type":"element","tagName":"h2","properties":{"id":"average-grade-for-passed-courses","style":"position:relative;"},"children":[{"type":"text","value":"Average grade for passed courses"},{"type":"element","tagName":"a","properties":{"href":"#average-grade-for-passed-courses","ariaLabel":"average grade for passed courses 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":"Please implement a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"average"}]},{"type":"text","value":" which takes a list of course attempts as its argument. The function calculates the average grade for the course attempts with grade 1 or above. It should work 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":"text","value":"s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduction to Programming\""}]},{"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":"5"}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Advanced Course in Programming\""}]},{"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":"0"}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Data Structures and Algorithms\""}]},{"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":"3"}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" average"},{"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":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"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":"ag"},{"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.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement the function using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" functions. NB: the exercise asks for a simple mean value, not a weighted average."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"While working on this exercise, it is likely worth remembering that "},{"type":"element","tagName":"a","properties":{"href":"/part-12/3-functional-programming#the-return-value-of-filter-is-an-iterator"},"children":[{"type":"text","value":"the return value of filter is an iterator"}]},{"type":"text","value":"."}]}]}]},"html":"

    After this section

      \n
    • You will know what functional programming means
    • \n
    • You will be able to make use of the functions map, reduce and filter in your own programs
    • \n

    Functional programming refers to a programming paradigm which avoids changes in program state as much as possible. Variables are generally avoided. Instead, chains of function calls form the backbone of the program.

    Lambda expressions and different types of comprehensions are common techniques in the functional programming style, as they let you process data without storing it in variables, so that the state of the program does not change. For example, a lambda expression is for all intents and purposes a function, but we do not need to store a named reference to it anywhere.

    As mentioned above, functional programming is a programming paradigm, or a style of programming. There are many different programming paradigms, and we've already come across some of them:

      \n
    • imperative programming, where the program consists of a sequence of commands which is executed in order
    • \n
    • procedural programming, where the program is grouped into procedures or sub-programs
    • \n
    • object-oriented programming, where the program and its state is stored in objects defined in classes.
    • \n

    There are differing opinions on the divisions between the different paradigms; for example, some maintain that imperative and procedural programming mean the same thing, while others place imperative programming as an umbrella term which covers both procedural and object-oriented programming. Th terminology and divisions are not that important, and neither is strictly sticking to one or the other paradigm, but it is important to understand that such different approaches exist, as they affect the choices programmers make.

    Many programming languages are designed with one or the other programming paradigm in mind, but Python is a rather versatile programming language, and allows for following several different programming paradigms, even within a single program. This lets us choose the most efficient and clear method for solving each problem.

    Let's have a look at some functional programming tools provided by Python.

    map

    The map function executes some operation on each item in an iterable series. This sounds a lot like the effect a comprehension has, but the syntax is different.

    Let's assume we have list of strings which we want to convert into a list of integers:

    str_list = [\"123\",\"-10\", \"23\", \"98\", \"0\", \"-110\"]\n\nintegers = map(lambda x : int(x), str_list)\n\nprint(integers) # this tells us the type of object we're dealing with\n\nfor number in integers:\n    print(number)

    <map object at 0x0000021A4BFA9A90>\n123\n-10\n23\n98\n0\n-110

    The general syntax for the map function is

    map(<function>, <series>)

    where function is the operation we want to execute on each item in the series.

    The map function returns an object of type map, which is iterable, and can be converted into a list:

    def capitalize(my_string: str):\n    first = my_string[0]\n    first = first.upper()\n    return first + my_string[1:]\n\ntest_list = [\"first\", \"second\", \"third\", \"fourth\"]\n\ncapitalized = map(capitalize, test_list)\n\ncapitalized_list = list(capitalized)\nprint(capitalized_list)

    ['First', 'Second', 'Third', 'Fourth']

    As you can see from the examples above, the map function accepts both an anonymous lambda function and a named function defined with the def keyword.

    We could achieve the same result with a list comprehension:

    def capitalize(my_string: str):\n    first = my_string[0]\n    first = first.upper()\n    return first + my_string[1:]\n\ntest_list = [\"first\", \"second\", \"third\", \"fourth\"]\n\ncapitalized_list = [capitalize(item) for item in test_list]\nprint(capitalized_list)

    ...or we could go through the original list with a for loop and save the processed items in a new list with the append method. Typically, in programming there are many different solutions to each problem. There are rarely any absolutely right or wrong answers. Knowing many different approaches helps you choose the most appropriate one for each situation, or one that best suits your own tastes.

    It is worth pointing out that the map function does not return a list, but an iterator object of type map. An iterator behaves in many ways like a list, but there are exceptions, as can be seen in the following example:

    def capitalize(my_string: str):\n    first = my_string[0]\n    first = first.upper()\n    return first + my_string[1:]\n\ntest_list = [\"first\", \"second\", \"third\", \"fourth\"]\n\n# store the return value from the map function\ncapitalized = map(capitalize, test_list)\n\nfor word in capitalized:\n  print(word)\n\nprint(\"print the same again:\")\nfor word in capitalized:\n  print(word)

    This would print out the following:

    First\nSecond\nThird\nFourth\nprint the same again:

    Above we tried to print out the contents of the map iterator twice, but the second attempt produced no printout. The reason is that map is an iterator; passing through it with a for loop \"depletes\" it, much like a generator is depleted once its maximum value is reached. Once the items in the iterator have been traversed with a for loop, there is nothing left to go through.

    If you need to go through the contents of a map iterator more than once, you could, for example, convert the map into a list:

    test_list = [\"first\", \"second\", \"third\", \"fourth\"]\n\n# convert the return value of the map function into a list\ncapitalized = list(map(capitalize, test_list))\n\nfor word in capitalized:\n  print(word)\n\nprint(\"print the same again:\")\nfor word in capitalized:\n  print(word)

    First\nSecond\nThird\nFourth\nprint the same again:\nFirst\nSecond\nThird\nFourth

    The map function and your own classes

    You can naturally also process instances of your own classes with the map function. There are no special gimmicks involved, as you can see in the example below:

    class BankAccount:\n    def __init__(self, account_number: str, name: str, balance: float):\n        self.__account_number = account_number\n        self.name = name\n        self.__balance = balance\n\n    def deposit(self, amount: float):\n        if amount > 0:\n            self.__balance += amount\n\n    def get_balance(self):\n        return self.__balance\n\na1 = BankAccount(\"123456\", \"Randy Riches\", 5000)\na2 = BankAccount(\"12321\", \"Paul Pauper\", 1)\na3 = BankAccount(\"223344\", \"Mary Millionaire \", 1000000)\n\naccounts = [a1, a2, a3]\n\nclients = map(lambda t: t.name, accounts)\nfor name in clients:\n  print(name)\n\nbalances = map(lambda t: t.get_balance(), accounts)\nfor balance in balances:\n  print(balance)

    Randy Riches\nPaul Pauper\nMary Millionaire\n5000\n1\n1000000

    Here we first collect the names of the account holders with the map function. An anonymous lambda function is used to retrieve the value of the name attribute from each BankAccount object:

    clients = map(lambda t: t.name, accounts)

    Similarly, the balance of each BankAccount is collected. The lambda function looks a bit different, because the balance is retrieved with a method call, not from the attribute directly:

    balances = map(lambda t: t.get_balance(), accounts)

    The exercise template contains the class definition for a CourseAttempt. It works like this:

    attempt = CourseAttempt(\"Peter Python\", \"Introduction to Programming\", 5)\nprint(attempt.student_name)\nprint(attempt.course_name)\nprint(attempt.grade)\nprint(attempt)

    Peter Python\nIntroduction to Programming\n5\nPeter Python, grade for the course Introduction to Programming 5

    Names of students

    Please write a function named names_of_students(attempts: list) which takes a list of CourseAttempt objects as its argument. The function should return a new list with the names of the students who have attempted the course.

    s1 = CourseAttempt(\"Peter Python\", \"Introduction to Programming\", 3)\ns2 = CourseAttempt(\"Olivia C. Objective\", \"Introduction to Programming\", 5)\ns3 = CourseAttempt(\"Peter Python\", \"Advanced Course in Programming\", 2)\n\nfor name in names_of_students([s1, s2, s3]):\n    print(name)

    Peter Python\nOlivia C. Objective\nPeter Python

    Please implement the function using the map function.

    Courses

    Please write a function named course_names(attempts: list) which takes a list of CourseAttempt objects as its argument. The function should return a new list containing the names of the courses on the original list in alphabetical order. Each course name should appear only once on the list.

    s1 = CourseAttempt(\"Peter Python\", \"Introduction to Programming\", 3)\ns2 = CourseAttempt(\"Olivia C. Objective\", \"Introduction to Programming\", 5)\ns3 = CourseAttempt(\"Peter Python\", \"Advanced Course in Programming\", 2)\n\nfor name in course_names([s1, s2, s3]):\n    print(name)

    Advanced Course in Programming\nIntroduction to Programming

    Please implement the function using the map function. That alone will likely not be enough, however. You will need something else, too, to make sure the course names are unique.

    filter

    The built-in Python function filter is similar to the map function, but, as the name implies, it doesn't take all the items from the source. Instead, it filters them with a criterion function, which is passed as an argument. If the criterion function returns True, the item is selected.

    Let's look at an example using filter:

    integers = [1, 2, 3, 5, 6, 4, 9, 10, 14, 15]\n\neven_numbers = filter(lambda number: number % 2 == 0, integers)\n\nfor number in even_numbers:\n    print(number)

    2\n6\n4\n10\n14

    It might make the above example a bit clearer if we used a named function instead:

    def is_it_even(number: int):\n    if number % 2 == 0:\n        return True\n    return False\n\nintegers = [1, 2, 3, 5, 6, 4, 9, 10, 14, 15]\n\neven_numbers = filter(is_it_even, integers)\n\nfor number in even_numbers:\n    print(number)

    These two programs are functionally completely identical. It is mostly a matter of opinion which you consider the better approach.

    Let's have a look at another filtering example. This program models fishes, and selects only those which weigh at least 1000 grams:

    class Fish:\n    \"\"\" The class models a fish of a certain species and weight \"\"\"\n    def __init__(self, species: str, weight: int):\n        self.species = species\n        self.weight = weight\n\n    def __repr__(self):\n        return f\"{self.species} ({self.weight} g.)\"\n\nif __name__ == \"__main__\":\n    f1 = Fish(\"Pike\", 1870)\n    f2 = Fish(\"Perch\", 763)\n    f3 = Fish(\"Pike\", 3410)\n    f4 = Fish(\"Cod\", 2449)\n    f5 = Fish(\"Roach\", 210)\n\n    fishes = [f1, f2, f3, f4, f5]\n\n    over_a_kilo = filter(lambda fish : fish.weight >= 1000, fishes)\n\n    for fish in over_a_kilo:\n        print(fish)

    Pike (1870 g.)\nPike (3410 g.)\nCod (2449 g.)

    We could just as well use a list comprehension and achieve the same result:

    over_a_kilo = [fish for fish in fishes if fish.weight >= 1000]

    The return value of filter is an iterator

    The filter function resembles the map function in also that it returns an iterator. There are situations where you should be especially careful with filter as iterators can only be traversed once. So, trying to print out the collection of large fishes twice will not work quite as straightforwardly as you might think:

    f1 = Fish(\"Pike\", 1870)\nf2 = Fish(\"Perch\", 763)\nf3 = Fish(\"Pike\", 3410)\nf4 = Fish(\"Cod\", 2449)\nf5 = Fish(\"Roach\", 210)\n\nfishes = [f1, f2, f3, f4, f5]\n\nover_a_kilo = filter(lambda fish : fish.weight >= 1000, fishes)\n\nfor fish in over_a_kilo:\n    print(fish)\n\nprint(\"print the same again:\")\n\nfor Fish in over_a_kilo:\n    print(Fish)

    This would print out the following:

    Pike (1870 g.)\nPike (3410 g.)\nCod (2449 g.)\nprint the same again:

    If you need to go through the contents of a filter iterator more than once, you could convert the result into a list:

    fishes = [f1, f2, f3, f4, f5]\n\n# convert the return value of the filter function into a list\nover_a_kilo = list(filter(lambda fish : fish.weight >= 1000, fishes))

    In this exercise we will continue with the CourseAttempt class.

    Accepted attempts

    Please write a function named accepted(attempts: list) which takes a list of CourseAttempt objects as its argument. The function should return a new list of CourseAttempt objects, including only those items from the original list whose grade is at least 1.

    s1 = CourseAttempt(\"Peter Python\", \"Introduction to Programming\", 3)\ns2 = CourseAttempt(\"Olivia C. Objective\", \"Introduction to Programming\", 5)\ns3 = CourseAttempt(\"Peter Python\", \"Advanced Course in Programming\", 0)\n\nfor attempt in accepted([s1, s2, s3]):\n    print(attempt)

    Peter Python, grade for the course Introduction to Programming 3\nOlivia C. Objective grade for the course Introduction to Programming 5

    Please implement the function using the filter function.

    Attempts with grade

    Please write a function named attempts_with_grade(attempts: list, grade: int) which takes a list of CourseAttempt objects and an integer as its arguments. The function should return a new list containing only those CourseAttempt objects from the original list whose grade matches the second argument.

    s1 = CourseAttempt(\"Peter Python\", \"Introduction to Programming\", 3)\ns2 = CourseAttempt(\"Olivia C. Objective\", \"Introduction to Programming\", 5)\ns3 = CourseAttempt(\"Peter Python\", \"Introduction to AI\", 3)\ns4 = CourseAttempt(\"Olivia C. Objective\", \"Data Structures and Algorithms\", 3)\n\nfor attempt in attempts_with_grade([s1, s2, s3, s4], 3):\n    print(attempt)

    Peter Python, grade for the course Introduction to Programming 3\nPeter Python, grade for the course Introduction to AI 3\nOlivia C. Objective, grade for the course Data Structures and Algorithms 3

    Please implement the function using the filter function.

    Students who passed the course

    Please write a function named passed_students(attempts: list, course: str) which takes a list of CourseAttempt objects and a course name as its arguments. The function should return an alphabetically ordered list of names of those students who passed the course, i.e. their grade for the given course was higher than 0.

    s1 = CourseAttempt(\"Peter Python\", \"Introduction to Programming\", 3)\ns2 = CourseAttempt(\"Olivia C. Objective\", \"Introduction to AI\", 5)\ns3 = CourseAttempt(\"Peter Python\", \"Introduction to AI\", 0)\ns4 = CourseAttempt(\"Jack Java\", \"Introduction to AI\", 3)\n\nfor attempt in passed_students([s1, s2, s3, s4], \"Introduction to AI\"):\n    print(attempt)

    Jack Java\nOlivia C. Objective

    Please implement the function using the filter and map functions.

    reduce

    A third cornerstone function in this introduction to functional programming principles is reduce, from the functools module. As the name implies, its purpose is to reduce the items in a series into a single value.

    The reduce function starts with an operation and an initial value. It performs the given operation on each item in the series in turn, so that the value changes at each step. Once all items have been processed, the resulting value is returned.

    We have done summation of lists of integers in different ways before, but here we have an example with the help of the reduce function. Notice the import statement; in Python versions 3 and higher it is necessary to access the reduce function. In older Python versions the import statement was not needed, so you may come across examples without it online.

    from functools import reduce\n\nmy_list = [2, 3, 1, 5]\n\nsum_of_numbers = reduce(lambda reduced_sum, item: reduced_sum + item, my_list, 0)\n\nprint(sum_of_numbers)

    11

    Let's take a closer look at what's happening here. The reduce function takes three arguments: a function, a series of items, and an initial value. In this case, the series is a list of integers, and as we are calculating a sum, a suitable initial value is zero.

    The first argument is a function, which represents the operation we want to perform on each item. Here the function is an anonymous lambda function:

    lambda reduced_sum, item: reduced_sum + item

    This function takes two arguments: the current reduced value and the item whose turn it is to be processed. These are used to calculate a new value for the reduced value. In this case the new value is the sum of the old value and the current item.

    It may be easier to comprehend what the reduce function actually does if we use a normal named function instead of a lambda function. That way we can also include helpful printouts:

    from functools import reduce\n\nmy_list = [2, 3, 1, 5]\n\n# a helper function for reduce, adds one value to the current reduced sum\ndef sum_helper(reduced_sum, item):\n  print(f\"the reduced sum is now {reduced_sum}, next item is {item}\")\n  # the new reduced sum is the old sum + the next item\n  return reduced_sum + item\n\nsum_of_numbers = reduce(sum_helper, my_list, 0)\n\nprint(sum_of_numbers)

    The program prints out:

    the reduced sum is now 0, next item is 2\nthe reduced sum is now 2, next item is 3\nthe reduced sum is now 5, next item is 1\nthe reduced sum is now 6, next item is 5\n11

    First, the function takes care of the item with value 2. To begin with, the reduced sum is 0, which is the initial value passed to the reduce function. The function calculates and returns the sum of these two: 0 + 2 = 2.

    This is the value stored in reduced_sum as the reduce function processes the next item on the list, with value 3. The function calculates and returns the sum of these two: 2 + 3 = 5. This result is then used when processing the next item, and so forth, and so forth.

    Now, summation is simple, as there is even the built-in sum function for this purpose. But how about multiplication? Only minor changes are needed to create a reduced product:

    from functools import reduce\n\nmy_list = [2, 2, 4, 3, 5, 2]\n\nproduct_of_list = reduce(lambda product, item: product * item, my_list, 1)\n\nprint(product_of_list)

    480

    As we are dealing with multiplication the initial value is not zero. Instead, we use 1. What would happen if we used 0 as the initial value?

    Above we have dealt largely with integers, but map, filter and reduce can all handle a collection of objects of any type.

    As an example, let's generate a sum total of the balances of all accounts in a bank, with the help of reduce:

    class BankAccount:\n    def __init__(self, account_number: str, name: str, balance: float):\n        self.__account_number = account_number\n        self.name = name\n        self.__balance = balance\n\n    def deposit(self, amount: float):\n        if amount > 0:\n            self.__balance += amount\n\n    def get_balance(self):\n        return self.__balance\n\na1 = BankAccount(\"123456\", \"Randy Riches\", 5000)\na2 = BankAccount(\"12321\", \"Paul Pauper\", 1)\na3 = BankAccount(\"223344\", \"Mary Millionaire \", 1000000)\n\naccounts = [a1, a2, a3]\n\nfrom functools import reduce\n\ndef balance_sum_helper(balance_sum, account):\n  return balance_sum + account.get_balance()\n\nbalances_total = reduce(balance_sum_helper, accounts, 0)\n\nprint(\"The total of the bank's balances:\")\nprint(balances_total)

    This program would print out:

    The total of the bank's balances:\n1005001

    The balance_sum_helper function grabs the balance of each bank account, with the method dedicated for the purpose in the BankAccount class definition:

    def balance_sum_helper(balance_sum, account):\n  return balance_sum + account.get_balance()

    You do not need to always pass a third argument to the reduce function. For instance, summation would work just as well without the initial value:

    my_list = [2, 3, 1, 5]\n\nsum_of_numbers = reduce(lambda reduced_sum, item: reduced_sum + item, my_list)\n\nprint(sum_of_numbers)

    If the initial value is left out, reduce takes the first item in the list as the initial value and starts reducing from the second item onwards.

    NB: if the items in the series are of a different type than the intended reduced result, the thrd argument is mandatory. The example with the bank accounts would not work without the initial value. That is, trying this

    balances_total = reduce(balance_sum_helper, accounts)

    would produce an error:

    TypeError: unsupported operand type(s) for +: 'BankAccount' and 'int'

    In the above case, when reduce tries to execute the balance_sum_helper function for the first time, the arguments it uses are the two first items in the list, which are both of type BankAccount. Specifically, the value assigned to the parameter balance_sum is the first item in the list. The balance_sum_helper function tries to add an integer value to it, but adding an integer directly to a BankAccount object is not a supported operation.

    In this exercise we will work with a slightly modified version of the CourseAttempt class. The name of the student is omitted, but the number of credits is included. The class works as follows:

    attempt = CourseAttempt(\"Data Structures and Algorithms\", 3, 10)\nprint(attempt)\nprint(attempt.course_name)\nprint(attempt.credits)\nprint(attempt.grade)

    Data Structures and Algorithms (10 cr) grade 3\nData Structures and Algorithms\n10\n3

    The sum of all credits

    Please implement a function named sum_of_all_credits which takes a list of course attempts as its argument. The function sums up the total number of study credits covered by the courses. It should work like this:

    s1 = CourseAttempt(\"Introduction to Programming\", 5, 5)\ns2 = CourseAttempt(\"Advanced Course in Programming\", 4, 5)\ns3 = CourseAttempt(\"Data Structures and Algorithms\", 3, 10)\ncredit_sum = sum_of_all_credits([s1, s2, s3])\nprint(credit_sum)

    20

    Please implement the function using the reduce function.

    The sum of passed credits

    Please implement a function named sum_of_passed_credits which takes a list of course attempts as its argument. The function sums up the credits for the course attempts with grade 1 or above. It should work like this:

    s1 = CourseAttempt(\"Introduction to Programming\", 5, 5)\ns2 = CourseAttempt(\"Advanced Course in Programming\", 0, 4)\ns3 = CourseAttempt(\"Data Structures and Algorithms\", 3, 10)\ncredit_sum = sum_of_passed_credits([s1, s2, s3])\nprint(credit_sum)

    15

    Please implement the function using the reduce and filter functions.

    Average grade for passed courses

    Please implement a function named average which takes a list of course attempts as its argument. The function calculates the average grade for the course attempts with grade 1 or above. It should work like this:

    s1 = CourseAttempt(\"Introduction to Programming\", 5, 5)\ns2 = CourseAttempt(\"Advanced Course in Programming\", 0, 4)\ns3 = CourseAttempt(\"Data Structures and Algorithms\", 3, 10)\nag = average([s1, s2, s3])\nprint(ag)

    4.0

    Please implement the function using the reduce and filter functions. NB: the exercise asks for a simple mean value, not a weighted average.

    While working on this exercise, it is likely worth remembering that the return value of filter is an iterator.

    ","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-12/3-functional-programming.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know what functional programming means"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to make use of the functions "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" in your own programs"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Functional programming refers to a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"programming paradigm"}]},{"type":"text","value":" which avoids changes in program state as much as possible. Variables are generally avoided. Instead, chains of function calls form the backbone of the program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lambda expressions and different types of comprehensions are common techniques in the functional programming style, as they let you process data without storing it in variables, so that the state of the program does not change. For example, a lambda expression is for all intents and purposes a function, but we do not need to store a named reference to it anywhere."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As mentioned above, functional programming is a programming paradigm, or a style of programming. There are many different programming paradigms, and we've already come across some of them:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"imperative programming, where the program consists of a sequence of commands which is executed in order"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"procedural programming, where the program is grouped into procedures or sub-programs"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"object-oriented programming, where the program and its state is stored in objects defined in classes."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are differing opinions on the divisions between the different paradigms; for example, some maintain that imperative and procedural programming mean the same thing, while others place imperative programming as an umbrella term which covers both procedural and object-oriented programming. Th terminology and divisions are not that important, and neither is strictly sticking to one or the other paradigm, but it is important to understand that such different approaches exist, as they affect the choices programmers make."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Many programming languages are designed with one or the other programming paradigm in mind, but Python is a rather versatile programming language, and allows for following several different programming paradigms, even within a single program. This lets us choose the most efficient and clear method for solving each problem."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at some functional programming tools provided by Python."}]},{"type":"element","tagName":"h2","properties":{"id":"map","style":"position:relative;"},"children":[{"type":"text","value":"map"},{"type":"element","tagName":"a","properties":{"href":"#map","ariaLabel":"map 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 "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function executes some operation on each item in an iterable series. This sounds a lot like the effect a comprehension has, but the syntax is different."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's assume we have list of strings which we want to convert into a list of integers:"}]},{"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":"str_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":"\"123\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"-10\""}]},{"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":"\"23\""}]},{"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":"\"98\""}]},{"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":"\"0\""}]},{"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":"\"-110\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nintegers "},{"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":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" x "},{"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":"text","value":"x"},{"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":" str_list"},{"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":"integers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# this tells us the type of object we're dealing with"}]},{"type":"text","value":"\n\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":" integers"},{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"\n123\n-10\n23\n98\n0\n-110"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The general syntax for the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function is"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map(, )"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"where "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" is the operation we want to execute on each item in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"series"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function returns an object of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":", which is iterable, and can be converted into 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":"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":"capitalize"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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 "},{"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":"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 first "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" first"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"upper"},{"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":" first "},{"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":"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":"text","value":"\n\ntest_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":"\"first\""}]},{"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":"\"second\""}]},{"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":"\"third\""}]},{"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":"\"fourth\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\ncapitalized "},{"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":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"capitalize"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" test_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ncapitalized_list "},{"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":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"capitalized"},{"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":"capitalized_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":"['First', 'Second', 'Third', 'Fourth']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see from the examples above, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function accepts both an anonymous lambda function and a named function defined with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" keyword."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We could achieve the same result with a list comprehension:"}]},{"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":"capitalize"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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 "},{"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":"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 first "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" first"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"upper"},{"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":" first "},{"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":"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":"text","value":"\n\ntest_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":"\"first\""}]},{"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":"\"second\""}]},{"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":"\"third\""}]},{"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":"\"fourth\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\ncapitalized_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":"text","value":"capitalize"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" test_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":"capitalized_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"...or we could go through the original list with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop and save the processed items in a new list with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"append"}]},{"type":"text","value":" method. Typically, in programming there are many different solutions to each problem. There are rarely any absolutely right or wrong answers. Knowing many different approaches helps you choose the most appropriate one for each situation, or one that best suits your own tastes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is worth pointing out that the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function does not return a list, but an "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"iterator"}]},{"type":"text","value":" object of type map. An iterator behaves in many ways like a list, but there are exceptions, as can be seen in the following example:"}]},{"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":"capitalize"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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 "},{"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":"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 first "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" first"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"upper"},{"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":" first "},{"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":"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":"text","value":"\n\ntest_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":"\"first\""}]},{"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":"\"second\""}]},{"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":"\"third\""}]},{"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":"\"fourth\""}]},{"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":"# store the return value from the map function"}]},{"type":"text","value":"\ncapitalized "},{"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":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"capitalize"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" test_list"},{"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":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" capitalized"},{"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":"word"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"print the same again:\""}]},{"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":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" capitalized"},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This would print out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First\nSecond\nThird\nFourth\nprint the same again:"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Above we tried to print out the contents of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" iterator twice, but the second attempt produced no printout. The reason is that "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" is an iterator; passing through it with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop \"depletes\" it, much like a generator is depleted once its maximum value is reached. Once the items in the iterator have been traversed with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop, there is nothing left to go through."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you need to go through the contents of a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" iterator more than once, you could, for example, convert the map into 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":"test_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":"\"first\""}]},{"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":"\"second\""}]},{"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":"\"third\""}]},{"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":"\"fourth\""}]},{"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":"# convert the return value of the map function into a list"}]},{"type":"text","value":"\ncapitalized "},{"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":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"capitalize"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" test_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\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" capitalized"},{"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":"word"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"print the same again:\""}]},{"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":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" capitalized"},{"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":"word"},{"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":"First\nSecond\nThird\nFourth\nprint the same again:\nFirst\nSecond\nThird\nFourth"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-map-function-and-your-own-classes","style":"position:relative;"},"children":[{"type":"text","value":"The map function and your own classes"},{"type":"element","tagName":"a","properties":{"href":"#the-map-function-and-your-own-classes","ariaLabel":"the map function and your own classes 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":"You can naturally also process instances of your own classes with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function. There are no special gimmicks involved, as you can see in the example below:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" account_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__account_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" account_number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\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":"deposit"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" amount"},{"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":"float"}]},{"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":"if"}]},{"type":"text","value":" amount "},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" amount\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":"get_balance"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance\n\na1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123456\""}]},{"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":"\"Randy Riches\""}]},{"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":"5000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12321\""}]},{"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 Pauper\""}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"223344\""}]},{"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":"\"Mary Millionaire \""}]},{"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":"1000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\naccounts "},{"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":"text","value":"a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nclients "},{"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":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" accounts"},{"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":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" clients"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbalances "},{"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":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_balance"},{"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":" accounts"},{"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":" balance "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" balances"},{"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":"balance"},{"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":"Randy Riches\nPaul Pauper\nMary Millionaire\n5000\n1\n1000000"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here we first collect the names of the account holders with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function. An anonymous lambda function is used to retrieve the value of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" attribute from each BankAccount object:"}]},{"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":"clients "},{"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":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" accounts"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Similarly, the balance of each BankAccount is collected. The lambda function looks a bit different, because the balance is retrieved with a method call, not from the attribute directly:"}]},{"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":"balances "},{"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":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_balance"},{"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":" accounts"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Attempted courses","tmcname":"part12-11_attempted_courses"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the class definition for a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CourseAttempt"}]},{"type":"text","value":". It works 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":"text","value":"attempt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Introduction to Programming\""}]},{"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":"5"}]},{"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":"attempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"student_name"},{"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":"attempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"course_name"},{"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":"attempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"grade"},{"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":"attempt"},{"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":"Peter Python\nIntroduction to Programming\n5\nPeter Python, grade for the course Introduction to Programming 5"}]}]},{"type":"element","tagName":"h2","properties":{"id":"names-of-students","style":"position:relative;"},"children":[{"type":"text","value":"Names of students"},{"type":"element","tagName":"a","properties":{"href":"#names-of-students","ariaLabel":"names of students 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"names_of_students(attempts: list)"}]},{"type":"text","value":" which takes a list of CourseAttempt objects as its argument. The function should return a new list with the names of the students who have attempted the course."}]},{"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":"s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Introduction to Programming\""}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olivia C. Objective\""}]},{"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":"\"Introduction to Programming\""}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Advanced Course in Programming\""}]},{"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":"2"}]},{"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":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" names_of_students"},{"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":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"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 "},{"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"},{"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":"Peter Python\nOlivia C. Objective\nPeter Python"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement the function using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function."}]},{"type":"element","tagName":"h2","properties":{"id":"courses","style":"position:relative;"},"children":[{"type":"text","value":"Courses"},{"type":"element","tagName":"a","properties":{"href":"#courses","ariaLabel":"courses 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"course_names(attempts: list)"}]},{"type":"text","value":" which takes a list of CourseAttempt objects as its argument. The function should return a new list containing the names of the courses on the original list in alphabetical order. Each course name should appear only once on the 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":"s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Introduction to Programming\""}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olivia C. Objective\""}]},{"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":"\"Introduction to Programming\""}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Advanced Course in Programming\""}]},{"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":"2"}]},{"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":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" course_names"},{"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":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"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 "},{"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"},{"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":"Advanced Course in Programming\nIntroduction to Programming"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement the function using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function. That alone will likely not be enough, however. You will need something else, too, to make sure the course names are unique."}]}]},{"type":"element","tagName":"h2","properties":{"id":"filter","style":"position:relative;"},"children":[{"type":"text","value":"filter"},{"type":"element","tagName":"a","properties":{"href":"#filter","ariaLabel":"filter 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 built-in Python function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" is similar to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function, but, as the name implies, it doesn't take all the items from the source. Instead, it filters them with a criterion function, which is passed as an argument. If the criterion function returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":", the item is selected."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's look at an example using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":":"}]},{"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":"integers "},{"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"10"}]},{"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":"14"}]},{"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":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\neven_numbers "},{"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":"filter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"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":"2"}]},{"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" integers"},{"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":" number "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" even_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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n6\n4\n10\n14"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It might make the above example a bit clearer if we used a named function instead:"}]},{"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":"is_it_even"}]},{"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":" "},{"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":"if"}]},{"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":"2"}]},{"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":"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":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\nintegers "},{"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"10"}]},{"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":"14"}]},{"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":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\neven_numbers "},{"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":"filter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"is_it_even"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" integers"},{"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":" number "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" even_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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"These two programs are functionally completely identical. It is mostly a matter of opinion which you consider the better approach."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at another filtering example. This program models fishes, and selects only those which weigh at least 1000 grams:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Fish"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The class models a fish of a certain species and weight \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" species"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" weight"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"species "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" species\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weight "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" weight\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":"__repr__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"species"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weight"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" g.)\""}]}]},{"type":"text","value":"\n\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 f1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pike\""}]},{"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":"1870"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n f2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Perch\""}]},{"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":"763"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n f3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pike\""}]},{"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":"3410"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n f4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Cod\""}]},{"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":"2449"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n f5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Roach\""}]},{"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":"210"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n fishes "},{"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":"text","value":"f1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n over_a_kilo "},{"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":"filter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" fish "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weight "},{"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":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fishes"},{"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":" fish "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" over_a_kilo"},{"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":"fish"},{"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":"Pike (1870 g.)\nPike (3410 g.)\nCod (2449 g.)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We could just as well use a list comprehension and achieve the same result:"}]},{"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":"over_a_kilo "},{"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":"text","value":"fish "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" fish "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fishes "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weight "},{"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":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-return-value-of-filter-is-an-iterator","style":"position:relative;"},"children":[{"type":"text","value":"The return value of filter is an iterator"},{"type":"element","tagName":"a","properties":{"href":"#the-return-value-of-filter-is-an-iterator","ariaLabel":"the return value of filter is an iterator 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 "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" function resembles the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" function in also that it returns an "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"iterator"}]},{"type":"text","value":". There are situations where you should be especially careful with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" as iterators can only be traversed once. So, trying to print out the collection of large fishes twice will not work quite as straightforwardly as you might think:"}]},{"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":"f1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pike\""}]},{"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":"1870"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nf2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Perch\""}]},{"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":"763"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nf3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pike\""}]},{"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":"3410"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nf4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Cod\""}]},{"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":"2449"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nf5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Roach\""}]},{"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":"210"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nfishes "},{"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":"text","value":"f1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f5"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nover_a_kilo "},{"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":"filter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" fish "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weight "},{"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":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fishes"},{"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":" fish "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" over_a_kilo"},{"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":"fish"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"print the same again:\""}]},{"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":" Fish "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" over_a_kilo"},{"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":"Fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This would print out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pike (1870 g.)\nPike (3410 g.)\nCod (2449 g.)\nprint the same again:"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you need to go through the contents of a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" iterator more than once, you could convert the result into 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":"fishes "},{"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":"text","value":"f1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" f5"},{"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":"# convert the return value of the filter function into a list"}]},{"type":"text","value":"\nover_a_kilo "},{"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":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"filter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" fish "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" fish"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weight "},{"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":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" fishes"},{"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":"Filtering attempts","tmcname":"part12-12_filtering_attempts"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise we will continue with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CourseAttempt"}]},{"type":"text","value":" class."}]},{"type":"element","tagName":"h2","properties":{"id":"accepted-attempts","style":"position:relative;"},"children":[{"type":"text","value":"Accepted attempts"},{"type":"element","tagName":"a","properties":{"href":"#accepted-attempts","ariaLabel":"accepted attempts 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"accepted(attempts: list)"}]},{"type":"text","value":" which takes a list of CourseAttempt objects as its argument. The function should return a new list of CourseAttempt objects, including only those items from the original list whose grade is at least 1."}]},{"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":"s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Introduction to Programming\""}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olivia C. Objective\""}]},{"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":"\"Introduction to Programming\""}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Advanced Course in Programming\""}]},{"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":"0"}]},{"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":" attempt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" accepted"},{"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":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"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 "},{"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":"attempt"},{"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":"Peter Python, grade for the course Introduction to Programming 3\nOlivia C. Objective grade for the course Introduction to Programming 5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement the function using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" function."}]},{"type":"element","tagName":"h2","properties":{"id":"attempts-with-grade","style":"position:relative;"},"children":[{"type":"text","value":"Attempts with grade"},{"type":"element","tagName":"a","properties":{"href":"#attempts-with-grade","ariaLabel":"attempts with grade 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"attempts_with_grade(attempts: list, grade: int)"}]},{"type":"text","value":" which takes a list of CourseAttempt objects and an integer as its arguments. The function should return a new list containing only those CourseAttempt objects from the original list whose grade matches the second argument."}]},{"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":"s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Introduction to Programming\""}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olivia C. Objective\""}]},{"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":"\"Introduction to Programming\""}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Introduction to AI\""}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olivia C. Objective\""}]},{"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":"\"Data Structures and Algorithms\""}]},{"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":"3"}]},{"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":" attempt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" attempts_with_grade"},{"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":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s4"},{"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":" "},{"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","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":"attempt"},{"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":"Peter Python, grade for the course Introduction to Programming 3\nPeter Python, grade for the course Introduction to AI 3\nOlivia C. Objective, grade for the course Data Structures and Algorithms 3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement the function using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" function."}]},{"type":"element","tagName":"h2","properties":{"id":"students-who-passed-the-course","style":"position:relative;"},"children":[{"type":"text","value":"Students who passed the course"},{"type":"element","tagName":"a","properties":{"href":"#students-who-passed-the-course","ariaLabel":"students who passed the course 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"passed_students(attempts: list, course: str)"}]},{"type":"text","value":" which takes a list of CourseAttempt objects and a course name as its arguments. The function should return an "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"alphabetically ordered"}]},{"type":"text","value":" list of names of those students who passed the course, i.e. their grade for the given course was higher than 0."}]},{"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":"s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Introduction to Programming\""}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olivia C. Objective\""}]},{"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":"\"Introduction to AI\""}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter 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":"\"Introduction to AI\""}]},{"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jack Java\""}]},{"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":"\"Introduction to AI\""}]},{"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":"3"}]},{"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":" attempt "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" passed_students"},{"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":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s4"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduction to AI\""}]},{"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":"attempt"},{"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":"Jack Java\nOlivia C. Objective"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement the function using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" functions."}]}]},{"type":"element","tagName":"h2","properties":{"id":"reduce","style":"position:relative;"},"children":[{"type":"text","value":"reduce"},{"type":"element","tagName":"a","properties":{"href":"#reduce","ariaLabel":"reduce 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":"A third cornerstone function in this introduction to functional programming principles is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":", from the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"functools"}]},{"type":"text","value":" module. As the name implies, its purpose is to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" the items in a series into a single value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" function starts with an operation and an initial value. It performs the given operation on each item in the series in turn, so that the value changes at each step. Once all items have been processed, the resulting value is returned."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We have done summation of lists of integers in different ways before, but here we have an example with the help of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" function. Notice the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement; in Python versions 3 and higher it is necessary to access the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" function. In older Python versions the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement was not needed, so you may come across examples without it online."}]},{"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":"from"}]},{"type":"text","value":" functools "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"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":"2"}]},{"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":"3"}]},{"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":"1"}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nsum_of_numbers "},{"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":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" reduced_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" reduced_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" item"},{"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":" "},{"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\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":"sum_of_numbers"},{"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":"11"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's take a closer look at what's happening here. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" function takes three arguments: a function, a series of items, and an initial value. In this case, the series is a list of integers, and as we are calculating a sum, a suitable initial value is zero."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The first argument is a function, which represents the operation we want to perform on each item. Here the function is an anonymous lambda 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","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" reduced_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" reduced_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" item"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This function takes two arguments: the current reduced value and the item whose turn it is to be processed. These are used to calculate a new value for the reduced value. In this case the new value is the sum of the old value and the current item."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It may be easier to comprehend what the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" function actually does if we use a normal named function instead of a lambda function. That way we can also include helpful printouts:"}]},{"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":"from"}]},{"type":"text","value":" functools "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"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":"2"}]},{"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":"3"}]},{"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":"1"}]},{"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":"5"}]},{"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":"# a helper function for reduce, adds one value to the current reduced sum"}]},{"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":"sum_helper"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"reduced_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" item"},{"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\"the reduced sum is now "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"reduced_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", next item 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":"item"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the new reduced sum is the old sum + the next item"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" reduced_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" item\n\nsum_of_numbers "},{"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":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sum_helper"},{"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":" "},{"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\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":"sum_of_numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program prints out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"the reduced sum is now 0, next item is 2\nthe reduced sum is now 2, next item is 3\nthe reduced sum is now 5, next item is 1\nthe reduced sum is now 6, next item is 5\n11"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First, the function takes care of the item with value 2. To begin with, the reduced sum is 0, which is the initial value passed to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" function. The function calculates and returns the sum of these two: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0 + 2 = 2"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is the value stored in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduced_sum"}]},{"type":"text","value":" as the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" function processes the next item on the list, with value 3. The function calculates and returns the sum of these two: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 + 3 = 5"}]},{"type":"text","value":". This result is then used when processing the next item, and so forth, and so forth."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, summation is simple, as there is even the built-in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" function for this purpose. But how about multiplication? Only minor changes are needed to create a reduced product:"}]},{"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":"from"}]},{"type":"text","value":" functools "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"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":"2"}]},{"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":"2"}]},{"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":"4"}]},{"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":"3"}]},{"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":"5"}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nproduct_of_list "},{"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":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" product "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" item"},{"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":" "},{"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\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":"product_of_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":"480"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As we are dealing with multiplication the initial value is not zero. Instead, we use 1. What would happen if we used 0 as the initial value?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Above we have dealt largely with integers, but "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" can all handle a collection of objects of any type."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As an example, let's generate a sum total of the balances of all accounts in a bank, with the help of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" account_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__account_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" account_number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\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":"deposit"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" amount"},{"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":"float"}]},{"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":"if"}]},{"type":"text","value":" amount "},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" amount\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":"get_balance"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance\n\na1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123456\""}]},{"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":"\"Randy Riches\""}]},{"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":"5000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12321\""}]},{"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 Pauper\""}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"223344\""}]},{"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":"\"Mary Millionaire \""}]},{"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":"1000000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\naccounts "},{"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":"text","value":"a1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a3"},{"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":"from"}]},{"type":"text","value":" functools "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"reduce"}]},{"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":"balance_sum_helper"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"balance_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" account"},{"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":" balance_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_balance"},{"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\nbalances_total "},{"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":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"balance_sum_helper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" accounts"},{"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":"0"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The total of the bank's balances:\""}]},{"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":"balances_total"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program would print out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The total of the bank's balances:\n1005001"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balance_sum_helper"}]},{"type":"text","value":" function grabs the balance of each bank account, with the method dedicated for the purpose in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" class definition:"}]},{"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":"balance_sum_helper"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"balance_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" account"},{"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":" balance_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_balance"},{"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":"text-box","properties":{"variant":"hint","name":"Reduce without an initial value"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You do not need to always pass a third argument to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" function. For instance, summation would work just as well "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"without"}]},{"type":"text","value":" the initial value:"}]},{"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":"2"}]},{"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":"3"}]},{"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":"1"}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nsum_of_numbers "},{"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":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"lambda"}]},{"type":"text","value":" reduced_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" reduced_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" item"},{"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\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":"sum_of_numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the initial value is left out, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" takes the first item in the list as the initial value and starts reducing from the second item onwards."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" if the items in the series are of a different type than the intended reduced result, the thrd argument is mandatory. The example with the bank accounts would not work without the initial value. That is, trying 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":"text","value":"balances_total "},{"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":"reduce"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"balance_sum_helper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" accounts"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"would produce an error:"}]},{"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":"TypeError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" unsupported operand "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"s"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"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":"'BankAccount'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'int'"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the above case, when "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" tries to execute the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balance_sum_helper"}]},{"type":"text","value":" function for the first time, the arguments it uses are the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"two first items in the list"}]},{"type":"text","value":", which are both of type BankAccount. Specifically, the value assigned to the parameter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balance_sum"}]},{"type":"text","value":" is the first item in the list. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balance_sum_helper"}]},{"type":"text","value":" function tries to add an integer value to it, but adding an integer directly to a BankAccount object is not a supported operation."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Study credits","tmcname":"part12-13_credits"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise we will work with a slightly modified version of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CourseAttempt"}]},{"type":"text","value":" class. The name of the student is omitted, but the number of credits is included. The class works as follows:"}]},{"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":"attempt "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Data Structures and Algorithms\""}]},{"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":"3"}]},{"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":"10"}]},{"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":"attempt"},{"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":"attempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"course_name"},{"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":"attempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits"},{"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":"attempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"grade"},{"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":"Data Structures and Algorithms (10 cr) grade 3\nData Structures and Algorithms\n10\n3"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-sum-of-all-credits","style":"position:relative;"},"children":[{"type":"text","value":"The sum of all credits"},{"type":"element","tagName":"a","properties":{"href":"#the-sum-of-all-credits","ariaLabel":"the sum of all credits 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":"Please implement a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum_of_all_credits"}]},{"type":"text","value":" which takes a list of course attempts as its argument. The function sums up the total number of study credits covered by the courses. It should work 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":"text","value":"s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduction to Programming\""}]},{"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":"5"}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Advanced Course in Programming\""}]},{"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":"4"}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Data Structures and Algorithms\""}]},{"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":"3"}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncredit_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sum_of_all_credits"},{"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":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"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":"credit_sum"},{"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":"20"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement the function using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" function."}]},{"type":"element","tagName":"h2","properties":{"id":"the-sum-of-passed-credits","style":"position:relative;"},"children":[{"type":"text","value":"The sum of passed credits"},{"type":"element","tagName":"a","properties":{"href":"#the-sum-of-passed-credits","ariaLabel":"the sum of passed credits 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":"Please implement a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum_of_passed_credits"}]},{"type":"text","value":" which takes a list of course attempts as its argument. The function sums up the credits for the course attempts with grade 1 or above. It should work 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":"text","value":"s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduction to Programming\""}]},{"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":"5"}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Advanced Course in Programming\""}]},{"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":"0"}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Data Structures and Algorithms\""}]},{"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":"3"}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncredit_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sum_of_passed_credits"},{"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":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"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":"credit_sum"},{"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":"15"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement the function using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" functions."}]},{"type":"element","tagName":"h2","properties":{"id":"average-grade-for-passed-courses","style":"position:relative;"},"children":[{"type":"text","value":"Average grade for passed courses"},{"type":"element","tagName":"a","properties":{"href":"#average-grade-for-passed-courses","ariaLabel":"average grade for passed courses 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":"Please implement a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"average"}]},{"type":"text","value":" which takes a list of course attempts as its argument. The function calculates the average grade for the course attempts with grade 1 or above. It should work 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":"text","value":"s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduction to Programming\""}]},{"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":"5"}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Advanced Course in Programming\""}]},{"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":"0"}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ns3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CourseAttempt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Data Structures and Algorithms\""}]},{"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":"3"}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nag "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" average"},{"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":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"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":"ag"},{"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.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement the function using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reduce"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"filter"}]},{"type":"text","value":" functions. NB: the exercise asks for a simple mean value, not a weighted average."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"While working on this exercise, it is likely worth remembering that "},{"type":"element","tagName":"a","properties":{"href":"/part-12/3-functional-programming#the-return-value-of-filter-is-an-iterator"},"children":[{"type":"text","value":"the return value of filter is an iterator"}]},{"type":"text","value":"."}]}]}]},"html":"

    After this section

      \n
    • You will know what functional programming means
    • \n
    • You will be able to make use of the functions map, reduce and filter in your own programs
    • \n

    Functional programming refers to a programming paradigm which avoids changes in program state as much as possible. Variables are generally avoided. Instead, chains of function calls form the backbone of the program.

    Lambda expressions and different types of comprehensions are common techniques in the functional programming style, as they let you process data without storing it in variables, so that the state of the program does not change. For example, a lambda expression is for all intents and purposes a function, but we do not need to store a named reference to it anywhere.

    As mentioned above, functional programming is a programming paradigm, or a style of programming. There are many different programming paradigms, and we've already come across some of them:

      \n
    • imperative programming, where the program consists of a sequence of commands which is executed in order
    • \n
    • procedural programming, where the program is grouped into procedures or sub-programs
    • \n
    • object-oriented programming, where the program and its state is stored in objects defined in classes.
    • \n

    There are differing opinions on the divisions between the different paradigms; for example, some maintain that imperative and procedural programming mean the same thing, while others place imperative programming as an umbrella term which covers both procedural and object-oriented programming. Th terminology and divisions are not that important, and neither is strictly sticking to one or the other paradigm, but it is important to understand that such different approaches exist, as they affect the choices programmers make.

    Many programming languages are designed with one or the other programming paradigm in mind, but Python is a rather versatile programming language, and allows for following several different programming paradigms, even within a single program. This lets us choose the most efficient and clear method for solving each problem.

    Let's have a look at some functional programming tools provided by Python.

    map

    The map function executes some operation on each item in an iterable series. This sounds a lot like the effect a comprehension has, but the syntax is different.

    Let's assume we have list of strings which we want to convert into a list of integers:

    str_list = [\"123\",\"-10\", \"23\", \"98\", \"0\", \"-110\"]\n\nintegers = map(lambda x : int(x), str_list)\n\nprint(integers) # this tells us the type of object we're dealing with\n\nfor number in integers:\n    print(number)

    <map object at 0x0000021A4BFA9A90>\n123\n-10\n23\n98\n0\n-110

    The general syntax for the map function is

    map(<function>, <series>)

    where function is the operation we want to execute on each item in the series.

    The map function returns an object of type map, which is iterable, and can be converted into a list:

    def capitalize(my_string: str):\n    first = my_string[0]\n    first = first.upper()\n    return first + my_string[1:]\n\ntest_list = [\"first\", \"second\", \"third\", \"fourth\"]\n\ncapitalized = map(capitalize, test_list)\n\ncapitalized_list = list(capitalized)\nprint(capitalized_list)

    ['First', 'Second', 'Third', 'Fourth']

    As you can see from the examples above, the map function accepts both an anonymous lambda function and a named function defined with the def keyword.

    We could achieve the same result with a list comprehension:

    def capitalize(my_string: str):\n    first = my_string[0]\n    first = first.upper()\n    return first + my_string[1:]\n\ntest_list = [\"first\", \"second\", \"third\", \"fourth\"]\n\ncapitalized_list = [capitalize(item) for item in test_list]\nprint(capitalized_list)

    ...or we could go through the original list with a for loop and save the processed items in a new list with the append method. Typically, in programming there are many different solutions to each problem. There are rarely any absolutely right or wrong answers. Knowing many different approaches helps you choose the most appropriate one for each situation, or one that best suits your own tastes.

    It is worth pointing out that the map function does not return a list, but an iterator object of type map. An iterator behaves in many ways like a list, but there are exceptions, as can be seen in the following example:

    def capitalize(my_string: str):\n    first = my_string[0]\n    first = first.upper()\n    return first + my_string[1:]\n\ntest_list = [\"first\", \"second\", \"third\", \"fourth\"]\n\n# store the return value from the map function\ncapitalized = map(capitalize, test_list)\n\nfor word in capitalized:\n  print(word)\n\nprint(\"print the same again:\")\nfor word in capitalized:\n  print(word)

    This would print out the following:

    First\nSecond\nThird\nFourth\nprint the same again:

    Above we tried to print out the contents of the map iterator twice, but the second attempt produced no printout. The reason is that map is an iterator; passing through it with a for loop \"depletes\" it, much like a generator is depleted once its maximum value is reached. Once the items in the iterator have been traversed with a for loop, there is nothing left to go through.

    If you need to go through the contents of a map iterator more than once, you could, for example, convert the map into a list:

    test_list = [\"first\", \"second\", \"third\", \"fourth\"]\n\n# convert the return value of the map function into a list\ncapitalized = list(map(capitalize, test_list))\n\nfor word in capitalized:\n  print(word)\n\nprint(\"print the same again:\")\nfor word in capitalized:\n  print(word)

    First\nSecond\nThird\nFourth\nprint the same again:\nFirst\nSecond\nThird\nFourth

    The map function and your own classes

    You can naturally also process instances of your own classes with the map function. There are no special gimmicks involved, as you can see in the example below:

    class BankAccount:\n    def __init__(self, account_number: str, name: str, balance: float):\n        self.__account_number = account_number\n        self.name = name\n        self.__balance = balance\n\n    def deposit(self, amount: float):\n        if amount > 0:\n            self.__balance += amount\n\n    def get_balance(self):\n        return self.__balance\n\na1 = BankAccount(\"123456\", \"Randy Riches\", 5000)\na2 = BankAccount(\"12321\", \"Paul Pauper\", 1)\na3 = BankAccount(\"223344\", \"Mary Millionaire \", 1000000)\n\naccounts = [a1, a2, a3]\n\nclients = map(lambda t: t.name, accounts)\nfor name in clients:\n  print(name)\n\nbalances = map(lambda t: t.get_balance(), accounts)\nfor balance in balances:\n  print(balance)

    Randy Riches\nPaul Pauper\nMary Millionaire\n5000\n1\n1000000

    Here we first collect the names of the account holders with the map function. An anonymous lambda function is used to retrieve the value of the name attribute from each BankAccount object:

    clients = map(lambda t: t.name, accounts)

    Similarly, the balance of each BankAccount is collected. The lambda function looks a bit different, because the balance is retrieved with a method call, not from the attribute directly:

    balances = map(lambda t: t.get_balance(), accounts)

    The exercise template contains the class definition for a CourseAttempt. It works like this:

    attempt = CourseAttempt(\"Peter Python\", \"Introduction to Programming\", 5)\nprint(attempt.student_name)\nprint(attempt.course_name)\nprint(attempt.grade)\nprint(attempt)

    Peter Python\nIntroduction to Programming\n5\nPeter Python, grade for the course Introduction to Programming 5

    Names of students

    Please write a function named names_of_students(attempts: list) which takes a list of CourseAttempt objects as its argument. The function should return a new list with the names of the students who have attempted the course.

    s1 = CourseAttempt(\"Peter Python\", \"Introduction to Programming\", 3)\ns2 = CourseAttempt(\"Olivia C. Objective\", \"Introduction to Programming\", 5)\ns3 = CourseAttempt(\"Peter Python\", \"Advanced Course in Programming\", 2)\n\nfor name in names_of_students([s1, s2, s3]):\n    print(name)

    Peter Python\nOlivia C. Objective\nPeter Python

    Please implement the function using the map function.

    Courses

    Please write a function named course_names(attempts: list) which takes a list of CourseAttempt objects as its argument. The function should return a new list containing the names of the courses on the original list in alphabetical order. Each course name should appear only once on the list.

    s1 = CourseAttempt(\"Peter Python\", \"Introduction to Programming\", 3)\ns2 = CourseAttempt(\"Olivia C. Objective\", \"Introduction to Programming\", 5)\ns3 = CourseAttempt(\"Peter Python\", \"Advanced Course in Programming\", 2)\n\nfor name in course_names([s1, s2, s3]):\n    print(name)

    Advanced Course in Programming\nIntroduction to Programming

    Please implement the function using the map function. That alone will likely not be enough, however. You will need something else, too, to make sure the course names are unique.

    filter

    The built-in Python function filter is similar to the map function, but, as the name implies, it doesn't take all the items from the source. Instead, it filters them with a criterion function, which is passed as an argument. If the criterion function returns True, the item is selected.

    Let's look at an example using filter:

    integers = [1, 2, 3, 5, 6, 4, 9, 10, 14, 15]\n\neven_numbers = filter(lambda number: number % 2 == 0, integers)\n\nfor number in even_numbers:\n    print(number)

    2\n6\n4\n10\n14

    It might make the above example a bit clearer if we used a named function instead:

    def is_it_even(number: int):\n    if number % 2 == 0:\n        return True\n    return False\n\nintegers = [1, 2, 3, 5, 6, 4, 9, 10, 14, 15]\n\neven_numbers = filter(is_it_even, integers)\n\nfor number in even_numbers:\n    print(number)

    These two programs are functionally completely identical. It is mostly a matter of opinion which you consider the better approach.

    Let's have a look at another filtering example. This program models fishes, and selects only those which weigh at least 1000 grams:

    class Fish:\n    \"\"\" The class models a fish of a certain species and weight \"\"\"\n    def __init__(self, species: str, weight: int):\n        self.species = species\n        self.weight = weight\n\n    def __repr__(self):\n        return f\"{self.species} ({self.weight} g.)\"\n\nif __name__ == \"__main__\":\n    f1 = Fish(\"Pike\", 1870)\n    f2 = Fish(\"Perch\", 763)\n    f3 = Fish(\"Pike\", 3410)\n    f4 = Fish(\"Cod\", 2449)\n    f5 = Fish(\"Roach\", 210)\n\n    fishes = [f1, f2, f3, f4, f5]\n\n    over_a_kilo = filter(lambda fish : fish.weight >= 1000, fishes)\n\n    for fish in over_a_kilo:\n        print(fish)

    Pike (1870 g.)\nPike (3410 g.)\nCod (2449 g.)

    We could just as well use a list comprehension and achieve the same result:

    over_a_kilo = [fish for fish in fishes if fish.weight >= 1000]

    The return value of filter is an iterator

    The filter function resembles the map function in also that it returns an iterator. There are situations where you should be especially careful with filter as iterators can only be traversed once. So, trying to print out the collection of large fishes twice will not work quite as straightforwardly as you might think:

    f1 = Fish(\"Pike\", 1870)\nf2 = Fish(\"Perch\", 763)\nf3 = Fish(\"Pike\", 3410)\nf4 = Fish(\"Cod\", 2449)\nf5 = Fish(\"Roach\", 210)\n\nfishes = [f1, f2, f3, f4, f5]\n\nover_a_kilo = filter(lambda fish : fish.weight >= 1000, fishes)\n\nfor fish in over_a_kilo:\n    print(fish)\n\nprint(\"print the same again:\")\n\nfor Fish in over_a_kilo:\n    print(Fish)

    This would print out the following:

    Pike (1870 g.)\nPike (3410 g.)\nCod (2449 g.)\nprint the same again:

    If you need to go through the contents of a filter iterator more than once, you could convert the result into a list:

    fishes = [f1, f2, f3, f4, f5]\n\n# convert the return value of the filter function into a list\nover_a_kilo = list(filter(lambda fish : fish.weight >= 1000, fishes))

    In this exercise we will continue with the CourseAttempt class.

    Accepted attempts

    Please write a function named accepted(attempts: list) which takes a list of CourseAttempt objects as its argument. The function should return a new list of CourseAttempt objects, including only those items from the original list whose grade is at least 1.

    s1 = CourseAttempt(\"Peter Python\", \"Introduction to Programming\", 3)\ns2 = CourseAttempt(\"Olivia C. Objective\", \"Introduction to Programming\", 5)\ns3 = CourseAttempt(\"Peter Python\", \"Advanced Course in Programming\", 0)\n\nfor attempt in accepted([s1, s2, s3]):\n    print(attempt)

    Peter Python, grade for the course Introduction to Programming 3\nOlivia C. Objective grade for the course Introduction to Programming 5

    Please implement the function using the filter function.

    Attempts with grade

    Please write a function named attempts_with_grade(attempts: list, grade: int) which takes a list of CourseAttempt objects and an integer as its arguments. The function should return a new list containing only those CourseAttempt objects from the original list whose grade matches the second argument.

    s1 = CourseAttempt(\"Peter Python\", \"Introduction to Programming\", 3)\ns2 = CourseAttempt(\"Olivia C. Objective\", \"Introduction to Programming\", 5)\ns3 = CourseAttempt(\"Peter Python\", \"Introduction to AI\", 3)\ns4 = CourseAttempt(\"Olivia C. Objective\", \"Data Structures and Algorithms\", 3)\n\nfor attempt in attempts_with_grade([s1, s2, s3, s4], 3):\n    print(attempt)

    Peter Python, grade for the course Introduction to Programming 3\nPeter Python, grade for the course Introduction to AI 3\nOlivia C. Objective, grade for the course Data Structures and Algorithms 3

    Please implement the function using the filter function.

    Students who passed the course

    Please write a function named passed_students(attempts: list, course: str) which takes a list of CourseAttempt objects and a course name as its arguments. The function should return an alphabetically ordered list of names of those students who passed the course, i.e. their grade for the given course was higher than 0.

    s1 = CourseAttempt(\"Peter Python\", \"Introduction to Programming\", 3)\ns2 = CourseAttempt(\"Olivia C. Objective\", \"Introduction to AI\", 5)\ns3 = CourseAttempt(\"Peter Python\", \"Introduction to AI\", 0)\ns4 = CourseAttempt(\"Jack Java\", \"Introduction to AI\", 3)\n\nfor attempt in passed_students([s1, s2, s3, s4], \"Introduction to AI\"):\n    print(attempt)

    Jack Java\nOlivia C. Objective

    Please implement the function using the filter and map functions.

    reduce

    A third cornerstone function in this introduction to functional programming principles is reduce, from the functools module. As the name implies, its purpose is to reduce the items in a series into a single value.

    The reduce function starts with an operation and an initial value. It performs the given operation on each item in the series in turn, so that the value changes at each step. Once all items have been processed, the resulting value is returned.

    We have done summation of lists of integers in different ways before, but here we have an example with the help of the reduce function. Notice the import statement; in Python versions 3 and higher it is necessary to access the reduce function. In older Python versions the import statement was not needed, so you may come across examples without it online.

    from functools import reduce\n\nmy_list = [2, 3, 1, 5]\n\nsum_of_numbers = reduce(lambda reduced_sum, item: reduced_sum + item, my_list, 0)\n\nprint(sum_of_numbers)

    11

    Let's take a closer look at what's happening here. The reduce function takes three arguments: a function, a series of items, and an initial value. In this case, the series is a list of integers, and as we are calculating a sum, a suitable initial value is zero.

    The first argument is a function, which represents the operation we want to perform on each item. Here the function is an anonymous lambda function:

    lambda reduced_sum, item: reduced_sum + item

    This function takes two arguments: the current reduced value and the item whose turn it is to be processed. These are used to calculate a new value for the reduced value. In this case the new value is the sum of the old value and the current item.

    It may be easier to comprehend what the reduce function actually does if we use a normal named function instead of a lambda function. That way we can also include helpful printouts:

    from functools import reduce\n\nmy_list = [2, 3, 1, 5]\n\n# a helper function for reduce, adds one value to the current reduced sum\ndef sum_helper(reduced_sum, item):\n  print(f\"the reduced sum is now {reduced_sum}, next item is {item}\")\n  # the new reduced sum is the old sum + the next item\n  return reduced_sum + item\n\nsum_of_numbers = reduce(sum_helper, my_list, 0)\n\nprint(sum_of_numbers)

    The program prints out:

    the reduced sum is now 0, next item is 2\nthe reduced sum is now 2, next item is 3\nthe reduced sum is now 5, next item is 1\nthe reduced sum is now 6, next item is 5\n11

    First, the function takes care of the item with value 2. To begin with, the reduced sum is 0, which is the initial value passed to the reduce function. The function calculates and returns the sum of these two: 0 + 2 = 2.

    This is the value stored in reduced_sum as the reduce function processes the next item on the list, with value 3. The function calculates and returns the sum of these two: 2 + 3 = 5. This result is then used when processing the next item, and so forth, and so forth.

    Now, summation is simple, as there is even the built-in sum function for this purpose. But how about multiplication? Only minor changes are needed to create a reduced product:

    from functools import reduce\n\nmy_list = [2, 2, 4, 3, 5, 2]\n\nproduct_of_list = reduce(lambda product, item: product * item, my_list, 1)\n\nprint(product_of_list)

    480

    As we are dealing with multiplication the initial value is not zero. Instead, we use 1. What would happen if we used 0 as the initial value?

    Above we have dealt largely with integers, but map, filter and reduce can all handle a collection of objects of any type.

    As an example, let's generate a sum total of the balances of all accounts in a bank, with the help of reduce:

    class BankAccount:\n    def __init__(self, account_number: str, name: str, balance: float):\n        self.__account_number = account_number\n        self.name = name\n        self.__balance = balance\n\n    def deposit(self, amount: float):\n        if amount > 0:\n            self.__balance += amount\n\n    def get_balance(self):\n        return self.__balance\n\na1 = BankAccount(\"123456\", \"Randy Riches\", 5000)\na2 = BankAccount(\"12321\", \"Paul Pauper\", 1)\na3 = BankAccount(\"223344\", \"Mary Millionaire \", 1000000)\n\naccounts = [a1, a2, a3]\n\nfrom functools import reduce\n\ndef balance_sum_helper(balance_sum, account):\n  return balance_sum + account.get_balance()\n\nbalances_total = reduce(balance_sum_helper, accounts, 0)\n\nprint(\"The total of the bank's balances:\")\nprint(balances_total)

    This program would print out:

    The total of the bank's balances:\n1005001

    The balance_sum_helper function grabs the balance of each bank account, with the method dedicated for the purpose in the BankAccount class definition:

    def balance_sum_helper(balance_sum, account):\n  return balance_sum + account.get_balance()

    You do not need to always pass a third argument to the reduce function. For instance, summation would work just as well without the initial value:

    my_list = [2, 3, 1, 5]\n\nsum_of_numbers = reduce(lambda reduced_sum, item: reduced_sum + item, my_list)\n\nprint(sum_of_numbers)

    If the initial value is left out, reduce takes the first item in the list as the initial value and starts reducing from the second item onwards.

    NB: if the items in the series are of a different type than the intended reduced result, the thrd argument is mandatory. The example with the bank accounts would not work without the initial value. That is, trying this

    balances_total = reduce(balance_sum_helper, accounts)

    would produce an error:

    TypeError: unsupported operand type(s) for +: 'BankAccount' and 'int'

    In the above case, when reduce tries to execute the balance_sum_helper function for the first time, the arguments it uses are the two first items in the list, which are both of type BankAccount. Specifically, the value assigned to the parameter balance_sum is the first item in the list. The balance_sum_helper function tries to add an integer value to it, but adding an integer directly to a BankAccount object is not a supported operation.

    In this exercise we will work with a slightly modified version of the CourseAttempt class. The name of the student is omitted, but the number of credits is included. The class works as follows:

    attempt = CourseAttempt(\"Data Structures and Algorithms\", 3, 10)\nprint(attempt)\nprint(attempt.course_name)\nprint(attempt.credits)\nprint(attempt.grade)

    Data Structures and Algorithms (10 cr) grade 3\nData Structures and Algorithms\n10\n3

    The sum of all credits

    Please implement a function named sum_of_all_credits which takes a list of course attempts as its argument. The function sums up the total number of study credits covered by the courses. It should work like this:

    s1 = CourseAttempt(\"Introduction to Programming\", 5, 5)\ns2 = CourseAttempt(\"Advanced Course in Programming\", 4, 5)\ns3 = CourseAttempt(\"Data Structures and Algorithms\", 3, 10)\ncredit_sum = sum_of_all_credits([s1, s2, s3])\nprint(credit_sum)

    20

    Please implement the function using the reduce function.

    The sum of passed credits

    Please implement a function named sum_of_passed_credits which takes a list of course attempts as its argument. The function sums up the credits for the course attempts with grade 1 or above. It should work like this:

    s1 = CourseAttempt(\"Introduction to Programming\", 5, 5)\ns2 = CourseAttempt(\"Advanced Course in Programming\", 0, 4)\ns3 = CourseAttempt(\"Data Structures and Algorithms\", 3, 10)\ncredit_sum = sum_of_passed_credits([s1, s2, s3])\nprint(credit_sum)

    15

    Please implement the function using the reduce and filter functions.

    Average grade for passed courses

    Please implement a function named average which takes a list of course attempts as its argument. The function calculates the average grade for the course attempts with grade 1 or above. It should work like this:

    s1 = CourseAttempt(\"Introduction to Programming\", 5, 5)\ns2 = CourseAttempt(\"Advanced Course in Programming\", 0, 4)\ns3 = CourseAttempt(\"Data Structures and Algorithms\", 3, 10)\nag = average([s1, s2, s3])\nprint(ag)

    4.0

    Please implement the function using the reduce and filter functions. NB: the exercise asks for a simple mean value, not a weighted average.

    While working on this exercise, it is likely worth remembering that the return value of filter is an iterator.

    ","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-12/3-functional-programming.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-12/4-regular-expressions/page-data.json b/page-data/part-12/4-regular-expressions/page-data.json index 574b0ce38..5d9c7a590 100644 --- a/page-data/part-12/4-regular-expressions/page-data.json +++ b/page-data/part-12/4-regular-expressions/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-12/4-regular-expressions", - "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 know what regular expressions are"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use regular expressions in your own programs"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We have already established that Python is an excellent environment for processing text. One additional powerful tool for text processing is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"regular expressions"}]},{"type":"text","value":", often shortened as "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"regex"}]},{"type":"text","value":" or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"regexp"}]},{"type":"text","value":". They are a way of selecting and searching for strings which follow a certain pattern. This section introduces you to the basics of regular expression, but you will find much more information online, including in the Python "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/howto/regex.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"tutorial"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"what-are-regular-expressions","style":"position:relative;"},"children":[{"type":"text","value":"What are regular expressions?"},{"type":"element","tagName":"a","properties":{"href":"#what-are-regular-expressions","ariaLabel":"what are regular expressions 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":"Regular expressions are not just a Python feature. They represent, in a way, a programming language within a programming language. They are, to an extent, compatible across many different programming languages. Regular expressions have their own specific syntax. The idea is to define a collection of strings which follow certain rules."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's begin with a simple example, before diving deeper into the syntax:"}]},{"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":"import"}]},{"type":"text","value":" re\n\nwords "},{"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":"\"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":"\"Pantone\""}]},{"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":"\"Pontoon\""}]},{"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":"\"Pollute\""}]},{"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":"\"Pantheon\""}]},{"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":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" words"},{"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":"# the string should begin with \"P\" and end with \"on\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" re"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"^P.*on$\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" word"},{"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":"word"},{"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":"\"found!\""}]},{"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 found!\nPontoon found!\nPantheon found!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We need to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"re"}]},{"type":"text","value":" module in order to use regular expressions in Python. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"re"}]},{"type":"text","value":" module contains many functions for working with regular expressions. In the example above, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"search"}]},{"type":"text","value":" function takes two string arguments: the pattern string, and the target string where the pattern is looked for."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This second example looks for any numbers in a string. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"findall"}]},{"type":"text","value":" function returns a list of all the instances which match the pattern:"}]},{"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":"import"}]},{"type":"text","value":" re\n\nsentence "},{"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":"\"First, 2 !#third 44 five 678xyz962\""}]},{"type":"text","value":"\n\nnumbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" re"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"findall"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\d+\""}]},{"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":"\n\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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n44\n678\n962"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-syntax-of-regular-expressions","style":"position:relative;"},"children":[{"type":"text","value":"The syntax of regular expressions"},{"type":"element","tagName":"a","properties":{"href":"#the-syntax-of-regular-expressions","ariaLabel":"the syntax of regular expressions 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":"Let's get familiar with the basic syntax of regular expressions. Most of the following examples make use of this testing program:"}]},{"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":"import"}]},{"type":"text","value":" re\n\nexpression "},{"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 an expression: \""}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n input_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":"if"}]},{"type":"text","value":" input_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":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" re"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"expression"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" input_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 "},{"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":"\"Found!\""}]},{"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":"else"}]},{"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":"\"Not found.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h3","properties":{"id":"alternate-substrings","style":"position:relative;"},"children":[{"type":"text","value":"Alternate substrings"},{"type":"element","tagName":"a","properties":{"href":"#alternate-substrings","ariaLabel":"alternate substrings 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 vertical bar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"|"}]},{"type":"text","value":", also called the pipe character, allows you to match alternate substrings. Its significance is thus "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"or"}]},{"type":"text","value":". For example, the expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"911|112"}]},{"type":"text","value":" matches strings which include either the substring "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"911"}]},{"type":"text","value":" or the substring "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"112"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example with the testing program:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in an expression: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"aa|ee|ii"}]},{"type":"text","value":"\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"aardvark"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"feelings"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"radii"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"smooch"}]},{"type":"text","value":"\nNot found.\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"continuum"}]},{"type":"text","value":"\nNot found."}]}]},{"type":"element","tagName":"h3","properties":{"id":"groups-of-characters","style":"position:relative;"},"children":[{"type":"text","value":"Groups of characters"},{"type":"element","tagName":"a","properties":{"href":"#groups-of-characters","ariaLabel":"groups of characters 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":"Square brackets are used to signify groups of accepted characters. For example, the expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[aeio]"}]},{"type":"text","value":" would match all strings which contain any of the characters a, e, i, or o."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A dash is also allowed for matching ranges of characters. For example, the expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[0-68a-d]"}]},{"type":"text","value":" would match all strings which contain a digit between 0 and 6, or an eight, or a character between a and d. In this notation all ranges are "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inclusive"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Combining two sets of brackets lets you match two consecutive characters. For example, the expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[1-3][0-9]"}]},{"type":"text","value":" would match any two digit number between 10 and 39, inclusive."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example with the testing program:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in an expression: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"[C-FRSO]"}]},{"type":"text","value":"\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"C"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"E"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"G"}]},{"type":"text","value":"\nNot found.\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"R"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"O"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"T"}]},{"type":"text","value":"\nNot found."}]}]},{"type":"element","tagName":"h3","properties":{"id":"repeated-matches","style":"position:relative;"},"children":[{"type":"text","value":"Repeated matches"},{"type":"element","tagName":"a","properties":{"href":"#repeated-matches","ariaLabel":"repeated matches 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":"Any part of an expression can be repeated with the following operators:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" repeats for any number of times, including zero"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" repeats for any number of times, but at least once"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"{m}"}]},{"type":"text","value":" repeats for exactly "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"m"}]},{"type":"text","value":" times"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"These operators work on the part of the expression immediately preceding the operator. For example, the expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ba+b"}]},{"type":"text","value":" would match the substrings "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bab"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"baab"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"baaaaaaaaaaab"}]},{"type":"text","value":", among others. The expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"A[BCDE]*Z"}]},{"type":"text","value":" would match the substrings "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"AZ"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ADZ"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ABCDEBCDEBCDEZ"}]},{"type":"text","value":", among others."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example with the testing program:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in an expression: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1[234]*5"}]},{"type":"text","value":"\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"15"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"125"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"145"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"12342345"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"126"}]},{"type":"text","value":"\nNot found.\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"165"}]},{"type":"text","value":"\nNot found."}]}]},{"type":"element","tagName":"h3","properties":{"id":"other-special-characters","style":"position:relative;"},"children":[{"type":"text","value":"Other special characters"},{"type":"element","tagName":"a","properties":{"href":"#other-special-characters","ariaLabel":"other special characters 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":"A dot is a wildcard character which can match any single character. For example, the expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"c...o"}]},{"type":"text","value":" would match any five character substring beginning with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"c"}]},{"type":"text","value":" and ending with an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"o"}]},{"type":"text","value":", such as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"c-3po"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cello"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"^"}]},{"type":"text","value":" character specifies that the match must be at the beginning of the string, and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"$"}]},{"type":"text","value":" specifies that the match must be at the end of the string. These can also be used to exclude from the matches any other characters than those specified:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in an expression: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"^[123]*$"}]},{"type":"text","value":"\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nNot found.\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1221"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"333333333"}]},{"type":"text","value":"\nFound!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sometimes you need to match for the special characters reserved for regular expression syntax. The backslash "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\\"}]},{"type":"text","value":" can be used to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"escape"}]},{"type":"text","value":" special characters. So, the expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1+"}]},{"type":"text","value":" matches one or more numbers "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":", but the expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1\\+"}]},{"type":"text","value":" matches the string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1+"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in an expression: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"^\\*"}]},{"type":"text","value":"\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"moi*"}]},{"type":"text","value":"\nNot found.\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"m*o*i"}]},{"type":"text","value":"\nNot found.\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"*moi"}]},{"type":"text","value":"\nFound!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Round brackets can be used to group together different parts of the expression. For example, the expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(ab)+c"}]},{"type":"text","value":" would match the substrings "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"abc"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ababc"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ababababababc"}]},{"type":"text","value":", but not the strings "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ac"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bc"}]},{"type":"text","value":", as the entire substring "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ab"}]},{"type":"text","value":" would have to appear at least once."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in an expression: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"^(jabba).*(hut)$"}]},{"type":"text","value":"\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jabba the hut"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jabba a hut"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jarjar the hut"}]},{"type":"text","value":"\nNot found.\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jabba the smut"}]},{"type":"text","value":"\nNot found."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Regular expressions","tmcname":"part12-14_regular_expressions"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here are some exercises for familiarizing yourself with regular expression syntax."}]},{"type":"element","tagName":"h2","properties":{"id":"days-of-the-week","style":"position:relative;"},"children":[{"type":"text","value":"Days of the week"},{"type":"element","tagName":"a","properties":{"href":"#days-of-the-week","ariaLabel":"days of the week 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":"Using a regular expression, please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"is_dotw(my_string: str)"}]},{"type":"text","value":". The function should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" if the string passed as an argument contains an abbreviation for a day of the week (Mon, Tue, Wed, Thu, Fri, Sat, Sun)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples 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":"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_dotw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mon\""}]},{"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_dotw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fri\""}]},{"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_dotw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tui\""}]},{"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":"True\nTrue\nFalse"}]}]},{"type":"element","tagName":"h2","properties":{"id":"check-for-vowels","style":"position:relative;"},"children":[{"type":"text","value":"Check for vowels"},{"type":"element","tagName":"a","properties":{"href":"#check-for-vowels","ariaLabel":"check for vowels 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"all_vowels(my_string: str)"}]},{"type":"text","value":" which uses a regular expression to check whether all characters in the given string are vowels."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples 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":"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":"all_vowels"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"eioueioieoieou\""}]},{"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":"all_vowels"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"autoooo\""}]},{"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":"True\nFalse"}]}]},{"type":"element","tagName":"h2","properties":{"id":"time-of-day","style":"position:relative;"},"children":[{"type":"text","value":"Time of day"},{"type":"element","tagName":"a","properties":{"href":"#time-of-day","ariaLabel":"time of day 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"time_of_day(my_string: str)"}]},{"type":"text","value":" which uses a regular expression to check whether a string in the format "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"XX:YY:ZZ"}]},{"type":"text","value":" is a valid time in the 24-hour format, with two digits each for hours, minutes and seconds."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples 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":"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":"time_of_day"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12:43:01\""}]},{"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":"time_of_day"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"AB:01:CD\""}]},{"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":"time_of_day"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"17:59:59\""}]},{"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":"time_of_day"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"33:66:77\""}]},{"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":"True\nFalse\nTrue\nFalse"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"grand-finale","style":"position:relative;"},"children":[{"type":"text","value":"Grand finale"},{"type":"element","tagName":"a","properties":{"href":"#grand-finale","ariaLabel":"grand finale 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":"To finish off this part of the material, let's work some more on objects and classes by building a slightly more extensive program. This exercise does not necessarily involve regular expressions, but the sections on "},{"type":"element","tagName":"a","properties":{"href":"/part-12/1-functions-as-arguments"},"children":[{"type":"text","value":"functions as arguments"}]},{"type":"text","value":" and "},{"type":"element","tagName":"a","properties":{"href":"/part-11/1-list-comprehensions"},"children":[{"type":"text","value":"list comprehensions"}]},{"type":"text","value":" will likely be useful."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may also find the example set in "},{"type":"element","tagName":"a","properties":{"href":"/part-10/4-application-development"},"children":[{"type":"text","value":"part 10"}]},{"type":"text","value":" helpful."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Hockey statistics","tmcname":"part12-15_hockey_statistics"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will build an application for examining hockey league statistics from the NHL in a couple of different ways."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains two JSON files: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"partial.json"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"all.json"}]},{"type":"text","value":". The first of these is mostly meant for testing. The latter contains a lot of data, as all the NHL player stats for the 2019-20 season are included in the file."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The entry for a single player is in the following format:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"json"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"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":"\"Patrik Laine\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"nationality\""}]},{"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":"\"FIN\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"assists\""}]},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"goals\""}]},{"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":"28"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"penalties\""}]},{"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":"22"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"team\""}]},{"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":"\"WPG\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"games\""}]},{"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":"68"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Both files contain a list of entries in the above format."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you need a refresher on handling JSON files, please take a look at "},{"type":"element","tagName":"a","properties":{"href":"/part-7/4-data-processing#reading-json-files"},"children":[{"type":"text","value":"part 7 of this course material"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"search-and-list","style":"position:relative;"},"children":[{"type":"text","value":"Search and list"},{"type":"element","tagName":"a","properties":{"href":"#search-and-list","ariaLabel":"search and list 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":"Please write an interactive application which first asks for the name of the file, and then offers the following functions:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"search by name for a single player's stats"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"list all the abbreviations for team names in alphabetical order"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"list all the abbreviations for countries in alphabetical order"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"These functionalities grant you one exercise point. Your application should now work as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"file name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"partial.json"}]},{"type":"text","value":"\nread the data of 14 players"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"commands:\n0 quit\n1 search for player\n2 teams\n3 countries\n4 players in team\n5 players from country\n6 most points\n7 most goals"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Travis Zajac"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Travis Zajac NJD  9 + 16 = 25\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nBUF\nCGY\nDAL\nNJD\nNYI\nOTT\nPIT\nWPG\nWSH"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nCAN\nCHE\nCZE\nSWE\nUSA"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the printout format for a player must be exactly as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Leon Draisaitl EDM 43 + 67 = 110\nConnor McDavid EDM 34 + 63 = 97\nTravis Zajac NJD  9 + 16 = 25\nMike Green EDM 3 + 8 = 11\nMarkus Granlund EDM 3 + 1 = 4\n123456789012345678901234567890123456789\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The last line in the sample above is there to help you calculate the widths of the different fields in the output; you should not print the numbers line yourself in your final submission."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The abbreviation for the team is printed from the 22nd character onwards. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" sign is the 30th character and the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sign is the 35th character. All the fields should be justified to the right edge. All whitespace is space characters."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"F-strings are probably the easiest way to achieve the required printout. The process is similar to "},{"type":"element","tagName":"a","properties":{"href":"/part-6/1-reading-files#programming-exercise-course-grading-part-3"},"children":[{"type":"text","value":"this exercise"}]},{"type":"text","value":" from part 6."}]},{"type":"element","tagName":"h2","properties":{"id":"list-players-by-points","style":"position:relative;"},"children":[{"type":"text","value":"List players by points"},{"type":"element","tagName":"a","properties":{"href":"#list-players-by-points","ariaLabel":"list players by points 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":"These two functionalities will grant you a second exercise point:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"list players in a specific team in the order of points scored, from highest to lowest. Points equals "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"goals"}]},{"type":"text","value":" + "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"assists"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"list players from a specific country in the order of points scored, from highest to lowest"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your application should now work as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"file name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"partial.json"}]},{"type":"text","value":"\nread the data of 14 players"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"commands:\n0 quit\n1 search for player\n2 teams\n3 countries\n4 players in team\n5 players from country\n6 most points\n7 most goals"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nteam: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"OTT"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Drake Batherson OTT 3 + 7 = 10\nJonathan Davidsson OTT 0 + 1 = 1\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\ncountry: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"CAN"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Jared McCann PIT 14 + 21 = 35\nTravis Zajac NJD 9 + 16 = 25\nTaylor Fedun DAL 2 + 7 = 9\nMark Jankowski CGY 5 + 2 = 7\nLogan Shaw WPG 3 + 2 = 5\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"most-successful-players","style":"position:relative;"},"children":[{"type":"text","value":"Most successful players"},{"type":"element","tagName":"a","properties":{"href":"#most-successful-players","ariaLabel":"most successful players 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":"These two functionalities will grant you a third exercise point:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"list of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" players who've scored the most points\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"if two players have the same score, whoever has scored the higher number of goals comes first"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"list of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" players who've scored the most goals\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"if two players have the same number of goals, whoever has played the lower number of games comes first"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your application should now work as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"file name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"partial.json"}]},{"type":"text","value":"\nread the data of 14 players"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"commands:\n0 quit\n1 search for player\n2 teams\n3 countries\n4 players in team\n5 players from country\n6 most points\n7 most goals"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nhow many: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Jakub Vrana WSH 25 + 27 = 52\nJared McCann PIT 14 + 21 = 35\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nhow many: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Jakub Vrana WSH 25 + 27 = 52\nJared McCann PIT 14 + 21 = 35\nJohn Klingberg DAL 6 + 26 = 32\nTravis Zajac NJD 9 + 16 = 25\nConor Sheary BUF 10 + 13 = 23\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nhow many: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Jakub Vrana WSH 25 + 27 = 52\nJared McCann PIT 14 + 21 = 35\nConor Sheary BUF 10 + 13 = 23\nTravis Zajac NJD 9 + 16 = 25\nJohn Klingberg DAL 6 + 26 = 32\nMark Jankowski CGY 5 + 2 = 7\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this part of the course."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"6a606b63-c674-5231-9e79-55d7372e582a"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will know what regular expressions are
    • \n
    • You will be able to use regular expressions in your own programs
    • \n

    We have already established that Python is an excellent environment for processing text. One additional powerful tool for text processing is regular expressions, often shortened as regex or regexp. They are a way of selecting and searching for strings which follow a certain pattern. This section introduces you to the basics of regular expression, but you will find much more information online, including in the Python tutorial.

    What are regular expressions?

    Regular expressions are not just a Python feature. They represent, in a way, a programming language within a programming language. They are, to an extent, compatible across many different programming languages. Regular expressions have their own specific syntax. The idea is to define a collection of strings which follow certain rules.

    Let's begin with a simple example, before diving deeper into the syntax:

    import re\n\nwords = [\"Python\", \"Pantone\", \"Pontoon\", \"Pollute\", \"Pantheon\"]\n\nfor word in words:\n    # the string should begin with \"P\" and end with \"on\"\n    if re.search(\"^P.*on$\", word):\n        print(word, \"found!\")

    Python found!\nPontoon found!\nPantheon found!

    We need to import the re module in order to use regular expressions in Python. The re module contains many functions for working with regular expressions. In the example above, the search function takes two string arguments: the pattern string, and the target string where the pattern is looked for.

    This second example looks for any numbers in a string. The findall function returns a list of all the instances which match the pattern:

    import re\n\nsentence = \"First, 2 !#third 44 five 678xyz962\"\n\nnumbers = re.findall(\"\\d+\", sentence)\n\nfor number in numbers:\n    print(number)

    2\n44\n678\n962

    The syntax of regular expressions

    Let's get familiar with the basic syntax of regular expressions. Most of the following examples make use of this testing program:

    import re\n\nexpression = input(\"Please type in an expression: \")\n\nwhile True:\n    input_string = input(\"Please type in a string: \")\n    if input_string == \"\":\n        break\n    if re.search(expression, input_string):\n        print(\"Found!\")\n    else:\n        print(\"Not found.\")

    Alternate substrings

    The vertical bar |, also called the pipe character, allows you to match alternate substrings. Its significance is thus or. For example, the expression 911|112 matches strings which include either the substring 911 or the substring 112.

    An example with the testing program:

    Please type in an expression: aa|ee|ii\nPlease type in a string: aardvark\nFound!\nPlease type in a string: feelings\nFound!\nPlease type in a string: radii\nFound!\nPlease type in a string: smooch\nNot found.\nPlease type in a string: continuum\nNot found.

    Groups of characters

    Square brackets are used to signify groups of accepted characters. For example, the expression [aeio] would match all strings which contain any of the characters a, e, i, or o.

    A dash is also allowed for matching ranges of characters. For example, the expression [0-68a-d] would match all strings which contain a digit between 0 and 6, or an eight, or a character between a and d. In this notation all ranges are inclusive.

    Combining two sets of brackets lets you match two consecutive characters. For example, the expression [1-3][0-9] would match any two digit number between 10 and 39, inclusive.

    An example with the testing program:

    Please type in an expression: [C-FRSO]\nPlease type in a string: C\nFound!\nPlease type in a string: E\nFound!\nPlease type in a string: G\nNot found.\nPlease type in a string: R\nFound!\nPlease type in a string: O\nFound!\nPlease type in a string: T\nNot found.

    Repeated matches

    Any part of an expression can be repeated with the following operators:

      \n
    • * repeats for any number of times, including zero
    • \n
    • + repeats for any number of times, but at least once
    • \n
    • {m} repeats for exactly m times
    • \n

    These operators work on the part of the expression immediately preceding the operator. For example, the expression ba+b would match the substrings bab, baab and baaaaaaaaaaab, among others. The expression A[BCDE]*Z would match the substrings AZ, ADZ or ABCDEBCDEBCDEZ, among others.

    An example with the testing program:

    Please type in an expression: 1[234]*5\nPlease type in a string: 15\nFound!\nPlease type in a string: 125\nFound!\nPlease type in a string: 145\nFound!\nPlease type in a string: 12342345\nFound!\nPlease type in a string: 126\nNot found.\nPlease type in a string: 165\nNot found.

    Other special characters

    A dot is a wildcard character which can match any single character. For example, the expression c...o would match any five character substring beginning with a c and ending with an o, such as c-3po or cello.

    The ^ character specifies that the match must be at the beginning of the string, and $ specifies that the match must be at the end of the string. These can also be used to exclude from the matches any other characters than those specified:

    Please type in an expression: ^[123]*$\nPlease type in a string: 4\nNot found.\nPlease type in a string: 1221\nFound!\nPlease type in a string: 333333333\nFound!

    Sometimes you need to match for the special characters reserved for regular expression syntax. The backslash \\ can be used to escape special characters. So, the expression 1+ matches one or more numbers 1, but the expression 1\\+ matches the string 1+.

    Please type in an expression: ^\\*\nPlease type in a string: moi*\nNot found.\nPlease type in a string: m*o*i\nNot found.\nPlease type in a string: *moi\nFound!

    Round brackets can be used to group together different parts of the expression. For example, the expression (ab)+c would match the substrings abc, ababc and ababababababc, but not the strings ac or bc, as the entire substring ab would have to appear at least once.

    Please type in an expression: ^(jabba).*(hut)$\nPlease type in a string: jabba the hut\nFound!\nPlease type in a string: jabba a hut\nFound!\nPlease type in a string: jarjar the hut\nNot found.\nPlease type in a string: jabba the smut\nNot found.

    Here are some exercises for familiarizing yourself with regular expression syntax.

    Days of the week

    Using a regular expression, please write a function named is_dotw(my_string: str). The function should return True if the string passed as an argument contains an abbreviation for a day of the week (Mon, Tue, Wed, Thu, Fri, Sat, Sun).

    Some examples of how the function should work:

    print(is_dotw(\"Mon\"))\nprint(is_dotw(\"Fri\"))\nprint(is_dotw(\"Tui\"))

    True\nTrue\nFalse

    Check for vowels

    Please write a function named all_vowels(my_string: str) which uses a regular expression to check whether all characters in the given string are vowels.

    Some examples of how the function should work:

    print(all_vowels(\"eioueioieoieou\"))\nprint(all_vowels(\"autoooo\"))

    True\nFalse

    Time of day

    Please write a function named time_of_day(my_string: str) which uses a regular expression to check whether a string in the format XX:YY:ZZ is a valid time in the 24-hour format, with two digits each for hours, minutes and seconds.

    Some examples of how the function should work:

    print(time_of_day(\"12:43:01\"))\nprint(time_of_day(\"AB:01:CD\"))\nprint(time_of_day(\"17:59:59\"))\nprint(time_of_day(\"33:66:77\"))

    True\nFalse\nTrue\nFalse

    Grand finale

    To finish off this part of the material, let's work some more on objects and classes by building a slightly more extensive program. This exercise does not necessarily involve regular expressions, but the sections on functions as arguments and list comprehensions will likely be useful.

    You may also find the example set in part 10 helpful.

    In this exercise you will build an application for examining hockey league statistics from the NHL in a couple of different ways.

    The exercise template contains two JSON files: partial.json and all.json. The first of these is mostly meant for testing. The latter contains a lot of data, as all the NHL player stats for the 2019-20 season are included in the file.

    The entry for a single player is in the following format:

    {\n    \"name\": \"Patrik Laine\",\n    \"nationality\": \"FIN\",\n    \"assists\": 35,\n    \"goals\": 28,\n    \"penalties\": 22,\n    \"team\": \"WPG\",\n    \"games\": 68\n}

    Both files contain a list of entries in the above format.

    If you need a refresher on handling JSON files, please take a look at part 7 of this course material.

    Search and list

    Please write an interactive application which first asks for the name of the file, and then offers the following functions:

      \n
    • search by name for a single player's stats
    • \n
    • list all the abbreviations for team names in alphabetical order
    • \n
    • list all the abbreviations for countries in alphabetical order
    • \n

    These functionalities grant you one exercise point. Your application should now work as follows:

    file name: partial.json\nread the data of 14 players

    commands:\n0 quit\n1 search for player\n2 teams\n3 countries\n4 players in team\n5 players from country\n6 most points\n7 most goals

    command: 1\nname: Travis Zajac

    \nTravis Zajac         NJD   9 + 16 =  25\n

    command: 2\nBUF\nCGY\nDAL\nNJD\nNYI\nOTT\nPIT\nWPG\nWSH

    command: 3\nCAN\nCHE\nCZE\nSWE\nUSA

    command: 0

    NB: the printout format for a player must be exactly as follows:

    \nLeon Draisaitl       EDM  43 + 67 = 110\nConnor McDavid       EDM  34 + 63 =  97\nTravis Zajac         NJD   9 + 16 =  25\nMike Green           EDM   3 +  8 =  11\nMarkus Granlund      EDM   3 +  1 =   4\n123456789012345678901234567890123456789\n

    The last line in the sample above is there to help you calculate the widths of the different fields in the output; you should not print the numbers line yourself in your final submission.

    The abbreviation for the team is printed from the 22nd character onwards. The + sign is the 30th character and the = sign is the 35th character. All the fields should be justified to the right edge. All whitespace is space characters.

    F-strings are probably the easiest way to achieve the required printout. The process is similar to this exercise from part 6.

    List players by points

    These two functionalities will grant you a second exercise point:

      \n
    • list players in a specific team in the order of points scored, from highest to lowest. Points equals goals + assists
    • \n
    • list players from a specific country in the order of points scored, from highest to lowest
    • \n

    Your application should now work as follows:

    file name: partial.json\nread the data of 14 players

    commands:\n0 quit\n1 search for player\n2 teams\n3 countries\n4 players in team\n5 players from country\n6 most points\n7 most goals

    command: 4\nteam: OTT

    \nDrake Batherson      OTT   3 +  7 =  10\nJonathan Davidsson   OTT   0 +  1 =   1\n

    command: 5\ncountry: CAN

    \nJared McCann         PIT  14 + 21 =  35\nTravis Zajac         NJD   9 + 16 =  25\nTaylor Fedun         DAL   2 +  7 =   9\nMark Jankowski       CGY   5 +  2 =   7\nLogan Shaw           WPG   3 +  2 =   5\n

    command: 0

    Most successful players

    These two functionalities will grant you a third exercise point:

      \n
    • list of n players who've scored the most points\n
        \n
      • if two players have the same score, whoever has scored the higher number of goals comes first
      • \n
      \n
    • \n
    • list of n players who've scored the most goals\n
        \n
      • if two players have the same number of goals, whoever has played the lower number of games comes first
      • \n
      \n
    • \n

    Your application should now work as follows:

    file name: partial.json\nread the data of 14 players

    commands:\n0 quit\n1 search for player\n2 teams\n3 countries\n4 players in team\n5 players from country\n6 most points\n7 most goals

    command: 6\nhow many: 2

    \nJakub Vrana          WSH  25 + 27 =  52\nJared McCann         PIT  14 + 21 =  35\n

    command: 6\nhow many: 5

    \nJakub Vrana          WSH  25 + 27 =  52\nJared McCann         PIT  14 + 21 =  35\nJohn Klingberg       DAL   6 + 26 =  32\nTravis Zajac         NJD   9 + 16 =  25\nConor Sheary         BUF  10 + 13 =  23\n

    command: 7\nhow many: 6

    \nJakub Vrana          WSH  25 + 27 =  52\nJared McCann         PIT  14 + 21 =  35\nConor Sheary         BUF  10 + 13 =  23\nTravis Zajac         NJD   9 + 16 =  25\nJohn Klingberg       DAL   6 + 26 =  32\nMark Jankowski       CGY   5 +  2 =   7\n

    command: 0

    Please respond to a quick questionnaire on this part of the course.

    ","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-12/4-regular-expressions.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know what regular expressions are"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use regular expressions in your own programs"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We have already established that Python is an excellent environment for processing text. One additional powerful tool for text processing is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"regular expressions"}]},{"type":"text","value":", often shortened as "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"regex"}]},{"type":"text","value":" or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"regexp"}]},{"type":"text","value":". They are a way of selecting and searching for strings which follow a certain pattern. This section introduces you to the basics of regular expression, but you will find much more information online, including in the Python "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/howto/regex.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"tutorial"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"what-are-regular-expressions","style":"position:relative;"},"children":[{"type":"text","value":"What are regular expressions?"},{"type":"element","tagName":"a","properties":{"href":"#what-are-regular-expressions","ariaLabel":"what are regular expressions 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":"Regular expressions are not just a Python feature. They represent, in a way, a programming language within a programming language. They are, to an extent, compatible across many different programming languages. Regular expressions have their own specific syntax. The idea is to define a collection of strings which follow certain rules."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's begin with a simple example, before diving deeper into the syntax:"}]},{"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":"import"}]},{"type":"text","value":" re\n\nwords "},{"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":"\"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":"\"Pantone\""}]},{"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":"\"Pontoon\""}]},{"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":"\"Pollute\""}]},{"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":"\"Pantheon\""}]},{"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":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" words"},{"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":"# the string should begin with \"P\" and end with \"on\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" re"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"^P.*on$\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" word"},{"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":"word"},{"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":"\"found!\""}]},{"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 found!\nPontoon found!\nPantheon found!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We need to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"re"}]},{"type":"text","value":" module in order to use regular expressions in Python. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"re"}]},{"type":"text","value":" module contains many functions for working with regular expressions. In the example above, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"search"}]},{"type":"text","value":" function takes two string arguments: the pattern string, and the target string where the pattern is looked for."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This second example looks for any numbers in a string. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"findall"}]},{"type":"text","value":" function returns a list of all the instances which match the pattern:"}]},{"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":"import"}]},{"type":"text","value":" re\n\nsentence "},{"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":"\"First, 2 !#third 44 five 678xyz962\""}]},{"type":"text","value":"\n\nnumbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" re"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"findall"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\d+\""}]},{"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":"\n\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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n44\n678\n962"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-syntax-of-regular-expressions","style":"position:relative;"},"children":[{"type":"text","value":"The syntax of regular expressions"},{"type":"element","tagName":"a","properties":{"href":"#the-syntax-of-regular-expressions","ariaLabel":"the syntax of regular expressions 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":"Let's get familiar with the basic syntax of regular expressions. Most of the following examples make use of this testing program:"}]},{"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":"import"}]},{"type":"text","value":" re\n\nexpression "},{"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 an expression: \""}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n input_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":"if"}]},{"type":"text","value":" input_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":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" re"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"search"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"expression"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" input_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 "},{"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":"\"Found!\""}]},{"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":"else"}]},{"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":"\"Not found.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h3","properties":{"id":"alternate-substrings","style":"position:relative;"},"children":[{"type":"text","value":"Alternate substrings"},{"type":"element","tagName":"a","properties":{"href":"#alternate-substrings","ariaLabel":"alternate substrings 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 vertical bar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"|"}]},{"type":"text","value":", also called the pipe character, allows you to match alternate substrings. Its significance is thus "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"or"}]},{"type":"text","value":". For example, the expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"911|112"}]},{"type":"text","value":" matches strings which include either the substring "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"911"}]},{"type":"text","value":" or the substring "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"112"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example with the testing program:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in an expression: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"aa|ee|ii"}]},{"type":"text","value":"\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"aardvark"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"feelings"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"radii"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"smooch"}]},{"type":"text","value":"\nNot found.\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"continuum"}]},{"type":"text","value":"\nNot found."}]}]},{"type":"element","tagName":"h3","properties":{"id":"groups-of-characters","style":"position:relative;"},"children":[{"type":"text","value":"Groups of characters"},{"type":"element","tagName":"a","properties":{"href":"#groups-of-characters","ariaLabel":"groups of characters 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":"Square brackets are used to signify groups of accepted characters. For example, the expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[aeio]"}]},{"type":"text","value":" would match all strings which contain any of the characters a, e, i, or o."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A dash is also allowed for matching ranges of characters. For example, the expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[0-68a-d]"}]},{"type":"text","value":" would match all strings which contain a digit between 0 and 6, or an eight, or a character between a and d. In this notation all ranges are "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inclusive"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Combining two sets of brackets lets you match two consecutive characters. For example, the expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[1-3][0-9]"}]},{"type":"text","value":" would match any two digit number between 10 and 39, inclusive."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example with the testing program:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in an expression: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"[C-FRSO]"}]},{"type":"text","value":"\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"C"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"E"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"G"}]},{"type":"text","value":"\nNot found.\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"R"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"O"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"T"}]},{"type":"text","value":"\nNot found."}]}]},{"type":"element","tagName":"h3","properties":{"id":"repeated-matches","style":"position:relative;"},"children":[{"type":"text","value":"Repeated matches"},{"type":"element","tagName":"a","properties":{"href":"#repeated-matches","ariaLabel":"repeated matches 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":"Any part of an expression can be repeated with the following operators:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" repeats for any number of times, including zero"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" repeats for any number of times, but at least once"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"{m}"}]},{"type":"text","value":" repeats for exactly "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"m"}]},{"type":"text","value":" times"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"These operators work on the part of the expression immediately preceding the operator. For example, the expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ba+b"}]},{"type":"text","value":" would match the substrings "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bab"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"baab"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"baaaaaaaaaaab"}]},{"type":"text","value":", among others. The expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"A[BCDE]*Z"}]},{"type":"text","value":" would match the substrings "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"AZ"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ADZ"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ABCDEBCDEBCDEZ"}]},{"type":"text","value":", among others."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example with the testing program:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in an expression: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1[234]*5"}]},{"type":"text","value":"\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"15"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"125"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"145"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"12342345"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"126"}]},{"type":"text","value":"\nNot found.\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"165"}]},{"type":"text","value":"\nNot found."}]}]},{"type":"element","tagName":"h3","properties":{"id":"other-special-characters","style":"position:relative;"},"children":[{"type":"text","value":"Other special characters"},{"type":"element","tagName":"a","properties":{"href":"#other-special-characters","ariaLabel":"other special characters 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":"A dot is a wildcard character which can match any single character. For example, the expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"c...o"}]},{"type":"text","value":" would match any five character substring beginning with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"c"}]},{"type":"text","value":" and ending with an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"o"}]},{"type":"text","value":", such as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"c-3po"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cello"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"^"}]},{"type":"text","value":" character specifies that the match must be at the beginning of the string, and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"$"}]},{"type":"text","value":" specifies that the match must be at the end of the string. These can also be used to exclude from the matches any other characters than those specified:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in an expression: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"^[123]*$"}]},{"type":"text","value":"\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nNot found.\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1221"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"333333333"}]},{"type":"text","value":"\nFound!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sometimes you need to match for the special characters reserved for regular expression syntax. The backslash "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\\"}]},{"type":"text","value":" can be used to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"escape"}]},{"type":"text","value":" special characters. So, the expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1+"}]},{"type":"text","value":" matches one or more numbers "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":", but the expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1\\+"}]},{"type":"text","value":" matches the string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1+"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in an expression: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"^\\*"}]},{"type":"text","value":"\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"moi*"}]},{"type":"text","value":"\nNot found.\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"m*o*i"}]},{"type":"text","value":"\nNot found.\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"*moi"}]},{"type":"text","value":"\nFound!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Round brackets can be used to group together different parts of the expression. For example, the expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(ab)+c"}]},{"type":"text","value":" would match the substrings "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"abc"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ababc"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ababababababc"}]},{"type":"text","value":", but not the strings "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ac"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bc"}]},{"type":"text","value":", as the entire substring "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ab"}]},{"type":"text","value":" would have to appear at least once."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in an expression: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"^(jabba).*(hut)$"}]},{"type":"text","value":"\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jabba the hut"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jabba a hut"}]},{"type":"text","value":"\nFound!\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jarjar the hut"}]},{"type":"text","value":"\nNot found.\nPlease type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jabba the smut"}]},{"type":"text","value":"\nNot found."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Regular expressions","tmcname":"part12-14_regular_expressions"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here are some exercises for familiarizing yourself with regular expression syntax."}]},{"type":"element","tagName":"h2","properties":{"id":"days-of-the-week","style":"position:relative;"},"children":[{"type":"text","value":"Days of the week"},{"type":"element","tagName":"a","properties":{"href":"#days-of-the-week","ariaLabel":"days of the week 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":"Using a regular expression, please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"is_dotw(my_string: str)"}]},{"type":"text","value":". The function should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" if the string passed as an argument contains an abbreviation for a day of the week (Mon, Tue, Wed, Thu, Fri, Sat, Sun)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples 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":"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_dotw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mon\""}]},{"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_dotw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fri\""}]},{"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_dotw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tui\""}]},{"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":"True\nTrue\nFalse"}]}]},{"type":"element","tagName":"h2","properties":{"id":"check-for-vowels","style":"position:relative;"},"children":[{"type":"text","value":"Check for vowels"},{"type":"element","tagName":"a","properties":{"href":"#check-for-vowels","ariaLabel":"check for vowels 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"all_vowels(my_string: str)"}]},{"type":"text","value":" which uses a regular expression to check whether all characters in the given string are vowels."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples 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":"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":"all_vowels"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"eioueioieoieou\""}]},{"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":"all_vowels"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"autoooo\""}]},{"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":"True\nFalse"}]}]},{"type":"element","tagName":"h2","properties":{"id":"time-of-day","style":"position:relative;"},"children":[{"type":"text","value":"Time of day"},{"type":"element","tagName":"a","properties":{"href":"#time-of-day","ariaLabel":"time of day 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"time_of_day(my_string: str)"}]},{"type":"text","value":" which uses a regular expression to check whether a string in the format "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"XX:YY:ZZ"}]},{"type":"text","value":" is a valid time in the 24-hour format, with two digits each for hours, minutes and seconds."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples 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":"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":"time_of_day"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12:43:01\""}]},{"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":"time_of_day"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"AB:01:CD\""}]},{"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":"time_of_day"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"17:59:59\""}]},{"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":"time_of_day"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"33:66:77\""}]},{"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":"True\nFalse\nTrue\nFalse"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"grand-finale","style":"position:relative;"},"children":[{"type":"text","value":"Grand finale"},{"type":"element","tagName":"a","properties":{"href":"#grand-finale","ariaLabel":"grand finale 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":"To finish off this part of the material, let's work some more on objects and classes by building a slightly more extensive program. This exercise does not necessarily involve regular expressions, but the sections on "},{"type":"element","tagName":"a","properties":{"href":"/part-12/1-functions-as-arguments"},"children":[{"type":"text","value":"functions as arguments"}]},{"type":"text","value":" and "},{"type":"element","tagName":"a","properties":{"href":"/part-11/1-list-comprehensions"},"children":[{"type":"text","value":"list comprehensions"}]},{"type":"text","value":" will likely be useful."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may also find the example set in "},{"type":"element","tagName":"a","properties":{"href":"/part-10/4-application-development"},"children":[{"type":"text","value":"part 10"}]},{"type":"text","value":" helpful."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Hockey statistics","tmcname":"part12-15_hockey_statistics"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will build an application for examining hockey league statistics from the NHL in a couple of different ways."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains two JSON files: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"partial.json"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"all.json"}]},{"type":"text","value":". The first of these is mostly meant for testing. The latter contains a lot of data, as all the NHL player stats for the 2019-20 season are included in the file."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The entry for a single player is in the following format:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"json"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"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":"\"Patrik Laine\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"nationality\""}]},{"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":"\"FIN\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"assists\""}]},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"goals\""}]},{"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":"28"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"penalties\""}]},{"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":"22"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"team\""}]},{"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":"\"WPG\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"games\""}]},{"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":"68"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Both files contain a list of entries in the above format."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you need a refresher on handling JSON files, please take a look at "},{"type":"element","tagName":"a","properties":{"href":"/part-7/4-data-processing#reading-json-files"},"children":[{"type":"text","value":"part 7 of this course material"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"search-and-list","style":"position:relative;"},"children":[{"type":"text","value":"Search and list"},{"type":"element","tagName":"a","properties":{"href":"#search-and-list","ariaLabel":"search and list 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":"Please write an interactive application which first asks for the name of the file, and then offers the following functions:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"search by name for a single player's stats"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"list all the abbreviations for team names in alphabetical order"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"list all the abbreviations for countries in alphabetical order"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"These functionalities grant you one exercise point. Your application should now work as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"file name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"partial.json"}]},{"type":"text","value":"\nread the data of 14 players"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"commands:\n0 quit\n1 search for player\n2 teams\n3 countries\n4 players in team\n5 players from country\n6 most points\n7 most goals"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Travis Zajac"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Travis Zajac NJD  9 + 16 = 25\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nBUF\nCGY\nDAL\nNJD\nNYI\nOTT\nPIT\nWPG\nWSH"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nCAN\nCHE\nCZE\nSWE\nUSA"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the printout format for a player must be exactly as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Leon Draisaitl EDM 43 + 67 = 110\nConnor McDavid EDM 34 + 63 = 97\nTravis Zajac NJD  9 + 16 = 25\nMike Green EDM 3 + 8 = 11\nMarkus Granlund EDM 3 + 1 = 4\n123456789012345678901234567890123456789\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The last line in the sample above is there to help you calculate the widths of the different fields in the output; you should not print the numbers line yourself in your final submission."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The abbreviation for the team is printed from the 22nd character onwards. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" sign is the 30th character and the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sign is the 35th character. All the fields should be justified to the right edge. All whitespace is space characters."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"F-strings are probably the easiest way to achieve the required printout. The process is similar to "},{"type":"element","tagName":"a","properties":{"href":"/part-6/1-reading-files#programming-exercise-course-grading-part-3"},"children":[{"type":"text","value":"this exercise"}]},{"type":"text","value":" from part 6."}]},{"type":"element","tagName":"h2","properties":{"id":"list-players-by-points","style":"position:relative;"},"children":[{"type":"text","value":"List players by points"},{"type":"element","tagName":"a","properties":{"href":"#list-players-by-points","ariaLabel":"list players by points 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":"These two functionalities will grant you a second exercise point:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"list players in a specific team in the order of points scored, from highest to lowest. Points equals "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"goals"}]},{"type":"text","value":" + "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"assists"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"list players from a specific country in the order of points scored, from highest to lowest"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your application should now work as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"file name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"partial.json"}]},{"type":"text","value":"\nread the data of 14 players"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"commands:\n0 quit\n1 search for player\n2 teams\n3 countries\n4 players in team\n5 players from country\n6 most points\n7 most goals"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nteam: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"OTT"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Drake Batherson OTT 3 + 7 = 10\nJonathan Davidsson OTT 0 + 1 = 1\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\ncountry: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"CAN"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Jared McCann PIT 14 + 21 = 35\nTravis Zajac NJD 9 + 16 = 25\nTaylor Fedun DAL 2 + 7 = 9\nMark Jankowski CGY 5 + 2 = 7\nLogan Shaw WPG 3 + 2 = 5\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"most-successful-players","style":"position:relative;"},"children":[{"type":"text","value":"Most successful players"},{"type":"element","tagName":"a","properties":{"href":"#most-successful-players","ariaLabel":"most successful players 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":"These two functionalities will grant you a third exercise point:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"list of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" players who've scored the most points\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"if two players have the same score, whoever has scored the higher number of goals comes first"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"list of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" players who've scored the most goals\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"if two players have the same number of goals, whoever has played the lower number of games comes first"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your application should now work as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"file name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"partial.json"}]},{"type":"text","value":"\nread the data of 14 players"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"commands:\n0 quit\n1 search for player\n2 teams\n3 countries\n4 players in team\n5 players from country\n6 most points\n7 most goals"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nhow many: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Jakub Vrana WSH 25 + 27 = 52\nJared McCann PIT 14 + 21 = 35\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nhow many: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Jakub Vrana WSH 25 + 27 = 52\nJared McCann PIT 14 + 21 = 35\nJohn Klingberg DAL 6 + 26 = 32\nTravis Zajac NJD 9 + 16 = 25\nConor Sheary BUF 10 + 13 = 23\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nhow many: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Jakub Vrana WSH 25 + 27 = 52\nJared McCann PIT 14 + 21 = 35\nConor Sheary BUF 10 + 13 = 23\nTravis Zajac NJD 9 + 16 = 25\nJohn Klingberg DAL 6 + 26 = 32\nMark Jankowski CGY 5 + 2 = 7\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this part of the course."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"6a606b63-c674-5231-9e79-55d7372e582a"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will know what regular expressions are
    • \n
    • You will be able to use regular expressions in your own programs
    • \n

    We have already established that Python is an excellent environment for processing text. One additional powerful tool for text processing is regular expressions, often shortened as regex or regexp. They are a way of selecting and searching for strings which follow a certain pattern. This section introduces you to the basics of regular expression, but you will find much more information online, including in the Python tutorial.

    What are regular expressions?

    Regular expressions are not just a Python feature. They represent, in a way, a programming language within a programming language. They are, to an extent, compatible across many different programming languages. Regular expressions have their own specific syntax. The idea is to define a collection of strings which follow certain rules.

    Let's begin with a simple example, before diving deeper into the syntax:

    import re\n\nwords = [\"Python\", \"Pantone\", \"Pontoon\", \"Pollute\", \"Pantheon\"]\n\nfor word in words:\n    # the string should begin with \"P\" and end with \"on\"\n    if re.search(\"^P.*on$\", word):\n        print(word, \"found!\")

    Python found!\nPontoon found!\nPantheon found!

    We need to import the re module in order to use regular expressions in Python. The re module contains many functions for working with regular expressions. In the example above, the search function takes two string arguments: the pattern string, and the target string where the pattern is looked for.

    This second example looks for any numbers in a string. The findall function returns a list of all the instances which match the pattern:

    import re\n\nsentence = \"First, 2 !#third 44 five 678xyz962\"\n\nnumbers = re.findall(\"\\d+\", sentence)\n\nfor number in numbers:\n    print(number)

    2\n44\n678\n962

    The syntax of regular expressions

    Let's get familiar with the basic syntax of regular expressions. Most of the following examples make use of this testing program:

    import re\n\nexpression = input(\"Please type in an expression: \")\n\nwhile True:\n    input_string = input(\"Please type in a string: \")\n    if input_string == \"\":\n        break\n    if re.search(expression, input_string):\n        print(\"Found!\")\n    else:\n        print(\"Not found.\")

    Alternate substrings

    The vertical bar |, also called the pipe character, allows you to match alternate substrings. Its significance is thus or. For example, the expression 911|112 matches strings which include either the substring 911 or the substring 112.

    An example with the testing program:

    Please type in an expression: aa|ee|ii\nPlease type in a string: aardvark\nFound!\nPlease type in a string: feelings\nFound!\nPlease type in a string: radii\nFound!\nPlease type in a string: smooch\nNot found.\nPlease type in a string: continuum\nNot found.

    Groups of characters

    Square brackets are used to signify groups of accepted characters. For example, the expression [aeio] would match all strings which contain any of the characters a, e, i, or o.

    A dash is also allowed for matching ranges of characters. For example, the expression [0-68a-d] would match all strings which contain a digit between 0 and 6, or an eight, or a character between a and d. In this notation all ranges are inclusive.

    Combining two sets of brackets lets you match two consecutive characters. For example, the expression [1-3][0-9] would match any two digit number between 10 and 39, inclusive.

    An example with the testing program:

    Please type in an expression: [C-FRSO]\nPlease type in a string: C\nFound!\nPlease type in a string: E\nFound!\nPlease type in a string: G\nNot found.\nPlease type in a string: R\nFound!\nPlease type in a string: O\nFound!\nPlease type in a string: T\nNot found.

    Repeated matches

    Any part of an expression can be repeated with the following operators:

      \n
    • * repeats for any number of times, including zero
    • \n
    • + repeats for any number of times, but at least once
    • \n
    • {m} repeats for exactly m times
    • \n

    These operators work on the part of the expression immediately preceding the operator. For example, the expression ba+b would match the substrings bab, baab and baaaaaaaaaaab, among others. The expression A[BCDE]*Z would match the substrings AZ, ADZ or ABCDEBCDEBCDEZ, among others.

    An example with the testing program:

    Please type in an expression: 1[234]*5\nPlease type in a string: 15\nFound!\nPlease type in a string: 125\nFound!\nPlease type in a string: 145\nFound!\nPlease type in a string: 12342345\nFound!\nPlease type in a string: 126\nNot found.\nPlease type in a string: 165\nNot found.

    Other special characters

    A dot is a wildcard character which can match any single character. For example, the expression c...o would match any five character substring beginning with a c and ending with an o, such as c-3po or cello.

    The ^ character specifies that the match must be at the beginning of the string, and $ specifies that the match must be at the end of the string. These can also be used to exclude from the matches any other characters than those specified:

    Please type in an expression: ^[123]*$\nPlease type in a string: 4\nNot found.\nPlease type in a string: 1221\nFound!\nPlease type in a string: 333333333\nFound!

    Sometimes you need to match for the special characters reserved for regular expression syntax. The backslash \\ can be used to escape special characters. So, the expression 1+ matches one or more numbers 1, but the expression 1\\+ matches the string 1+.

    Please type in an expression: ^\\*\nPlease type in a string: moi*\nNot found.\nPlease type in a string: m*o*i\nNot found.\nPlease type in a string: *moi\nFound!

    Round brackets can be used to group together different parts of the expression. For example, the expression (ab)+c would match the substrings abc, ababc and ababababababc, but not the strings ac or bc, as the entire substring ab would have to appear at least once.

    Please type in an expression: ^(jabba).*(hut)$\nPlease type in a string: jabba the hut\nFound!\nPlease type in a string: jabba a hut\nFound!\nPlease type in a string: jarjar the hut\nNot found.\nPlease type in a string: jabba the smut\nNot found.

    Here are some exercises for familiarizing yourself with regular expression syntax.

    Days of the week

    Using a regular expression, please write a function named is_dotw(my_string: str). The function should return True if the string passed as an argument contains an abbreviation for a day of the week (Mon, Tue, Wed, Thu, Fri, Sat, Sun).

    Some examples of how the function should work:

    print(is_dotw(\"Mon\"))\nprint(is_dotw(\"Fri\"))\nprint(is_dotw(\"Tui\"))

    True\nTrue\nFalse

    Check for vowels

    Please write a function named all_vowels(my_string: str) which uses a regular expression to check whether all characters in the given string are vowels.

    Some examples of how the function should work:

    print(all_vowels(\"eioueioieoieou\"))\nprint(all_vowels(\"autoooo\"))

    True\nFalse

    Time of day

    Please write a function named time_of_day(my_string: str) which uses a regular expression to check whether a string in the format XX:YY:ZZ is a valid time in the 24-hour format, with two digits each for hours, minutes and seconds.

    Some examples of how the function should work:

    print(time_of_day(\"12:43:01\"))\nprint(time_of_day(\"AB:01:CD\"))\nprint(time_of_day(\"17:59:59\"))\nprint(time_of_day(\"33:66:77\"))

    True\nFalse\nTrue\nFalse

    Grand finale

    To finish off this part of the material, let's work some more on objects and classes by building a slightly more extensive program. This exercise does not necessarily involve regular expressions, but the sections on functions as arguments and list comprehensions will likely be useful.

    You may also find the example set in part 10 helpful.

    In this exercise you will build an application for examining hockey league statistics from the NHL in a couple of different ways.

    The exercise template contains two JSON files: partial.json and all.json. The first of these is mostly meant for testing. The latter contains a lot of data, as all the NHL player stats for the 2019-20 season are included in the file.

    The entry for a single player is in the following format:

    {\n    \"name\": \"Patrik Laine\",\n    \"nationality\": \"FIN\",\n    \"assists\": 35,\n    \"goals\": 28,\n    \"penalties\": 22,\n    \"team\": \"WPG\",\n    \"games\": 68\n}

    Both files contain a list of entries in the above format.

    If you need a refresher on handling JSON files, please take a look at part 7 of this course material.

    Search and list

    Please write an interactive application which first asks for the name of the file, and then offers the following functions:

      \n
    • search by name for a single player's stats
    • \n
    • list all the abbreviations for team names in alphabetical order
    • \n
    • list all the abbreviations for countries in alphabetical order
    • \n

    These functionalities grant you one exercise point. Your application should now work as follows:

    file name: partial.json\nread the data of 14 players

    commands:\n0 quit\n1 search for player\n2 teams\n3 countries\n4 players in team\n5 players from country\n6 most points\n7 most goals

    command: 1\nname: Travis Zajac

    \nTravis Zajac         NJD   9 + 16 =  25\n

    command: 2\nBUF\nCGY\nDAL\nNJD\nNYI\nOTT\nPIT\nWPG\nWSH

    command: 3\nCAN\nCHE\nCZE\nSWE\nUSA

    command: 0

    NB: the printout format for a player must be exactly as follows:

    \nLeon Draisaitl       EDM  43 + 67 = 110\nConnor McDavid       EDM  34 + 63 =  97\nTravis Zajac         NJD   9 + 16 =  25\nMike Green           EDM   3 +  8 =  11\nMarkus Granlund      EDM   3 +  1 =   4\n123456789012345678901234567890123456789\n

    The last line in the sample above is there to help you calculate the widths of the different fields in the output; you should not print the numbers line yourself in your final submission.

    The abbreviation for the team is printed from the 22nd character onwards. The + sign is the 30th character and the = sign is the 35th character. All the fields should be justified to the right edge. All whitespace is space characters.

    F-strings are probably the easiest way to achieve the required printout. The process is similar to this exercise from part 6.

    List players by points

    These two functionalities will grant you a second exercise point:

      \n
    • list players in a specific team in the order of points scored, from highest to lowest. Points equals goals + assists
    • \n
    • list players from a specific country in the order of points scored, from highest to lowest
    • \n

    Your application should now work as follows:

    file name: partial.json\nread the data of 14 players

    commands:\n0 quit\n1 search for player\n2 teams\n3 countries\n4 players in team\n5 players from country\n6 most points\n7 most goals

    command: 4\nteam: OTT

    \nDrake Batherson      OTT   3 +  7 =  10\nJonathan Davidsson   OTT   0 +  1 =   1\n

    command: 5\ncountry: CAN

    \nJared McCann         PIT  14 + 21 =  35\nTravis Zajac         NJD   9 + 16 =  25\nTaylor Fedun         DAL   2 +  7 =   9\nMark Jankowski       CGY   5 +  2 =   7\nLogan Shaw           WPG   3 +  2 =   5\n

    command: 0

    Most successful players

    These two functionalities will grant you a third exercise point:

      \n
    • list of n players who've scored the most points\n
        \n
      • if two players have the same score, whoever has scored the higher number of goals comes first
      • \n
      \n
    • \n
    • list of n players who've scored the most goals\n
        \n
      • if two players have the same number of goals, whoever has played the lower number of games comes first
      • \n
      \n
    • \n

    Your application should now work as follows:

    file name: partial.json\nread the data of 14 players

    commands:\n0 quit\n1 search for player\n2 teams\n3 countries\n4 players in team\n5 players from country\n6 most points\n7 most goals

    command: 6\nhow many: 2

    \nJakub Vrana          WSH  25 + 27 =  52\nJared McCann         PIT  14 + 21 =  35\n

    command: 6\nhow many: 5

    \nJakub Vrana          WSH  25 + 27 =  52\nJared McCann         PIT  14 + 21 =  35\nJohn Klingberg       DAL   6 + 26 =  32\nTravis Zajac         NJD   9 + 16 =  25\nConor Sheary         BUF  10 + 13 =  23\n

    command: 7\nhow many: 6

    \nJakub Vrana          WSH  25 + 27 =  52\nJared McCann         PIT  14 + 21 =  35\nConor Sheary         BUF  10 + 13 =  23\nTravis Zajac         NJD   9 + 16 =  25\nJohn Klingberg       DAL   6 + 26 =  32\nMark Jankowski       CGY   5 +  2 =   7\n

    command: 0

    Please respond to a quick questionnaire on this part of the course.

    ","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-12/4-regular-expressions.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-12/page-data.json b/page-data/part-12/page-data.json index 2596a29cc..4824dc34d 100644 --- a/page-data/part-12/page-data.json +++ b/page-data/part-12/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/part-12", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-12","title":"Part 12"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-12/index.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-12","title":"Part 12"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-12/index.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-13/1-pygame/page-data.json b/page-data/part-13/1-pygame/page-data.json index a47f90b9e..be182f9c3 100644 --- a/page-data/part-13/1-pygame/page-data.json +++ b/page-data/part-13/1-pygame/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-13/1-pygame", - "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 have installed the pygame library on your computer"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to create a pygame window and how to exit a program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use an image stored in a file in a pygame window"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In these last two parts of this course material we will get to grips with the pygame library. It is a Python library for programming games. It helps you create graphical elements, handle events from the keyboard and the mouse, and implement other features necessary in games."}]},{"type":"element","tagName":"h2","properties":{"id":"installing-pygame","style":"position:relative;"},"children":[{"type":"text","value":"Installing pygame"},{"type":"element","tagName":"a","properties":{"href":"#installing-pygame","ariaLabel":"installing pygame 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":"h3","properties":{"id":"linux","style":"position:relative;"},"children":[{"type":"text","value":"Linux"},{"type":"element","tagName":"a","properties":{"href":"#linux","ariaLabel":"linux 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":"Open a command line, type in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pip3 install pygame"}]},{"type":"text","value":" an press "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enter"}]},{"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: 399px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/a92634cb53af0aa1734e9ca12ca79562/a307d/pygame_linux.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: 60.434782608695656%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAyElEQVQoz+2Oy26CYBSE/5XXxssTKAWqUQSixmorKhEEfgQv7/8un8FqorELFm5MXHyZyZnJ5IhEBgSeyy4Oz5rGIZG/JokC9lvJerVgn0gOaYwMPOLQ57jbMp994/w8IrzuL5G5YtNzWKoTpOkSGkuc1oh07HOcRrj6FLOmYje/sBr6n9a1fxFWVWNY62B/6FgVDbuqk93Mssqo3j1nme8X2hhF5cKtv0cYJYV+VigpD2T3azYof+ZC5C3mHc09+PQP34MvPHgC7r1OntPJWSAAAAAASUVORK5CYII='); 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":["/static/a92634cb53af0aa1734e9ca12ca79562/a0b58/pygame_linux.webp 230w","/static/a92634cb53af0aa1734e9ca12ca79562/66086/pygame_linux.webp 399w"],"sizes":"(max-width: 399px) 100vw, 399px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a92634cb53af0aa1734e9ca12ca79562/81c8e/pygame_linux.png 230w","/static/a92634cb53af0aa1734e9ca12ca79562/a307d/pygame_linux.png 399w"],"sizes":"(max-width: 399px) 100vw, 399px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/a92634cb53af0aa1734e9ca12ca79562/a307d/pygame_linux.png","alt":"pygame linux","title":"pygame linux","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":"This should install the pygame library on your computer."}]},{"type":"element","tagName":"h3","properties":{"id":"windows","style":"position:relative;"},"children":[{"type":"text","value":"Windows"},{"type":"element","tagName":"a","properties":{"href":"#windows","ariaLabel":"windows 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":"Open the Windows terminal by opening the menu, typing in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cmd"}]},{"type":"text","value":" and pressing "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enter"}]},{"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: 392px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/76954afbb36ac5953d205b7cdab98ff6/0acb4/13_1_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: 173.47826086956525%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAjCAIAAAAblL1PAAAACXBIWXMAAA7DAAAOwwHHb6hkAAADp0lEQVRIx+2VT0gjVxzHp0g1kwlqNEmJqUTblB6EPewaEKGlINKlhW5Peyhb2GNPwkIPhUJvPRVakP6hQr0UlvoHm6yaSfaSVTdqEpdQt8ZsTGidxJ3JG2cmk0ky+fPevDKZVUrQrnruly+PuXze7/d+v/d7Q9hsNovFEgwGYUu1Wq3RaGiaJsuypmn4P0W43e6BgYHl5eVMJoMQKhQKsixDCBOJBITwPKxeryOEiJ6ens7OziBNY4xVVW02GkjTjJjay0QMDg66XK7FhYVcLndwcPB0d1dRFIPELxPhdDrtNtt3vz7YFfBGVt7IlsJpMZyWwmnxUVoKPxPXs0rk7+qZ1tOmKOqLb2fffW/i3tc/LCT4n0N/zDx8OvPwz9lw6qdA4n40v5yq+PZK/qTSZsJqtVos1Jff//bVjO/HQOJ+7PncE/AgVfUly/79si9Z9iWV3/dKZ1qHzaRpdiHAIfzsuL73vLLHVfdB7dQpUD/PLdhspldX/soe8IXCBUv1omBWq5UkyUBAb5VcKgHAHzJMVVUxxlBDSNNOrWm4zTpsMpGhUBBjLAgCy7KpVKpcLl8kBaNglvn5+XQ6I4kSAEBRlGKxCAAoFAo8zx8fH7Msq7ZyadtOhymKWlxcPDo6KhaLHMcBAGRZFgQBtCSKIsdxjUbj3Mhzc3PJ5L4sy7lcDgDAMAzLshzHGVvwPM+yLMMw+Xz+3xf+ReSlpSUAQLlcBgBIkiQIgrEqiiJJUrFYNHIRBAEh1F7t1dVVURQzmUw+n89mszzPX7RgJEnSNN1sNhVFqVQqqqrWarXmidomycAghPpUnfQ5YAwzwzDValU8kSAIZ8avVCr6PLf6bAqFQsZ++DL6H74s3NfXR5LkFWHj6b0i7Ha7nU4n3Xq3Lw17PB632x0IBK4C9/b2Xv3MY2NjXq/X7/efXvdLwA6Hg6KolZUVjHGtVoMQ1i8svVUGXK/XeZ4/PDxUVRUhpGGMEEIQYg1BCJGm6StCuPUBNQQRJCiK6ujomJ6ejsVi4XCYpulIJPJkJ76x9mh7OxqJxdY2t3biO4/XH29H41ubW+vrkXg8Hl3bjG1FCa/XOzk5SRD677Krq2t4+I3B113Eq+bR21P9Dvv4a92fjnsIgrj74biru+v66Jt3PnnnFYKw3r1mfqufGBoaGhkZMZlMDofDbDbb7HZ7v9Uy4Jn65pebH30wcX3is1t3eoff/vjzqWs3bozevHV76l5vn936vqd7yP4P6JyrnQ25/+oAAAAASUVORK5CYII='); 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":["/static/76954afbb36ac5953d205b7cdab98ff6/a0b58/13_1_1.webp 230w","/static/76954afbb36ac5953d205b7cdab98ff6/86099/13_1_1.webp 392w"],"sizes":"(max-width: 392px) 100vw, 392px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/76954afbb36ac5953d205b7cdab98ff6/81c8e/13_1_1.png 230w","/static/76954afbb36ac5953d205b7cdab98ff6/0acb4/13_1_1.png 392w"],"sizes":"(max-width: 392px) 100vw, 392px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/76954afbb36ac5953d205b7cdab98ff6/0acb4/13_1_1.png","alt":"13 1 1","title":"13 1 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":"p","properties":{},"children":[{"type":"text","value":"The command line interpreter window should open. Type in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pip3 install pygame"}]},{"type":"text","value":" and press "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enter"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This should install the pygame library on your computer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Installation may require system administrator privileges. If the above doesn't work, you can try running the terminal application as an administrator: open the Windows menu, find the CMD application, right-click it and choose \"Run as administrator\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Installing and accessing pygame requires that your Python installation is added to path, as instructed "},{"type":"element","tagName":"a","properties":{"href":"https://www.mooc.fi/en/installation/vscode/#python3","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"here"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h3","properties":{"id":"mac","style":"position:relative;"},"children":[{"type":"text","value":"Mac"},{"type":"element","tagName":"a","properties":{"href":"#mac","ariaLabel":"mac 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":"Open the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Terminal"}]},{"type":"text","value":", for example through the magnifying glass symbol in the top right corner:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 746px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/77ceacb61dfaf80c7c442a4225b65e87/62de4/13-1-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: 24.347826086956523%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAIAAADKYVtkAAAACXBIWXMAABYlAAAWJQFJUiTwAAABGUlEQVQY02OYs2Rje9/sSTOWzlmycdKsZVPmrJg6Z+WC5Zt7py7s6J/dO2nehDnLW6bOb+mftWD55oUrN/VPXVNRPW3SzEVzlqxn0Lf207Hw1rPyhTF89Kx99W38tM08VE3dZE2c7S18z7gX99gnqll7G9n6alt6q5u561l56lv7MLCIaLGIaLGKaLMIQ0kWYU1GIQ1GYU1JMf1QKbs6OY9OeW8DcVMGQTUWIQ1mYQ1WEU0mIQ1mQXUGPnkzEJIzhSBeOVMeWRNZOQtHRYdAZRdzBTt+OVMGaQMOWWOQrKwJCMlASGMGfgVzfjlTXhkjXlljXhlDHil9PmkDNRlTKRkTNlljVjkTPnkzIXlzPnlzXpDRUJvAbFMAjAFKS35RLdoAAAAASUVORK5CYII='); 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":["/static/77ceacb61dfaf80c7c442a4225b65e87/a0b58/13-1-2.webp 230w","/static/77ceacb61dfaf80c7c442a4225b65e87/bc10c/13-1-2.webp 460w","/static/77ceacb61dfaf80c7c442a4225b65e87/f7ebd/13-1-2.webp 746w"],"sizes":"(max-width: 746px) 100vw, 746px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/77ceacb61dfaf80c7c442a4225b65e87/81c8e/13-1-2.png 230w","/static/77ceacb61dfaf80c7c442a4225b65e87/08a84/13-1-2.png 460w","/static/77ceacb61dfaf80c7c442a4225b65e87/62de4/13-1-2.png 746w"],"sizes":"(max-width: 746px) 100vw, 746px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/77ceacb61dfaf80c7c442a4225b65e87/62de4/13-1-2.png","alt":"13 1 2","title":"13 1 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 search tool should open. Type in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"terminal"}]},{"type":"text","value":" and press "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enter"}]},{"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: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/a310ec5ccf9438989514ad8da107dcc3/47218/13-1-3.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: 66.95652173913044%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAABYlAAAWJQFJUiTwAAACa0lEQVQozy2STXLTQBCFdYFwgzh3IlmFDeQGkAQ2LKGo4hBAQhWp7FglQMohln8iO7Yly7JkzWime370M5LsACeg5KTq23W97ur3njX3poNup2t3Bv0+53z9+aL63ilGcx1zlRZK50pnSmdCapQKUQHKvCi/nZ0PnKFlDyZt22nbo+vu3cQnvhP4HvEXHEArnUn1qASUHAQHAahUmg+c0cwPrIOP3dcn+PbcvDw1h6fF8Vn55swcfjVOkJs8lypXOi9MvVr/Xf/5V5Q1CiVUWlYrqVLr99XNz+vBzdB3PDKeM2/BZyF4IeeodZoJmZqy/vTl5PmLg929vXfvPxSm0UuVAUirb/fa1+3e7W0UkygmhFCaJJRSFFKoFIQq6/XR8fHOTuvJ1tbT3T1TrQGl0hmgsJxhMBxPnYnrBVFMgaFuAM1AMZCUYW7ql6+Otre3W63W/v6zLDeMI0rNAK1Ob2L3h+2b3tW17frhkkAYs3DJ4gQJEyRBDspx7i4vf11c/Oh0eknCCWEJw0UYW+4s6N+OnOHE88NFRBdLuohoEBHKxcNxAIkgXW8+Hk8TyilhyyWlCUw93/LcmTube37QpJLmchOPUBnKdIMWQgMIShkhjDHgXHCOHCRNuEVRzfxgNJ66ng/YxCBUo0GhUahHUAmppUql1KIhlSprfg4TGS1JGMVhFLPGx2bQ3NyIYQMKHUZkPPFcz3+ojc7yxu04pjFJHiCUMY4gFEeVbKymDBgXwWLpDO96fcfu9jmI5i+ZMY6WTvMGnWmdZXluTFlWa1OtTFkXpipMWZgKmmKiaJaKsqrr1X29ui/L+j/LRoPjTcXpPgAAAABJRU5ErkJggg=='); 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":["/static/a310ec5ccf9438989514ad8da107dcc3/a0b58/13-1-3.webp 230w","/static/a310ec5ccf9438989514ad8da107dcc3/bc10c/13-1-3.webp 460w","/static/a310ec5ccf9438989514ad8da107dcc3/966d8/13-1-3.webp 920w","/static/a310ec5ccf9438989514ad8da107dcc3/3dddb/13-1-3.webp 1344w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a310ec5ccf9438989514ad8da107dcc3/81c8e/13-1-3.png 230w","/static/a310ec5ccf9438989514ad8da107dcc3/08a84/13-1-3.png 460w","/static/a310ec5ccf9438989514ad8da107dcc3/c0255/13-1-3.png 920w","/static/a310ec5ccf9438989514ad8da107dcc3/47218/13-1-3.png 1344w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/a310ec5ccf9438989514ad8da107dcc3/c0255/13-1-3.png","alt":"13 1 3","title":"13 1 3","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":"Type in the following and press "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enter"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pip3 install pygame"}]}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/cc8d7c319bc7baf0232b2d92350f9631/d56e1/13-1-4.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: 28.695652173913043%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAiElEQVQY06XNwQoCIRSFYd//ZXoJ4dpUIpqEYRZii1y4cRai4xgMGEEboW954PAjcd6BYXd1OR0PhBDGGABgjAkhADBNe8651lpKKYSglHLOHx2yz9ftqp1z1lpjjFIqhDDPc4zRe19Kaa2tm9atHVpqXTa11pRSzrkNQ7/TJ/JdGz3/VR4/vwF1E1iIv4njxgAAAABJRU5ErkJggg=='); 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":["/static/cc8d7c319bc7baf0232b2d92350f9631/a0b58/13-1-4.webp 230w","/static/cc8d7c319bc7baf0232b2d92350f9631/bc10c/13-1-4.webp 460w","/static/cc8d7c319bc7baf0232b2d92350f9631/966d8/13-1-4.webp 920w","/static/cc8d7c319bc7baf0232b2d92350f9631/bbe5b/13-1-4.webp 1130w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cc8d7c319bc7baf0232b2d92350f9631/81c8e/13-1-4.png 230w","/static/cc8d7c319bc7baf0232b2d92350f9631/08a84/13-1-4.png 460w","/static/cc8d7c319bc7baf0232b2d92350f9631/c0255/13-1-4.png 920w","/static/cc8d7c319bc7baf0232b2d92350f9631/d56e1/13-1-4.png 1130w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/cc8d7c319bc7baf0232b2d92350f9631/c0255/13-1-4.png","alt":"13 1 4","title":"13 1 4","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":"This should install the pygame library on your computer."}]},{"type":"element","tagName":"h2","properties":{"id":"your-first-program","style":"position:relative;"},"children":[{"type":"text","value":"Your first program"},{"type":"element","tagName":"a","properties":{"href":"#your-first-program","ariaLabel":"your first program 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":"Here is a simple program for checking your pygame installation works correctly:"}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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\nwindow"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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":"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":"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":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"When this program is run, it should display a window:"}]},{"type":"element","tagName":"img","properties":{"src":"/d11704399cba1d2075b1135cef373b65/pygame_first.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program only consists of displaying a window, and it runs until the user closes the window."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's take a closer look at the steps required to achieve this. The first line takes the pygame library into use: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import pygame"}]},{"type":"text","value":". The next command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.init"}]},{"type":"text","value":" initializes the pygame modules, and the next one creates a window with the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.display.set_mode"}]},{"type":"text","value":"."}]},{"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":"pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"set_mode"}]},{"type":"text","value":" function takes the window dimensions as an argument. The tuple "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(640, 480)"}]},{"type":"text","value":" indicates that the window is 640 pixels wide and 480 pixels high. The variable name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"window"}]},{"type":"text","value":" can be used later to access the window, for example to draw something in it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following two commands do just that:"}]},{"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":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fill"}]},{"type":"text","value":" method fills the window with the colour passed as an argument. In this case the colour is black, passed as an RGB value in the tuple "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(0, 0, 0)"}]},{"type":"text","value":". The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.display.flip"}]},{"type":"text","value":" updates the contents of the window."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"After these initialization commands the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"main loop"}]},{"type":"text","value":" of the program begins:"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"The main loop handles all events the operating system passes to the program. With each iteration the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.event.get"}]},{"type":"text","value":" returns a list of any events collected since the previous iteration."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the example above the program only handles events of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.QUIT"}]},{"type":"text","value":". This event is raised by, for example, clicking on the exit button in the corner of the window. If the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.QUIT"}]},{"type":"text","value":" event is raised, the program exits through the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exit"}]},{"type":"text","value":" function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can try and see what happens if your program doesn't handle the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.QUIT"}]},{"type":"text","value":" event. This should mean that clicking on the exit button does nothing, which would be confusing for the user. As the program is run from the command line, you can still stop it from the command line with Control+C."}]},{"type":"element","tagName":"h2","properties":{"id":"add-an-image","style":"position:relative;"},"children":[{"type":"text","value":"Add an image"},{"type":"element","tagName":"a","properties":{"href":"#add-an-image","ariaLabel":"add an image 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":"Let's add an image to the window:"}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nwindow"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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":"\nwindow"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"100"}]},{"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":"50"}]},{"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":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"The program uses this image of a robot, which is stored in the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot.png"}]},{"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: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/c90257b456f195bb0d06e65ddbbc1728/a1812/robot.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: 172%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAiCAYAAABfqvm9AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAEyklEQVRIx7WWX0hbdxzFTzRWqNTODLWpsWoRfbGQFtMqtIUykGJsuxWq0dptqTWWVTtFR11u/m0VX/YwN6jazUopQx0Iw7AOmzUmG7bGJMJGYd1gjMFehDFSGIPVJX7Hub3tqrONe9gPvtyv53zOSYi54QLrTnl5ebbZbIbJZPIdPnz495ycnMbHHndq9MiQRbpTWFiIPXv2YMeOHV0HDhwQAAdLS0vB4U6NHhmy6c4Wg8HwWklJiWN7bu45AKM7d+5sNZlMHRzu1OiRIcvMRkU67frChQsXxO/3y2d+v8zevi3T09MyOTmpDndq9MiQZWZdx5o/cn0+36/xpaXVyy7XQ1dPT3JqamolGo2qw50aPTJkmXle4fZ33e7Ep8GgfASkfgbkUmenLMRi6nCnRo8MWWaeW+hzuxOfh8OiGI2pcUB8TqdEolF1uFOjR8a3mUKnoiRi0ah8MjOTujYzI7O3bklobk4d7tTokSGbvtDpTMRiMYkvLqa+jceFwcVIRB3u1OiRIZu2UFGURCQSkQ9HRlJDw8O8ytDoqDrq/khLkVE28w69Hk9iNhSSlszM1HlAXuXnqA13avTIkE1XmOt1u3/7IhgUG/CXA0ieBlIeQDjcqdEjQzbt18bj8SSXlpbk6vi4jFy7JmM3bsjo9eurHO7U6JEh+6x3+OT+P3LkSKCrqyvS0919p6+396vzHR3Lb/X1CYc7NXpkyDKzpsHv92N5eVl9BRHJAJCl3Z9bRXhn4ePW1lbhcNe0rRqTpWV07GAXxsbGUFdXp9Pp1HecdeLEiV8GBgb+UBTlgdvtfuB0Oh+63W7haPsDemTIMsMsO9iFioqKzKqqKpSUlPRXVlb+6PV6k/fv35eFhQWJRqMS5/cuHl/VRtXokSHLDLPsYBeOHz+ut9ls2L9//3X+eiiKsrK4uJgKBAKrwWAwFQwGeRVtVI0eGbLMMMsOdsFqteobGxtRXV090t7eLi6Xa4V3AcPhcFg2GnpkyDLDLDvYhYaGBrXQYrFcdTgcTxfKcwrlcSEzzLKDXf9DodWqbzp1Cpbq6qsd587950JmmGUHu3D05En9yTNnsK+2dtTe3a1C8U18hnGtkBlm2cEuHC0u1r9SVoZ9hYVjr5eWinLp0p/RWCz5ZSCQ0v6zqbm5uSRH+68LPTJkmWGWHeyCHdC/CeCljIz3LwPyTn+/fHPvnty9c4ffOfVnan5+Xh3u1OiRIcsMs+xgF5CfDxw7BlitvJWKDh46FHI4HBFbc/NyW1ubNDc3/2S32+9yuFOjR4YsM2qWHex6coxG9cJ7NRwKoaio6AOGAZyemJgAhzs1emS0+/pJds0pKChQfyAGBwe38KnAYrFcuXjxohiNxrP/vKbxLDV6ZMgyo2U3PvX19fqmpibs3bv3Smdnp+Tl5dlNJhOfdcCdGj0yZJ/9DAKATz61NTVZbe3tMJvNw7xP1xdSo0eGbPaznkWePhUWC2oslqHenh4xGAz2XcXF4HCnRo/M5k52dhk7t+3ePdFgs0nOtm32FwsKwOFOjR4Zjd34vA3ovQDqgIl5QL4H5CYgu4A3qgBwuN/UPDJkvVr2X4WNQGYLgH1A/3vAd8PA1x6d7od84OVdj8rAnRo9MmRbtOzjor8BpWPSyH+RAgAAAAAASUVORK5CYII='); 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":["/static/c90257b456f195bb0d06e65ddbbc1728/6ac13/robot.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/c90257b456f195bb0d06e65ddbbc1728/a1812/robot.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/c90257b456f195bb0d06e65ddbbc1728/a1812/robot.png","alt":"robot","title":"robot","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 file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot.png"}]},{"type":"text","value":" has to be in the same directory with the source code of the your program, or the program won't be able to find it. In the exercise templates for this part the images are waiting in the exercise directory."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The window should now look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/0f7db0536914ccd471841bcee4201632/pygame_pic.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.image.load"}]},{"type":"text","value":" loads the image in the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot.png"}]},{"type":"text","value":" and stores a reference to it in the variable named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot"}]},{"type":"text","value":". The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"blit"}]},{"type":"text","value":" draws the image at the location "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(100, 50)"}]},{"type":"text","value":", and the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.display.flip"}]},{"type":"text","value":" updates the window contents, as before. The location "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(100, 50)"}]},{"type":"text","value":" means that the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"top left corner"}]},{"type":"text","value":" of the image is at that location within the window."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In pygame the origo point "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(0, 0)"}]},{"type":"text","value":" is in the top left corner of the window. The x coordinates increase to the right, and the y coordinates increase downwards, so that the bottom right corner has the coordinates "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(640, 480)"}]},{"type":"text","value":". This is contrary to how coordinates are usually handled in e.g. mathematics, but it is quite common in a programming context, and worth getting used to."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Once you have loaded an image, you can use it many times within the same window. The following code draws the image of the robot at three different locations:"}]},{"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":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"0"}]},{"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":"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":"\nwindow"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"300"}]},{"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":"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":"\nwindow"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"100"}]},{"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":"200"}]},{"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":"The window should look like this as a result:"}]},{"type":"element","tagName":"img","properties":{"src":"/a7807afa43ad1cc7d9686968e6838712/pygame_pic2.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here we set the location of the image so that it lies at the centre of the window:"}]},{"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":"width "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"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":"\nheight "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"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":"\nwindow"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"320"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"width"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"240"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"height"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The window should now look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/498f521486feca567f74dd51c44e371b/pygame_pic3.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"get_width"}]},{"type":"text","value":" returns the width of the image, and the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"get_height"}]},{"type":"text","value":" returns its height, both in pixels. The centre of the window is at half its width and height, so at "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(320, 240)"}]},{"type":"text","value":", which we can use to calculate a suitable location for the top left corner of the image, so that it lies exactly at the centre."}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Pygame exercises"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercises in this part of the course have no automated tests, as the results as visually verified. The tests grant points automatically as you submit your solution to the server, no matter what your implementation. Only submit your solution when you are ready, and your solution matches the exercise description. The exercises may not have automatic tests, but the course staff will still see your solution. Also submitting incomplete solution to TMC Paste grants points automatically, so it should not be used when asking for help with the exercises in this part. You can use "},{"type":"element","tagName":"a","properties":{"href":"https://pastebin.com/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Pastebin.com"}]},{"type":"text","value":" or some another internet pastebin service when asking for help in the course's support channels."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If your solution clearly does not match the exercise description, you may lose the points granted for the exercises in this part."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Four robots","tmcname":"part13-01_four_robots"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which draws a robot in each of the four corners of the window. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/8ba6d635c4ceb93c2b86711f83589b3a/pygame_four.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Robots in a row","tmcname":"part13-02_robots_row"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which draws ten robots in a row. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/8dc589e5970721e2bf8bfe2bd22fa6cf/pygame_row.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"A hundred robots","tmcname":"part13-03_hundred_robots"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which draws a hundred robots: ten rows with ten robots in each row. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/7012bfc1e6ade044e03c53f07f35e7a6/pygame_hundred.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Random robots","tmcname":"part13-04_random_robots"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which draws "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"a thousand"}]},{"type":"text","value":" robots in random places. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/61a30808255873a3e24df2fc559d9367/pygame_thousand.gif"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will have installed the pygame library on your computer
    • \n
    • You will know how to create a pygame window and how to exit a program
    • \n
    • You will be able to use an image stored in a file in a pygame window
    • \n

    In these last two parts of this course material we will get to grips with the pygame library. It is a Python library for programming games. It helps you create graphical elements, handle events from the keyboard and the mouse, and implement other features necessary in games.

    Installing pygame

    Linux

    Open a command line, type in pip3 install pygame an press enter.

    \n \n

    Installing and accessing pygame requires that your Python installation is added to path, as instructed here.

    Mac

    Open the Terminal, for example through the magnifying glass symbol in the top right corner:

    \n \n \n \n \n \n \"13\n \n \n

    Type in the following and press enter:

    pip3 install pygame

    \n \n \n \n \n \n \"13\n \n \n

    This should install the pygame library on your computer.

    Your first program

    Here is a simple program for checking your pygame installation works correctly:

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nwindow.fill((0,0,0))\npygame.display.flip()\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.QUIT:\n            exit()

    When this program is run, it should display a window:

    The program only consists of displaying a window, and it runs until the user closes the window.

    Let's take a closer look at the steps required to achieve this. The first line takes the pygame library into use: import pygame. The next command pygame.init initializes the pygame modules, and the next one creates a window with the function pygame.display.set_mode.

    pygame.init()\nwindow = pygame.display.set_mode((640, 480))

    The set_mode function takes the window dimensions as an argument. The tuple (640, 480) indicates that the window is 640 pixels wide and 480 pixels high. The variable name window can be used later to access the window, for example to draw something in it.

    The following two commands do just that:

    window.fill((0, 0, 0))\npygame.display.flip()

    The fill method fills the window with the colour passed as an argument. In this case the colour is black, passed as an RGB value in the tuple (0, 0, 0). The pygame.display.flip updates the contents of the window.

    After these initialization commands the main loop of the program begins:

    while True:\n    for event in pygame.event.get():\n        if event.type == pygame.QUIT:\n            exit()

    The main loop handles all events the operating system passes to the program. With each iteration the function pygame.event.get returns a list of any events collected since the previous iteration.

    In the example above the program only handles events of type pygame.QUIT. This event is raised by, for example, clicking on the exit button in the corner of the window. If the pygame.QUIT event is raised, the program exits through the exit function.

    You can try and see what happens if your program doesn't handle the pygame.QUIT event. This should mean that clicking on the exit button does nothing, which would be confusing for the user. As the program is run from the command line, you can still stop it from the command line with Control+C.

    Add an image

    Let's add an image to the window:

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nwindow.fill((0, 0, 0))\nwindow.blit(robot, (100, 50))\npygame.display.flip()\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.QUIT:\n            exit()

    The program uses this image of a robot, which is stored in the file robot.png:

    \n \n

    The window should now look like this:

    The function pygame.image.load loads the image in the file robot.png and stores a reference to it in the variable named robot. The method blit draws the image at the location (100, 50), and the function pygame.display.flip updates the window contents, as before. The location (100, 50) means that the top left corner of the image is at that location within the window.

    In pygame the origo point (0, 0) is in the top left corner of the window. The x coordinates increase to the right, and the y coordinates increase downwards, so that the bottom right corner has the coordinates (640, 480). This is contrary to how coordinates are usually handled in e.g. mathematics, but it is quite common in a programming context, and worth getting used to.

    Once you have loaded an image, you can use it many times within the same window. The following code draws the image of the robot at three different locations:

    window.blit(robot, (0, 0))\nwindow.blit(robot, (300, 0))\nwindow.blit(robot, (100, 200))

    The window should look like this as a result:

    Here we set the location of the image so that it lies at the centre of the window:

    width = robot.get_width()\nheight = robot.get_height()\nwindow.blit(robot, (320-width/2, 240-height/2))

    The window should now look like this:

    The method get_width returns the width of the image, and the method get_height returns its height, both in pixels. The centre of the window is at half its width and height, so at (320, 240), which we can use to calculate a suitable location for the top left corner of the image, so that it lies exactly at the centre.

    The exercises in this part of the course have no automated tests, as the results as visually verified. The tests grant points automatically as you submit your solution to the server, no matter what your implementation. Only submit your solution when you are ready, and your solution matches the exercise description. The exercises may not have automatic tests, but the course staff will still see your solution. Also submitting incomplete solution to TMC Paste grants points automatically, so it should not be used when asking for help with the exercises in this part. You can use Pastebin.com or some another internet pastebin service when asking for help in the course's support channels.

    If your solution clearly does not match the exercise description, you may lose the points granted for the exercises in this part.

    Please write a program which draws a robot in each of the four corners of the window. The end result should look like this:

    Please write a program which draws ten robots in a row. The end result should look like this:

    Please write a program which draws a hundred robots: ten rows with ten robots in each row. The end result should look like this:

    Please write a program which draws a thousand robots in random places. The end result should look like this:

    ","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-13/1-pygame.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 have installed the pygame library on your computer"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to create a pygame window and how to exit a program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use an image stored in a file in a pygame window"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In these last two parts of this course material we will get to grips with the pygame library. It is a Python library for programming games. It helps you create graphical elements, handle events from the keyboard and the mouse, and implement other features necessary in games."}]},{"type":"element","tagName":"h2","properties":{"id":"installing-pygame","style":"position:relative;"},"children":[{"type":"text","value":"Installing pygame"},{"type":"element","tagName":"a","properties":{"href":"#installing-pygame","ariaLabel":"installing pygame 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":"h3","properties":{"id":"linux","style":"position:relative;"},"children":[{"type":"text","value":"Linux"},{"type":"element","tagName":"a","properties":{"href":"#linux","ariaLabel":"linux 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":"Open a command line, type in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pip3 install pygame"}]},{"type":"text","value":" an press "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enter"}]},{"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: 399px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/a92634cb53af0aa1734e9ca12ca79562/a307d/pygame_linux.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: 60.434782608695656%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAyElEQVQoz+2Oy26CYBSE/5XXxssTKAWqUQSixmorKhEEfgQv7/8un8FqorELFm5MXHyZyZnJ5IhEBgSeyy4Oz5rGIZG/JokC9lvJerVgn0gOaYwMPOLQ57jbMp994/w8IrzuL5G5YtNzWKoTpOkSGkuc1oh07HOcRrj6FLOmYje/sBr6n9a1fxFWVWNY62B/6FgVDbuqk93Mssqo3j1nme8X2hhF5cKtv0cYJYV+VigpD2T3azYof+ZC5C3mHc09+PQP34MvPHgC7r1OntPJWSAAAAAASUVORK5CYII='); 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":["/static/a92634cb53af0aa1734e9ca12ca79562/a0b58/pygame_linux.webp 230w","/static/a92634cb53af0aa1734e9ca12ca79562/66086/pygame_linux.webp 399w"],"sizes":"(max-width: 399px) 100vw, 399px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a92634cb53af0aa1734e9ca12ca79562/81c8e/pygame_linux.png 230w","/static/a92634cb53af0aa1734e9ca12ca79562/a307d/pygame_linux.png 399w"],"sizes":"(max-width: 399px) 100vw, 399px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/a92634cb53af0aa1734e9ca12ca79562/a307d/pygame_linux.png","alt":"pygame linux","title":"pygame linux","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":"This should install the pygame library on your computer."}]},{"type":"element","tagName":"h3","properties":{"id":"windows","style":"position:relative;"},"children":[{"type":"text","value":"Windows"},{"type":"element","tagName":"a","properties":{"href":"#windows","ariaLabel":"windows 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":"Open the Windows terminal by opening the menu, typing in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cmd"}]},{"type":"text","value":" and pressing "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enter"}]},{"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: 392px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/76954afbb36ac5953d205b7cdab98ff6/0acb4/13_1_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: 173.47826086956525%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAjCAIAAAAblL1PAAAACXBIWXMAAA7DAAAOwwHHb6hkAAADp0lEQVRIx+2VT0gjVxzHp0g1kwlqNEmJqUTblB6EPewaEKGlINKlhW5Peyhb2GNPwkIPhUJvPRVakP6hQr0UlvoHm6yaSfaSVTdqEpdQt8ZsTGidxJ3JG2cmk0ky+fPevDKZVUrQrnruly+PuXze7/d+v/d7Q9hsNovFEgwGYUu1Wq3RaGiaJsuypmn4P0W43e6BgYHl5eVMJoMQKhQKsixDCBOJBITwPKxeryOEiJ6ens7OziBNY4xVVW02GkjTjJjay0QMDg66XK7FhYVcLndwcPB0d1dRFIPELxPhdDrtNtt3vz7YFfBGVt7IlsJpMZyWwmnxUVoKPxPXs0rk7+qZ1tOmKOqLb2fffW/i3tc/LCT4n0N/zDx8OvPwz9lw6qdA4n40v5yq+PZK/qTSZsJqtVos1Jff//bVjO/HQOJ+7PncE/AgVfUly/79si9Z9iWV3/dKZ1qHzaRpdiHAIfzsuL73vLLHVfdB7dQpUD/PLdhspldX/soe8IXCBUv1omBWq5UkyUBAb5VcKgHAHzJMVVUxxlBDSNNOrWm4zTpsMpGhUBBjLAgCy7KpVKpcLl8kBaNglvn5+XQ6I4kSAEBRlGKxCAAoFAo8zx8fH7Msq7ZyadtOhymKWlxcPDo6KhaLHMcBAGRZFgQBtCSKIsdxjUbj3Mhzc3PJ5L4sy7lcDgDAMAzLshzHGVvwPM+yLMMw+Xz+3xf+ReSlpSUAQLlcBgBIkiQIgrEqiiJJUrFYNHIRBAEh1F7t1dVVURQzmUw+n89mszzPX7RgJEnSNN1sNhVFqVQqqqrWarXmidomycAghPpUnfQ5YAwzwzDValU8kSAIZ8avVCr6PLf6bAqFQsZ++DL6H74s3NfXR5LkFWHj6b0i7Ha7nU4n3Xq3Lw17PB632x0IBK4C9/b2Xv3MY2NjXq/X7/efXvdLwA6Hg6KolZUVjHGtVoMQ1i8svVUGXK/XeZ4/PDxUVRUhpGGMEEIQYg1BCJGm6StCuPUBNQQRJCiK6ujomJ6ejsVi4XCYpulIJPJkJ76x9mh7OxqJxdY2t3biO4/XH29H41ubW+vrkXg8Hl3bjG1FCa/XOzk5SRD677Krq2t4+I3B113Eq+bR21P9Dvv4a92fjnsIgrj74biru+v66Jt3PnnnFYKw3r1mfqufGBoaGhkZMZlMDofDbDbb7HZ7v9Uy4Jn65pebH30wcX3is1t3eoff/vjzqWs3bozevHV76l5vn936vqd7yP4P6JyrnQ25/+oAAAAASUVORK5CYII='); 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":["/static/76954afbb36ac5953d205b7cdab98ff6/a0b58/13_1_1.webp 230w","/static/76954afbb36ac5953d205b7cdab98ff6/86099/13_1_1.webp 392w"],"sizes":"(max-width: 392px) 100vw, 392px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/76954afbb36ac5953d205b7cdab98ff6/81c8e/13_1_1.png 230w","/static/76954afbb36ac5953d205b7cdab98ff6/0acb4/13_1_1.png 392w"],"sizes":"(max-width: 392px) 100vw, 392px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/76954afbb36ac5953d205b7cdab98ff6/0acb4/13_1_1.png","alt":"13 1 1","title":"13 1 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":"p","properties":{},"children":[{"type":"text","value":"The command line interpreter window should open. Type in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pip3 install pygame"}]},{"type":"text","value":" and press "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enter"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This should install the pygame library on your computer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Installation may require system administrator privileges. If the above doesn't work, you can try running the terminal application as an administrator: open the Windows menu, find the CMD application, right-click it and choose \"Run as administrator\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Installing and accessing pygame requires that your Python installation is added to path, as instructed "},{"type":"element","tagName":"a","properties":{"href":"https://www.mooc.fi/en/installation/vscode/#python3","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"here"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h3","properties":{"id":"mac","style":"position:relative;"},"children":[{"type":"text","value":"Mac"},{"type":"element","tagName":"a","properties":{"href":"#mac","ariaLabel":"mac 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":"Open the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Terminal"}]},{"type":"text","value":", for example through the magnifying glass symbol in the top right corner:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 746px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/77ceacb61dfaf80c7c442a4225b65e87/62de4/13-1-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: 24.347826086956523%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAIAAADKYVtkAAAACXBIWXMAABYlAAAWJQFJUiTwAAABGUlEQVQY02OYs2Rje9/sSTOWzlmycdKsZVPmrJg6Z+WC5Zt7py7s6J/dO2nehDnLW6bOb+mftWD55oUrN/VPXVNRPW3SzEVzlqxn0Lf207Hw1rPyhTF89Kx99W38tM08VE3dZE2c7S18z7gX99gnqll7G9n6alt6q5u561l56lv7MLCIaLGIaLGKaLMIQ0kWYU1GIQ1GYU1JMf1QKbs6OY9OeW8DcVMGQTUWIQ1mYQ1WEU0mIQ1mQXUGPnkzEJIzhSBeOVMeWRNZOQtHRYdAZRdzBTt+OVMGaQMOWWOQrKwJCMlASGMGfgVzfjlTXhkjXlljXhlDHil9PmkDNRlTKRkTNlljVjkTPnkzIXlzPnlzXpDRUJvAbFMAjAFKS35RLdoAAAAASUVORK5CYII='); 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":["/static/77ceacb61dfaf80c7c442a4225b65e87/a0b58/13-1-2.webp 230w","/static/77ceacb61dfaf80c7c442a4225b65e87/bc10c/13-1-2.webp 460w","/static/77ceacb61dfaf80c7c442a4225b65e87/f7ebd/13-1-2.webp 746w"],"sizes":"(max-width: 746px) 100vw, 746px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/77ceacb61dfaf80c7c442a4225b65e87/81c8e/13-1-2.png 230w","/static/77ceacb61dfaf80c7c442a4225b65e87/08a84/13-1-2.png 460w","/static/77ceacb61dfaf80c7c442a4225b65e87/62de4/13-1-2.png 746w"],"sizes":"(max-width: 746px) 100vw, 746px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/77ceacb61dfaf80c7c442a4225b65e87/62de4/13-1-2.png","alt":"13 1 2","title":"13 1 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 search tool should open. Type in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"terminal"}]},{"type":"text","value":" and press "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enter"}]},{"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: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/a310ec5ccf9438989514ad8da107dcc3/47218/13-1-3.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: 66.95652173913044%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAIAAAAmMtkJAAAACXBIWXMAABYlAAAWJQFJUiTwAAACa0lEQVQozy2STXLTQBCFdYFwgzh3IlmFDeQGkAQ2LKGo4hBAQhWp7FglQMohln8iO7Yly7JkzWime370M5LsACeg5KTq23W97ur3njX3poNup2t3Bv0+53z9+aL63ilGcx1zlRZK50pnSmdCapQKUQHKvCi/nZ0PnKFlDyZt22nbo+vu3cQnvhP4HvEXHEArnUn1qASUHAQHAahUmg+c0cwPrIOP3dcn+PbcvDw1h6fF8Vn55swcfjVOkJs8lypXOi9MvVr/Xf/5V5Q1CiVUWlYrqVLr99XNz+vBzdB3PDKeM2/BZyF4IeeodZoJmZqy/vTl5PmLg929vXfvPxSm0UuVAUirb/fa1+3e7W0UkygmhFCaJJRSFFKoFIQq6/XR8fHOTuvJ1tbT3T1TrQGl0hmgsJxhMBxPnYnrBVFMgaFuAM1AMZCUYW7ql6+Otre3W63W/v6zLDeMI0rNAK1Ob2L3h+2b3tW17frhkkAYs3DJ4gQJEyRBDspx7i4vf11c/Oh0eknCCWEJw0UYW+4s6N+OnOHE88NFRBdLuohoEBHKxcNxAIkgXW8+Hk8TyilhyyWlCUw93/LcmTube37QpJLmchOPUBnKdIMWQgMIShkhjDHgXHCOHCRNuEVRzfxgNJ66ng/YxCBUo0GhUahHUAmppUql1KIhlSprfg4TGS1JGMVhFLPGx2bQ3NyIYQMKHUZkPPFcz3+ojc7yxu04pjFJHiCUMY4gFEeVbKymDBgXwWLpDO96fcfu9jmI5i+ZMY6WTvMGnWmdZXluTFlWa1OtTFkXpipMWZgKmmKiaJaKsqrr1X29ui/L+j/LRoPjTcXpPgAAAABJRU5ErkJggg=='); 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":["/static/a310ec5ccf9438989514ad8da107dcc3/a0b58/13-1-3.webp 230w","/static/a310ec5ccf9438989514ad8da107dcc3/bc10c/13-1-3.webp 460w","/static/a310ec5ccf9438989514ad8da107dcc3/966d8/13-1-3.webp 920w","/static/a310ec5ccf9438989514ad8da107dcc3/3dddb/13-1-3.webp 1344w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a310ec5ccf9438989514ad8da107dcc3/81c8e/13-1-3.png 230w","/static/a310ec5ccf9438989514ad8da107dcc3/08a84/13-1-3.png 460w","/static/a310ec5ccf9438989514ad8da107dcc3/c0255/13-1-3.png 920w","/static/a310ec5ccf9438989514ad8da107dcc3/47218/13-1-3.png 1344w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/a310ec5ccf9438989514ad8da107dcc3/c0255/13-1-3.png","alt":"13 1 3","title":"13 1 3","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":"Type in the following and press "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enter"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pip3 install pygame"}]}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/cc8d7c319bc7baf0232b2d92350f9631/d56e1/13-1-4.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: 28.695652173913043%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAiElEQVQY06XNwQoCIRSFYd//ZXoJ4dpUIpqEYRZii1y4cRai4xgMGEEboW954PAjcd6BYXd1OR0PhBDGGABgjAkhADBNe8651lpKKYSglHLOHx2yz9ftqp1z1lpjjFIqhDDPc4zRe19Kaa2tm9atHVpqXTa11pRSzrkNQ7/TJ/JdGz3/VR4/vwF1E1iIv4njxgAAAABJRU5ErkJggg=='); 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":["/static/cc8d7c319bc7baf0232b2d92350f9631/a0b58/13-1-4.webp 230w","/static/cc8d7c319bc7baf0232b2d92350f9631/bc10c/13-1-4.webp 460w","/static/cc8d7c319bc7baf0232b2d92350f9631/966d8/13-1-4.webp 920w","/static/cc8d7c319bc7baf0232b2d92350f9631/bbe5b/13-1-4.webp 1130w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/cc8d7c319bc7baf0232b2d92350f9631/81c8e/13-1-4.png 230w","/static/cc8d7c319bc7baf0232b2d92350f9631/08a84/13-1-4.png 460w","/static/cc8d7c319bc7baf0232b2d92350f9631/c0255/13-1-4.png 920w","/static/cc8d7c319bc7baf0232b2d92350f9631/d56e1/13-1-4.png 1130w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/cc8d7c319bc7baf0232b2d92350f9631/c0255/13-1-4.png","alt":"13 1 4","title":"13 1 4","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":"This should install the pygame library on your computer."}]},{"type":"element","tagName":"h2","properties":{"id":"your-first-program","style":"position:relative;"},"children":[{"type":"text","value":"Your first program"},{"type":"element","tagName":"a","properties":{"href":"#your-first-program","ariaLabel":"your first program 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":"Here is a simple program for checking your pygame installation works correctly:"}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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\nwindow"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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":"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":"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":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"When this program is run, it should display a window:"}]},{"type":"element","tagName":"img","properties":{"src":"/d11704399cba1d2075b1135cef373b65/pygame_first.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program only consists of displaying a window, and it runs until the user closes the window."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's take a closer look at the steps required to achieve this. The first line takes the pygame library into use: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import pygame"}]},{"type":"text","value":". The next command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.init"}]},{"type":"text","value":" initializes the pygame modules, and the next one creates a window with the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.display.set_mode"}]},{"type":"text","value":"."}]},{"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":"pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"set_mode"}]},{"type":"text","value":" function takes the window dimensions as an argument. The tuple "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(640, 480)"}]},{"type":"text","value":" indicates that the window is 640 pixels wide and 480 pixels high. The variable name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"window"}]},{"type":"text","value":" can be used later to access the window, for example to draw something in it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following two commands do just that:"}]},{"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":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fill"}]},{"type":"text","value":" method fills the window with the colour passed as an argument. In this case the colour is black, passed as an RGB value in the tuple "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(0, 0, 0)"}]},{"type":"text","value":". The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.display.flip"}]},{"type":"text","value":" updates the contents of the window."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"After these initialization commands the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"main loop"}]},{"type":"text","value":" of the program begins:"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"The main loop handles all events the operating system passes to the program. With each iteration the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.event.get"}]},{"type":"text","value":" returns a list of any events collected since the previous iteration."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the example above the program only handles events of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.QUIT"}]},{"type":"text","value":". This event is raised by, for example, clicking on the exit button in the corner of the window. If the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.QUIT"}]},{"type":"text","value":" event is raised, the program exits through the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exit"}]},{"type":"text","value":" function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can try and see what happens if your program doesn't handle the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.QUIT"}]},{"type":"text","value":" event. This should mean that clicking on the exit button does nothing, which would be confusing for the user. As the program is run from the command line, you can still stop it from the command line with Control+C."}]},{"type":"element","tagName":"h2","properties":{"id":"add-an-image","style":"position:relative;"},"children":[{"type":"text","value":"Add an image"},{"type":"element","tagName":"a","properties":{"href":"#add-an-image","ariaLabel":"add an image 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":"Let's add an image to the window:"}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nwindow"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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":"\nwindow"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"100"}]},{"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":"50"}]},{"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":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"The program uses this image of a robot, which is stored in the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot.png"}]},{"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: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/c90257b456f195bb0d06e65ddbbc1728/a1812/robot.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: 172%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAiCAYAAABfqvm9AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAEyklEQVRIx7WWX0hbdxzFTzRWqNTODLWpsWoRfbGQFtMqtIUykGJsuxWq0dptqTWWVTtFR11u/m0VX/YwN6jazUopQx0Iw7AOmzUmG7bGJMJGYd1gjMFehDFSGIPVJX7Hub3tqrONe9gPvtyv53zOSYi54QLrTnl5ebbZbIbJZPIdPnz495ycnMbHHndq9MiQRbpTWFiIPXv2YMeOHV0HDhwQAAdLS0vB4U6NHhmy6c4Wg8HwWklJiWN7bu45AKM7d+5sNZlMHRzu1OiRIcvMRkU67frChQsXxO/3y2d+v8zevi3T09MyOTmpDndq9MiQZWZdx5o/cn0+36/xpaXVyy7XQ1dPT3JqamolGo2qw50aPTJkmXle4fZ33e7Ep8GgfASkfgbkUmenLMRi6nCnRo8MWWaeW+hzuxOfh8OiGI2pcUB8TqdEolF1uFOjR8a3mUKnoiRi0ah8MjOTujYzI7O3bklobk4d7tTokSGbvtDpTMRiMYkvLqa+jceFwcVIRB3u1OiRIZu2UFGURCQSkQ9HRlJDw8O8ytDoqDrq/khLkVE28w69Hk9iNhSSlszM1HlAXuXnqA13avTIkE1XmOt1u3/7IhgUG/CXA0ieBlIeQDjcqdEjQzbt18bj8SSXlpbk6vi4jFy7JmM3bsjo9eurHO7U6JEh+6x3+OT+P3LkSKCrqyvS0919p6+396vzHR3Lb/X1CYc7NXpkyDKzpsHv92N5eVl9BRHJAJCl3Z9bRXhn4ePW1lbhcNe0rRqTpWV07GAXxsbGUFdXp9Pp1HecdeLEiV8GBgb+UBTlgdvtfuB0Oh+63W7haPsDemTIMsMsO9iFioqKzKqqKpSUlPRXVlb+6PV6k/fv35eFhQWJRqMS5/cuHl/VRtXokSHLDLPsYBeOHz+ut9ls2L9//3X+eiiKsrK4uJgKBAKrwWAwFQwGeRVtVI0eGbLMMMsOdsFqteobGxtRXV090t7eLi6Xa4V3AcPhcFg2GnpkyDLDLDvYhYaGBrXQYrFcdTgcTxfKcwrlcSEzzLKDXf9DodWqbzp1Cpbq6qsd587950JmmGUHu3D05En9yTNnsK+2dtTe3a1C8U18hnGtkBlm2cEuHC0u1r9SVoZ9hYVjr5eWinLp0p/RWCz5ZSCQ0v6zqbm5uSRH+68LPTJkmWGWHeyCHdC/CeCljIz3LwPyTn+/fHPvnty9c4ffOfVnan5+Xh3u1OiRIcsMs+xgF5CfDxw7BlitvJWKDh46FHI4HBFbc/NyW1ubNDc3/2S32+9yuFOjR4YsM2qWHex6coxG9cJ7NRwKoaio6AOGAZyemJgAhzs1emS0+/pJds0pKChQfyAGBwe38KnAYrFcuXjxohiNxrP/vKbxLDV6ZMgyo2U3PvX19fqmpibs3bv3Smdnp+Tl5dlNJhOfdcCdGj0yZJ/9DAKATz61NTVZbe3tMJvNw7xP1xdSo0eGbPaznkWePhUWC2oslqHenh4xGAz2XcXF4HCnRo/M5k52dhk7t+3ePdFgs0nOtm32FwsKwOFOjR4Zjd34vA3ovQDqgIl5QL4H5CYgu4A3qgBwuN/UPDJkvVr2X4WNQGYLgH1A/3vAd8PA1x6d7od84OVdj8rAnRo9MmRbtOzjor8BpWPSyH+RAgAAAAAASUVORK5CYII='); 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":["/static/c90257b456f195bb0d06e65ddbbc1728/6ac13/robot.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/c90257b456f195bb0d06e65ddbbc1728/a1812/robot.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/c90257b456f195bb0d06e65ddbbc1728/a1812/robot.png","alt":"robot","title":"robot","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 file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot.png"}]},{"type":"text","value":" has to be in the same directory with the source code of the your program, or the program won't be able to find it. In the exercise templates for this part the images are waiting in the exercise directory."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The window should now look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/0f7db0536914ccd471841bcee4201632/pygame_pic.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.image.load"}]},{"type":"text","value":" loads the image in the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot.png"}]},{"type":"text","value":" and stores a reference to it in the variable named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot"}]},{"type":"text","value":". The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"blit"}]},{"type":"text","value":" draws the image at the location "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(100, 50)"}]},{"type":"text","value":", and the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.display.flip"}]},{"type":"text","value":" updates the window contents, as before. The location "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(100, 50)"}]},{"type":"text","value":" means that the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"top left corner"}]},{"type":"text","value":" of the image is at that location within the window."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In pygame the origo point "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(0, 0)"}]},{"type":"text","value":" is in the top left corner of the window. The x coordinates increase to the right, and the y coordinates increase downwards, so that the bottom right corner has the coordinates "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(640, 480)"}]},{"type":"text","value":". This is contrary to how coordinates are usually handled in e.g. mathematics, but it is quite common in a programming context, and worth getting used to."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Once you have loaded an image, you can use it many times within the same window. The following code draws the image of the robot at three different locations:"}]},{"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":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"0"}]},{"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":"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":"\nwindow"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"300"}]},{"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":"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":"\nwindow"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"100"}]},{"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":"200"}]},{"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":"The window should look like this as a result:"}]},{"type":"element","tagName":"img","properties":{"src":"/a7807afa43ad1cc7d9686968e6838712/pygame_pic2.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here we set the location of the image so that it lies at the centre of the window:"}]},{"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":"width "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"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":"\nheight "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"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":"\nwindow"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"320"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"width"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"240"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"height"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The window should now look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/498f521486feca567f74dd51c44e371b/pygame_pic3.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"get_width"}]},{"type":"text","value":" returns the width of the image, and the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"get_height"}]},{"type":"text","value":" returns its height, both in pixels. The centre of the window is at half its width and height, so at "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(320, 240)"}]},{"type":"text","value":", which we can use to calculate a suitable location for the top left corner of the image, so that it lies exactly at the centre."}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Pygame exercises"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercises in this part of the course have no automated tests, as the results as visually verified. The tests grant points automatically as you submit your solution to the server, no matter what your implementation. Only submit your solution when you are ready, and your solution matches the exercise description. The exercises may not have automatic tests, but the course staff will still see your solution. Also submitting incomplete solution to TMC Paste grants points automatically, so it should not be used when asking for help with the exercises in this part. You can use "},{"type":"element","tagName":"a","properties":{"href":"https://pastebin.com/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Pastebin.com"}]},{"type":"text","value":" or some another internet pastebin service when asking for help in the course's support channels."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If your solution clearly does not match the exercise description, you may lose the points granted for the exercises in this part."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Four robots","tmcname":"part13-01_four_robots"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which draws a robot in each of the four corners of the window. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/8ba6d635c4ceb93c2b86711f83589b3a/pygame_four.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Robots in a row","tmcname":"part13-02_robots_row"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which draws ten robots in a row. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/8dc589e5970721e2bf8bfe2bd22fa6cf/pygame_row.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"A hundred robots","tmcname":"part13-03_hundred_robots"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which draws a hundred robots: ten rows with ten robots in each row. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/7012bfc1e6ade044e03c53f07f35e7a6/pygame_hundred.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Random robots","tmcname":"part13-04_random_robots"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which draws "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"a thousand"}]},{"type":"text","value":" robots in random places. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/61a30808255873a3e24df2fc559d9367/pygame_thousand.gif"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will have installed the pygame library on your computer
    • \n
    • You will know how to create a pygame window and how to exit a program
    • \n
    • You will be able to use an image stored in a file in a pygame window
    • \n

    In these last two parts of this course material we will get to grips with the pygame library. It is a Python library for programming games. It helps you create graphical elements, handle events from the keyboard and the mouse, and implement other features necessary in games.

    Installing pygame

    Linux

    Open a command line, type in pip3 install pygame an press enter.

    \n \n

    Installing and accessing pygame requires that your Python installation is added to path, as instructed here.

    Mac

    Open the Terminal, for example through the magnifying glass symbol in the top right corner:

    \n \n \n \n \n \n \"13\n \n \n

    Type in the following and press enter:

    pip3 install pygame

    \n \n \n \n \n \n \"13\n \n \n

    This should install the pygame library on your computer.

    Your first program

    Here is a simple program for checking your pygame installation works correctly:

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nwindow.fill((0,0,0))\npygame.display.flip()\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.QUIT:\n            exit()

    When this program is run, it should display a window:

    The program only consists of displaying a window, and it runs until the user closes the window.

    Let's take a closer look at the steps required to achieve this. The first line takes the pygame library into use: import pygame. The next command pygame.init initializes the pygame modules, and the next one creates a window with the function pygame.display.set_mode.

    pygame.init()\nwindow = pygame.display.set_mode((640, 480))

    The set_mode function takes the window dimensions as an argument. The tuple (640, 480) indicates that the window is 640 pixels wide and 480 pixels high. The variable name window can be used later to access the window, for example to draw something in it.

    The following two commands do just that:

    window.fill((0, 0, 0))\npygame.display.flip()

    The fill method fills the window with the colour passed as an argument. In this case the colour is black, passed as an RGB value in the tuple (0, 0, 0). The pygame.display.flip updates the contents of the window.

    After these initialization commands the main loop of the program begins:

    while True:\n    for event in pygame.event.get():\n        if event.type == pygame.QUIT:\n            exit()

    The main loop handles all events the operating system passes to the program. With each iteration the function pygame.event.get returns a list of any events collected since the previous iteration.

    In the example above the program only handles events of type pygame.QUIT. This event is raised by, for example, clicking on the exit button in the corner of the window. If the pygame.QUIT event is raised, the program exits through the exit function.

    You can try and see what happens if your program doesn't handle the pygame.QUIT event. This should mean that clicking on the exit button does nothing, which would be confusing for the user. As the program is run from the command line, you can still stop it from the command line with Control+C.

    Add an image

    Let's add an image to the window:

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nwindow.fill((0, 0, 0))\nwindow.blit(robot, (100, 50))\npygame.display.flip()\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.QUIT:\n            exit()

    The program uses this image of a robot, which is stored in the file robot.png:

    \n \n

    The window should now look like this:

    The function pygame.image.load loads the image in the file robot.png and stores a reference to it in the variable named robot. The method blit draws the image at the location (100, 50), and the function pygame.display.flip updates the window contents, as before. The location (100, 50) means that the top left corner of the image is at that location within the window.

    In pygame the origo point (0, 0) is in the top left corner of the window. The x coordinates increase to the right, and the y coordinates increase downwards, so that the bottom right corner has the coordinates (640, 480). This is contrary to how coordinates are usually handled in e.g. mathematics, but it is quite common in a programming context, and worth getting used to.

    Once you have loaded an image, you can use it many times within the same window. The following code draws the image of the robot at three different locations:

    window.blit(robot, (0, 0))\nwindow.blit(robot, (300, 0))\nwindow.blit(robot, (100, 200))

    The window should look like this as a result:

    Here we set the location of the image so that it lies at the centre of the window:

    width = robot.get_width()\nheight = robot.get_height()\nwindow.blit(robot, (320-width/2, 240-height/2))

    The window should now look like this:

    The method get_width returns the width of the image, and the method get_height returns its height, both in pixels. The centre of the window is at half its width and height, so at (320, 240), which we can use to calculate a suitable location for the top left corner of the image, so that it lies exactly at the centre.

    The exercises in this part of the course have no automated tests, as the results as visually verified. The tests grant points automatically as you submit your solution to the server, no matter what your implementation. Only submit your solution when you are ready, and your solution matches the exercise description. The exercises may not have automatic tests, but the course staff will still see your solution. Also submitting incomplete solution to TMC Paste grants points automatically, so it should not be used when asking for help with the exercises in this part. You can use Pastebin.com or some another internet pastebin service when asking for help in the course's support channels.

    If your solution clearly does not match the exercise description, you may lose the points granted for the exercises in this part.

    Please write a program which draws a robot in each of the four corners of the window. The end result should look like this:

    Please write a program which draws ten robots in a row. The end result should look like this:

    Please write a program which draws a hundred robots: ten rows with ten robots in each row. The end result should look like this:

    Please write a program which draws a thousand robots in random places. The end result should look like this:

    ","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-13/1-pygame.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-13/2-animation/page-data.json b/page-data/part-13/2-animation/page-data.json index 602a43671..880fab275 100644 --- a/page-data/part-13/2-animation/page-data.json +++ b/page-data/part-13/2-animation/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-13/2-animation", - "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 know how to create an animation with pygame"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use a clock to set the speed of your program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use basic trigonometric functions in your animations"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Many games have moving characters, so a logical next step is creating animations. We can create the illusion of movement by drawing the same image in different locations on the screen and timing the changes appropriately."}]},{"type":"element","tagName":"h2","properties":{"id":"creating-an-animation","style":"position:relative;"},"children":[{"type":"text","value":"Creating an animation"},{"type":"element","tagName":"a","properties":{"href":"#creating-an-animation","ariaLabel":"creating an animation 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 following code creates an animation where a robot moves from left to right in a pygame window:"}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nx "},{"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":"text","value":"\ny "},{"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":"text","value":"\nclock "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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 x "},{"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 clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When this is executed, the result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/e0ac2065e7c54214a7440186eb4b9a5b/pygame_animation.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's take a closer look at the commands involved. If we want to trace the movement of the image on the screen, we need to know its location, which is why we have two variables for the coordinates of the top left corner of the image:"}]},{"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":"x "},{"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":"text","value":"\ny "},{"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":"p","properties":{},"children":[{"type":"text","value":"We also have a clock, which we use to make sure the speed of the animation is just right:"}]},{"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":"clock "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"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":"The main loop draws the image at its current location with each iteration:"}]},{"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":" window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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":"First the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fill"}]},{"type":"text","value":" fills the window with black, as before. The colour is passed as a tuple containing the RGB values for the colour. In this case the argument is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(0, 0, 0)"}]},{"type":"text","value":", which means that all three components - red, green and blue - have value 0. Each component can have a value between 0 and 255. So, if we passed "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(255, 255, 255)"}]},{"type":"text","value":" as the argument, we'd get a white window, and with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(255, 0, 0)"}]},{"type":"text","value":" we'd get a red window. RGB colour codes form the backbone of digital colouring, and there are many tools online for working with them, for example "},{"type":"element","tagName":"a","properties":{"href":"https://www.rapidtables.com/web/color/RGB_Color.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"RGB Color Codes Chart"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"After the window is filled with colour the image is drawn at the given location with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"blit"}]},{"type":"text","value":" method. Then the contents of the window are updated with the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.display.flip"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Finally, the value stored in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" is incremented, which makes the image move one pixel to the right with each iteration:"}]},{"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":" x "},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The clock method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":" is called at the end:"}]},{"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":" clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":" takes care of the speed of the animation. The argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"60"}]},{"type":"text","value":" dictates that the loop should be executed 60 times a second, which means that the image moves 60 pixels to the right each second. This approximately matches the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"FPS"}]},{"type":"text","value":" or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"frames per second"}]},{"type":"text","value":" value used with games."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In principle, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":" method makes sure that the animation runs at the same speed on every computer. If there was no such timing involved, the speed of the animation would depend on the speed of the computer."}]},{"type":"element","tagName":"h2","properties":{"id":"bouncing-off-a-wall","style":"position:relative;"},"children":[{"type":"text","value":"Bouncing off a wall"},{"type":"element","tagName":"a","properties":{"href":"#bouncing-off-a-wall","ariaLabel":"bouncing off a wall 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 previous animation was otherwise excellent, but as the robot reached a wall, it just kept going out of sight. Let's make the robot bounce off the wall."}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nx "},{"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":"text","value":"\ny "},{"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":"text","value":"\nvelocity "},{"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":"\nclock "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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 x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" velocity\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" velocity "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"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":" "},{"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":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n velocity "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"velocity\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" velocity "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" x "},{"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 velocity "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"velocity\n\n clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Running the above code should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/5dac9b07ba4879c217b0b35a817a727f/pygame_animation2.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There is a new variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"velocity"}]},{"type":"text","value":" which determines the direction of the movement. If the value is above zero, movement is to the right, and if it is below zero, movement is to the left. More precisely in this case, if the value is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":", the robot moves to the right, and if it is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":", the robot moves to the left."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following lines make the robot bounce off the side walls:"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" velocity "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"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":" "},{"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":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n velocity "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"velocity\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" velocity "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" x "},{"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 velocity "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"velocity"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the velocity is above zero so that the robot is moving to the right, and the right edge if the image goes beyond the right edge of the window, the direction is reversed and the robot starts moving to the left. Similarly, if the velocity is below zero so that the robot is moving to the left, and the left edge of the image reaches the left edge of the window, the direction is again reversed and the robot starts moving to the right again."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This makes the robot move on a path from the left edge of the window to the right edge, and back to the left, and then to the right again, repeated ad infinitum."}]},{"type":"element","tagName":"h2","properties":{"id":"rotation","style":"position:relative;"},"children":[{"type":"text","value":"Rotation"},{"type":"element","tagName":"a","properties":{"href":"#rotation","ariaLabel":"rotation 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":"Let's create one more animation. This time the robot should "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"rotate"}]},{"type":"text","value":" in a circle around the centre of the window:"}]},{"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":"import"}]},{"type":"text","value":" pygame\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" math\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nangle "},{"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":"text","value":"\nclock "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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 x "},{"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":"320"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"cos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"angle"},{"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":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"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":"2"}]},{"type":"text","value":"\n y "},{"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":"240"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sin"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"angle"},{"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":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"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":"2"}]},{"type":"text","value":"\n\n window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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 angle "},{"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.01"}]},{"type":"text","value":"\n clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Running the above code should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/ce164a1f214ed31343271340f9157733/pygame_rotation.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rotation in a relatively precise circle is achieved with the help of some basic trigonometric functions. The varible "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"angle"}]},{"type":"text","value":" contains the angle of the robots location in relation to the centre of the window and the horizontal line running through it. The sine and cosine functions from the Python math library are used to calculate the coordinates of the robot's location:"}]},{"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":" x "},{"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":"320"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"cos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"angle"},{"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":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"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":"2"}]},{"type":"text","value":"\n y "},{"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":"240"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sin"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"angle"},{"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":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"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":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The robot rotates around a circle of radius 100 around the centre of the window. The hypotenuse in this scenario is the radius of the circle. The cosine function gives the length of the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"adjacent"}]},{"type":"text","value":" side of a right triangle in relation to the hypotenuse, which means that it gives us the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" coordinate of the location. The sine function gives the length of the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"opposite"}]},{"type":"text","value":" side, i.e. the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" coordinate. The location is then adjusted for the size of the image, so that the centre of the circle is at the centre of the window."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With each iteration the size of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"angle"}]},{"type":"text","value":" is incremented by 0.01:llä. As we are using radians, a full circle is 2π, which equals about 6.28. It takes about 628 iterations for the robot to go a full circle, and at 60 iterations per second this takes just over 10 seconds."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Vertical movement","tmcname":"part13-05_vertical_movement"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create an animation where the robot moves up and down in an endless loop. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/7dfa8e1aefab9c5f0586d8148b819543/pygame_vertical.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Round the perimeter","tmcname":"part13-06_round_the_perimeter"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create an animation where the robot traces the perimeter of the window. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/b313e2cd6e747c9b47375655616b0e2d/pygame_perimeter.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Two robots","tmcname":"part13-07_two_robots"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create an animation where two robots move back and forth to the left and right. The lower robot should move at double the speed of the upper one. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/5b2b134a4afcdbaa9c11796d3cadb5e2/pygame_move2.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Robots in a circle","tmcname":"part13-08_robot_circle"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create an animation where ten robots go round in a circle. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/c8832b391e97f55d98358c8f0e5bfd57/pygame_circle.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bouncing ball","tmcname":"part13-09_bouncing_ball"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create an animation where a ball bounces from the edges of the window. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/e9503906920a8a19bc3a33143b0dde81/pygame_bounce.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the image "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ball.png"}]},{"type":"text","value":"."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Robot invasion","tmcname":"part13-10_robot_invasion"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create an animation where robots fall from the sky randomly. When a robot reaches the ground, it starts moving to the left or to the right, and finaly disappears off the screen. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/23bd928da4c554324efdf7de4806cc60/pygame_invasion.gif"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will know how to create an animation with pygame
    • \n
    • You will be able to use a clock to set the speed of your program
    • \n
    • You will be able to use basic trigonometric functions in your animations
    • \n

    Many games have moving characters, so a logical next step is creating animations. We can create the illusion of movement by drawing the same image in different locations on the screen and timing the changes appropriately.

    Creating an animation

    The following code creates an animation where a robot moves from left to right in a pygame window:

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nx = 0\ny = 0\nclock = pygame.time.Clock()\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.QUIT:\n            exit()\n\n    window.fill((0, 0, 0))\n    window.blit(robot, (x, y))\n    pygame.display.flip()\n\n    x += 1\n    clock.tick(60)

    When this is executed, the result should look like this:

    Let's take a closer look at the commands involved. If we want to trace the movement of the image on the screen, we need to know its location, which is why we have two variables for the coordinates of the top left corner of the image:

    x = 0\ny = 0

    We also have a clock, which we use to make sure the speed of the animation is just right:

    clock = pygame.time.Clock()

    The main loop draws the image at its current location with each iteration:

        window.fill((0, 0, 0))\n    window.blit(robot, (x, y))\n    pygame.display.flip()

    First the method fill fills the window with black, as before. The colour is passed as a tuple containing the RGB values for the colour. In this case the argument is (0, 0, 0), which means that all three components - red, green and blue - have value 0. Each component can have a value between 0 and 255. So, if we passed (255, 255, 255) as the argument, we'd get a white window, and with (255, 0, 0) we'd get a red window. RGB colour codes form the backbone of digital colouring, and there are many tools online for working with them, for example RGB Color Codes Chart.

    After the window is filled with colour the image is drawn at the given location with the blit method. Then the contents of the window are updated with the function pygame.display.flip.

    Finally, the value stored in x is incremented, which makes the image move one pixel to the right with each iteration:

        x += 1

    The clock method tick is called at the end:

        clock.tick(60)

    The method tick takes care of the speed of the animation. The argument 60 dictates that the loop should be executed 60 times a second, which means that the image moves 60 pixels to the right each second. This approximately matches the FPS or frames per second value used with games.

    In principle, the tick method makes sure that the animation runs at the same speed on every computer. If there was no such timing involved, the speed of the animation would depend on the speed of the computer.

    Bouncing off a wall

    The previous animation was otherwise excellent, but as the robot reached a wall, it just kept going out of sight. Let's make the robot bounce off the wall.

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nx = 0\ny = 0\nvelocity = 1\nclock = pygame.time.Clock()\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.QUIT:\n            exit()\n\n    window.fill((0, 0, 0))\n    window.blit(robot, (x, y))\n    pygame.display.flip()\n    \n    x += velocity\n    if velocity > 0 and x+robot.get_width() >= 640:\n        velocity = -velocity\n    if velocity < 0 and x <= 0:\n        velocity = -velocity\n\n    clock.tick(60)

    Running the above code should look like this:

    There is a new variable velocity which determines the direction of the movement. If the value is above zero, movement is to the right, and if it is below zero, movement is to the left. More precisely in this case, if the value is 1, the robot moves to the right, and if it is -1, the robot moves to the left.

    The following lines make the robot bounce off the side walls:

        if velocity > 0 and x+robot.get_width() >= 640:\n        velocity = -velocity\n    if velocity < 0 and x <= 0:\n        velocity = -velocity

    If the velocity is above zero so that the robot is moving to the right, and the right edge if the image goes beyond the right edge of the window, the direction is reversed and the robot starts moving to the left. Similarly, if the velocity is below zero so that the robot is moving to the left, and the left edge of the image reaches the left edge of the window, the direction is again reversed and the robot starts moving to the right again.

    This makes the robot move on a path from the left edge of the window to the right edge, and back to the left, and then to the right again, repeated ad infinitum.

    Rotation

    Let's create one more animation. This time the robot should rotate in a circle around the centre of the window:

    import pygame\nimport math\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nangle = 0\nclock = pygame.time.Clock()\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.QUIT:\n            exit()\n\n    x = 320+math.cos(angle)*100-robot.get_width()/2\n    y = 240+math.sin(angle)*100-robot.get_height()/2\n\n    window.fill((0, 0, 0))\n    window.blit(robot, (x, y))\n    pygame.display.flip()\n\n    angle += 0.01\n    clock.tick(60)

    Running the above code should look like this:

    Rotation in a relatively precise circle is achieved with the help of some basic trigonometric functions. The varible angle contains the angle of the robots location in relation to the centre of the window and the horizontal line running through it. The sine and cosine functions from the Python math library are used to calculate the coordinates of the robot's location:

            x = 320+math.cos(angle)*100-robot.get_width()/2\n        y = 240+math.sin(angle)*100-robot.get_height()/2

    The robot rotates around a circle of radius 100 around the centre of the window. The hypotenuse in this scenario is the radius of the circle. The cosine function gives the length of the adjacent side of a right triangle in relation to the hypotenuse, which means that it gives us the x coordinate of the location. The sine function gives the length of the opposite side, i.e. the y coordinate. The location is then adjusted for the size of the image, so that the centre of the circle is at the centre of the window.

    With each iteration the size of the angle is incremented by 0.01:llä. As we are using radians, a full circle is 2π, which equals about 6.28. It takes about 628 iterations for the robot to go a full circle, and at 60 iterations per second this takes just over 10 seconds.

    Please create an animation where the robot moves up and down in an endless loop. The end result should look like this:

    Please create an animation where the robot traces the perimeter of the window. The end result should look like this:

    Please create an animation where two robots move back and forth to the left and right. The lower robot should move at double the speed of the upper one. The end result should look like this:

    Please create an animation where ten robots go round in a circle. The end result should look like this:

    Please create an animation where a ball bounces from the edges of the window. The end result should look like this:

    The exercise template contains the image ball.png.

    Please create an animation where robots fall from the sky randomly. When a robot reaches the ground, it starts moving to the left or to the right, and finaly disappears off the screen. The end result should look like this:

    ","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-13/2-animation.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know how to create an animation with pygame"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use a clock to set the speed of your program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use basic trigonometric functions in your animations"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Many games have moving characters, so a logical next step is creating animations. We can create the illusion of movement by drawing the same image in different locations on the screen and timing the changes appropriately."}]},{"type":"element","tagName":"h2","properties":{"id":"creating-an-animation","style":"position:relative;"},"children":[{"type":"text","value":"Creating an animation"},{"type":"element","tagName":"a","properties":{"href":"#creating-an-animation","ariaLabel":"creating an animation 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 following code creates an animation where a robot moves from left to right in a pygame window:"}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nx "},{"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":"text","value":"\ny "},{"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":"text","value":"\nclock "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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 x "},{"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 clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When this is executed, the result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/e0ac2065e7c54214a7440186eb4b9a5b/pygame_animation.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's take a closer look at the commands involved. If we want to trace the movement of the image on the screen, we need to know its location, which is why we have two variables for the coordinates of the top left corner of the image:"}]},{"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":"x "},{"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":"text","value":"\ny "},{"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":"p","properties":{},"children":[{"type":"text","value":"We also have a clock, which we use to make sure the speed of the animation is just right:"}]},{"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":"clock "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"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":"The main loop draws the image at its current location with each iteration:"}]},{"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":" window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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":"First the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fill"}]},{"type":"text","value":" fills the window with black, as before. The colour is passed as a tuple containing the RGB values for the colour. In this case the argument is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(0, 0, 0)"}]},{"type":"text","value":", which means that all three components - red, green and blue - have value 0. Each component can have a value between 0 and 255. So, if we passed "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(255, 255, 255)"}]},{"type":"text","value":" as the argument, we'd get a white window, and with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(255, 0, 0)"}]},{"type":"text","value":" we'd get a red window. RGB colour codes form the backbone of digital colouring, and there are many tools online for working with them, for example "},{"type":"element","tagName":"a","properties":{"href":"https://www.rapidtables.com/web/color/RGB_Color.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"RGB Color Codes Chart"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"After the window is filled with colour the image is drawn at the given location with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"blit"}]},{"type":"text","value":" method. Then the contents of the window are updated with the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.display.flip"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Finally, the value stored in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" is incremented, which makes the image move one pixel to the right with each iteration:"}]},{"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":" x "},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The clock method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":" is called at the end:"}]},{"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":" clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":" takes care of the speed of the animation. The argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"60"}]},{"type":"text","value":" dictates that the loop should be executed 60 times a second, which means that the image moves 60 pixels to the right each second. This approximately matches the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"FPS"}]},{"type":"text","value":" or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"frames per second"}]},{"type":"text","value":" value used with games."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In principle, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":" method makes sure that the animation runs at the same speed on every computer. If there was no such timing involved, the speed of the animation would depend on the speed of the computer."}]},{"type":"element","tagName":"h2","properties":{"id":"bouncing-off-a-wall","style":"position:relative;"},"children":[{"type":"text","value":"Bouncing off a wall"},{"type":"element","tagName":"a","properties":{"href":"#bouncing-off-a-wall","ariaLabel":"bouncing off a wall 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 previous animation was otherwise excellent, but as the robot reached a wall, it just kept going out of sight. Let's make the robot bounce off the wall."}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nx "},{"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":"text","value":"\ny "},{"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":"text","value":"\nvelocity "},{"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":"\nclock "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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 x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" velocity\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" velocity "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"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":" "},{"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":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n velocity "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"velocity\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" velocity "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" x "},{"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 velocity "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"velocity\n\n clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Running the above code should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/5dac9b07ba4879c217b0b35a817a727f/pygame_animation2.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There is a new variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"velocity"}]},{"type":"text","value":" which determines the direction of the movement. If the value is above zero, movement is to the right, and if it is below zero, movement is to the left. More precisely in this case, if the value is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":", the robot moves to the right, and if it is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":", the robot moves to the left."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following lines make the robot bounce off the side walls:"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" velocity "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"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":" "},{"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":"640"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n velocity "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"velocity\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" velocity "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" x "},{"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 velocity "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"velocity"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the velocity is above zero so that the robot is moving to the right, and the right edge if the image goes beyond the right edge of the window, the direction is reversed and the robot starts moving to the left. Similarly, if the velocity is below zero so that the robot is moving to the left, and the left edge of the image reaches the left edge of the window, the direction is again reversed and the robot starts moving to the right again."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This makes the robot move on a path from the left edge of the window to the right edge, and back to the left, and then to the right again, repeated ad infinitum."}]},{"type":"element","tagName":"h2","properties":{"id":"rotation","style":"position:relative;"},"children":[{"type":"text","value":"Rotation"},{"type":"element","tagName":"a","properties":{"href":"#rotation","ariaLabel":"rotation 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":"Let's create one more animation. This time the robot should "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"rotate"}]},{"type":"text","value":" in a circle around the centre of the window:"}]},{"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":"import"}]},{"type":"text","value":" pygame\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" math\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nangle "},{"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":"text","value":"\nclock "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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 x "},{"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":"320"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"cos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"angle"},{"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":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"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":"2"}]},{"type":"text","value":"\n y "},{"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":"240"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sin"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"angle"},{"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":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"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":"2"}]},{"type":"text","value":"\n\n window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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 angle "},{"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.01"}]},{"type":"text","value":"\n clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Running the above code should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/ce164a1f214ed31343271340f9157733/pygame_rotation.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Rotation in a relatively precise circle is achieved with the help of some basic trigonometric functions. The varible "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"angle"}]},{"type":"text","value":" contains the angle of the robots location in relation to the centre of the window and the horizontal line running through it. The sine and cosine functions from the Python math library are used to calculate the coordinates of the robot's location:"}]},{"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":" x "},{"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":"320"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"cos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"angle"},{"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":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"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":"2"}]},{"type":"text","value":"\n y "},{"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":"240"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sin"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"angle"},{"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":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"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":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The robot rotates around a circle of radius 100 around the centre of the window. The hypotenuse in this scenario is the radius of the circle. The cosine function gives the length of the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"adjacent"}]},{"type":"text","value":" side of a right triangle in relation to the hypotenuse, which means that it gives us the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" coordinate of the location. The sine function gives the length of the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"opposite"}]},{"type":"text","value":" side, i.e. the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" coordinate. The location is then adjusted for the size of the image, so that the centre of the circle is at the centre of the window."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With each iteration the size of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"angle"}]},{"type":"text","value":" is incremented by 0.01:llä. As we are using radians, a full circle is 2π, which equals about 6.28. It takes about 628 iterations for the robot to go a full circle, and at 60 iterations per second this takes just over 10 seconds."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Vertical movement","tmcname":"part13-05_vertical_movement"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create an animation where the robot moves up and down in an endless loop. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/7dfa8e1aefab9c5f0586d8148b819543/pygame_vertical.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Round the perimeter","tmcname":"part13-06_round_the_perimeter"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create an animation where the robot traces the perimeter of the window. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/b313e2cd6e747c9b47375655616b0e2d/pygame_perimeter.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Two robots","tmcname":"part13-07_two_robots"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create an animation where two robots move back and forth to the left and right. The lower robot should move at double the speed of the upper one. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/5b2b134a4afcdbaa9c11796d3cadb5e2/pygame_move2.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Robots in a circle","tmcname":"part13-08_robot_circle"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create an animation where ten robots go round in a circle. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/c8832b391e97f55d98358c8f0e5bfd57/pygame_circle.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Bouncing ball","tmcname":"part13-09_bouncing_ball"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create an animation where a ball bounces from the edges of the window. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/e9503906920a8a19bc3a33143b0dde81/pygame_bounce.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the image "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ball.png"}]},{"type":"text","value":"."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Robot invasion","tmcname":"part13-10_robot_invasion"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create an animation where robots fall from the sky randomly. When a robot reaches the ground, it starts moving to the left or to the right, and finaly disappears off the screen. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/23bd928da4c554324efdf7de4806cc60/pygame_invasion.gif"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will know how to create an animation with pygame
    • \n
    • You will be able to use a clock to set the speed of your program
    • \n
    • You will be able to use basic trigonometric functions in your animations
    • \n

    Many games have moving characters, so a logical next step is creating animations. We can create the illusion of movement by drawing the same image in different locations on the screen and timing the changes appropriately.

    Creating an animation

    The following code creates an animation where a robot moves from left to right in a pygame window:

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nx = 0\ny = 0\nclock = pygame.time.Clock()\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.QUIT:\n            exit()\n\n    window.fill((0, 0, 0))\n    window.blit(robot, (x, y))\n    pygame.display.flip()\n\n    x += 1\n    clock.tick(60)

    When this is executed, the result should look like this:

    Let's take a closer look at the commands involved. If we want to trace the movement of the image on the screen, we need to know its location, which is why we have two variables for the coordinates of the top left corner of the image:

    x = 0\ny = 0

    We also have a clock, which we use to make sure the speed of the animation is just right:

    clock = pygame.time.Clock()

    The main loop draws the image at its current location with each iteration:

        window.fill((0, 0, 0))\n    window.blit(robot, (x, y))\n    pygame.display.flip()

    First the method fill fills the window with black, as before. The colour is passed as a tuple containing the RGB values for the colour. In this case the argument is (0, 0, 0), which means that all three components - red, green and blue - have value 0. Each component can have a value between 0 and 255. So, if we passed (255, 255, 255) as the argument, we'd get a white window, and with (255, 0, 0) we'd get a red window. RGB colour codes form the backbone of digital colouring, and there are many tools online for working with them, for example RGB Color Codes Chart.

    After the window is filled with colour the image is drawn at the given location with the blit method. Then the contents of the window are updated with the function pygame.display.flip.

    Finally, the value stored in x is incremented, which makes the image move one pixel to the right with each iteration:

        x += 1

    The clock method tick is called at the end:

        clock.tick(60)

    The method tick takes care of the speed of the animation. The argument 60 dictates that the loop should be executed 60 times a second, which means that the image moves 60 pixels to the right each second. This approximately matches the FPS or frames per second value used with games.

    In principle, the tick method makes sure that the animation runs at the same speed on every computer. If there was no such timing involved, the speed of the animation would depend on the speed of the computer.

    Bouncing off a wall

    The previous animation was otherwise excellent, but as the robot reached a wall, it just kept going out of sight. Let's make the robot bounce off the wall.

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nx = 0\ny = 0\nvelocity = 1\nclock = pygame.time.Clock()\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.QUIT:\n            exit()\n\n    window.fill((0, 0, 0))\n    window.blit(robot, (x, y))\n    pygame.display.flip()\n    \n    x += velocity\n    if velocity > 0 and x+robot.get_width() >= 640:\n        velocity = -velocity\n    if velocity < 0 and x <= 0:\n        velocity = -velocity\n\n    clock.tick(60)

    Running the above code should look like this:

    There is a new variable velocity which determines the direction of the movement. If the value is above zero, movement is to the right, and if it is below zero, movement is to the left. More precisely in this case, if the value is 1, the robot moves to the right, and if it is -1, the robot moves to the left.

    The following lines make the robot bounce off the side walls:

        if velocity > 0 and x+robot.get_width() >= 640:\n        velocity = -velocity\n    if velocity < 0 and x <= 0:\n        velocity = -velocity

    If the velocity is above zero so that the robot is moving to the right, and the right edge if the image goes beyond the right edge of the window, the direction is reversed and the robot starts moving to the left. Similarly, if the velocity is below zero so that the robot is moving to the left, and the left edge of the image reaches the left edge of the window, the direction is again reversed and the robot starts moving to the right again.

    This makes the robot move on a path from the left edge of the window to the right edge, and back to the left, and then to the right again, repeated ad infinitum.

    Rotation

    Let's create one more animation. This time the robot should rotate in a circle around the centre of the window:

    import pygame\nimport math\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nangle = 0\nclock = pygame.time.Clock()\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.QUIT:\n            exit()\n\n    x = 320+math.cos(angle)*100-robot.get_width()/2\n    y = 240+math.sin(angle)*100-robot.get_height()/2\n\n    window.fill((0, 0, 0))\n    window.blit(robot, (x, y))\n    pygame.display.flip()\n\n    angle += 0.01\n    clock.tick(60)

    Running the above code should look like this:

    Rotation in a relatively precise circle is achieved with the help of some basic trigonometric functions. The varible angle contains the angle of the robots location in relation to the centre of the window and the horizontal line running through it. The sine and cosine functions from the Python math library are used to calculate the coordinates of the robot's location:

            x = 320+math.cos(angle)*100-robot.get_width()/2\n        y = 240+math.sin(angle)*100-robot.get_height()/2

    The robot rotates around a circle of radius 100 around the centre of the window. The hypotenuse in this scenario is the radius of the circle. The cosine function gives the length of the adjacent side of a right triangle in relation to the hypotenuse, which means that it gives us the x coordinate of the location. The sine function gives the length of the opposite side, i.e. the y coordinate. The location is then adjusted for the size of the image, so that the centre of the circle is at the centre of the window.

    With each iteration the size of the angle is incremented by 0.01:llä. As we are using radians, a full circle is 2π, which equals about 6.28. It takes about 628 iterations for the robot to go a full circle, and at 60 iterations per second this takes just over 10 seconds.

    Please create an animation where the robot moves up and down in an endless loop. The end result should look like this:

    Please create an animation where the robot traces the perimeter of the window. The end result should look like this:

    Please create an animation where two robots move back and forth to the left and right. The lower robot should move at double the speed of the upper one. The end result should look like this:

    Please create an animation where ten robots go round in a circle. The end result should look like this:

    Please create an animation where a ball bounces from the edges of the window. The end result should look like this:

    The exercise template contains the image ball.png.

    Please create an animation where robots fall from the sky randomly. When a robot reaches the ground, it starts moving to the left or to the right, and finaly disappears off the screen. The end result should look like this:

    ","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-13/2-animation.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-13/3-events/page-data.json b/page-data/part-13/3-events/page-data.json index 53f977d03..809afcaed 100644 --- a/page-data/part-13/3-events/page-data.json +++ b/page-data/part-13/3-events/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-13/3-events", - "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 pygame events"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to write a program which reacts to key presses"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to write a program which reacts to mouse events"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Thus far our main loops have only executed predetermined animations and reacted to only "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.QUIT"}]},{"type":"text","value":" type events, even though the loop gets a list of all events from the operating system. Let's get to grips with some other types of events, then."}]},{"type":"element","tagName":"h2","properties":{"id":"handling-events","style":"position:relative;"},"children":[{"type":"text","value":"Handling events"},{"type":"element","tagName":"a","properties":{"href":"#handling-events","ariaLabel":"handling events 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 program prints out information about all the events passed by the operating system to the pygame program, while it is running:"}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"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":"event"},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"Let's assume the program was left running for a while, and then the exit button was clicked. The program prints out the following info:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The first few events concern mouse usage, ten there are some events from the keyboard, and finally the last event closes the program. Each event has at least a type, but they may also offer some other identifying info, such as the location of the mouse cursor or the key that was pressed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can look for event descriptions in the "},{"type":"element","tagName":"a","properties":{"href":"https://www.pygame.org/docs/ref/event.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"pygame documentation"}]},{"type":"text","value":", but it can sometimes be easier to print out events with the code above, and look for the event that occurs when something you want to react to happens."}]},{"type":"element","tagName":"h2","properties":{"id":"keyboard-events","style":"position:relative;"},"children":[{"type":"text","value":"Keyboard events"},{"type":"element","tagName":"a","properties":{"href":"#keyboard-events","ariaLabel":"keyboard events 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 program can process events where the user presses the arrow key either to the right or to the left on their keyboard. The program prints out which key was pressed."}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"KEYDOWN"},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_LEFT"},{"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":"\"left\""}]},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_RIGHT"},{"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":"\"right\""}]},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"The constants "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.K_LEFT"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.K_RIGHT"}]},{"type":"text","value":" refer to the arrow keys to the left and right. The pgyame key constants for the different keys on a keyboard are listed in the "},{"type":"element","tagName":"a","properties":{"href":"https://www.pygame.org/docs/ref/key.html#key-constants-label","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"pygame documentation"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, if the user presses the arrow key to the right twice, then the left one once, and then the right one once more, the program prints out"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":"right\nright\nleft\nright"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We now have all the tools needed to move a character, or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"sprite"}]},{"type":"text","value":", on the screen to the right and left with the arrow keys. The following code will achieve 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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nx "},{"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":"text","value":"\ny "},{"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":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"KEYDOWN"},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_LEFT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_RIGHT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"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":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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":"Depending on how you use your arrow keys, running the program could look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/aac8387e39d3bb4adbfc1c36cae57345/pygame_move_robot.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the code above we have the variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" which contain the coordinate location for the sprite. The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" is set so that the sprite appears at the bottom of the window. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" value does not change throughout the execution of the program. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" value, however, increases by 10 whenever the user presses the arrow key to the right, and decreases by 10 whenever the left arrow key is pressed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program works otherwise quite well, but the key needs to be pressed again each time we want to move again. It would be better if the movement was continuous as the key was held down. The following program offfers this functionality:"}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nx "},{"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":"text","value":"\ny "},{"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":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"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\nto_right "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\nto_left "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\nclock "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"KEYDOWN"},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_LEFT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n to_left "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_RIGHT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n to_right "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"KEYUP"},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_LEFT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n to_left "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_RIGHT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n to_right "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"if"}]},{"type":"text","value":" to_right"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"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":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" to_left"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"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":"2"}]},{"type":"text","value":"\n\n window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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 clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The code now contains the variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"to_right"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"to_left"}]},{"type":"text","value":". These contain knowledge of whether the sprite should be moving to the right or to the left at any given moment. When the user presses down an arrow key, the value stored in the relevant variable become "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":". When the key is released, the value changes to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The clock is used to time the movements of the sprite, so that they potentially happen 60 times each second. If an arrow key is pressed, the sprite moves two pixels to the right or to the left. This means the sprite moves 120 pixels per second if the key is kept pressed down."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Four directions","tmcname":"part13-11_four_directions"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program where the player can move a robot in four directions with the arrow keys on the keyboard. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/0461dfa5cb7291b279185dba96dfd819/pygame_four_directions.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Four walls","tmcname":"part13-12_four_walls"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please improve the program in the previous exercise so that the robot cannot pass outside the window in any of the four directions. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/bddcb9af6c636563eea121e3360b7b37/pygame_four_walls.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Two players","tmcname":"part13-13_two_players"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program where two players each direct their own robot. One of the players should use the arrow keys while the other could use, for example, the w-s-a-d keys. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/57101a815ad1b5363307bc90dc112da6/pygame_two_players.gif"},"children":[]}]},{"type":"element","tagName":"h2","properties":{"id":"mouse-events","style":"position:relative;"},"children":[{"type":"text","value":"Mouse events"},{"type":"element","tagName":"a","properties":{"href":"#mouse-events","ariaLabel":"mouse events 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 following code reacts to events where a mouse button is pressed down while the cursor is within the window area:"}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"MOUSEBUTTONDOWN"},{"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":"\"you pressed the button number\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"button"},{"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":"\"at location\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pos"},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"The execution of this program should look more or less like this:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":"you pressed the button number 1 at location (82, 135)\nyou pressed the button number 1 at location (369, 135)\nyou pressed the button number 1 at location (269, 297)\nyou pressed the button number 3 at location (515, 324)"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Button number 1 refers to the left mouse button and button number 3 refers to the right mouse button."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This next program combines mouse event handling and drawing an image on the screen. When the user presses a mouse button while the mouse cursor is within the bounds of the window, an image of a robot is drawn at that location."}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"MOUSEBUTTONDOWN"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pos"},{"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","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"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":"2"}]},{"type":"text","value":"\n y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pos"},{"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","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"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":"2"}]},{"type":"text","value":"\n\n window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"The execution of the program could look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/a41511bd13655f68553cc7e34de61edf/pygame_cursor.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program contains an animation where the robot sprite follows the mouse cursor.The location of the sprite is stored in the variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_x"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_y"}]},{"type":"text","value":". When the mouse moves, its location is stored in the variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"target_x"}]},{"type":"text","value":" ja "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"target_y"}]},{"type":"text","value":". If the robot is not at this location, it moves to the approproate direction."}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrobot_x "},{"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":"text","value":"\nrobot_y "},{"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":"text","value":"\ntarget_x "},{"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":"text","value":"\ntarget_y "},{"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":"text","value":"\n\nclock "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"MOUSEMOTION"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n target_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pos"},{"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","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"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":"2"}]},{"type":"text","value":"\n target_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pos"},{"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","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"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":"2"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" robot_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" target_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n robot_x "},{"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":"if"}]},{"type":"text","value":" robot_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" target_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n robot_x "},{"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":"if"}]},{"type":"text","value":" robot_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" target_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n robot_y "},{"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":"if"}]},{"type":"text","value":" robot_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" target_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n robot_y "},{"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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" robot_y"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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 clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program's execution should look more or less like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/cd4d53df2a1564c181f38b7a2d45e7c9/pygame_cursor2.gif"},"children":[]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Robot and mouse","tmcname":"part13-14_robot_and_mouse"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program where the robot follows the mouse cursor so that the centre of the robot is always directly at the mouse cursor. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/58d3b2e994913c8089688bcde5e4ed86/pygame_robot_cursor.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The location of the robot","tmcname":"part13-15_robot_location"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program where the robot appears at a random location within the window. When the player clicks on the robot with the mouse, the robot moves to a new location. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/93991031970283be78df3ba4341a7362/pygame_robot_location.gif"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will be familiar with pygame events
    • \n
    • You will be able to write a program which reacts to key presses
    • \n
    • You will be able to write a program which reacts to mouse events
    • \n

    Thus far our main loops have only executed predetermined animations and reacted to only pygame.QUIT type events, even though the loop gets a list of all events from the operating system. Let's get to grips with some other types of events, then.

    Handling events

    This program prints out information about all the events passed by the operating system to the pygame program, while it is running:

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nwhile True:\n    for event in pygame.event.get():\n        print(event)\n        if event.type == pygame.QUIT:\n            exit()

    Let's assume the program was left running for a while, and then the exit button was clicked. The program prints out the following info:

    <Event(4-MouseMotion {'pos': (495, 274), 'rel': (495, 274), 'buttons': (0, 0, 0), 'window': None})>\n<Event(4-MouseMotion {'pos': (494, 274), 'rel': (-1, 0), 'buttons': (0, 0, 0), 'window': None})>\n<Event(4-MouseMotion {'pos': (492, 274), 'rel': (-2, 0), 'buttons': (0, 0, 0), 'window': None})>\n<Event(4-MouseMotion {'pos': (491, 274), 'rel': (-1, 0), 'buttons': (0, 0, 0), 'window': None})>\n<Event(5-MouseButtonDown {'pos': (491, 274), 'button': 1, 'window': None})>\n<Event(6-MouseButtonUp {'pos': (491, 274), 'button': 1, 'window': None})>\n<Event(2-KeyDown {'unicode': 'a', 'key': 97, 'mod': 0, 'scancode': 38, 'window': None})>\n<Event(3-KeyUp {'key': 97, 'mod': 0, 'scancode': 38, 'window': None})>\n<Event(2-KeyDown {'unicode': 'b', 'key': 98, 'mod': 0, 'scancode': 56, 'window': None})>\n<Event(3-KeyUp {'key': 98, 'mod': 0, 'scancode': 56, 'window': None})>\n<Event(2-KeyDown {'unicode': 'c', 'key': 99, 'mod': 0, 'scancode': 54, 'window': None})>\n<Event(3-KeyUp {'key': 99, 'mod': 0, 'scancode': 54, 'window': None})>\n<Event(12-Quit {})>

    The first few events concern mouse usage, ten there are some events from the keyboard, and finally the last event closes the program. Each event has at least a type, but they may also offer some other identifying info, such as the location of the mouse cursor or the key that was pressed.

    You can look for event descriptions in the pygame documentation, but it can sometimes be easier to print out events with the code above, and look for the event that occurs when something you want to react to happens.

    Keyboard events

    This program can process events where the user presses the arrow key either to the right or to the left on their keyboard. The program prints out which key was pressed.

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.KEYDOWN:\n            if event.key == pygame.K_LEFT:\n                print(\"left\")\n            if event.key == pygame.K_RIGHT:\n                print(\"right\")\n\n        if event.type == pygame.QUIT:\n            exit()

    The constants pygame.K_LEFT and pygame.K_RIGHT refer to the arrow keys to the left and right. The pgyame key constants for the different keys on a keyboard are listed in the pygame documentation.

    For example, if the user presses the arrow key to the right twice, then the left one once, and then the right one once more, the program prints out

    right\nright\nleft\nright

    We now have all the tools needed to move a character, or sprite, on the screen to the right and left with the arrow keys. The following code will achieve this:

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\nx = 0\ny = 480-robot.get_height()\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.KEYDOWN:\n            if event.key == pygame.K_LEFT:\n                x -= 10\n            if event.key == pygame.K_RIGHT:\n                x += 10\n\n        if event.type == pygame.QUIT:\n            exit()\n\n    window.fill((0, 0, 0))\n    window.blit(robot, (x, y))\n    pygame.display.flip()

    Depending on how you use your arrow keys, running the program could look like this:

    In the code above we have the variables x and y which contain the coordinate location for the sprite. The variable y is set so that the sprite appears at the bottom of the window. The y value does not change throughout the execution of the program. The x value, however, increases by 10 whenever the user presses the arrow key to the right, and decreases by 10 whenever the left arrow key is pressed.

    The program works otherwise quite well, but the key needs to be pressed again each time we want to move again. It would be better if the movement was continuous as the key was held down. The following program offfers this functionality:

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\nx = 0\ny = 480-robot.get_height()\n\nto_right = False\nto_left = False\n\nclock = pygame.time.Clock()\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.KEYDOWN:\n            if event.key == pygame.K_LEFT:\n                to_left = True\n            if event.key == pygame.K_RIGHT:\n                to_right = True\n\n        if event.type == pygame.KEYUP:\n            if event.key == pygame.K_LEFT:\n                to_left = False\n            if event.key == pygame.K_RIGHT:\n                to_right = False\n\n        if event.type == pygame.QUIT:\n            exit()\n\n    if to_right:\n        x += 2\n    if to_left:\n        x -= 2\n\n    window.fill((0, 0, 0))\n    window.blit(robot, (x, y))\n    pygame.display.flip()\n\n    clock.tick(60)

    The code now contains the variables to_right and to_left. These contain knowledge of whether the sprite should be moving to the right or to the left at any given moment. When the user presses down an arrow key, the value stored in the relevant variable become True. When the key is released, the value changes to False.

    The clock is used to time the movements of the sprite, so that they potentially happen 60 times each second. If an arrow key is pressed, the sprite moves two pixels to the right or to the left. This means the sprite moves 120 pixels per second if the key is kept pressed down.

    Please write a program where the player can move a robot in four directions with the arrow keys on the keyboard. The end result should look like this:

    Please improve the program in the previous exercise so that the robot cannot pass outside the window in any of the four directions. The end result should look like this:

    Please write a program where two players each direct their own robot. One of the players should use the arrow keys while the other could use, for example, the w-s-a-d keys. The end result should look like this:

    Mouse events

    The following code reacts to events where a mouse button is pressed down while the cursor is within the window area:

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.MOUSEBUTTONDOWN:\n            print(\"you pressed the button number\", event.button, \"at location\", event.pos)\n\n        if event.type == pygame.QUIT:\n            exit()

    The execution of this program should look more or less like this:

    you pressed the button number 1 at location (82, 135)\nyou pressed the button number 1 at location (369, 135)\nyou pressed the button number 1 at location (269, 297)\nyou pressed the button number 3 at location (515, 324)

    Button number 1 refers to the left mouse button and button number 3 refers to the right mouse button.

    This next program combines mouse event handling and drawing an image on the screen. When the user presses a mouse button while the mouse cursor is within the bounds of the window, an image of a robot is drawn at that location.

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.MOUSEBUTTONDOWN:\n            x = event.pos[0]-robot.get_width()/2\n            y = event.pos[1]-robot.get_height()/2\n\n            window.fill((0, 0, 0))\n            window.blit(robot, (x, y))\n            pygame.display.flip()\n\n        if event.type == pygame.QUIT:\n            exit()

    The execution of the program could look like this:

    The following program contains an animation where the robot sprite follows the mouse cursor.The location of the sprite is stored in the variables robot_x and robot_y. When the mouse moves, its location is stored in the variables target_x ja target_y. If the robot is not at this location, it moves to the approproate direction.

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nrobot_x = 0\nrobot_y = 0\ntarget_x = 0\ntarget_y = 0\n\nclock = pygame.time.Clock()\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.MOUSEMOTION:\n            target_x = event.pos[0]-robot.get_width()/2\n            target_y = event.pos[1]-robot.get_height()/2\n\n        if event.type == pygame.QUIT:\n            exit(0)\n\n    if robot_x > target_x:\n        robot_x -= 1\n    if robot_x < target_x:\n        robot_x += 1\n    if robot_y > target_y:\n        robot_y -= 1\n    if robot_y < target_y:\n        robot_y += 1\n\n    window.fill((0, 0, 0))\n    window.blit(robot, (robot_x, robot_y))\n    pygame.display.flip()\n\n    clock.tick(60)

    The program's execution should look more or less like this:

    Please write a program where the robot follows the mouse cursor so that the centre of the robot is always directly at the mouse cursor. The end result should look like this:

    Please write a program where the robot appears at a random location within the window. When the player clicks on the robot with the mouse, the robot moves to a new location. The end result should look like this:

    ","frontmatter":{"path":"/part-13/3-events","title":"Events"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-13/3-events.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 pygame events"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to write a program which reacts to key presses"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to write a program which reacts to mouse events"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Thus far our main loops have only executed predetermined animations and reacted to only "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.QUIT"}]},{"type":"text","value":" type events, even though the loop gets a list of all events from the operating system. Let's get to grips with some other types of events, then."}]},{"type":"element","tagName":"h2","properties":{"id":"handling-events","style":"position:relative;"},"children":[{"type":"text","value":"Handling events"},{"type":"element","tagName":"a","properties":{"href":"#handling-events","ariaLabel":"handling events 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 program prints out information about all the events passed by the operating system to the pygame program, while it is running:"}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"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":"event"},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"Let's assume the program was left running for a while, and then the exit button was clicked. The program prints out the following info:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":"\n\n\n\n\n\n\n\n\n\n\n\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The first few events concern mouse usage, ten there are some events from the keyboard, and finally the last event closes the program. Each event has at least a type, but they may also offer some other identifying info, such as the location of the mouse cursor or the key that was pressed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can look for event descriptions in the "},{"type":"element","tagName":"a","properties":{"href":"https://www.pygame.org/docs/ref/event.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"pygame documentation"}]},{"type":"text","value":", but it can sometimes be easier to print out events with the code above, and look for the event that occurs when something you want to react to happens."}]},{"type":"element","tagName":"h2","properties":{"id":"keyboard-events","style":"position:relative;"},"children":[{"type":"text","value":"Keyboard events"},{"type":"element","tagName":"a","properties":{"href":"#keyboard-events","ariaLabel":"keyboard events 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 program can process events where the user presses the arrow key either to the right or to the left on their keyboard. The program prints out which key was pressed."}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"KEYDOWN"},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_LEFT"},{"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":"\"left\""}]},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_RIGHT"},{"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":"\"right\""}]},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"The constants "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.K_LEFT"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.K_RIGHT"}]},{"type":"text","value":" refer to the arrow keys to the left and right. The pgyame key constants for the different keys on a keyboard are listed in the "},{"type":"element","tagName":"a","properties":{"href":"https://www.pygame.org/docs/ref/key.html#key-constants-label","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"pygame documentation"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, if the user presses the arrow key to the right twice, then the left one once, and then the right one once more, the program prints out"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":"right\nright\nleft\nright"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We now have all the tools needed to move a character, or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"sprite"}]},{"type":"text","value":", on the screen to the right and left with the arrow keys. The following code will achieve 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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nx "},{"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":"text","value":"\ny "},{"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":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"KEYDOWN"},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_LEFT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_RIGHT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"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":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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":"Depending on how you use your arrow keys, running the program could look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/aac8387e39d3bb4adbfc1c36cae57345/pygame_move_robot.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the code above we have the variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" which contain the coordinate location for the sprite. The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" is set so that the sprite appears at the bottom of the window. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" value does not change throughout the execution of the program. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" value, however, increases by 10 whenever the user presses the arrow key to the right, and decreases by 10 whenever the left arrow key is pressed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program works otherwise quite well, but the key needs to be pressed again each time we want to move again. It would be better if the movement was continuous as the key was held down. The following program offfers this functionality:"}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nx "},{"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":"text","value":"\ny "},{"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":"480"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"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\nto_right "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\nto_left "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\nclock "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"KEYDOWN"},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_LEFT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n to_left "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_RIGHT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n to_right "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"KEYUP"},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_LEFT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n to_left "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_RIGHT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n to_right "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"if"}]},{"type":"text","value":" to_right"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"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":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" to_left"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"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":"2"}]},{"type":"text","value":"\n\n window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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 clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The code now contains the variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"to_right"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"to_left"}]},{"type":"text","value":". These contain knowledge of whether the sprite should be moving to the right or to the left at any given moment. When the user presses down an arrow key, the value stored in the relevant variable become "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":". When the key is released, the value changes to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The clock is used to time the movements of the sprite, so that they potentially happen 60 times each second. If an arrow key is pressed, the sprite moves two pixels to the right or to the left. This means the sprite moves 120 pixels per second if the key is kept pressed down."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Four directions","tmcname":"part13-11_four_directions"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program where the player can move a robot in four directions with the arrow keys on the keyboard. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/0461dfa5cb7291b279185dba96dfd819/pygame_four_directions.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Four walls","tmcname":"part13-12_four_walls"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please improve the program in the previous exercise so that the robot cannot pass outside the window in any of the four directions. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/bddcb9af6c636563eea121e3360b7b37/pygame_four_walls.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Two players","tmcname":"part13-13_two_players"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program where two players each direct their own robot. One of the players should use the arrow keys while the other could use, for example, the w-s-a-d keys. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/57101a815ad1b5363307bc90dc112da6/pygame_two_players.gif"},"children":[]}]},{"type":"element","tagName":"h2","properties":{"id":"mouse-events","style":"position:relative;"},"children":[{"type":"text","value":"Mouse events"},{"type":"element","tagName":"a","properties":{"href":"#mouse-events","ariaLabel":"mouse events 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 following code reacts to events where a mouse button is pressed down while the cursor is within the window area:"}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"MOUSEBUTTONDOWN"},{"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":"\"you pressed the button number\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"button"},{"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":"\"at location\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pos"},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"The execution of this program should look more or less like this:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":"you pressed the button number 1 at location (82, 135)\nyou pressed the button number 1 at location (369, 135)\nyou pressed the button number 1 at location (269, 297)\nyou pressed the button number 3 at location (515, 324)"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Button number 1 refers to the left mouse button and button number 3 refers to the right mouse button."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This next program combines mouse event handling and drawing an image on the screen. When the user presses a mouse button while the mouse cursor is within the bounds of the window, an image of a robot is drawn at that location."}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"MOUSEBUTTONDOWN"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pos"},{"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","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"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":"2"}]},{"type":"text","value":"\n y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pos"},{"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","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"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":"2"}]},{"type":"text","value":"\n\n window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"The execution of the program could look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/a41511bd13655f68553cc7e34de61edf/pygame_cursor.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program contains an animation where the robot sprite follows the mouse cursor.The location of the sprite is stored in the variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_x"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_y"}]},{"type":"text","value":". When the mouse moves, its location is stored in the variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"target_x"}]},{"type":"text","value":" ja "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"target_y"}]},{"type":"text","value":". If the robot is not at this location, it moves to the approproate direction."}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\nwindow "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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\nrobot "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"robot.png\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrobot_x "},{"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":"text","value":"\nrobot_y "},{"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":"text","value":"\ntarget_x "},{"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":"text","value":"\ntarget_y "},{"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":"text","value":"\n\nclock "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"Clock"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"MOUSEMOTION"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n target_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pos"},{"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","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"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":"2"}]},{"type":"text","value":"\n target_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pos"},{"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","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"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":"2"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" robot_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" target_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n robot_x "},{"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":"if"}]},{"type":"text","value":" robot_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" target_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n robot_x "},{"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":"if"}]},{"type":"text","value":" robot_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" target_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n robot_y "},{"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":"if"}]},{"type":"text","value":" robot_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" target_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n robot_y "},{"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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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 window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"robot_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" robot_y"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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 clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program's execution should look more or less like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/cd4d53df2a1564c181f38b7a2d45e7c9/pygame_cursor2.gif"},"children":[]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Robot and mouse","tmcname":"part13-14_robot_and_mouse"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program where the robot follows the mouse cursor so that the centre of the robot is always directly at the mouse cursor. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/58d3b2e994913c8089688bcde5e4ed86/pygame_robot_cursor.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The location of the robot","tmcname":"part13-15_robot_location"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program where the robot appears at a random location within the window. When the player clicks on the robot with the mouse, the robot moves to a new location. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/93991031970283be78df3ba4341a7362/pygame_robot_location.gif"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will be familiar with pygame events
    • \n
    • You will be able to write a program which reacts to key presses
    • \n
    • You will be able to write a program which reacts to mouse events
    • \n

    Thus far our main loops have only executed predetermined animations and reacted to only pygame.QUIT type events, even though the loop gets a list of all events from the operating system. Let's get to grips with some other types of events, then.

    Handling events

    This program prints out information about all the events passed by the operating system to the pygame program, while it is running:

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nwhile True:\n    for event in pygame.event.get():\n        print(event)\n        if event.type == pygame.QUIT:\n            exit()

    Let's assume the program was left running for a while, and then the exit button was clicked. The program prints out the following info:

    <Event(4-MouseMotion {'pos': (495, 274), 'rel': (495, 274), 'buttons': (0, 0, 0), 'window': None})>\n<Event(4-MouseMotion {'pos': (494, 274), 'rel': (-1, 0), 'buttons': (0, 0, 0), 'window': None})>\n<Event(4-MouseMotion {'pos': (492, 274), 'rel': (-2, 0), 'buttons': (0, 0, 0), 'window': None})>\n<Event(4-MouseMotion {'pos': (491, 274), 'rel': (-1, 0), 'buttons': (0, 0, 0), 'window': None})>\n<Event(5-MouseButtonDown {'pos': (491, 274), 'button': 1, 'window': None})>\n<Event(6-MouseButtonUp {'pos': (491, 274), 'button': 1, 'window': None})>\n<Event(2-KeyDown {'unicode': 'a', 'key': 97, 'mod': 0, 'scancode': 38, 'window': None})>\n<Event(3-KeyUp {'key': 97, 'mod': 0, 'scancode': 38, 'window': None})>\n<Event(2-KeyDown {'unicode': 'b', 'key': 98, 'mod': 0, 'scancode': 56, 'window': None})>\n<Event(3-KeyUp {'key': 98, 'mod': 0, 'scancode': 56, 'window': None})>\n<Event(2-KeyDown {'unicode': 'c', 'key': 99, 'mod': 0, 'scancode': 54, 'window': None})>\n<Event(3-KeyUp {'key': 99, 'mod': 0, 'scancode': 54, 'window': None})>\n<Event(12-Quit {})>

    The first few events concern mouse usage, ten there are some events from the keyboard, and finally the last event closes the program. Each event has at least a type, but they may also offer some other identifying info, such as the location of the mouse cursor or the key that was pressed.

    You can look for event descriptions in the pygame documentation, but it can sometimes be easier to print out events with the code above, and look for the event that occurs when something you want to react to happens.

    Keyboard events

    This program can process events where the user presses the arrow key either to the right or to the left on their keyboard. The program prints out which key was pressed.

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.KEYDOWN:\n            if event.key == pygame.K_LEFT:\n                print(\"left\")\n            if event.key == pygame.K_RIGHT:\n                print(\"right\")\n\n        if event.type == pygame.QUIT:\n            exit()

    The constants pygame.K_LEFT and pygame.K_RIGHT refer to the arrow keys to the left and right. The pgyame key constants for the different keys on a keyboard are listed in the pygame documentation.

    For example, if the user presses the arrow key to the right twice, then the left one once, and then the right one once more, the program prints out

    right\nright\nleft\nright

    We now have all the tools needed to move a character, or sprite, on the screen to the right and left with the arrow keys. The following code will achieve this:

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\nx = 0\ny = 480-robot.get_height()\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.KEYDOWN:\n            if event.key == pygame.K_LEFT:\n                x -= 10\n            if event.key == pygame.K_RIGHT:\n                x += 10\n\n        if event.type == pygame.QUIT:\n            exit()\n\n    window.fill((0, 0, 0))\n    window.blit(robot, (x, y))\n    pygame.display.flip()

    Depending on how you use your arrow keys, running the program could look like this:

    In the code above we have the variables x and y which contain the coordinate location for the sprite. The variable y is set so that the sprite appears at the bottom of the window. The y value does not change throughout the execution of the program. The x value, however, increases by 10 whenever the user presses the arrow key to the right, and decreases by 10 whenever the left arrow key is pressed.

    The program works otherwise quite well, but the key needs to be pressed again each time we want to move again. It would be better if the movement was continuous as the key was held down. The following program offfers this functionality:

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\nx = 0\ny = 480-robot.get_height()\n\nto_right = False\nto_left = False\n\nclock = pygame.time.Clock()\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.KEYDOWN:\n            if event.key == pygame.K_LEFT:\n                to_left = True\n            if event.key == pygame.K_RIGHT:\n                to_right = True\n\n        if event.type == pygame.KEYUP:\n            if event.key == pygame.K_LEFT:\n                to_left = False\n            if event.key == pygame.K_RIGHT:\n                to_right = False\n\n        if event.type == pygame.QUIT:\n            exit()\n\n    if to_right:\n        x += 2\n    if to_left:\n        x -= 2\n\n    window.fill((0, 0, 0))\n    window.blit(robot, (x, y))\n    pygame.display.flip()\n\n    clock.tick(60)

    The code now contains the variables to_right and to_left. These contain knowledge of whether the sprite should be moving to the right or to the left at any given moment. When the user presses down an arrow key, the value stored in the relevant variable become True. When the key is released, the value changes to False.

    The clock is used to time the movements of the sprite, so that they potentially happen 60 times each second. If an arrow key is pressed, the sprite moves two pixels to the right or to the left. This means the sprite moves 120 pixels per second if the key is kept pressed down.

    Please write a program where the player can move a robot in four directions with the arrow keys on the keyboard. The end result should look like this:

    Please improve the program in the previous exercise so that the robot cannot pass outside the window in any of the four directions. The end result should look like this:

    Please write a program where two players each direct their own robot. One of the players should use the arrow keys while the other could use, for example, the w-s-a-d keys. The end result should look like this:

    Mouse events

    The following code reacts to events where a mouse button is pressed down while the cursor is within the window area:

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.MOUSEBUTTONDOWN:\n            print(\"you pressed the button number\", event.button, \"at location\", event.pos)\n\n        if event.type == pygame.QUIT:\n            exit()

    The execution of this program should look more or less like this:

    you pressed the button number 1 at location (82, 135)\nyou pressed the button number 1 at location (369, 135)\nyou pressed the button number 1 at location (269, 297)\nyou pressed the button number 3 at location (515, 324)

    Button number 1 refers to the left mouse button and button number 3 refers to the right mouse button.

    This next program combines mouse event handling and drawing an image on the screen. When the user presses a mouse button while the mouse cursor is within the bounds of the window, an image of a robot is drawn at that location.

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.MOUSEBUTTONDOWN:\n            x = event.pos[0]-robot.get_width()/2\n            y = event.pos[1]-robot.get_height()/2\n\n            window.fill((0, 0, 0))\n            window.blit(robot, (x, y))\n            pygame.display.flip()\n\n        if event.type == pygame.QUIT:\n            exit()

    The execution of the program could look like this:

    The following program contains an animation where the robot sprite follows the mouse cursor.The location of the sprite is stored in the variables robot_x and robot_y. When the mouse moves, its location is stored in the variables target_x ja target_y. If the robot is not at this location, it moves to the approproate direction.

    import pygame\n\npygame.init()\nwindow = pygame.display.set_mode((640, 480))\n\nrobot = pygame.image.load(\"robot.png\")\n\nrobot_x = 0\nrobot_y = 0\ntarget_x = 0\ntarget_y = 0\n\nclock = pygame.time.Clock()\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.MOUSEMOTION:\n            target_x = event.pos[0]-robot.get_width()/2\n            target_y = event.pos[1]-robot.get_height()/2\n\n        if event.type == pygame.QUIT:\n            exit(0)\n\n    if robot_x > target_x:\n        robot_x -= 1\n    if robot_x < target_x:\n        robot_x += 1\n    if robot_y > target_y:\n        robot_y -= 1\n    if robot_y < target_y:\n        robot_y += 1\n\n    window.fill((0, 0, 0))\n    window.blit(robot, (robot_x, robot_y))\n    pygame.display.flip()\n\n    clock.tick(60)

    The program's execution should look more or less like this:

    Please write a program where the robot follows the mouse cursor so that the centre of the robot is always directly at the mouse cursor. The end result should look like this:

    Please write a program where the robot appears at a random location within the window. When the player clicks on the robot with the mouse, the robot moves to a new location. The end result should look like this:

    ","frontmatter":{"path":"/part-13/3-events","title":"Events"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-13/3-events.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-13/4-more-pygame-techniques/page-data.json b/page-data/part-13/4-more-pygame-techniques/page-data.json index dac954fe1..f3d66ec4f 100644 --- a/page-data/part-13/4-more-pygame-techniques/page-data.json +++ b/page-data/part-13/4-more-pygame-techniques/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-13/4-more-pygame-techniques", - "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 know how the title of the pygame window"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to draw shapes with pygame"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to display text in your window"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-window-title","style":"position:relative;"},"children":[{"type":"text","value":"The window title"},{"type":"element","tagName":"a","properties":{"href":"#the-window-title","ariaLabel":"the window title 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":"Your programs will look more professional if instead of \"pygame window\" the window title contains the actual name of the program.The title is set with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.display.set_caption"}]},{"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":"text","value":"pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_caption"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Great Adventure\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"drawing-shapes","style":"position:relative;"},"children":[{"type":"text","value":"Drawing shapes"},{"type":"element","tagName":"a","properties":{"href":"#drawing-shapes","ariaLabel":"drawing shapes 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 following program draws a rectangle, a circle and a line on the screen:"}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\ndisplay "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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":"\ndisplay"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rect"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"0"}]},{"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":"255"}]},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"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":"100"}]},{"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":"200"}]},{"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":"250"}]},{"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":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"circle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"255"}]},{"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":"0"}]},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"200"}]},{"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":"150"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"0"}]},{"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":"0"}]},{"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":"255"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"80"}]},{"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":"120"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"300"}]},{"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":"160"}]},{"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":" "},{"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\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"Running the above code should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/9a7380e2e818c94a080d41344f073d5a/pygame_shapes.gif"},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"drawing-text","style":"position:relative;"},"children":[{"type":"text","value":"Drawing text"},{"type":"element","tagName":"a","properties":{"href":"#drawing-text","ariaLabel":"drawing text 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":"Text in pygame is drawn in two steps: first we create an image containing the desired text, and then this image is drawn on the screen. It works 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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\ndisplay "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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":"\ndisplay"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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\ngame_font "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"SysFont"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arial\""}]},{"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":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntext "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" game_font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"render"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Moikka!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"255"}]},{"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":"0"}]},{"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":"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":"\ndisplay"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"100"}]},{"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":"50"}]},{"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":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"Running the above code should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/ff536f08ddabdda8d522cc90c967ca8e/pygame_text.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.font.SysFont"}]},{"type":"text","value":" creates a font object, which uses the system font Arial in size 24. The the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"render"}]},{"type":"text","value":" creates an image of the specified text in the given colour. This image is drawn on the window with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"blit"}]},{"type":"text","value":" method, just as before."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: different systems will have different fonts available. If the system this program is exeuted on doesn't have the Arial font, even though Arial is a very common font available on most systems, the default system font is used instead. If you need to have a specific font available for your game, you can include the font file in the game directory and specify its location for the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.font.Font"}]},{"type":"text","value":" method."}]},{"type":"element","tagName":"h2","properties":{"id":"exercises","style":"position:relative;"},"children":[{"type":"text","value":"Exercises"},{"type":"element","tagName":"a","properties":{"href":"#exercises","ariaLabel":"exercises 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":"Here are some more advanced exercises for practicing what you have learned in this part of the course material."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Clock","tmcname":"part13-16_clock"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which displays a clock face which displays the system time. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/0e95a9664e02d0dd939be3ebfd4725d5/pygame_clock.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Asteroids","tmcname":"part13-17_asteroids"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create a game where asteroids fall from the sky. The player moves a robot left and right and tries to collect the falling rocks. The player gets a point for each asteroid collected, and the points total is shown at the top of the window. The game ends when the player misses an asteroid. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/589bb5f7fafc7f948dfab758f0bd6273/pygame_asteroids.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the above animation is from the Finnish version of the course. You may use the word \"Points\" instead of the Finnish equivalent \"Pisteet\" depicted above."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the image "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rock.png"}]},{"type":"text","value":" for the asteroid."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this part of the course."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will know how the title of the pygame window
    • \n
    • You will be able to draw shapes with pygame
    • \n
    • You will know how to display text in your window
    • \n

    The window title

    Your programs will look more professional if instead of \"pygame window\" the window title contains the actual name of the program.The title is set with the pygame.display.set_caption function:

    pygame.display.set_caption(\"Great Adventure\")

    Drawing shapes

    The following program draws a rectangle, a circle and a line on the screen:

    import pygame\n\npygame.init()\ndisplay = pygame.display.set_mode((640, 480))\ndisplay.fill((0, 0, 0))\n\npygame.draw.rect(display, (0, 255, 0), (50, 100, 200, 250))\npygame.draw.circle(display, (255, 0, 0), (200, 150), 40)\npygame.draw.line(display, (0, 0, 255), (80, 120), (300, 160), 2)\n\npygame.display.flip()\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.QUIT:\n            exit()

    Running the above code should look like this:

    Drawing text

    Text in pygame is drawn in two steps: first we create an image containing the desired text, and then this image is drawn on the screen. It works like this:

    import pygame\n\npygame.init()\ndisplay = pygame.display.set_mode((640, 480))\ndisplay.fill((0, 0, 0))\n\ngame_font = pygame.font.SysFont(\"Arial\", 24)\ntext = game_font.render(\"Moikka!\", True, (255, 0, 0))\ndisplay.blit(text, (100, 50))\npygame.display.flip()\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.QUIT:\n            exit()

    Running the above code should look like this:

    Here the method pygame.font.SysFont creates a font object, which uses the system font Arial in size 24. The the method render creates an image of the specified text in the given colour. This image is drawn on the window with the blit method, just as before.

    NB: different systems will have different fonts available. If the system this program is exeuted on doesn't have the Arial font, even though Arial is a very common font available on most systems, the default system font is used instead. If you need to have a specific font available for your game, you can include the font file in the game directory and specify its location for the pygame.font.Font method.

    Exercises

    Here are some more advanced exercises for practicing what you have learned in this part of the course material.

    Please write a program which displays a clock face which displays the system time. The end result should look like this:

    Please create a game where asteroids fall from the sky. The player moves a robot left and right and tries to collect the falling rocks. The player gets a point for each asteroid collected, and the points total is shown at the top of the window. The game ends when the player misses an asteroid. The end result should look like this:

    NB: the above animation is from the Finnish version of the course. You may use the word \"Points\" instead of the Finnish equivalent \"Pisteet\" depicted above.

    The exercise template contains the image rock.png for the asteroid.

    Please respond to a quick questionnaire on this part of the course.

    ","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-13/4-more-pygame-techniques.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know how the title of the pygame window"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to draw shapes with pygame"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to display text in your window"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-window-title","style":"position:relative;"},"children":[{"type":"text","value":"The window title"},{"type":"element","tagName":"a","properties":{"href":"#the-window-title","ariaLabel":"the window title 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":"Your programs will look more professional if instead of \"pygame window\" the window title contains the actual name of the program.The title is set with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.display.set_caption"}]},{"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":"text","value":"pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_caption"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Great Adventure\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"drawing-shapes","style":"position:relative;"},"children":[{"type":"text","value":"Drawing shapes"},{"type":"element","tagName":"a","properties":{"href":"#drawing-shapes","ariaLabel":"drawing shapes 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 following program draws a rectangle, a circle and a line on the screen:"}]},{"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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\ndisplay "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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":"\ndisplay"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rect"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"0"}]},{"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":"255"}]},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"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":"100"}]},{"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":"200"}]},{"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":"250"}]},{"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":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"circle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"255"}]},{"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":"0"}]},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"200"}]},{"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":"150"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"0"}]},{"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":"0"}]},{"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":"255"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"80"}]},{"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":"120"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"300"}]},{"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":"160"}]},{"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":" "},{"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\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"Running the above code should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/9a7380e2e818c94a080d41344f073d5a/pygame_shapes.gif"},"children":[]},{"type":"element","tagName":"h2","properties":{"id":"drawing-text","style":"position:relative;"},"children":[{"type":"text","value":"Drawing text"},{"type":"element","tagName":"a","properties":{"href":"#drawing-text","ariaLabel":"drawing text 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":"Text in pygame is drawn in two steps: first we create an image containing the desired text, and then this image is drawn on the screen. It works 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":"import"}]},{"type":"text","value":" pygame\n\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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":"\ndisplay "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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","number"]},"children":[{"type":"text","value":"640"}]},{"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":"480"}]},{"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":"\ndisplay"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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\ngame_font "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"SysFont"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arial\""}]},{"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":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntext "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" game_font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"render"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Moikka!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"255"}]},{"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":"0"}]},{"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":"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":"\ndisplay"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"100"}]},{"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":"50"}]},{"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":"\npygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"Running the above code should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/ff536f08ddabdda8d522cc90c967ca8e/pygame_text.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.font.SysFont"}]},{"type":"text","value":" creates a font object, which uses the system font Arial in size 24. The the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"render"}]},{"type":"text","value":" creates an image of the specified text in the given colour. This image is drawn on the window with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"blit"}]},{"type":"text","value":" method, just as before."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: different systems will have different fonts available. If the system this program is exeuted on doesn't have the Arial font, even though Arial is a very common font available on most systems, the default system font is used instead. If you need to have a specific font available for your game, you can include the font file in the game directory and specify its location for the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pygame.font.Font"}]},{"type":"text","value":" method."}]},{"type":"element","tagName":"h2","properties":{"id":"exercises","style":"position:relative;"},"children":[{"type":"text","value":"Exercises"},{"type":"element","tagName":"a","properties":{"href":"#exercises","ariaLabel":"exercises 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":"Here are some more advanced exercises for practicing what you have learned in this part of the course material."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Clock","tmcname":"part13-16_clock"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which displays a clock face which displays the system time. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/0e95a9664e02d0dd939be3ebfd4725d5/pygame_clock.gif"},"children":[]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Asteroids","tmcname":"part13-17_asteroids"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create a game where asteroids fall from the sky. The player moves a robot left and right and tries to collect the falling rocks. The player gets a point for each asteroid collected, and the points total is shown at the top of the window. The game ends when the player misses an asteroid. The end result should look like this:"}]},{"type":"element","tagName":"img","properties":{"src":"/589bb5f7fafc7f948dfab758f0bd6273/pygame_asteroids.gif"},"children":[]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the above animation is from the Finnish version of the course. You may use the word \"Points\" instead of the Finnish equivalent \"Pisteet\" depicted above."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the image "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rock.png"}]},{"type":"text","value":" for the asteroid."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this part of the course."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will know how the title of the pygame window
    • \n
    • You will be able to draw shapes with pygame
    • \n
    • You will know how to display text in your window
    • \n

    The window title

    Your programs will look more professional if instead of \"pygame window\" the window title contains the actual name of the program.The title is set with the pygame.display.set_caption function:

    pygame.display.set_caption(\"Great Adventure\")

    Drawing shapes

    The following program draws a rectangle, a circle and a line on the screen:

    import pygame\n\npygame.init()\ndisplay = pygame.display.set_mode((640, 480))\ndisplay.fill((0, 0, 0))\n\npygame.draw.rect(display, (0, 255, 0), (50, 100, 200, 250))\npygame.draw.circle(display, (255, 0, 0), (200, 150), 40)\npygame.draw.line(display, (0, 0, 255), (80, 120), (300, 160), 2)\n\npygame.display.flip()\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.QUIT:\n            exit()

    Running the above code should look like this:

    Drawing text

    Text in pygame is drawn in two steps: first we create an image containing the desired text, and then this image is drawn on the screen. It works like this:

    import pygame\n\npygame.init()\ndisplay = pygame.display.set_mode((640, 480))\ndisplay.fill((0, 0, 0))\n\ngame_font = pygame.font.SysFont(\"Arial\", 24)\ntext = game_font.render(\"Moikka!\", True, (255, 0, 0))\ndisplay.blit(text, (100, 50))\npygame.display.flip()\n\nwhile True:\n    for event in pygame.event.get():\n        if event.type == pygame.QUIT:\n            exit()

    Running the above code should look like this:

    Here the method pygame.font.SysFont creates a font object, which uses the system font Arial in size 24. The the method render creates an image of the specified text in the given colour. This image is drawn on the window with the blit method, just as before.

    NB: different systems will have different fonts available. If the system this program is exeuted on doesn't have the Arial font, even though Arial is a very common font available on most systems, the default system font is used instead. If you need to have a specific font available for your game, you can include the font file in the game directory and specify its location for the pygame.font.Font method.

    Exercises

    Here are some more advanced exercises for practicing what you have learned in this part of the course material.

    Please write a program which displays a clock face which displays the system time. The end result should look like this:

    Please create a game where asteroids fall from the sky. The player moves a robot left and right and tries to collect the falling rocks. The player gets a point for each asteroid collected, and the points total is shown at the top of the window. The game ends when the player misses an asteroid. The end result should look like this:

    NB: the above animation is from the Finnish version of the course. You may use the word \"Points\" instead of the Finnish equivalent \"Pisteet\" depicted above.

    The exercise template contains the image rock.png for the asteroid.

    Please respond to a quick questionnaire on this part of the course.

    ","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-13/4-more-pygame-techniques.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-13/page-data.json b/page-data/part-13/page-data.json index 8384d4c8f..9fa5620e0 100644 --- a/page-data/part-13/page-data.json +++ b/page-data/part-13/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/part-13", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-13","title":"Part 13"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-13/index.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-13","title":"Part 13"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-13/index.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-14/1-game-project/page-data.json b/page-data/part-14/1-game-project/page-data.json index 6969fdcfa..0b8db4722 100644 --- a/page-data/part-14/1-game-project/page-data.json +++ b/page-data/part-14/1-game-project/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-14/1-game-project", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this part we will use pygame to create a somewhat larger game. It is a variation of the classic Sokoban game, where the player moves a robot on a grid and pushes boxes into correct locations with as few moves as possible."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The end result will look like this:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 852px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/e334a0f0fea9438564e48d0bba7cbbc4/47ff6/game.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: 44.34782608695652%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAAAsTAAALEwEAmpwYAAACGUlEQVQozyWO0UsaAQCH73RHlgm10ztbJUEvkkIzzrsHxRPShx56LUYRkqVPC2rhqZ2iKGiEiFDsdAX2T0SshxhGk/J9hG+tWlIRBA41xm+4fY+/7+H7EbFYLBQKybKcSqWi0agkSfF4PJlMbm5urq9vJBIJeWtLkqRYLJZOpyVJikQi4XBYluVMJkOYzWaP12u323t6epxOp9frHR4eHhkZmZ6eFkVRq9VOcdzMzMz4+Pjg4KAoim632+PxGI1GjuOIvb09ALVajef5x8dHAH6/f2lpCUCj0TCZTOfn5wD29/dnZ2cBtNttAKurq1arlSgmk/fV6tfT09/N5sPzM4AVv39hcRHAz+vr0bGx7+Xyj8vL6sVFs9l8eXl5enoCEAgEbDYbkd/ZAfCtUnGL4kOlUj07q9Zqv66u7iuV45OTer3euLn58/paKpXm5+dbrdb/dz6fr1tWFAXA8dHRu6Gh57s7AJ8V5UuxCOD29vbTxka9XgeQz+c5jgPQ6XQABIPByclJ4sPCwu7u7sry8qjJlM5my4eHgiBwdnvp4CC7va3T6T6urZXL5bm5OYvFUigUcrmcoigul0sQBEJDUQRBUG+6qMku/RpNf19fd1Srdb29lEqlJkmKojQazT9PqlQqkiR5niem9HoLy7IGg4GmLQxjZVlerx+laRPLvmcYM00LDDPBsm+7nmZZlmEYo9E4MDDgcDj+AisL8KSDCe9vAAAAAElFTkSuQmCC'); 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":["/static/e334a0f0fea9438564e48d0bba7cbbc4/a0b58/game.webp 230w","/static/e334a0f0fea9438564e48d0bba7cbbc4/bc10c/game.webp 460w","/static/e334a0f0fea9438564e48d0bba7cbbc4/39392/game.webp 852w"],"sizes":"(max-width: 852px) 100vw, 852px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/e334a0f0fea9438564e48d0bba7cbbc4/81c8e/game.png 230w","/static/e334a0f0fea9438564e48d0bba7cbbc4/08a84/game.png 460w","/static/e334a0f0fea9438564e48d0bba7cbbc4/47ff6/game.png 852w"],"sizes":"(max-width: 852px) 100vw, 852px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/e334a0f0fea9438564e48d0bba7cbbc4/47ff6/game.png","alt":"game","title":"game","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":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"the-game-map","style":"position:relative;"},"children":[{"type":"text","value":"The game map"},{"type":"element","tagName":"a","properties":{"href":"#the-game-map","ariaLabel":"the game map 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's begin by drawing the map used in the game. The game is implemented in the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Sokoban"}]},{"type":"text","value":", which will contain all functionality required to play the game. In this first stage the contents of the class are as follows:"}]},{"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":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Sokoban"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load_images"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"new_game"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"images"},{"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":"get_width"},{"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 window_height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height\n window_width "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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":"window_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" window_height"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_caption"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sokoban\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"main_loop"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"load_images"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"images "},{"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","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"\"floor\""}]},{"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":"\"wall\""}]},{"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":"\"target\""}]},{"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":"\"box\""}]},{"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":"\"robot\""}]},{"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":"\"done\""}]},{"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":"\"target_robot\""}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"images"},{"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":"pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"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":"\".png\""}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"new_game"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"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","punctuation"]},"children":[{"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"text","value":"\n "},{"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":"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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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":"text","value":"\n "},{"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":"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":" "},{"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":" "},{"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","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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"2"}]},{"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":"3"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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":"text","value":"\n "},{"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":"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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"2"}]},{"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":"3"}]},{"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":"0"}]},{"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":"2"}]},{"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":"3"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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":"text","value":"\n "},{"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":"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","number"]},"children":[{"type":"text","value":"4"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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":"text","value":"\n "},{"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"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_loop"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"check_events"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"draw_window"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"check_events"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"for"}]},{"type":"text","value":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"draw_window"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" y "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height"},{"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":"for"}]},{"type":"text","value":" x "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width"},{"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 square "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"y"},{"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":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"images"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"square"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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":"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 Sokoban"},{"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":"p","properties":{},"children":[{"type":"text","value":"Running the program should display a window with the initial state of the game. Let's take a closer look at the code which achieves this."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"the-constructor","style":"position:relative;"},"children":[{"type":"text","value":"The constructor"},{"type":"element","tagName":"a","properties":{"href":"#the-constructor","ariaLabel":"the constructor 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The constructor of the class initializes the pygame modules and the essential variables and data structures involved in the game. It also calls the main loop method of the game."}]},{"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":" "},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load_images"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"new_game"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"images"},{"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":"get_width"},{"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 window_height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height\n window_width "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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":"window_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" window_height"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_caption"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sokoban\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"main_loop"},{"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":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"load_images"}]},{"type":"text","value":" method loads the images used in the game into a list named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"images"}]},{"type":"text","value":". The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"new_game"}]},{"type":"text","value":" method creates a two-dimensional list named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":", which contains the state of the game grid in the beginning of the game."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"height"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"width"}]},{"type":"text","value":" are initialized based on the dimensions of the game grid. The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"scale"}]},{"type":"text","value":" contains the length of the side of one square in the grid. As each image is a square of the exact same size, the size of all squares is covered by this one variable, and the width of the first image will do just fine for the value. This same value can be used to calculate the width and height of the entire grid, which lets us create a window of the appropriate size to display the game grid."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"loading-images","style":"position:relative;"},"children":[{"type":"text","value":"Loading images"},{"type":"element","tagName":"a","properties":{"href":"#loading-images","ariaLabel":"loading images 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"load_images"}]},{"type":"text","value":" method loads all the images used in the game:"}]},{"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":" "},{"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":"load_images"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"images "},{"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","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"\"floor\""}]},{"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":"\"wall\""}]},{"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":"\"target\""}]},{"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":"\"box\""}]},{"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":"\"robot\""}]},{"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":"\"done\""}]},{"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":"\"target_robot\""}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"images"},{"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":"pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"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":"\".png\""}]},{"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":"p","properties":{},"children":[{"type":"text","value":"The game makes use of the following images:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"floor-square","style":"position:relative;"},"children":[{"type":"text","value":"Floor square"},{"type":"element","tagName":"a","properties":{"href":"#floor-square","ariaLabel":"floor square 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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/9aefcdf4192b60fb3bf21288d49a0bd5/a1812/floor.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: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAO0lEQVQ4y2M4dOjQ/7Nnz1IFg8xiADGoBUBmwQ389+8fRZh+BpIKRg0cNXDUwMFp4OAtbahWwFK7CgAAGH0Lv64YzdcAAAAASUVORK5CYII='); 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":["/static/9aefcdf4192b60fb3bf21288d49a0bd5/6ac13/floor.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9aefcdf4192b60fb3bf21288d49a0bd5/a1812/floor.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/9aefcdf4192b60fb3bf21288d49a0bd5/a1812/floor.png","alt":"floor","title":"floor","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":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filename: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"floor.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position in list: 0"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"wall-square","style":"position:relative;"},"children":[{"type":"text","value":"Wall square"},{"type":"element","tagName":"a","properties":{"href":"#wall-square","ariaLabel":"wall square 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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/73ec5a44eba6d09dfe097ba346137a94/a1812/wall.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: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAASElEQVQ4y+3VMQoAMAgDwPhX/YSft0RwLrQZHQI6eGNEZpa7V0R8hQYtcAAgSVvUuZjZMzS3bQ2oyIILLrjgHZTVl7xg1S/gAHx86WTsjKFcAAAAAElFTkSuQmCC'); 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":["/static/73ec5a44eba6d09dfe097ba346137a94/6ac13/wall.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/73ec5a44eba6d09dfe097ba346137a94/a1812/wall.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/73ec5a44eba6d09dfe097ba346137a94/a1812/wall.png","alt":"wall","title":"wall","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":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filename: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"wall.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position in list: 1"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"target-square","style":"position:relative;"},"children":[{"type":"text","value":"Target square"},{"type":"element","tagName":"a","properties":{"href":"#target-square","ariaLabel":"target square 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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/36b1cacabf9fb62e1c67fc5e6cfa50e3/a1812/target.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: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABHElEQVQ4y7WVPQ6CUAzHWRw0yBG8gfEK3oCJlRNwB2/gBZzZPAU7I0zsbmiigQjav6nlgS+CUyVp0lfa3+vH4+EkSYI0TVWEWQ4rWg+zeiAR2fJ8gu53EL8z0ragpvn2JfoBZP3xGCBsK0vQ5WLbeMOPuHEgK00j+Z9OwG4HrNfAbAYsFoDvA3E81Gg2ngS2rThmGbBcAo4j4nmA6w7rIOiZ70xHgd1uyHPJhgPDUNa3G3C9AscjsNlY0OmSDXC7lYD9fnqcBhrHUnLXJhvIUhTiuFoBVSXB3AbOgKWuxcaZsp/v99O3gaa5h4M4RpEEmgEZYFfau3zu6XwOOp9BX+dQHahe8l+Gon5s1A/2Xz499ctB8/pSu2C1fwEvukCRvM6VtZAAAAAASUVORK5CYII='); 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":["/static/36b1cacabf9fb62e1c67fc5e6cfa50e3/6ac13/target.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/36b1cacabf9fb62e1c67fc5e6cfa50e3/a1812/target.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/36b1cacabf9fb62e1c67fc5e6cfa50e3/a1812/target.png","alt":"target","title":"target","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":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filename: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"target.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position in list: 2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The robot should move some box to this square"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"box","style":"position:relative;"},"children":[{"type":"text","value":"Box"},{"type":"element","tagName":"a","properties":{"href":"#box","ariaLabel":"box 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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/6e7afd4acd3f9b214a5c43273b23c834/a1812/box.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: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAzElEQVQ4y+2VPQqEMBCFvZxVjqAWAVN4CH8KtfEQQfAk9h5DC09go295Abcx7sIyWyzswIPATL6ZgckkGMcR0zSJiKyABykj6wnc9/0jyHEcThcgbds2zPOMZVneinHruvqBZ3XDMCAMQ2itEUUR4jj2ir4kSaCUctBLyyfQWossy9B1Heq6RtM0XtHHGEJZ6S2w73ukaeouFUVxqzzPHZSVsv2XQGMM2rZFWZaoqsorQpmU7f+BvwgUHRvxwRZ7euLL4WvrS2TBSn8BD7avDfrO6ztPAAAAAElFTkSuQmCC'); 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":["/static/6e7afd4acd3f9b214a5c43273b23c834/6ac13/box.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6e7afd4acd3f9b214a5c43273b23c834/a1812/box.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/6e7afd4acd3f9b214a5c43273b23c834/a1812/box.png","alt":"box","title":"box","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":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filename: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"box.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position in list: 3"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"robot","style":"position:relative;"},"children":[{"type":"text","value":"Robot"},{"type":"element","tagName":"a","properties":{"href":"#robot","ariaLabel":"robot 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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/5fb4f02fac6c4d361c9057099bc9f890/a1812/robot.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: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAB/klEQVQ4y5VV0WraYBjttbe+QR+iF32KXvdOWOeckVEGveqtF71qi6CusDFW6sSQJcwNklFHEcHZGfsAjdFCQCiFQew6qugZ58OUIqY1Hxw8nnP+w5+QP1mp1+uwbTsUlmWh1WoJyJ/KsmuFJGxG9/d4oyjo93oCcmphw66Hwul0KphMJvLf8zx8Oj5G/uQEFVUVkFOjx2E2WPdsYdd18S6Xw/v1dXwtlwXk1OhFLnT7fXw4OsLHjQ2Yug7TMIRToxe50HEcfC6XYZ2f47tpCsip0Ytc2Lu6QuHwEJl4HMlYDK9iMSjxuGj0ol8y72GhgNzuLrKKIiCn5i57D4MQZ+j70DQNVdNEMpMRkFOj9zj75A7l+RuNkM1mUSwW8a1axRdNE5BTo8fM/LrQQt/3kUgkUKlU5ITwBBDk1Oj5s10uVTgcDpFKpVAqldBoNFCr1QTk1OgxE6nwxdYWVFVFs9l82CE5NXqRCv/e3iKztob8/j5+np1B13UBef7gQDxmlioMzGvPww/LQjqdhmEYAnJq17OzPL8mdIfBL+d1MonBYCAgD+ZxdulCPhgvd3bkZUCQj6IWBua/8Ri/9vbwdnUV7uWlgJwavWcveX7uxmOcbm/jdHMTf25uBOTU7maFC1+wiz4B7XYbHdtGx3Hwu9tF5+JCINxxxGNm0SfgP7rpf7Qvj0ecAAAAAElFTkSuQmCC'); 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":["/static/5fb4f02fac6c4d361c9057099bc9f890/6ac13/robot.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/5fb4f02fac6c4d361c9057099bc9f890/a1812/robot.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/5fb4f02fac6c4d361c9057099bc9f890/a1812/robot.png","alt":"robot","title":"robot","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":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filename: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position in list: 4"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"box-on-a-target-square","style":"position:relative;"},"children":[{"type":"text","value":"Box on a target square"},{"type":"element","tagName":"a","properties":{"href":"#box-on-a-target-square","ariaLabel":"box on a target square 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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/15225a12ccfe0e42a543bea73f4ff889/a1812/done.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: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABk0lEQVQ4y7WVPWsCQRCG7UTEX5Em/0IQbLS18gM80MLOIpgYb/fOu9mNcpD8g4hglzZtCNjbpkqrhV0IITb6hllivrzEDy4LL7dwew8zs+/MxcbjMSaTSSRiVow3US1mfQCXy+VBkNVqZbQB5LVYLDCdTjGbzbaKz83n83DgOrrRaIRsNgvLslAqlVAul0PF7yqVCvL5vIFupLwGDgYDNBoNBEEAIoJS6pu075unT2TOMJQj/RU4HA5Rq9XMR1LKTzkOHCFwphRs14UUwkA5Uk7/T2C9XofWGo7jwGWQ66IrJU57PdzmcrjLZNAmgiYy6e8FZJAnhIGNikU8J5O4ajbR8bzDgB0inCuFfquFp1QK15aFVr8PVwhTlt2BXCvPw6BahW638XB8jPt0GidBYCLmmu4FvFAKZ1rj8egIr/G4Adq+b2rK4ovaC9gjMsCbQgEviQQuuW5EpqZ8SXsBv9rGkRJd24bd7Zq9fI9OCGF8utU2YcYmraF+mJx2NXZo64W04NbWi3w4/Nv4imTARv0LeAO8hwm47qNppgAAAABJRU5ErkJggg=='); 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":["/static/15225a12ccfe0e42a543bea73f4ff889/6ac13/done.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/15225a12ccfe0e42a543bea73f4ff889/a1812/done.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/15225a12ccfe0e42a543bea73f4ff889/a1812/done.png","alt":"done","title":"done","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":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filename: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"done.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position in list: 5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The box has been moved to the target square"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"robot-on-a-target-square","style":"position:relative;"},"children":[{"type":"text","value":"Robot on a target square"},{"type":"element","tagName":"a","properties":{"href":"#robot-on-a-target-square","ariaLabel":"robot on a target square 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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/70f904da050def3ee01b9b01b0b50b16/a1812/target_robot.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: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAC2klEQVQ4y41VXUsiURiemyANL7wRgm4q6D9U0B+IgrroIvrAqDVNWWKLrK66qD9gVEOrbF9axvSlSxq2hS1tVtoH1NWoSwlLdhEkFY3ps5wz4/bl2B54Zp55z/s+877nnHeG8fv9CAaDsvB6vQgEAhSE5/IlWgwhckN4fMRnoxG/o1EKwolNbhCtf4LpdJoilUrR59jlJaZnZjA2Nwfn4iIF4cQWi8WoD/HNxH0oGL64ADs6Cmt5Odbm5ymsFRVgLRaEef4/BYmTVFLk5AS28XF8q6uDZ3kZnpUVym0si0g8LgomkzRGXjCZBM3v+Bh8SQkcHAfv/j6+ezwU3oMDOBwO8DqdKEjipCzfCz49iRmenwN5eYgqFBi3WmFSq6FTKtGpVMKoVlNblGGA2lpRULbkjGBVFcAwiJhMYB0OWMxmDBuNFJaBAbBTU4gUFVGf1OysWLIgZBEkF55Hiry9tBSJeBzc6ipc6+vQmUwULrcbnM+HhNUqCtbUiLHJ5BtBKbv05CR1FDo7McyymLBY4Ha5sMRxFO61NUx8/YrhwUEI+flAQQHSNzdiMrkEb1tboe3thdNupx1COoAgsLcH59IStPX1uCWVqFQyglLJaXK+GAYJjQZ6rRb2hQX83NnB5uYmBeF2pxP65mYkiGB1tRj3ruQXm4LKSurc3tSERY7Dr91d+Le3KQgntvaWFlFwejrHpmQEz85wp1LBpNFgzG7Hj60tLHu9FIST9jMpFLhraHjVYbIHm06FQoiXlWGjvx9dej1WhoYougwGbPT1IW40Pi+T7MGWWg+CIN6vroCeHhg6OvCnsJDC0NYG2GzPxyyzmbk+Di/PFVmZT2YzwqenFIQL0o6SJfq45BeTD4KAvZERfCkuRiQSoSCc2B6kF76NeSX4dtwnk/B1d8PX2Iib62sKwontXhLM+oHN9gs4PDxEKBhEiOdxEA4jdHREQTnP0znik+0X8BeOmjrCN4yzpAAAAABJRU5ErkJggg=='); 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":["/static/70f904da050def3ee01b9b01b0b50b16/6ac13/target_robot.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/70f904da050def3ee01b9b01b0b50b16/a1812/target_robot.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/70f904da050def3ee01b9b01b0b50b16/a1812/target_robot.png","alt":"target robot","title":"target robot","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":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filename: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"target_robot.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position in list: 6"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The robot can also be on an empty target square"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"creating-the-grid","style":"position:relative;"},"children":[{"type":"text","value":"Creating the grid"},{"type":"element","tagName":"a","properties":{"href":"#creating-the-grid","ariaLabel":"creating the grid 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"new_game"}]},{"type":"text","value":" method creates the initial state of the game grid:"}]},{"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":" "},{"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":"new_game"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"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","punctuation"]},"children":[{"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"text","value":"\n "},{"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":"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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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":"text","value":"\n "},{"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":"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":" "},{"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":" "},{"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","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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"2"}]},{"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":"3"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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":"text","value":"\n "},{"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":"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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"2"}]},{"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":"3"}]},{"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":"0"}]},{"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":"2"}]},{"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":"3"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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":"text","value":"\n "},{"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":"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","number"]},"children":[{"type":"text","value":"4"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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":"text","value":"\n "},{"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method creates a two.dimensional list named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" which uses the numbered positions of the images in "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"their"}]},{"type":"text","value":" list to mark up which image goes where. This way the game contains a record of the state of the game grid at all times."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: in the beginning all spaces on the grid contain a number between 0 and 4. The numbers 5 and 6 are not included, as in the beginning no box or robot is on a target square."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"the-main-loop","style":"position:relative;"},"children":[{"type":"text","value":"The main loop"},{"type":"element","tagName":"a","properties":{"href":"#the-main-loop","ariaLabel":"the main loop 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"main_loop"}]},{"type":"text","value":" method is rather short. With each iteration it calls two methods: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"check_events"}]},{"type":"text","value":" goes through any events collected since the previous iteration, and the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"draw_window"}]},{"type":"text","value":" method updates the contents of the window."}]},{"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":" "},{"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_loop"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"check_events"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"draw_window"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"check_events"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"for"}]},{"type":"text","value":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"draw_window"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" y "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height"},{"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":"for"}]},{"type":"text","value":" x "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width"},{"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 square "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"y"},{"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":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"images"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"square"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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":"p","properties":{},"children":[{"type":"text","value":"At this stage the only event actually handled by the game is closing the game window, e.g. from the exit button. The game then exits by calling the Python "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exit"}]},{"type":"text","value":" function."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each time "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"draw_window"}]},{"type":"text","value":" method is called the entire game grid is matrix is traversed, and the image corresponding to each square in the grid is drawn in the correct location."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the coordinates x and y are used in two different ways in the game. When dealing with the indexes of a two-dimensional list, it is logical to give the y coordinate first, as the y refers to the number of the row while x is the number of the column. On the other hand, when using pygame methods, x is usually passed first, as it quite often is when dealing with graphics, and also in mathematical contexts."}]}],"data":{"quirksMode":false}},"html":"

    In this part we will use pygame to create a somewhat larger game. It is a variation of the classic Sokoban game, where the player moves a robot on a grid and pushes boxes into correct locations with as few moves as possible.

    \n

    The end result will look like this:

    \n\n \n \n \n \n \n \"game\"\n \n \n \n

    The game map

    \n

    Let's begin by drawing the map used in the game. The game is implemented in the class Sokoban, which will contain all functionality required to play the game. In this first stage the contents of the class are as follows:

    \n
    import pygame\n\nclass Sokoban:\n    def __init__(self):\n        pygame.init()\n        \n        self.load_images()\n        self.new_game()\n        \n        self.height = len(self.map)\n        self.width = len(self.map[0])\n        self.scale = self.images[0].get_width()\n\n        window_height = self.scale * self.height\n        window_width = self.scale * self.width\n        self.window = pygame.display.set_mode((window_width, window_height))\n\n        pygame.display.set_caption(\"Sokoban\")\n\n        self.main_loop()\n\n    def load_images(self):\n        self.images = []\n        for name in [\"floor\", \"wall\", \"target\", \"box\", \"robot\", \"done\", \"target_robot\"]:\n            self.images.append(pygame.image.load(name + \".png\"))\n\n    def new_game(self):\n        self.map = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n                    [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1],\n                    [1, 2, 3, 0, 0, 0, 1, 0, 0, 1, 2, 3, 0, 0, 0, 0, 1],\n                    [1, 0, 0, 1, 2, 3, 0, 2, 3, 0, 0, 0, 1, 0, 0, 0, 1],\n                    [1, 0, 4, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1],\n                    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]\n\n    def main_loop(self):\n        while True:\n            self.check_events()\n            self.draw_window()\n\n    def check_events(self):\n        for event in pygame.event.get():\n            if event.type == pygame.QUIT:\n                exit()\n\n    def draw_window(self):\n        self.window.fill((0, 0, 0))\n\n        for y in range(self.height):\n            for x in range(self.width):\n                square = self.map[y][x]\n                self.window.blit(self.images[square], (x * self.scale, y * self.scale))\n\n        pygame.display.flip()\n\nif __name__ == \"__main__\":\n    Sokoban()
    \n

    Running the program should display a window with the initial state of the game. Let's take a closer look at the code which achieves this.

    \n

    The constructor

    \n

    The constructor of the class initializes the pygame modules and the essential variables and data structures involved in the game. It also calls the main loop method of the game.

    \n
        def __init__(self):\n        pygame.init()\n        \n        self.load_images()\n        self.new_game()\n        \n        self.height = len(self.map)\n        self.width = len(self.map[0])\n        self.scale = self.images[0].get_width()\n\n        window_height = self.scale * self.height\n        window_width = self.scale * self.width\n        self.window = pygame.display.set_mode((window_width, window_height))\n\n        pygame.display.set_caption(\"Sokoban\")\n\n        self.main_loop()
    \n

    The load_images method loads the images used in the game into a list named images. The new_game method creates a two-dimensional list named map, which contains the state of the game grid in the beginning of the game.

    \n

    The variables height and width are initialized based on the dimensions of the game grid. The variable scale contains the length of the side of one square in the grid. As each image is a square of the exact same size, the size of all squares is covered by this one variable, and the width of the first image will do just fine for the value. This same value can be used to calculate the width and height of the entire grid, which lets us create a window of the appropriate size to display the game grid.

    \n

    Loading images

    \n

    The load_images method loads all the images used in the game:

    \n
        def load_images(self):\n        self.images = []\n        for name in [\"floor\", \"wall\", \"target\", \"box\", \"robot\", \"done\", \"target_robot\"]:\n            self.images.append(pygame.image.load(name + \".png\"))
    \n

    The game makes use of the following images:

    \n

    Floor square

    \n\n \n \n \n \n \n \"wall\"\n \n \n \n
      \n
    • Filename: wall.png
    • \n
    • Position in list: 1
    • \n
    \n

    Target square

    \n\n \n \n \n \n \n \"box\"\n \n \n \n
      \n
    • Filename: box.png
    • \n
    • Position in list: 3
    • \n
    \n

    Robot

    \n\n \n \n \n \n \n \"robot\"\n \n \n \n
      \n
    • Filename: robot.png
    • \n
    • Position in list: 4
    • \n
    \n

    Box on a target square

    \n\n \n \n \n \n \n \"done\"\n \n \n \n
      \n
    • Filename: done.png
    • \n
    • Position in list: 5
    • \n
    • The box has been moved to the target square
    • \n
    \n

    Robot on a target square

    \n\n \n \n \n \n \n \"target\n \n \n \n
      \n
    • Filename: target_robot.png
    • \n
    • Position in list: 6
    • \n
    • The robot can also be on an empty target square
    • \n
    \n

    Creating the grid

    \n

    The new_game method creates the initial state of the game grid:

    \n
        def new_game(self):\n        self.map = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n                    [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1],\n                    [1, 2, 3, 0, 0, 0, 1, 0, 0, 1, 2, 3, 0, 0, 0, 0, 1],\n                    [1, 0, 0, 1, 2, 3, 0, 2, 3, 0, 0, 0, 1, 0, 0, 0, 1],\n                    [1, 0, 4, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1],\n                    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
    \n

    The method creates a two.dimensional list named map which uses the numbered positions of the images in their list to mark up which image goes where. This way the game contains a record of the state of the game grid at all times.

    \n

    NB: in the beginning all spaces on the grid contain a number between 0 and 4. The numbers 5 and 6 are not included, as in the beginning no box or robot is on a target square.

    \n

    The main loop

    \n

    The main_loop method is rather short. With each iteration it calls two methods: check_events goes through any events collected since the previous iteration, and the draw_window method updates the contents of the window.

    \n
        def main_loop(self):\n        while True:\n            self.check_events()\n            self.draw_window()\n\n    def check_events(self):\n        for event in pygame.event.get():\n            if event.type == pygame.QUIT:\n                exit()\n\n    def draw_window(self):\n        self.window.fill((0, 0, 0))\n\n        for y in range(self.height):\n            for x in range(self.width):\n                square = self.map[y][x]\n                self.window.blit(self.images[square], (x * self.scale, y * self.scale))\n\n        pygame.display.flip()
    \n

    At this stage the only event actually handled by the game is closing the game window, e.g. from the exit button. The game then exits by calling the Python exit function.

    \n

    Each time draw_window method is called the entire game grid is matrix is traversed, and the image corresponding to each square in the grid is drawn in the correct location.

    \n

    NB: the coordinates x and y are used in two different ways in the game. When dealing with the indexes of a two-dimensional list, it is logical to give the y coordinate first, as the y refers to the number of the row while x is the number of the column. On the other hand, when using pygame methods, x is usually passed first, as it quite often is when dealing with graphics, and also in mathematical contexts.

    ","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-14/1-game-project.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this part we will use pygame to create a somewhat larger game. It is a variation of the classic Sokoban game, where the player moves a robot on a grid and pushes boxes into correct locations with as few moves as possible."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The end result will look like this:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 852px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/e334a0f0fea9438564e48d0bba7cbbc4/47ff6/game.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: 44.34782608695652%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAAAsTAAALEwEAmpwYAAACGUlEQVQozyWO0UsaAQCH73RHlgm10ztbJUEvkkIzzrsHxRPShx56LUYRkqVPC2rhqZ2iKGiEiFDsdAX2T0SshxhGk/J9hG+tWlIRBA41xm+4fY+/7+H7EbFYLBQKybKcSqWi0agkSfF4PJlMbm5urq9vJBIJeWtLkqRYLJZOpyVJikQi4XBYluVMJkOYzWaP12u323t6epxOp9frHR4eHhkZmZ6eFkVRq9VOcdzMzMz4+Pjg4KAoim632+PxGI1GjuOIvb09ALVajef5x8dHAH6/f2lpCUCj0TCZTOfn5wD29/dnZ2cBtNttAKurq1arlSgmk/fV6tfT09/N5sPzM4AVv39hcRHAz+vr0bGx7+Xyj8vL6sVFs9l8eXl5enoCEAgEbDYbkd/ZAfCtUnGL4kOlUj07q9Zqv66u7iuV45OTer3euLn58/paKpXm5+dbrdb/dz6fr1tWFAXA8dHRu6Gh57s7AJ8V5UuxCOD29vbTxka9XgeQz+c5jgPQ6XQABIPByclJ4sPCwu7u7sry8qjJlM5my4eHgiBwdnvp4CC7va3T6T6urZXL5bm5OYvFUigUcrmcoigul0sQBEJDUQRBUG+6qMku/RpNf19fd1Srdb29lEqlJkmKojQazT9PqlQqkiR5niem9HoLy7IGg4GmLQxjZVlerx+laRPLvmcYM00LDDPBsm+7nmZZlmEYo9E4MDDgcDj+AisL8KSDCe9vAAAAAElFTkSuQmCC'); 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":["/static/e334a0f0fea9438564e48d0bba7cbbc4/a0b58/game.webp 230w","/static/e334a0f0fea9438564e48d0bba7cbbc4/bc10c/game.webp 460w","/static/e334a0f0fea9438564e48d0bba7cbbc4/39392/game.webp 852w"],"sizes":"(max-width: 852px) 100vw, 852px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/e334a0f0fea9438564e48d0bba7cbbc4/81c8e/game.png 230w","/static/e334a0f0fea9438564e48d0bba7cbbc4/08a84/game.png 460w","/static/e334a0f0fea9438564e48d0bba7cbbc4/47ff6/game.png 852w"],"sizes":"(max-width: 852px) 100vw, 852px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/e334a0f0fea9438564e48d0bba7cbbc4/47ff6/game.png","alt":"game","title":"game","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":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"the-game-map","style":"position:relative;"},"children":[{"type":"text","value":"The game map"},{"type":"element","tagName":"a","properties":{"href":"#the-game-map","ariaLabel":"the game map 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's begin by drawing the map used in the game. The game is implemented in the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Sokoban"}]},{"type":"text","value":", which will contain all functionality required to play the game. In this first stage the contents of the class are as follows:"}]},{"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":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" pygame\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Sokoban"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load_images"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"new_game"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"images"},{"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":"get_width"},{"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 window_height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height\n window_width "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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":"window_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" window_height"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_caption"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sokoban\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"main_loop"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"load_images"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"images "},{"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","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"\"floor\""}]},{"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":"\"wall\""}]},{"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":"\"target\""}]},{"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":"\"box\""}]},{"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":"\"robot\""}]},{"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":"\"done\""}]},{"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":"\"target_robot\""}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"images"},{"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":"pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"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":"\".png\""}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"new_game"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"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","punctuation"]},"children":[{"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"text","value":"\n "},{"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":"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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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":"text","value":"\n "},{"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":"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":" "},{"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":" "},{"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","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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"2"}]},{"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":"3"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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":"text","value":"\n "},{"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":"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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"2"}]},{"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":"3"}]},{"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":"0"}]},{"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":"2"}]},{"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":"3"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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":"text","value":"\n "},{"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":"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","number"]},"children":[{"type":"text","value":"4"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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":"text","value":"\n "},{"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"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_loop"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"check_events"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"draw_window"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"check_events"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"for"}]},{"type":"text","value":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"draw_window"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" y "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height"},{"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":"for"}]},{"type":"text","value":" x "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width"},{"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 square "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"y"},{"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":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"images"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"square"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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":"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 Sokoban"},{"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":"p","properties":{},"children":[{"type":"text","value":"Running the program should display a window with the initial state of the game. Let's take a closer look at the code which achieves this."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"the-constructor","style":"position:relative;"},"children":[{"type":"text","value":"The constructor"},{"type":"element","tagName":"a","properties":{"href":"#the-constructor","ariaLabel":"the constructor 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The constructor of the class initializes the pygame modules and the essential variables and data structures involved in the game. It also calls the main loop method of the game."}]},{"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":" "},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"init"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load_images"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"new_game"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"images"},{"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":"get_width"},{"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 window_height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height\n window_width "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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":"window_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" window_height"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_caption"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sokoban\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"main_loop"},{"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":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"load_images"}]},{"type":"text","value":" method loads the images used in the game into a list named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"images"}]},{"type":"text","value":". The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"new_game"}]},{"type":"text","value":" method creates a two-dimensional list named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":", which contains the state of the game grid in the beginning of the game."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"height"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"width"}]},{"type":"text","value":" are initialized based on the dimensions of the game grid. The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"scale"}]},{"type":"text","value":" contains the length of the side of one square in the grid. As each image is a square of the exact same size, the size of all squares is covered by this one variable, and the width of the first image will do just fine for the value. This same value can be used to calculate the width and height of the entire grid, which lets us create a window of the appropriate size to display the game grid."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"loading-images","style":"position:relative;"},"children":[{"type":"text","value":"Loading images"},{"type":"element","tagName":"a","properties":{"href":"#loading-images","ariaLabel":"loading images 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"load_images"}]},{"type":"text","value":" method loads all the images used in the game:"}]},{"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":" "},{"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":"load_images"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"images "},{"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","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"\"floor\""}]},{"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":"\"wall\""}]},{"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":"\"target\""}]},{"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":"\"box\""}]},{"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":"\"robot\""}]},{"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":"\"done\""}]},{"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":"\"target_robot\""}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"images"},{"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":"pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"image"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"load"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"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":"\".png\""}]},{"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":"p","properties":{},"children":[{"type":"text","value":"The game makes use of the following images:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"floor-square","style":"position:relative;"},"children":[{"type":"text","value":"Floor square"},{"type":"element","tagName":"a","properties":{"href":"#floor-square","ariaLabel":"floor square 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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/9aefcdf4192b60fb3bf21288d49a0bd5/a1812/floor.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: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAO0lEQVQ4y2M4dOjQ/7Nnz1IFg8xiADGoBUBmwQ389+8fRZh+BpIKRg0cNXDUwMFp4OAtbahWwFK7CgAAGH0Lv64YzdcAAAAASUVORK5CYII='); 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":["/static/9aefcdf4192b60fb3bf21288d49a0bd5/6ac13/floor.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9aefcdf4192b60fb3bf21288d49a0bd5/a1812/floor.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/9aefcdf4192b60fb3bf21288d49a0bd5/a1812/floor.png","alt":"floor","title":"floor","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":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filename: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"floor.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position in list: 0"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"wall-square","style":"position:relative;"},"children":[{"type":"text","value":"Wall square"},{"type":"element","tagName":"a","properties":{"href":"#wall-square","ariaLabel":"wall square 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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/73ec5a44eba6d09dfe097ba346137a94/a1812/wall.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: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAASElEQVQ4y+3VMQoAMAgDwPhX/YSft0RwLrQZHQI6eGNEZpa7V0R8hQYtcAAgSVvUuZjZMzS3bQ2oyIILLrjgHZTVl7xg1S/gAHx86WTsjKFcAAAAAElFTkSuQmCC'); 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":["/static/73ec5a44eba6d09dfe097ba346137a94/6ac13/wall.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/73ec5a44eba6d09dfe097ba346137a94/a1812/wall.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/73ec5a44eba6d09dfe097ba346137a94/a1812/wall.png","alt":"wall","title":"wall","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":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filename: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"wall.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position in list: 1"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"target-square","style":"position:relative;"},"children":[{"type":"text","value":"Target square"},{"type":"element","tagName":"a","properties":{"href":"#target-square","ariaLabel":"target square 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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/36b1cacabf9fb62e1c67fc5e6cfa50e3/a1812/target.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: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABHElEQVQ4y7WVPQ6CUAzHWRw0yBG8gfEK3oCJlRNwB2/gBZzZPAU7I0zsbmiigQjav6nlgS+CUyVp0lfa3+vH4+EkSYI0TVWEWQ4rWg+zeiAR2fJ8gu53EL8z0ragpvn2JfoBZP3xGCBsK0vQ5WLbeMOPuHEgK00j+Z9OwG4HrNfAbAYsFoDvA3E81Gg2ngS2rThmGbBcAo4j4nmA6w7rIOiZ70xHgd1uyHPJhgPDUNa3G3C9AscjsNlY0OmSDXC7lYD9fnqcBhrHUnLXJhvIUhTiuFoBVSXB3AbOgKWuxcaZsp/v99O3gaa5h4M4RpEEmgEZYFfau3zu6XwOOp9BX+dQHahe8l+Gon5s1A/2Xz499ctB8/pSu2C1fwEvukCRvM6VtZAAAAAASUVORK5CYII='); 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":["/static/36b1cacabf9fb62e1c67fc5e6cfa50e3/6ac13/target.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/36b1cacabf9fb62e1c67fc5e6cfa50e3/a1812/target.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/36b1cacabf9fb62e1c67fc5e6cfa50e3/a1812/target.png","alt":"target","title":"target","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":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filename: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"target.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position in list: 2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The robot should move some box to this square"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"box","style":"position:relative;"},"children":[{"type":"text","value":"Box"},{"type":"element","tagName":"a","properties":{"href":"#box","ariaLabel":"box 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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/6e7afd4acd3f9b214a5c43273b23c834/a1812/box.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: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAzElEQVQ4y+2VPQqEMBCFvZxVjqAWAVN4CH8KtfEQQfAk9h5DC09go295Abcx7sIyWyzswIPATL6ZgckkGMcR0zSJiKyABykj6wnc9/0jyHEcThcgbds2zPOMZVneinHruvqBZ3XDMCAMQ2itEUUR4jj2ir4kSaCUctBLyyfQWossy9B1Heq6RtM0XtHHGEJZ6S2w73ukaeouFUVxqzzPHZSVsv2XQGMM2rZFWZaoqsorQpmU7f+BvwgUHRvxwRZ7euLL4WvrS2TBSn8BD7avDfrO6ztPAAAAAElFTkSuQmCC'); 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":["/static/6e7afd4acd3f9b214a5c43273b23c834/6ac13/box.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6e7afd4acd3f9b214a5c43273b23c834/a1812/box.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/6e7afd4acd3f9b214a5c43273b23c834/a1812/box.png","alt":"box","title":"box","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":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filename: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"box.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position in list: 3"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"robot","style":"position:relative;"},"children":[{"type":"text","value":"Robot"},{"type":"element","tagName":"a","properties":{"href":"#robot","ariaLabel":"robot 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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/5fb4f02fac6c4d361c9057099bc9f890/a1812/robot.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: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAB/klEQVQ4y5VV0WraYBjttbe+QR+iF32KXvdOWOeckVEGveqtF71qi6CusDFW6sSQJcwNklFHEcHZGfsAjdFCQCiFQew6qugZ58OUIqY1Hxw8nnP+w5+QP1mp1+uwbTsUlmWh1WoJyJ/KsmuFJGxG9/d4oyjo93oCcmphw66Hwul0KphMJvLf8zx8Oj5G/uQEFVUVkFOjx2E2WPdsYdd18S6Xw/v1dXwtlwXk1OhFLnT7fXw4OsLHjQ2Yug7TMIRToxe50HEcfC6XYZ2f47tpCsip0Ytc2Lu6QuHwEJl4HMlYDK9iMSjxuGj0ol8y72GhgNzuLrKKIiCn5i57D4MQZ+j70DQNVdNEMpMRkFOj9zj75A7l+RuNkM1mUSwW8a1axRdNE5BTo8fM/LrQQt/3kUgkUKlU5ITwBBDk1Oj5s10uVTgcDpFKpVAqldBoNFCr1QTk1OgxE6nwxdYWVFVFs9l82CE5NXqRCv/e3iKztob8/j5+np1B13UBef7gQDxmlioMzGvPww/LQjqdhmEYAnJq17OzPL8mdIfBL+d1MonBYCAgD+ZxdulCPhgvd3bkZUCQj6IWBua/8Ri/9vbwdnUV7uWlgJwavWcveX7uxmOcbm/jdHMTf25uBOTU7maFC1+wiz4B7XYbHdtGx3Hwu9tF5+JCINxxxGNm0SfgP7rpf7Qvj0ecAAAAAElFTkSuQmCC'); 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":["/static/5fb4f02fac6c4d361c9057099bc9f890/6ac13/robot.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/5fb4f02fac6c4d361c9057099bc9f890/a1812/robot.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/5fb4f02fac6c4d361c9057099bc9f890/a1812/robot.png","alt":"robot","title":"robot","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":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filename: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position in list: 4"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"box-on-a-target-square","style":"position:relative;"},"children":[{"type":"text","value":"Box on a target square"},{"type":"element","tagName":"a","properties":{"href":"#box-on-a-target-square","ariaLabel":"box on a target square 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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/15225a12ccfe0e42a543bea73f4ff889/a1812/done.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: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABk0lEQVQ4y7WVPWsCQRCG7UTEX5Em/0IQbLS18gM80MLOIpgYb/fOu9mNcpD8g4hglzZtCNjbpkqrhV0IITb6hllivrzEDy4LL7dwew8zs+/MxcbjMSaTSSRiVow3US1mfQCXy+VBkNVqZbQB5LVYLDCdTjGbzbaKz83n83DgOrrRaIRsNgvLslAqlVAul0PF7yqVCvL5vIFupLwGDgYDNBoNBEEAIoJS6pu075unT2TOMJQj/RU4HA5Rq9XMR1LKTzkOHCFwphRs14UUwkA5Uk7/T2C9XofWGo7jwGWQ66IrJU57PdzmcrjLZNAmgiYy6e8FZJAnhIGNikU8J5O4ajbR8bzDgB0inCuFfquFp1QK15aFVr8PVwhTlt2BXCvPw6BahW638XB8jPt0GidBYCLmmu4FvFAKZ1rj8egIr/G4Adq+b2rK4ovaC9gjMsCbQgEviQQuuW5EpqZ8SXsBv9rGkRJd24bd7Zq9fI9OCGF8utU2YcYmraF+mJx2NXZo64W04NbWi3w4/Nv4imTARv0LeAO8hwm47qNppgAAAABJRU5ErkJggg=='); 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":["/static/15225a12ccfe0e42a543bea73f4ff889/6ac13/done.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/15225a12ccfe0e42a543bea73f4ff889/a1812/done.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/15225a12ccfe0e42a543bea73f4ff889/a1812/done.png","alt":"done","title":"done","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":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filename: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"done.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position in list: 5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The box has been moved to the target square"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"robot-on-a-target-square","style":"position:relative;"},"children":[{"type":"text","value":"Robot on a target square"},{"type":"element","tagName":"a","properties":{"href":"#robot-on-a-target-square","ariaLabel":"robot on a target square 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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 50px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/70f904da050def3ee01b9b01b0b50b16/a1812/target_robot.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: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAC2klEQVQ4y41VXUsiURiemyANL7wRgm4q6D9U0B+IgrroIvrAqDVNWWKLrK66qD9gVEOrbF9axvSlSxq2hS1tVtoH1NWoSwlLdhEkFY3ps5wz4/bl2B54Zp55z/s+877nnHeG8fv9CAaDsvB6vQgEAhSE5/IlWgwhckN4fMRnoxG/o1EKwolNbhCtf4LpdJoilUrR59jlJaZnZjA2Nwfn4iIF4cQWi8WoD/HNxH0oGL64ADs6Cmt5Odbm5ymsFRVgLRaEef4/BYmTVFLk5AS28XF8q6uDZ3kZnpUVym0si0g8LgomkzRGXjCZBM3v+Bh8SQkcHAfv/j6+ezwU3oMDOBwO8DqdKEjipCzfCz49iRmenwN5eYgqFBi3WmFSq6FTKtGpVMKoVlNblGGA2lpRULbkjGBVFcAwiJhMYB0OWMxmDBuNFJaBAbBTU4gUFVGf1OysWLIgZBEkF55Hiry9tBSJeBzc6ipc6+vQmUwULrcbnM+HhNUqCtbUiLHJ5BtBKbv05CR1FDo7McyymLBY4Ha5sMRxFO61NUx8/YrhwUEI+flAQQHSNzdiMrkEb1tboe3thdNupx1COoAgsLcH59IStPX1uCWVqFQyglLJaXK+GAYJjQZ6rRb2hQX83NnB5uYmBeF2pxP65mYkiGB1tRj3ruQXm4LKSurc3tSERY7Dr91d+Le3KQgntvaWFlFwejrHpmQEz85wp1LBpNFgzG7Hj60tLHu9FIST9jMpFLhraHjVYbIHm06FQoiXlWGjvx9dej1WhoYougwGbPT1IW40Pi+T7MGWWg+CIN6vroCeHhg6OvCnsJDC0NYG2GzPxyyzmbk+Di/PFVmZT2YzwqenFIQL0o6SJfq45BeTD4KAvZERfCkuRiQSoSCc2B6kF76NeSX4dtwnk/B1d8PX2Iib62sKwontXhLM+oHN9gs4PDxEKBhEiOdxEA4jdHREQTnP0znik+0X8BeOmjrCN4yzpAAAAABJRU5ErkJggg=='); 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":["/static/70f904da050def3ee01b9b01b0b50b16/6ac13/target_robot.webp 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/70f904da050def3ee01b9b01b0b50b16/a1812/target_robot.png 50w"],"sizes":"(max-width: 50px) 100vw, 50px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/70f904da050def3ee01b9b01b0b50b16/a1812/target_robot.png","alt":"target robot","title":"target robot","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":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Filename: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"target_robot.png"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Position in list: 6"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The robot can also be on an empty target square"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"creating-the-grid","style":"position:relative;"},"children":[{"type":"text","value":"Creating the grid"},{"type":"element","tagName":"a","properties":{"href":"#creating-the-grid","ariaLabel":"creating the grid 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"new_game"}]},{"type":"text","value":" method creates the initial state of the game grid:"}]},{"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":" "},{"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":"new_game"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"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","punctuation"]},"children":[{"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"text","value":"\n "},{"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":"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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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":"text","value":"\n "},{"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":"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":" "},{"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":" "},{"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","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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"2"}]},{"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":"3"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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":"text","value":"\n "},{"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":"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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"2"}]},{"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":"3"}]},{"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":"0"}]},{"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":"2"}]},{"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":"3"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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":"text","value":"\n "},{"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":"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","number"]},"children":[{"type":"text","value":"4"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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":"text","value":"\n "},{"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method creates a two.dimensional list named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"map"}]},{"type":"text","value":" which uses the numbered positions of the images in "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"their"}]},{"type":"text","value":" list to mark up which image goes where. This way the game contains a record of the state of the game grid at all times."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: in the beginning all spaces on the grid contain a number between 0 and 4. The numbers 5 and 6 are not included, as in the beginning no box or robot is on a target square."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"the-main-loop","style":"position:relative;"},"children":[{"type":"text","value":"The main loop"},{"type":"element","tagName":"a","properties":{"href":"#the-main-loop","ariaLabel":"the main loop 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"main_loop"}]},{"type":"text","value":" method is rather short. With each iteration it calls two methods: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"check_events"}]},{"type":"text","value":" goes through any events collected since the previous iteration, and the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"draw_window"}]},{"type":"text","value":" method updates the contents of the window."}]},{"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":" "},{"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_loop"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"check_events"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"draw_window"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"check_events"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"for"}]},{"type":"text","value":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"draw_window"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill"},{"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","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","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","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\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" y "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height"},{"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":"for"}]},{"type":"text","value":" x "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width"},{"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 square "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"y"},{"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":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"images"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"square"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale"},{"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 pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"flip"},{"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":"p","properties":{},"children":[{"type":"text","value":"At this stage the only event actually handled by the game is closing the game window, e.g. from the exit button. The game then exits by calling the Python "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exit"}]},{"type":"text","value":" function."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each time "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"draw_window"}]},{"type":"text","value":" method is called the entire game grid is matrix is traversed, and the image corresponding to each square in the grid is drawn in the correct location."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the coordinates x and y are used in two different ways in the game. When dealing with the indexes of a two-dimensional list, it is logical to give the y coordinate first, as the y refers to the number of the row while x is the number of the column. On the other hand, when using pygame methods, x is usually passed first, as it quite often is when dealing with graphics, and also in mathematical contexts."}]}],"data":{"quirksMode":false}},"html":"

    In this part we will use pygame to create a somewhat larger game. It is a variation of the classic Sokoban game, where the player moves a robot on a grid and pushes boxes into correct locations with as few moves as possible.

    \n

    The end result will look like this:

    \n\n \n \n \n \n \n \"game\"\n \n \n \n

    The game map

    \n

    Let's begin by drawing the map used in the game. The game is implemented in the class Sokoban, which will contain all functionality required to play the game. In this first stage the contents of the class are as follows:

    \n
    import pygame\n\nclass Sokoban:\n    def __init__(self):\n        pygame.init()\n        \n        self.load_images()\n        self.new_game()\n        \n        self.height = len(self.map)\n        self.width = len(self.map[0])\n        self.scale = self.images[0].get_width()\n\n        window_height = self.scale * self.height\n        window_width = self.scale * self.width\n        self.window = pygame.display.set_mode((window_width, window_height))\n\n        pygame.display.set_caption(\"Sokoban\")\n\n        self.main_loop()\n\n    def load_images(self):\n        self.images = []\n        for name in [\"floor\", \"wall\", \"target\", \"box\", \"robot\", \"done\", \"target_robot\"]:\n            self.images.append(pygame.image.load(name + \".png\"))\n\n    def new_game(self):\n        self.map = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n                    [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1],\n                    [1, 2, 3, 0, 0, 0, 1, 0, 0, 1, 2, 3, 0, 0, 0, 0, 1],\n                    [1, 0, 0, 1, 2, 3, 0, 2, 3, 0, 0, 0, 1, 0, 0, 0, 1],\n                    [1, 0, 4, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1],\n                    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]\n\n    def main_loop(self):\n        while True:\n            self.check_events()\n            self.draw_window()\n\n    def check_events(self):\n        for event in pygame.event.get():\n            if event.type == pygame.QUIT:\n                exit()\n\n    def draw_window(self):\n        self.window.fill((0, 0, 0))\n\n        for y in range(self.height):\n            for x in range(self.width):\n                square = self.map[y][x]\n                self.window.blit(self.images[square], (x * self.scale, y * self.scale))\n\n        pygame.display.flip()\n\nif __name__ == \"__main__\":\n    Sokoban()
    \n

    Running the program should display a window with the initial state of the game. Let's take a closer look at the code which achieves this.

    \n

    The constructor

    \n

    The constructor of the class initializes the pygame modules and the essential variables and data structures involved in the game. It also calls the main loop method of the game.

    \n
        def __init__(self):\n        pygame.init()\n        \n        self.load_images()\n        self.new_game()\n        \n        self.height = len(self.map)\n        self.width = len(self.map[0])\n        self.scale = self.images[0].get_width()\n\n        window_height = self.scale * self.height\n        window_width = self.scale * self.width\n        self.window = pygame.display.set_mode((window_width, window_height))\n\n        pygame.display.set_caption(\"Sokoban\")\n\n        self.main_loop()
    \n

    The load_images method loads the images used in the game into a list named images. The new_game method creates a two-dimensional list named map, which contains the state of the game grid in the beginning of the game.

    \n

    The variables height and width are initialized based on the dimensions of the game grid. The variable scale contains the length of the side of one square in the grid. As each image is a square of the exact same size, the size of all squares is covered by this one variable, and the width of the first image will do just fine for the value. This same value can be used to calculate the width and height of the entire grid, which lets us create a window of the appropriate size to display the game grid.

    \n

    Loading images

    \n

    The load_images method loads all the images used in the game:

    \n
        def load_images(self):\n        self.images = []\n        for name in [\"floor\", \"wall\", \"target\", \"box\", \"robot\", \"done\", \"target_robot\"]:\n            self.images.append(pygame.image.load(name + \".png\"))
    \n

    The game makes use of the following images:

    \n

    Floor square

    \n\n \n \n \n \n \n \"wall\"\n \n \n \n
      \n
    • Filename: wall.png
    • \n
    • Position in list: 1
    • \n
    \n

    Target square

    \n\n \n \n \n \n \n \"box\"\n \n \n \n
      \n
    • Filename: box.png
    • \n
    • Position in list: 3
    • \n
    \n

    Robot

    \n\n \n \n \n \n \n \"robot\"\n \n \n \n
      \n
    • Filename: robot.png
    • \n
    • Position in list: 4
    • \n
    \n

    Box on a target square

    \n\n \n \n \n \n \n \"done\"\n \n \n \n
      \n
    • Filename: done.png
    • \n
    • Position in list: 5
    • \n
    • The box has been moved to the target square
    • \n
    \n

    Robot on a target square

    \n\n \n \n \n \n \n \"target\n \n \n \n
      \n
    • Filename: target_robot.png
    • \n
    • Position in list: 6
    • \n
    • The robot can also be on an empty target square
    • \n
    \n

    Creating the grid

    \n

    The new_game method creates the initial state of the game grid:

    \n
        def new_game(self):\n        self.map = [[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],\n                    [1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1],\n                    [1, 2, 3, 0, 0, 0, 1, 0, 0, 1, 2, 3, 0, 0, 0, 0, 1],\n                    [1, 0, 0, 1, 2, 3, 0, 2, 3, 0, 0, 0, 1, 0, 0, 0, 1],\n                    [1, 0, 4, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0, 1],\n                    [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]
    \n

    The method creates a two.dimensional list named map which uses the numbered positions of the images in their list to mark up which image goes where. This way the game contains a record of the state of the game grid at all times.

    \n

    NB: in the beginning all spaces on the grid contain a number between 0 and 4. The numbers 5 and 6 are not included, as in the beginning no box or robot is on a target square.

    \n

    The main loop

    \n

    The main_loop method is rather short. With each iteration it calls two methods: check_events goes through any events collected since the previous iteration, and the draw_window method updates the contents of the window.

    \n
        def main_loop(self):\n        while True:\n            self.check_events()\n            self.draw_window()\n\n    def check_events(self):\n        for event in pygame.event.get():\n            if event.type == pygame.QUIT:\n                exit()\n\n    def draw_window(self):\n        self.window.fill((0, 0, 0))\n\n        for y in range(self.height):\n            for x in range(self.width):\n                square = self.map[y][x]\n                self.window.blit(self.images[square], (x * self.scale, y * self.scale))\n\n        pygame.display.flip()
    \n

    At this stage the only event actually handled by the game is closing the game window, e.g. from the exit button. The game then exits by calling the Python exit function.

    \n

    Each time draw_window method is called the entire game grid is matrix is traversed, and the image corresponding to each square in the grid is drawn in the correct location.

    \n

    NB: the coordinates x and y are used in two different ways in the game. When dealing with the indexes of a two-dimensional list, it is logical to give the y coordinate first, as the y refers to the number of the row while x is the number of the column. On the other hand, when using pygame methods, x is usually passed first, as it quite often is when dealing with graphics, and also in mathematical contexts.

    ","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-14/1-game-project.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-14/2-robot-and-boxes/page-data.json b/page-data/part-14/2-robot-and-boxes/page-data.json index 01edbd0cf..f78493af1 100644 --- a/page-data/part-14/2-robot-and-boxes/page-data.json +++ b/page-data/part-14/2-robot-and-boxes/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-14/2-robot-and-boxes", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The most difficult thing to implement in a Sokoban style game tends to be moving the robot so that it can push boxes in the desired direction. The game should be able to tell when the robot can move in a direction specified, and be able to handle any situation where a box should move also. Let's tackle this challenge now."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"handling-key-events","style":"position:relative;"},"children":[{"type":"text","value":"Handling key events"},{"type":"element","tagName":"a","properties":{"href":"#handling-key-events","ariaLabel":"handling key events 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The player guides the robot with the four arrow keys, so our event handler should also be able to react to the appropriate key events:"}]},{"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":" "},{"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":"check_events"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"for"}]},{"type":"text","value":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"KEYDOWN"},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_LEFT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"move"},{"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":"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_RIGHT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"move"},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_UP"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"move"},{"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":" "},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_DOWN"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"move"},{"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":"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\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"p","properties":{},"children":[{"type":"text","value":"Now whenever the player presses an arrow key, the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"move"}]},{"type":"text","value":" is called with an appropriate pair of arguments. The first argument contains the movement in the vertical direction, while the second contains the movement in the horizontal direction."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"searching-for-the-robot","style":"position:relative;"},"children":[{"type":"text","value":"Searching for the robot"},{"type":"element","tagName":"a","properties":{"href":"#searching-for-the-robot","ariaLabel":"searching for the robot 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The game has to know the location of the robot in order to move it correctly. Let's add the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find_robot"}]},{"type":"text","value":" which figures out the location of the robot:"}]},{"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":" "},{"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":"find_robot"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self "},{"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":"for"}]},{"type":"text","value":" y "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height"},{"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":"for"}]},{"type":"text","value":" x "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width"},{"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":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"y"},{"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":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"4"}]},{"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":"6"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method goes through all the squares in the game grid and returns the coordinates of the square which contains either the number 4 (the robot on its own) or the number 6 (the robot on a target square)."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The idea is that whenever the player presses an arrow key, first the location of the robot is established by going through the squares of the grid. This may seem a bit slow and superfluous, as we could just as well keep the location of the robot in a separate variable or two. The advantage of this search approach is that we are not storing the location of the robot in two different locations (in the game grid "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" separate variables), but instead we just have to worry about the one location (the game grid), which means that the state of the game in computer memory is simpler to handle."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"changes-to-the-game-grid","style":"position:relative;"},"children":[{"type":"text","value":"Changes to the game grid"},{"type":"element","tagName":"a","properties":{"href":"#changes-to-the-game-grid","ariaLabel":"changes to the game grid 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We already called the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"move"}]},{"type":"text","value":" above, but we haven't actually defined it yet. Let's do that now."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"move"}]},{"type":"text","value":" method takes the direction the player wants to move to as its arguments. It then updates the game grid accordingly, or determines the move is not allowed and leaves the grid unchanged."}]},{"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":" "},{"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":"move"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" move_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" move_x"},{"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 robot_old_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" robot_old_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find_robot"},{"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 robot_new_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_old_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" move_y\n robot_new_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_old_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" move_x\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_new_y"},{"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":"robot_new_x"},{"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":"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":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_new_y"},{"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":"robot_new_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"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","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n box_new_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_new_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" move_y\n box_new_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_new_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" move_x\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"box_new_y"},{"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":"box_new_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"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":" "},{"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","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":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_new_y"},{"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":"robot_new_x"},{"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":"3"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"box_new_y"},{"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":"box_new_x"},{"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":"3"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_old_y"},{"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":"robot_old_x"},{"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":"4"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_new_y"},{"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":"robot_new_x"},{"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":"4"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method has quite a lot of different stages, so let's take a look at each one in turn:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"the-old-and-new-location-of-the-robot","style":"position:relative;"},"children":[{"type":"text","value":"The old and new location of the robot"},{"type":"element","tagName":"a","properties":{"href":"#the-old-and-new-location-of-the-robot","ariaLabel":"the old and new location of the robot 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":"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":" robot_old_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" robot_old_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find_robot"},{"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 robot_new_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_old_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" move_y\n robot_new_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_old_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" move_x"}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First, the method calls the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find_robot"}]},{"type":"text","value":" in order to find the current location of the robot, before the move. This is stored in the variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_old_y"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_old_x"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then the new location of the robot after the prospective move is stored in the variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_new_y"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_new_x"}]},{"type":"text","value":". The new coordinates can be easily calculated by adding the values passed as arguments to the old location of the robot, as both contained vertical and horizontal values."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"did-the-robot-hit-a-wall","style":"position:relative;"},"children":[{"type":"text","value":"Did the robot hit a wall?"},{"type":"element","tagName":"a","properties":{"href":"#did-the-robot-hit-a-wall","ariaLabel":"did the robot hit a wall 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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_new_y"},{"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":"robot_new_x"},{"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":"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":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statement above takes care of the situation where the robot would hit a wall as a result of the move. Remember, 1 was the position of a wall square in the list of images. This is not allowed, so the method simply returns without any further ado."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"moving-a-box","style":"position:relative;"},"children":[{"type":"text","value":"Moving a box"},{"type":"element","tagName":"a","properties":{"href":"#moving-a-box","ariaLabel":"moving a box 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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_new_y"},{"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":"robot_new_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"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","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n box_new_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_new_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" move_y\n box_new_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_new_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" move_x\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"box_new_y"},{"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":"box_new_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"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":" "},{"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","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":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_new_y"},{"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":"robot_new_x"},{"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":"3"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"box_new_y"},{"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":"box_new_x"},{"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":"3"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the new prospective location of the robot contains a number 3 (a box on its own) or a number 5 (a box in a target square), the robot attempts to move the box to the next square along. For this purpose we need two new variables: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"box_new_y"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"box_new_x"}]},{"type":"text","value":", which contain the location of the box after the move."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Similarly to the robot, the box cannot be moved to a wall square, with the identifier 1. Neither can the box move onto another box, or a target square with a box on it. If this would happen as a result of the move, the method again simply returns without making any changes to the grid."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In any other case the box can move. The value in the box's current grid location is decreased by 3, and the value in its new grid location is increased by 3. Because of the clever ordering of the items in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"images"}]},{"type":"text","value":" list, this works out correctly both when the squares involved are floor squares and target squares."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"moving-the-robot","style":"position:relative;"},"children":[{"type":"text","value":"Moving the robot"},{"type":"element","tagName":"a","properties":{"href":"#moving-the-robot","ariaLabel":"moving the robot 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":"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_old_y"},{"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":"robot_old_x"},{"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":"4"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_new_y"},{"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":"robot_new_x"},{"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":"4"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the execution of the method has reached this point without returning, it is time to move the robot as well. The procedure is similar to moving the box, but the value subtracted from and added to the appropriate locations in the grid is 4 this time around. This ensures, again through the clever ordering of the items in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"images"}]},{"type":"text","value":" list, that the final result on the grid is correct both when floor and target squares are involved in the move."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"refactoring","style":"position:relative;"},"children":[{"type":"text","value":"Refactoring?"},{"type":"element","tagName":"a","properties":{"href":"#refactoring","ariaLabel":"refactoring 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Using only the grid to store the state of the game at all times is very handy in the sense that only one variable is permanently invlved in the whole process, and it is relatively easy to update the state of the grid through simple additions and subtractions."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The downside is that it can be a tad difficult to understand the program code of the game. If someone unfamiliar with the logic used saw this following line of code, they would likely be a bit perplexed:"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"box_new_y"},{"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":"box_new_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"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":" "},{"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","punctuation"]},"children":[{"type":"text","value":":"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The code snippet above makes use of "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"magic numbers"}]},{"type":"text","value":" to represent the squares in the grid. ANyone reading the code would have to know that 1 means wall, 3 means a box and 5 means a box in a target square."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The lines involving the clever subtractions and additions would look even more baffling:"}]},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_new_y"},{"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":"robot_new_x"},{"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":"3"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The number 3 meant a box just previously, but now it is subtracted from the value of a square on the grid. This works in the context of our numbering scheme, as it changes a box (3) into a normal floor square (0), or a target square with a box (5) into an empty target square (2), but understanding this requiares a primer in the numbering scheme used."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We could make it easier for anyone reading the code by "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"refactoring"}]},{"type":"text","value":" our implementation. That means improving the structure and readability of the code. One way to achieve this would be to use the names of the squares instead of the numbers 0 to 6, even though this would still not explain how and why numbers can be added and subtracted while maintaining the integrity of the grid."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Making the program code truly accessible would likely require much more fundamentally transformative refactoring. For example, we could keep the structure of the game map in one location, and store the locations of the robot and the boxes in some separate data structure. The downside of "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"this"}]},{"type":"text","value":" would be that this would likely result in a lot more code, and the internal structure of the game would become much more complicated."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Refactoring and code quality is a subject for some subsequent courses, such as "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Software Development Methods"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Software Engineering"}]},{"type":"text","value":"."}]}],"data":{"quirksMode":false}},"html":"

    The most difficult thing to implement in a Sokoban style game tends to be moving the robot so that it can push boxes in the desired direction. The game should be able to tell when the robot can move in a direction specified, and be able to handle any situation where a box should move also. Let's tackle this challenge now.

    \n

    Handling key events

    \n

    The player guides the robot with the four arrow keys, so our event handler should also be able to react to the appropriate key events:

    \n
        def check_events(self):\n        for event in pygame.event.get():\n            if event.type == pygame.KEYDOWN:\n                if event.key == pygame.K_LEFT:\n                    self.move(0, -1)\n                if event.key == pygame.K_RIGHT:\n                    self.move(0, 1)\n                if event.key == pygame.K_UP:\n                    self.move(-1, 0)\n                if event.key == pygame.K_DOWN:\n                    self.move(1, 0)\n\n            if event.type == pygame.QUIT:\n                exit()
    \n

    Now whenever the player presses an arrow key, the method move is called with an appropriate pair of arguments. The first argument contains the movement in the vertical direction, while the second contains the movement in the horizontal direction.

    \n

    Searching for the robot

    \n

    The game has to know the location of the robot in order to move it correctly. Let's add the method find_robot which figures out the location of the robot:

    \n
        def find_robot(self ):\n        for y in range(self.height):\n            for x in range(self.width):\n                if self.map[y][x] in [4, 6]:\n                    return (y, x)
    \n

    The method goes through all the squares in the game grid and returns the coordinates of the square which contains either the number 4 (the robot on its own) or the number 6 (the robot on a target square).

    \n

    The idea is that whenever the player presses an arrow key, first the location of the robot is established by going through the squares of the grid. This may seem a bit slow and superfluous, as we could just as well keep the location of the robot in a separate variable or two. The advantage of this search approach is that we are not storing the location of the robot in two different locations (in the game grid and separate variables), but instead we just have to worry about the one location (the game grid), which means that the state of the game in computer memory is simpler to handle.

    \n

    Changes to the game grid

    \n

    We already called the method move above, but we haven't actually defined it yet. Let's do that now.

    \n

    The move method takes the direction the player wants to move to as its arguments. It then updates the game grid accordingly, or determines the move is not allowed and leaves the grid unchanged.

    \n
        def move(self, move_y, move_x):\n        robot_old_y, robot_old_x = self.find_robot() \n        robot_new_y = robot_old_y + move_y\n        robot_new_x = robot_old_x + move_x\n\n        if self.map[robot_new_y][robot_new_x] == 1:\n            return\n\n        if self.map[robot_new_y][robot_new_x] in [3, 5]:\n            box_new_y = robot_new_y + move_y\n            box_new_x = robot_new_x + move_x\n\n            if self.map[box_new_y][box_new_x] in [1, 3, 5]:\n                return\n\n            self.map[robot_new_y][robot_new_x] -= 3\n            self.map[box_new_y][box_new_x] += 3\n\n        self.map[robot_old_y][robot_old_x] -= 4\n        self.map[robot_new_y][robot_new_x] += 4
    \n

    The method has quite a lot of different stages, so let's take a look at each one in turn:

    \n

    The old and new location of the robot

    \n
            robot_old_y, robot_old_x = self.find_robot() \n        robot_new_y = robot_old_y + move_y\n        robot_new_x = robot_old_x + move_x
    \n

    First, the method calls the find_robot in order to find the current location of the robot, before the move. This is stored in the variables robot_old_y and robot_old_x.

    \n

    Then the new location of the robot after the prospective move is stored in the variables robot_new_y and robot_new_x. The new coordinates can be easily calculated by adding the values passed as arguments to the old location of the robot, as both contained vertical and horizontal values.

    \n

    Did the robot hit a wall?

    \n
            if self.map[robot_new_y][robot_new_x] == 1:\n            return
    \n

    The if statement above takes care of the situation where the robot would hit a wall as a result of the move. Remember, 1 was the position of a wall square in the list of images. This is not allowed, so the method simply returns without any further ado.

    \n

    Moving a box

    \n
            if self.map[robot_new_y][robot_new_x] in [3, 5]:\n            box_new_y = robot_new_y + move_y\n            box_new_x = robot_new_x + move_x\n\n            if self.map[box_new_y][box_new_x] in [1, 3, 5]:\n                return\n\n            self.map[robot_new_y][robot_new_x] -= 3\n            self.map[box_new_y][box_new_x] += 3
    \n

    If the new prospective location of the robot contains a number 3 (a box on its own) or a number 5 (a box in a target square), the robot attempts to move the box to the next square along. For this purpose we need two new variables: box_new_y and box_new_x, which contain the location of the box after the move.

    \n

    Similarly to the robot, the box cannot be moved to a wall square, with the identifier 1. Neither can the box move onto another box, or a target square with a box on it. If this would happen as a result of the move, the method again simply returns without making any changes to the grid.

    \n

    In any other case the box can move. The value in the box's current grid location is decreased by 3, and the value in its new grid location is increased by 3. Because of the clever ordering of the items in the images list, this works out correctly both when the squares involved are floor squares and target squares.

    \n

    Moving the robot

    \n
            self.map[robot_old_y][robot_old_x] -= 4\n        self.map[robot_new_y][robot_new_x] += 4
    \n

    If the execution of the method has reached this point without returning, it is time to move the robot as well. The procedure is similar to moving the box, but the value subtracted from and added to the appropriate locations in the grid is 4 this time around. This ensures, again through the clever ordering of the items in the images list, that the final result on the grid is correct both when floor and target squares are involved in the move.

    \n

    Refactoring?

    \n

    Using only the grid to store the state of the game at all times is very handy in the sense that only one variable is permanently invlved in the whole process, and it is relatively easy to update the state of the grid through simple additions and subtractions.

    \n

    The downside is that it can be a tad difficult to understand the program code of the game. If someone unfamiliar with the logic used saw this following line of code, they would likely be a bit perplexed:

    \n
                if self.map[box_new_y][box_new_x] in [1, 3, 5]:
    \n

    The code snippet above makes use of magic numbers to represent the squares in the grid. ANyone reading the code would have to know that 1 means wall, 3 means a box and 5 means a box in a target square.

    \n

    The lines involving the clever subtractions and additions would look even more baffling:

    \n
                self.map[robot_new_y][robot_new_x] -= 3
    \n

    The number 3 meant a box just previously, but now it is subtracted from the value of a square on the grid. This works in the context of our numbering scheme, as it changes a box (3) into a normal floor square (0), or a target square with a box (5) into an empty target square (2), but understanding this requiares a primer in the numbering scheme used.

    \n

    We could make it easier for anyone reading the code by refactoring our implementation. That means improving the structure and readability of the code. One way to achieve this would be to use the names of the squares instead of the numbers 0 to 6, even though this would still not explain how and why numbers can be added and subtracted while maintaining the integrity of the grid.

    \n

    Making the program code truly accessible would likely require much more fundamentally transformative refactoring. For example, we could keep the structure of the game map in one location, and store the locations of the robot and the boxes in some separate data structure. The downside of this would be that this would likely result in a lot more code, and the internal structure of the game would become much more complicated.

    \n

    Refactoring and code quality is a subject for some subsequent courses, such as Software Development Methods and Software Engineering.

    ","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-14/2-robot-and-boxes.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The most difficult thing to implement in a Sokoban style game tends to be moving the robot so that it can push boxes in the desired direction. The game should be able to tell when the robot can move in a direction specified, and be able to handle any situation where a box should move also. Let's tackle this challenge now."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"handling-key-events","style":"position:relative;"},"children":[{"type":"text","value":"Handling key events"},{"type":"element","tagName":"a","properties":{"href":"#handling-key-events","ariaLabel":"handling key events 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The player guides the robot with the four arrow keys, so our event handler should also be able to react to the appropriate key events:"}]},{"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":" "},{"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":"check_events"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"for"}]},{"type":"text","value":" event "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"KEYDOWN"},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_LEFT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"move"},{"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":"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_RIGHT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"move"},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_UP"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"move"},{"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":" "},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_DOWN"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"move"},{"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":"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\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"QUIT"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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":"p","properties":{},"children":[{"type":"text","value":"Now whenever the player presses an arrow key, the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"move"}]},{"type":"text","value":" is called with an appropriate pair of arguments. The first argument contains the movement in the vertical direction, while the second contains the movement in the horizontal direction."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"searching-for-the-robot","style":"position:relative;"},"children":[{"type":"text","value":"Searching for the robot"},{"type":"element","tagName":"a","properties":{"href":"#searching-for-the-robot","ariaLabel":"searching for the robot 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The game has to know the location of the robot in order to move it correctly. Let's add the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find_robot"}]},{"type":"text","value":" which figures out the location of the robot:"}]},{"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":" "},{"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":"find_robot"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self "},{"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":"for"}]},{"type":"text","value":" y "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height"},{"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":"for"}]},{"type":"text","value":" x "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width"},{"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":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"y"},{"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":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"4"}]},{"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":"6"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method goes through all the squares in the game grid and returns the coordinates of the square which contains either the number 4 (the robot on its own) or the number 6 (the robot on a target square)."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The idea is that whenever the player presses an arrow key, first the location of the robot is established by going through the squares of the grid. This may seem a bit slow and superfluous, as we could just as well keep the location of the robot in a separate variable or two. The advantage of this search approach is that we are not storing the location of the robot in two different locations (in the game grid "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" separate variables), but instead we just have to worry about the one location (the game grid), which means that the state of the game in computer memory is simpler to handle."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"changes-to-the-game-grid","style":"position:relative;"},"children":[{"type":"text","value":"Changes to the game grid"},{"type":"element","tagName":"a","properties":{"href":"#changes-to-the-game-grid","ariaLabel":"changes to the game grid 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We already called the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"move"}]},{"type":"text","value":" above, but we haven't actually defined it yet. Let's do that now."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"move"}]},{"type":"text","value":" method takes the direction the player wants to move to as its arguments. It then updates the game grid accordingly, or determines the move is not allowed and leaves the grid unchanged."}]},{"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":" "},{"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":"move"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" move_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" move_x"},{"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 robot_old_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" robot_old_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find_robot"},{"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 robot_new_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_old_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" move_y\n robot_new_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_old_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" move_x\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_new_y"},{"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":"robot_new_x"},{"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":"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":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_new_y"},{"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":"robot_new_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"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","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n box_new_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_new_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" move_y\n box_new_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_new_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" move_x\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"box_new_y"},{"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":"box_new_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"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":" "},{"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","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":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_new_y"},{"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":"robot_new_x"},{"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":"3"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"box_new_y"},{"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":"box_new_x"},{"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":"3"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_old_y"},{"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":"robot_old_x"},{"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":"4"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_new_y"},{"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":"robot_new_x"},{"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":"4"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method has quite a lot of different stages, so let's take a look at each one in turn:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"the-old-and-new-location-of-the-robot","style":"position:relative;"},"children":[{"type":"text","value":"The old and new location of the robot"},{"type":"element","tagName":"a","properties":{"href":"#the-old-and-new-location-of-the-robot","ariaLabel":"the old and new location of the robot 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":"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":" robot_old_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" robot_old_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find_robot"},{"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 robot_new_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_old_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" move_y\n robot_new_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_old_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" move_x"}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First, the method calls the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find_robot"}]},{"type":"text","value":" in order to find the current location of the robot, before the move. This is stored in the variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_old_y"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_old_x"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Then the new location of the robot after the prospective move is stored in the variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_new_y"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"robot_new_x"}]},{"type":"text","value":". The new coordinates can be easily calculated by adding the values passed as arguments to the old location of the robot, as both contained vertical and horizontal values."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"did-the-robot-hit-a-wall","style":"position:relative;"},"children":[{"type":"text","value":"Did the robot hit a wall?"},{"type":"element","tagName":"a","properties":{"href":"#did-the-robot-hit-a-wall","ariaLabel":"did the robot hit a wall 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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_new_y"},{"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":"robot_new_x"},{"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":"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":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statement above takes care of the situation where the robot would hit a wall as a result of the move. Remember, 1 was the position of a wall square in the list of images. This is not allowed, so the method simply returns without any further ado."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"moving-a-box","style":"position:relative;"},"children":[{"type":"text","value":"Moving a box"},{"type":"element","tagName":"a","properties":{"href":"#moving-a-box","ariaLabel":"moving a box 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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_new_y"},{"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":"robot_new_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"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","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n box_new_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_new_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" move_y\n box_new_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" robot_new_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" move_x\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"box_new_y"},{"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":"box_new_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"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":" "},{"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","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":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_new_y"},{"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":"robot_new_x"},{"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":"3"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"box_new_y"},{"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":"box_new_x"},{"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":"3"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the new prospective location of the robot contains a number 3 (a box on its own) or a number 5 (a box in a target square), the robot attempts to move the box to the next square along. For this purpose we need two new variables: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"box_new_y"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"box_new_x"}]},{"type":"text","value":", which contain the location of the box after the move."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Similarly to the robot, the box cannot be moved to a wall square, with the identifier 1. Neither can the box move onto another box, or a target square with a box on it. If this would happen as a result of the move, the method again simply returns without making any changes to the grid."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In any other case the box can move. The value in the box's current grid location is decreased by 3, and the value in its new grid location is increased by 3. Because of the clever ordering of the items in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"images"}]},{"type":"text","value":" list, this works out correctly both when the squares involved are floor squares and target squares."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"moving-the-robot","style":"position:relative;"},"children":[{"type":"text","value":"Moving the robot"},{"type":"element","tagName":"a","properties":{"href":"#moving-the-robot","ariaLabel":"moving the robot 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":"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_old_y"},{"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":"robot_old_x"},{"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":"4"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_new_y"},{"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":"robot_new_x"},{"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":"4"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the execution of the method has reached this point without returning, it is time to move the robot as well. The procedure is similar to moving the box, but the value subtracted from and added to the appropriate locations in the grid is 4 this time around. This ensures, again through the clever ordering of the items in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"images"}]},{"type":"text","value":" list, that the final result on the grid is correct both when floor and target squares are involved in the move."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"refactoring","style":"position:relative;"},"children":[{"type":"text","value":"Refactoring?"},{"type":"element","tagName":"a","properties":{"href":"#refactoring","ariaLabel":"refactoring 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Using only the grid to store the state of the game at all times is very handy in the sense that only one variable is permanently invlved in the whole process, and it is relatively easy to update the state of the grid through simple additions and subtractions."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The downside is that it can be a tad difficult to understand the program code of the game. If someone unfamiliar with the logic used saw this following line of code, they would likely be a bit perplexed:"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"box_new_y"},{"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":"box_new_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"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":" "},{"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","punctuation"]},"children":[{"type":"text","value":":"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The code snippet above makes use of "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"magic numbers"}]},{"type":"text","value":" to represent the squares in the grid. ANyone reading the code would have to know that 1 means wall, 3 means a box and 5 means a box in a target square."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The lines involving the clever subtractions and additions would look even more baffling:"}]},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"robot_new_y"},{"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":"robot_new_x"},{"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":"3"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The number 3 meant a box just previously, but now it is subtracted from the value of a square on the grid. This works in the context of our numbering scheme, as it changes a box (3) into a normal floor square (0), or a target square with a box (5) into an empty target square (2), but understanding this requiares a primer in the numbering scheme used."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We could make it easier for anyone reading the code by "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"refactoring"}]},{"type":"text","value":" our implementation. That means improving the structure and readability of the code. One way to achieve this would be to use the names of the squares instead of the numbers 0 to 6, even though this would still not explain how and why numbers can be added and subtracted while maintaining the integrity of the grid."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Making the program code truly accessible would likely require much more fundamentally transformative refactoring. For example, we could keep the structure of the game map in one location, and store the locations of the robot and the boxes in some separate data structure. The downside of "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"this"}]},{"type":"text","value":" would be that this would likely result in a lot more code, and the internal structure of the game would become much more complicated."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Refactoring and code quality is a subject for some subsequent courses, such as "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Software Development Methods"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Software Engineering"}]},{"type":"text","value":"."}]}],"data":{"quirksMode":false}},"html":"

    The most difficult thing to implement in a Sokoban style game tends to be moving the robot so that it can push boxes in the desired direction. The game should be able to tell when the robot can move in a direction specified, and be able to handle any situation where a box should move also. Let's tackle this challenge now.

    \n

    Handling key events

    \n

    The player guides the robot with the four arrow keys, so our event handler should also be able to react to the appropriate key events:

    \n
        def check_events(self):\n        for event in pygame.event.get():\n            if event.type == pygame.KEYDOWN:\n                if event.key == pygame.K_LEFT:\n                    self.move(0, -1)\n                if event.key == pygame.K_RIGHT:\n                    self.move(0, 1)\n                if event.key == pygame.K_UP:\n                    self.move(-1, 0)\n                if event.key == pygame.K_DOWN:\n                    self.move(1, 0)\n\n            if event.type == pygame.QUIT:\n                exit()
    \n

    Now whenever the player presses an arrow key, the method move is called with an appropriate pair of arguments. The first argument contains the movement in the vertical direction, while the second contains the movement in the horizontal direction.

    \n

    Searching for the robot

    \n

    The game has to know the location of the robot in order to move it correctly. Let's add the method find_robot which figures out the location of the robot:

    \n
        def find_robot(self ):\n        for y in range(self.height):\n            for x in range(self.width):\n                if self.map[y][x] in [4, 6]:\n                    return (y, x)
    \n

    The method goes through all the squares in the game grid and returns the coordinates of the square which contains either the number 4 (the robot on its own) or the number 6 (the robot on a target square).

    \n

    The idea is that whenever the player presses an arrow key, first the location of the robot is established by going through the squares of the grid. This may seem a bit slow and superfluous, as we could just as well keep the location of the robot in a separate variable or two. The advantage of this search approach is that we are not storing the location of the robot in two different locations (in the game grid and separate variables), but instead we just have to worry about the one location (the game grid), which means that the state of the game in computer memory is simpler to handle.

    \n

    Changes to the game grid

    \n

    We already called the method move above, but we haven't actually defined it yet. Let's do that now.

    \n

    The move method takes the direction the player wants to move to as its arguments. It then updates the game grid accordingly, or determines the move is not allowed and leaves the grid unchanged.

    \n
        def move(self, move_y, move_x):\n        robot_old_y, robot_old_x = self.find_robot() \n        robot_new_y = robot_old_y + move_y\n        robot_new_x = robot_old_x + move_x\n\n        if self.map[robot_new_y][robot_new_x] == 1:\n            return\n\n        if self.map[robot_new_y][robot_new_x] in [3, 5]:\n            box_new_y = robot_new_y + move_y\n            box_new_x = robot_new_x + move_x\n\n            if self.map[box_new_y][box_new_x] in [1, 3, 5]:\n                return\n\n            self.map[robot_new_y][robot_new_x] -= 3\n            self.map[box_new_y][box_new_x] += 3\n\n        self.map[robot_old_y][robot_old_x] -= 4\n        self.map[robot_new_y][robot_new_x] += 4
    \n

    The method has quite a lot of different stages, so let's take a look at each one in turn:

    \n

    The old and new location of the robot

    \n
            robot_old_y, robot_old_x = self.find_robot() \n        robot_new_y = robot_old_y + move_y\n        robot_new_x = robot_old_x + move_x
    \n

    First, the method calls the find_robot in order to find the current location of the robot, before the move. This is stored in the variables robot_old_y and robot_old_x.

    \n

    Then the new location of the robot after the prospective move is stored in the variables robot_new_y and robot_new_x. The new coordinates can be easily calculated by adding the values passed as arguments to the old location of the robot, as both contained vertical and horizontal values.

    \n

    Did the robot hit a wall?

    \n
            if self.map[robot_new_y][robot_new_x] == 1:\n            return
    \n

    The if statement above takes care of the situation where the robot would hit a wall as a result of the move. Remember, 1 was the position of a wall square in the list of images. This is not allowed, so the method simply returns without any further ado.

    \n

    Moving a box

    \n
            if self.map[robot_new_y][robot_new_x] in [3, 5]:\n            box_new_y = robot_new_y + move_y\n            box_new_x = robot_new_x + move_x\n\n            if self.map[box_new_y][box_new_x] in [1, 3, 5]:\n                return\n\n            self.map[robot_new_y][robot_new_x] -= 3\n            self.map[box_new_y][box_new_x] += 3
    \n

    If the new prospective location of the robot contains a number 3 (a box on its own) or a number 5 (a box in a target square), the robot attempts to move the box to the next square along. For this purpose we need two new variables: box_new_y and box_new_x, which contain the location of the box after the move.

    \n

    Similarly to the robot, the box cannot be moved to a wall square, with the identifier 1. Neither can the box move onto another box, or a target square with a box on it. If this would happen as a result of the move, the method again simply returns without making any changes to the grid.

    \n

    In any other case the box can move. The value in the box's current grid location is decreased by 3, and the value in its new grid location is increased by 3. Because of the clever ordering of the items in the images list, this works out correctly both when the squares involved are floor squares and target squares.

    \n

    Moving the robot

    \n
            self.map[robot_old_y][robot_old_x] -= 4\n        self.map[robot_new_y][robot_new_x] += 4
    \n

    If the execution of the method has reached this point without returning, it is time to move the robot as well. The procedure is similar to moving the box, but the value subtracted from and added to the appropriate locations in the grid is 4 this time around. This ensures, again through the clever ordering of the items in the images list, that the final result on the grid is correct both when floor and target squares are involved in the move.

    \n

    Refactoring?

    \n

    Using only the grid to store the state of the game at all times is very handy in the sense that only one variable is permanently invlved in the whole process, and it is relatively easy to update the state of the grid through simple additions and subtractions.

    \n

    The downside is that it can be a tad difficult to understand the program code of the game. If someone unfamiliar with the logic used saw this following line of code, they would likely be a bit perplexed:

    \n
                if self.map[box_new_y][box_new_x] in [1, 3, 5]:
    \n

    The code snippet above makes use of magic numbers to represent the squares in the grid. ANyone reading the code would have to know that 1 means wall, 3 means a box and 5 means a box in a target square.

    \n

    The lines involving the clever subtractions and additions would look even more baffling:

    \n
                self.map[robot_new_y][robot_new_x] -= 3
    \n

    The number 3 meant a box just previously, but now it is subtracted from the value of a square on the grid. This works in the context of our numbering scheme, as it changes a box (3) into a normal floor square (0), or a target square with a box (5) into an empty target square (2), but understanding this requiares a primer in the numbering scheme used.

    \n

    We could make it easier for anyone reading the code by refactoring our implementation. That means improving the structure and readability of the code. One way to achieve this would be to use the names of the squares instead of the numbers 0 to 6, even though this would still not explain how and why numbers can be added and subtracted while maintaining the integrity of the grid.

    \n

    Making the program code truly accessible would likely require much more fundamentally transformative refactoring. For example, we could keep the structure of the game map in one location, and store the locations of the robot and the boxes in some separate data structure. The downside of this would be that this would likely result in a lot more code, and the internal structure of the game would become much more complicated.

    \n

    Refactoring and code quality is a subject for some subsequent courses, such as Software Development Methods and Software Engineering.

    ","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-14/2-robot-and-boxes.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-14/3-finishing-the-game/page-data.json b/page-data/part-14/3-finishing-the-game/page-data.json index c9f661b4d..191a82bcd 100644 --- a/page-data/part-14/3-finishing-the-game/page-data.json +++ b/page-data/part-14/3-finishing-the-game/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-14/3-finishing-the-game", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Our game is already quite functional, so it is time to add some finishing touches to it. We will add a counter for displaying the moves taken, an option to start a new game and close the game with keyboard input, and a notification for when the player succeeds in solving the game."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"move-counter","style":"position:relative;"},"children":[{"type":"text","value":"Move counter"},{"type":"element","tagName":"a","properties":{"href":"#move-counter","ariaLabel":"move counter 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The move counter near the bottom edge of the game window displaye the number of moves taken by the player so far. This can be used to find the solution with the least number of moves."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The counter requires some shanges to the code. First, let's change the constructor so that there is adequate space for the counter, and that we have an appropriate font at our disposal in order to draw the text:"}]},{"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":" "},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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":"window_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" window_height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"game_font "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"SysFont"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arial\""}]},{"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":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"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"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The move counter is initialized to zero at the beginning of the game. Each move increases it by one:"}]},{"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":" "},{"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":"new_game"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"moves "},{"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":"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":" "},{"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":"move"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" move_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" move_x"},{"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","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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"moves "},{"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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each time the window contents are updated, the number of moves taken shown on the screen should also be updated:"}]},{"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":" "},{"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":"draw_window"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","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 game_text "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"game_font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"render"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Moves: \""}]},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"moves"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"255"}]},{"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":"0"}]},{"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":"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"game_text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"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":["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","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"},{"type":"element","tagName":"h2","properties":{"id":"new-game-and-exiting-the-game","style":"position:relative;"},"children":[{"type":"text","value":"New game and exiting the game"},{"type":"element","tagName":"a","properties":{"href":"#new-game-and-exiting-the-game","ariaLabel":"new game and exiting the game 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Next, let's add keyboard commands for starting a new game with F2 and exiting the game with Esc. Both are rather easy to implement:"}]},{"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":" "},{"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":"check_events"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_F2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"new_game"},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_ESCAPE"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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","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"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We should also add information about this functionality for the player to see:"}]},{"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":" "},{"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":"draw_window"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","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 game_text "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"game_font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"render"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"F2 = new game\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"255"}]},{"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":"0"}]},{"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":"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"game_text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"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":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n game_text "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"game_font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"render"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Esc = exit game\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"255"}]},{"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":"0"}]},{"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":"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"game_text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"400"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"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":["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","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"},{"type":"element","tagName":"h2","properties":{"id":"solving-the-game","style":"position:relative;"},"children":[{"type":"text","value":"Solving the game"},{"type":"element","tagName":"a","properties":{"href":"#solving-the-game","ariaLabel":"solving the game 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The player has solved the game when each box is in one of the target squares. The following method takes care of checking this:"}]},{"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":" "},{"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":"game_solved"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"for"}]},{"type":"text","value":" y "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height"},{"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":"for"}]},{"type":"text","value":" x "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width"},{"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":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"y"},{"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":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"2"}]},{"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":"6"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"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","boolean"]},"children":[{"type":"text","value":"True"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method goes through all the squares in the game grid. If any of the squares is a 2 (an empty target square) or a 6 (a robot in a target square) the game is not yet solved, so the method returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":". If no such square is present in the grid, all target squares must be occupied by boxes, the game is solved, and the method returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the player solves the game, we should display an appropriate message with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"draw_window"}]},{"type":"text","value":" method:"}]},{"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":" "},{"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":"draw_window"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"game_solved"},{"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 game_text "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"game_font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"render"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Congratulations, you solved the game!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"255"}]},{"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":"0"}]},{"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":"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 game_text_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width "},{"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":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" game_text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"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":" "},{"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":"2"}]},{"type":"text","value":"\n game_text_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height "},{"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":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" game_text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"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":" "},{"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":"2"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rect"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"0"}]},{"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":"0"}]},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"game_text_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" game_text_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" game_text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"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":" game_text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"game_text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"game_text_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" game_text_y"},{"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","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"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For completeness' sake, let's also change the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"move"}]},{"type":"text","value":" method so that the player can no longer move when they have solved the game:"}]},{"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":" "},{"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":"move"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" move_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" move_x"},{"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":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"game_solved"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"\n "},{"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"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The player can still see the game grid and the final state of the game, however."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"a-hint-for-testing","style":"position:relative;"},"children":[{"type":"text","value":"A hint for testing"},{"type":"element","tagName":"a","properties":{"href":"#a-hint-for-testing","ariaLabel":"a hint for testing 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When developing games it often happens that you'd want to check what happens in some later situation in the game. For example, in this game the moment where the game is solved is one such situation."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It can be difficult to test the correct functioning of a situation like that, as you'd normally ahve to solve the game to reach that point in the game. As programmers we can make some temporary alleviations in our games, to make it easier to test them. For example, we could add the following to make it temporarily easier to solve the game:"}]},{"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":" "},{"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":"game_solved"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now the method always returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":", which means that the game is \"solved\" to begin with. This makes it easy to check that the noification at the end looks good and the player can no longer move on the grid after solving. When this functionality is thoroughly tested, we can revoke the changes."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"your-game-on-github","style":"position:relative;"},"children":[{"type":"text","value":"Your game on GitHub?"},{"type":"element","tagName":"a","properties":{"href":"#your-game-on-github","ariaLabel":"your game on github 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The game is now finished. If you want an easy way to play around with the code and images, you can retrieve the source code from GitHub:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://github.com/moocfi/sokoban","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://github.com/moocfi/sokoban"}]}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"GitHub is a popular place for many kinds of programming projects. It can be used to store the source code and other materials of all your own programming projects as well, and your program will then be maintained through git version control, and it can be easily shared with others. You will become very familiar with git and GitHub if you continue on to other mooc.fi programming courses."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"how-many-moves-are-required","style":"position:relative;"},"children":[{"type":"text","value":"How many moves are required?"},{"type":"element","tagName":"a","properties":{"href":"#how-many-moves-are-required","ariaLabel":"how many moves are required 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The grid in this game is quite small, but the game is not all that easy. The first challenge is simply passing the game, but the next stage is trying to do so with as few moves as possible. How short is the shortest path to a solution?"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Looking for the shortest possible solution is not an easy task at all, but there are computational solutions to this as well. They are one of the subjects of the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Data Structures and Algorithms"}]},{"type":"text","value":" course."}]}],"data":{"quirksMode":false}},"html":"

    Our game is already quite functional, so it is time to add some finishing touches to it. We will add a counter for displaying the moves taken, an option to start a new game and close the game with keyboard input, and a notification for when the player succeeds in solving the game.

    \n

    Move counter

    \n

    The move counter near the bottom edge of the game window displaye the number of moves taken by the player so far. This can be used to find the solution with the least number of moves.

    \n

    The counter requires some shanges to the code. First, let's change the constructor so that there is adequate space for the counter, and that we have an appropriate font at our disposal in order to draw the text:

    \n
        def __init__(self):\n        ...\n        self.window = pygame.display.set_mode((window_width, window_height + self.scale))\n\n        self.game_font = pygame.font.SysFont(\"Arial\", 24)\n        ...
    \n

    The move counter is initialized to zero at the beginning of the game. Each move increases it by one:

    \n
        def new_game(self):\n        ...\n        self.moves = 0
    \n
        def move(self, move_y, move_x):\n        ...\n        self.moves += 1\n
    \n

    Each time the window contents are updated, the number of moves taken shown on the screen should also be updated:

    \n
        def draw_window(self):\n        ...\n        game_text = self.game_font.render(\"Moves: \" + str(self.moves), True, (255, 0, 0))\n        self.window.blit(game_text, (25, self.height * self.scale + 10))\n        ...
    \n

    New game and exiting the game

    \n

    Next, let's add keyboard commands for starting a new game with F2 and exiting the game with Esc. Both are rather easy to implement:

    \n
        def check_events(self):\n        ...\n                if event.key == pygame.K_F2:\n                    self.new_game()\n                if event.key == pygame.K_ESCAPE:\n                    exit()\n        ...
    \n

    We should also add information about this functionality for the player to see:

    \n
        def draw_window(self):\n        ...\n        game_text = self.game_font.render(\"F2 = new game\", True, (255, 0, 0))\n        self.window.blit(game_text, (200, self.height * self.scale + 10))\n\n        game_text = self.game_font.render(\"Esc = exit game\", True, (255, 0, 0))\n        self.window.blit(game_text, (400, self.height * self.scale + 10))\n        ...
    \n

    Solving the game

    \n

    The player has solved the game when each box is in one of the target squares. The following method takes care of checking this:

    \n
        def game_solved(self):\n        for y in range(self.height):\n            for x in range(self.width):\n                if self.map[y][x] in [2, 6]:\n                    return False\n        return True
    \n

    The method goes through all the squares in the game grid. If any of the squares is a 2 (an empty target square) or a 6 (a robot in a target square) the game is not yet solved, so the method returns False. If no such square is present in the grid, all target squares must be occupied by boxes, the game is solved, and the method returns True.

    \n

    If the player solves the game, we should display an appropriate message with the draw_window method:

    \n
        def draw_window(self):\n        ...\n        if self.game_solved():\n            game_text = self.game_font.render(\"Congratulations, you solved the game!\", True, (255, 0, 0))\n            game_text_x = self.scale * self.width / 2 - game_text.get_width() / 2\n            game_text_y = self.scale * self.height / 2 - game_text.get_height() / 2\n            pygame.draw.rect(self.window, (0, 0, 0), (game_text_x, game_text_y, game_text.get_width(), game_text.get_height()))\n            self.window.blit(game_text, (game_text_x, game_text_y))\n        ...
    \n

    For completeness' sake, let's also change the move method so that the player can no longer move when they have solved the game:

    \n
        def move(self, move_y, move_x):\n        if self.game_solved():\n            return\n        ...
    \n

    The player can still see the game grid and the final state of the game, however.

    \n

    A hint for testing

    \n

    When developing games it often happens that you'd want to check what happens in some later situation in the game. For example, in this game the moment where the game is solved is one such situation.

    \n

    It can be difficult to test the correct functioning of a situation like that, as you'd normally ahve to solve the game to reach that point in the game. As programmers we can make some temporary alleviations in our games, to make it easier to test them. For example, we could add the following to make it temporarily easier to solve the game:

    \n
        def game_solved(self):\n        return True
    \n

    Now the method always returns True, which means that the game is \"solved\" to begin with. This makes it easy to check that the noification at the end looks good and the player can no longer move on the grid after solving. When this functionality is thoroughly tested, we can revoke the changes.

    \n

    Your game on GitHub?

    \n

    The game is now finished. If you want an easy way to play around with the code and images, you can retrieve the source code from GitHub:

    \n\n

    GitHub is a popular place for many kinds of programming projects. It can be used to store the source code and other materials of all your own programming projects as well, and your program will then be maintained through git version control, and it can be easily shared with others. You will become very familiar with git and GitHub if you continue on to other mooc.fi programming courses.

    \n

    How many moves are required?

    \n

    The grid in this game is quite small, but the game is not all that easy. The first challenge is simply passing the game, but the next stage is trying to do so with as few moves as possible. How short is the shortest path to a solution?

    \n

    Looking for the shortest possible solution is not an easy task at all, but there are computational solutions to this as well. They are one of the subjects of the Data Structures and Algorithms course.

    ","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-14/3-finishing-the-game.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Our game is already quite functional, so it is time to add some finishing touches to it. We will add a counter for displaying the moves taken, an option to start a new game and close the game with keyboard input, and a notification for when the player succeeds in solving the game."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"move-counter","style":"position:relative;"},"children":[{"type":"text","value":"Move counter"},{"type":"element","tagName":"a","properties":{"href":"#move-counter","ariaLabel":"move counter 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The move counter near the bottom edge of the game window displaye the number of moves taken by the player so far. This can be used to find the solution with the least number of moves."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The counter requires some shanges to the code. First, let's change the constructor so that there is adequate space for the counter, and that we have an appropriate font at our disposal in order to draw the text:"}]},{"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":" "},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"display"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_mode"},{"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":"window_width"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" window_height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"game_font "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"SysFont"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Arial\""}]},{"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":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"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"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The move counter is initialized to zero at the beginning of the game. Each move increases it by one:"}]},{"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":" "},{"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":"new_game"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"moves "},{"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":"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":" "},{"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":"move"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" move_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" move_x"},{"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","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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"moves "},{"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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each time the window contents are updated, the number of moves taken shown on the screen should also be updated:"}]},{"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":" "},{"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":"draw_window"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","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 game_text "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"game_font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"render"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Moves: \""}]},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"moves"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"255"}]},{"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":"0"}]},{"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":"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"game_text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"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":["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","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"},{"type":"element","tagName":"h2","properties":{"id":"new-game-and-exiting-the-game","style":"position:relative;"},"children":[{"type":"text","value":"New game and exiting the game"},{"type":"element","tagName":"a","properties":{"href":"#new-game-and-exiting-the-game","ariaLabel":"new game and exiting the game 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Next, let's add keyboard commands for starting a new game with F2 and exiting the game with Esc. Both are rather easy to implement:"}]},{"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":" "},{"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":"check_events"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_F2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"new_game"},{"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":"if"}]},{"type":"text","value":" event"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"key "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"K_ESCAPE"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n exit"},{"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","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"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We should also add information about this functionality for the player to see:"}]},{"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":" "},{"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":"draw_window"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","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 game_text "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"game_font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"render"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"F2 = new game\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"255"}]},{"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":"0"}]},{"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":"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"game_text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"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":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n game_text "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"game_font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"render"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Esc = exit game\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"255"}]},{"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":"0"}]},{"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":"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"game_text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"400"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"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":["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","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"},{"type":"element","tagName":"h2","properties":{"id":"solving-the-game","style":"position:relative;"},"children":[{"type":"text","value":"Solving the game"},{"type":"element","tagName":"a","properties":{"href":"#solving-the-game","ariaLabel":"solving the game 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The player has solved the game when each box is in one of the target squares. The following method takes care of checking this:"}]},{"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":" "},{"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":"game_solved"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"for"}]},{"type":"text","value":" y "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height"},{"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":"for"}]},{"type":"text","value":" x "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width"},{"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":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"map"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"y"},{"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":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"2"}]},{"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":"6"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"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","boolean"]},"children":[{"type":"text","value":"True"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method goes through all the squares in the game grid. If any of the squares is a 2 (an empty target square) or a 6 (a robot in a target square) the game is not yet solved, so the method returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":". If no such square is present in the grid, all target squares must be occupied by boxes, the game is solved, and the method returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the player solves the game, we should display an appropriate message with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"draw_window"}]},{"type":"text","value":" method:"}]},{"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":" "},{"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":"draw_window"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"game_solved"},{"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 game_text "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"game_font"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"render"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Congratulations, you solved the game!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"255"}]},{"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":"0"}]},{"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":"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 game_text_x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width "},{"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":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" game_text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"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":" "},{"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":"2"}]},{"type":"text","value":"\n game_text_y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"scale "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height "},{"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":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" game_text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"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":" "},{"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":"2"}]},{"type":"text","value":"\n pygame"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rect"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"0"}]},{"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":"0"}]},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"game_text_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" game_text_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" game_text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_width"},{"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":" game_text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_height"},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"window"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"blit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"game_text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"game_text_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" game_text_y"},{"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","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"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For completeness' sake, let's also change the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"move"}]},{"type":"text","value":" method so that the player can no longer move when they have solved the game:"}]},{"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":" "},{"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":"move"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" move_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" move_x"},{"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":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"game_solved"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":"\n "},{"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"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The player can still see the game grid and the final state of the game, however."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"a-hint-for-testing","style":"position:relative;"},"children":[{"type":"text","value":"A hint for testing"},{"type":"element","tagName":"a","properties":{"href":"#a-hint-for-testing","ariaLabel":"a hint for testing 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When developing games it often happens that you'd want to check what happens in some later situation in the game. For example, in this game the moment where the game is solved is one such situation."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It can be difficult to test the correct functioning of a situation like that, as you'd normally ahve to solve the game to reach that point in the game. As programmers we can make some temporary alleviations in our games, to make it easier to test them. For example, we could add the following to make it temporarily easier to solve the game:"}]},{"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":" "},{"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":"game_solved"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]}]}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now the method always returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":", which means that the game is \"solved\" to begin with. This makes it easy to check that the noification at the end looks good and the player can no longer move on the grid after solving. When this functionality is thoroughly tested, we can revoke the changes."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"your-game-on-github","style":"position:relative;"},"children":[{"type":"text","value":"Your game on GitHub?"},{"type":"element","tagName":"a","properties":{"href":"#your-game-on-github","ariaLabel":"your game on github 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The game is now finished. If you want an easy way to play around with the code and images, you can retrieve the source code from GitHub:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://github.com/moocfi/sokoban","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://github.com/moocfi/sokoban"}]}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"GitHub is a popular place for many kinds of programming projects. It can be used to store the source code and other materials of all your own programming projects as well, and your program will then be maintained through git version control, and it can be easily shared with others. You will become very familiar with git and GitHub if you continue on to other mooc.fi programming courses."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"how-many-moves-are-required","style":"position:relative;"},"children":[{"type":"text","value":"How many moves are required?"},{"type":"element","tagName":"a","properties":{"href":"#how-many-moves-are-required","ariaLabel":"how many moves are required 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The grid in this game is quite small, but the game is not all that easy. The first challenge is simply passing the game, but the next stage is trying to do so with as few moves as possible. How short is the shortest path to a solution?"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Looking for the shortest possible solution is not an easy task at all, but there are computational solutions to this as well. They are one of the subjects of the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Data Structures and Algorithms"}]},{"type":"text","value":" course."}]}],"data":{"quirksMode":false}},"html":"

    Our game is already quite functional, so it is time to add some finishing touches to it. We will add a counter for displaying the moves taken, an option to start a new game and close the game with keyboard input, and a notification for when the player succeeds in solving the game.

    \n

    Move counter

    \n

    The move counter near the bottom edge of the game window displaye the number of moves taken by the player so far. This can be used to find the solution with the least number of moves.

    \n

    The counter requires some shanges to the code. First, let's change the constructor so that there is adequate space for the counter, and that we have an appropriate font at our disposal in order to draw the text:

    \n
        def __init__(self):\n        ...\n        self.window = pygame.display.set_mode((window_width, window_height + self.scale))\n\n        self.game_font = pygame.font.SysFont(\"Arial\", 24)\n        ...
    \n

    The move counter is initialized to zero at the beginning of the game. Each move increases it by one:

    \n
        def new_game(self):\n        ...\n        self.moves = 0
    \n
        def move(self, move_y, move_x):\n        ...\n        self.moves += 1\n
    \n

    Each time the window contents are updated, the number of moves taken shown on the screen should also be updated:

    \n
        def draw_window(self):\n        ...\n        game_text = self.game_font.render(\"Moves: \" + str(self.moves), True, (255, 0, 0))\n        self.window.blit(game_text, (25, self.height * self.scale + 10))\n        ...
    \n

    New game and exiting the game

    \n

    Next, let's add keyboard commands for starting a new game with F2 and exiting the game with Esc. Both are rather easy to implement:

    \n
        def check_events(self):\n        ...\n                if event.key == pygame.K_F2:\n                    self.new_game()\n                if event.key == pygame.K_ESCAPE:\n                    exit()\n        ...
    \n

    We should also add information about this functionality for the player to see:

    \n
        def draw_window(self):\n        ...\n        game_text = self.game_font.render(\"F2 = new game\", True, (255, 0, 0))\n        self.window.blit(game_text, (200, self.height * self.scale + 10))\n\n        game_text = self.game_font.render(\"Esc = exit game\", True, (255, 0, 0))\n        self.window.blit(game_text, (400, self.height * self.scale + 10))\n        ...
    \n

    Solving the game

    \n

    The player has solved the game when each box is in one of the target squares. The following method takes care of checking this:

    \n
        def game_solved(self):\n        for y in range(self.height):\n            for x in range(self.width):\n                if self.map[y][x] in [2, 6]:\n                    return False\n        return True
    \n

    The method goes through all the squares in the game grid. If any of the squares is a 2 (an empty target square) or a 6 (a robot in a target square) the game is not yet solved, so the method returns False. If no such square is present in the grid, all target squares must be occupied by boxes, the game is solved, and the method returns True.

    \n

    If the player solves the game, we should display an appropriate message with the draw_window method:

    \n
        def draw_window(self):\n        ...\n        if self.game_solved():\n            game_text = self.game_font.render(\"Congratulations, you solved the game!\", True, (255, 0, 0))\n            game_text_x = self.scale * self.width / 2 - game_text.get_width() / 2\n            game_text_y = self.scale * self.height / 2 - game_text.get_height() / 2\n            pygame.draw.rect(self.window, (0, 0, 0), (game_text_x, game_text_y, game_text.get_width(), game_text.get_height()))\n            self.window.blit(game_text, (game_text_x, game_text_y))\n        ...
    \n

    For completeness' sake, let's also change the move method so that the player can no longer move when they have solved the game:

    \n
        def move(self, move_y, move_x):\n        if self.game_solved():\n            return\n        ...
    \n

    The player can still see the game grid and the final state of the game, however.

    \n

    A hint for testing

    \n

    When developing games it often happens that you'd want to check what happens in some later situation in the game. For example, in this game the moment where the game is solved is one such situation.

    \n

    It can be difficult to test the correct functioning of a situation like that, as you'd normally ahve to solve the game to reach that point in the game. As programmers we can make some temporary alleviations in our games, to make it easier to test them. For example, we could add the following to make it temporarily easier to solve the game:

    \n
        def game_solved(self):\n        return True
    \n

    Now the method always returns True, which means that the game is \"solved\" to begin with. This makes it easy to check that the noification at the end looks good and the player can no longer move on the grid after solving. When this functionality is thoroughly tested, we can revoke the changes.

    \n

    Your game on GitHub?

    \n

    The game is now finished. If you want an easy way to play around with the code and images, you can retrieve the source code from GitHub:

    \n\n

    GitHub is a popular place for many kinds of programming projects. It can be used to store the source code and other materials of all your own programming projects as well, and your program will then be maintained through git version control, and it can be easily shared with others. You will become very familiar with git and GitHub if you continue on to other mooc.fi programming courses.

    \n

    How many moves are required?

    \n

    The grid in this game is quite small, but the game is not all that easy. The first challenge is simply passing the game, but the next stage is trying to do so with as few moves as possible. How short is the shortest path to a solution?

    \n

    Looking for the shortest possible solution is not an easy task at all, but there are computational solutions to this as well. They are one of the subjects of the Data Structures and Algorithms course.

    ","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-14/3-finishing-the-game.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-14/4-your-own-game/page-data.json b/page-data/part-14/4-your-own-game/page-data.json index 32d79f0dc..708e847d4 100644 --- a/page-data/part-14/4-your-own-game/page-data.json +++ b/page-data/part-14/4-your-own-game/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-14/4-your-own-game", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"h2","properties":{"id":"the-last-programming-exercise","style":"position:relative;"},"children":[{"type":"text","value":"The last programming exercise"},{"type":"element","tagName":"a","properties":{"href":"#the-last-programming-exercise","ariaLabel":"the last programming exercise 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The final programming exercise on this course is creating a small game of your own with pygame. The exercise template contains some images for you to use. Please don't use any other images in your game, or others will not be able to test your game based on the source code alone."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The subject of the game is up to you, but it should be about as complicated as the Sokoban example in this part of the material. The game should contain the following features:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The game has a sprite the player can move in some way"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The game has some collectable items and/or enemies"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The player needs to be set a clear task in the game"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The game contains a counter which tells the player how they are doing in the game"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The source code for the game is divided into functions like in the Sokoban example"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the game is finished, submit it for peer revies with the following form. After this, familiarize yourself with the games of two of your coursemates, give the games a grade you deem appropriate,a dn write a short review of the game. Please take into account both how interesting and playable the game was, and how readable the program code is, when grading and reviewing the games."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"submitting-the-game","style":"position:relative;"},"children":[{"type":"text","value":"Submitting the game"},{"type":"element","tagName":"a","properties":{"href":"#submitting-the-game","ariaLabel":"submitting the game 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You should submit the source code of your game through the TMC Paste feature, and include the link you receive in the form below."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With your solution open in the editor, click the TMC menu button in Visual Studio Code (next to the eye symbol). This opens a menu where you can look for the TMC Paste feature by typing in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"send"}]},{"type":"text","value":":"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 626px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/6bf3f5cce371044c253dc34c755ee566/af590/tmc_paste_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: 10.869565217391305%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAACCAIAAADXZGvcAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAWElEQVQI12PQsfGwDYy38o208Aw1tPcwsHM3tPcwtPfQtXbVwY307b2U1bUZhHXs+c1D+I19hY09lUwc5Y0c5IzslUwdhTUt+NXMBNSxIEF1M1lDeylFNQBUlRfSchJdXQAAAABJRU5ErkJggg=='); 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":["/static/6bf3f5cce371044c253dc34c755ee566/a0b58/tmc_paste_1.webp 230w","/static/6bf3f5cce371044c253dc34c755ee566/bc10c/tmc_paste_1.webp 460w","/static/6bf3f5cce371044c253dc34c755ee566/63b03/tmc_paste_1.webp 626w"],"sizes":"(max-width: 626px) 100vw, 626px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6bf3f5cce371044c253dc34c755ee566/81c8e/tmc_paste_1.png 230w","/static/6bf3f5cce371044c253dc34c755ee566/08a84/tmc_paste_1.png 460w","/static/6bf3f5cce371044c253dc34c755ee566/af590/tmc_paste_1.png 626w"],"sizes":"(max-width: 626px) 100vw, 626px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/6bf3f5cce371044c253dc34c755ee566/af590/tmc_paste_1.png","alt":"tmc paste 1","title":"tmc paste 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Select the option "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Send Exercise to TMC Paste"}]},{"type":"text","value":", and there should be a notification in the bottom right hand corner of the window telling you that the source code has been sent to the TMC server:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 463px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/8513d666220a4b7cb4b0fe987d121287/71ce0/tmc_paste_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: 26.521739130434778%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAIAAADKYVtkAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAA40lEQVQY02XKW3KEIBQEUPeRivLwKggCFy4wTh46TrKG/GQXs4asJFloasxnuk519Uc3nQ7KZevROJwOxidQpuXQyaGTYyugFcMBWK8Y6NGiDcTU3HAdqb68v+2X7bK9btu6Xvf96bxEjBHRe59SSjGlRJnIGtML7t1cIvbaNm0/aqVjooDR++BcQIz3Zy6ZckqZ8rFLrfXkneMTMkPdFDuNDUzhVAgxLsu5llJzrqVmIgAQQkgppRBS/EUKxvjnl7392Nv3w/rRtDCBsr0yoCzo+eg7Mej/eK9YeOb5yml/NPkXc5s2qFqAD9IAAAAASUVORK5CYII='); 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":["/static/8513d666220a4b7cb4b0fe987d121287/a0b58/tmc_paste_2.webp 230w","/static/8513d666220a4b7cb4b0fe987d121287/bc10c/tmc_paste_2.webp 460w","/static/8513d666220a4b7cb4b0fe987d121287/a7d21/tmc_paste_2.webp 463w"],"sizes":"(max-width: 463px) 100vw, 463px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8513d666220a4b7cb4b0fe987d121287/81c8e/tmc_paste_2.png 230w","/static/8513d666220a4b7cb4b0fe987d121287/08a84/tmc_paste_2.png 460w","/static/8513d666220a4b7cb4b0fe987d121287/71ce0/tmc_paste_2.png 463w"],"sizes":"(max-width: 463px) 100vw, 463px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/8513d666220a4b7cb4b0fe987d121287/71ce0/tmc_paste_2.png","alt":"tmc paste 2","title":"tmc paste 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can copy the link by clicking on the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Open URL"}]},{"type":"text","value":" button in the notification. A popup should appear, and it should contain the option to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Copy"}]},{"type":"text","value":":"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 463px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/9de4d62e51a1cf0025cc5500310d1edf/71ce0/tmc_paste_3.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: 31.304347826086953%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABHUlEQVQY002Ly07CQBhG+8A+lQuXunGLISyUpAO4oUiAkrbQmfnn2s6FztRSA4nRLydfchYnqfKdrMsiP56KgjE2fMcYuj6GGG//j7v2IYbOex9jnEwmyWwLT0vpXFBtZ1zUJjY2qjYY17e2b13f2Nje6LUJXHXG9dfrMI5jmqbJ4/z88HLyPhDhhL5Q6YV2VFiuPIaWcINpQ7ipaUOFxdy1NgzDb+ycG8fRaCk5tEoqKSRnQIngjAGVQmgpgBJSny8XP973FzPGStBZXu8qWhDJOQcApZSUEgA4F0prxhgAUABCCMaYEGKtnU6nCRD8ecDPb+nrbJluq2N+QAiVZVmVxQItsmyDMc7W6/n7x2q5WiCEUoTSdLP52u/3P8PDR9k+xN7gAAAAAElFTkSuQmCC'); 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":["/static/9de4d62e51a1cf0025cc5500310d1edf/a0b58/tmc_paste_3.webp 230w","/static/9de4d62e51a1cf0025cc5500310d1edf/bc10c/tmc_paste_3.webp 460w","/static/9de4d62e51a1cf0025cc5500310d1edf/a7d21/tmc_paste_3.webp 463w"],"sizes":"(max-width: 463px) 100vw, 463px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9de4d62e51a1cf0025cc5500310d1edf/81c8e/tmc_paste_3.png 230w","/static/9de4d62e51a1cf0025cc5500310d1edf/08a84/tmc_paste_3.png 460w","/static/9de4d62e51a1cf0025cc5500310d1edf/71ce0/tmc_paste_3.png 463w"],"sizes":"(max-width: 463px) 100vw, 463px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/9de4d62e51a1cf0025cc5500310d1edf/71ce0/tmc_paste_3.png","alt":"tmc paste 3","title":"tmc paste 3","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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is the link your should paste in the form below."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"how-part-14-is-graded","style":"position:relative;"},"children":[{"type":"text","value":"How part 14 is graded"},{"type":"element","tagName":"a","properties":{"href":"#how-part-14-is-graded","ariaLabel":"how part 14 is graded 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The goal of this part is to build a small, working game. As this part contains only a single task, "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"you get points for effort"}]},{"type":"text","value":". Please submit your game even if you can't get it to work quite the way you wanted to."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If your game doesn't work the way you intended, please include comments in your code about how it "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"should"}]},{"type":"text","value":" work, or any features you think are missing but did not manage to implement."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If a game you are reviewing is incomplete or doesn't work quite right, please include comments about what you think could have been done to make it work, if at all possible."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"some-game-ideas","style":"position:relative;"},"children":[{"type":"text","value":"Some game ideas"},{"type":"element","tagName":"a","properties":{"href":"#some-game-ideas","ariaLabel":"some game ideas 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is completely up to you what your game will be about, but here are some ideas to help you get started. Remember the material in part 13; those examples can help you work on your ideas, too."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"a-collecting-game","style":"position:relative;"},"children":[{"type":"text","value":"A collecting game"},{"type":"element","tagName":"a","properties":{"href":"#a-collecting-game","ariaLabel":"a collecting game 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":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The player moves the robot with the arrow keys."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"A coin appears in a random location on the screen. When the robot reaches it, the coin moves to a new location."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"There are also monsters on the screen, and the robot must avoid them."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"a-rain-of-coins","style":"position:relative;"},"children":[{"type":"text","value":"A rain of coins"},{"type":"element","tagName":"a","properties":{"href":"#a-rain-of-coins","ariaLabel":"a rain of coins 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":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The player moves the robot to the left and right along the bottom of the screen."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Coins rain from the sky. The robot must collect these."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Also monsters rain from the sky. The robot must avoid these."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"peer-review","style":"position:relative;"},"children":[{"type":"text","value":"Peer review"},{"type":"element","tagName":"a","properties":{"href":"#peer-review","ariaLabel":"peer review 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You should assess the game based on the following criteria:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"What does the game look like?"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Is the game "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"playable"}]},{"type":"text","value":"? Is it fun to play and easy to use?"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Is the game idea interesting?"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"How well is it programmed? Is the code readable, and does it make appropriate use of classes and functions?"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A good review usually points out both good features and some suggestions for improvements."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050"},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"questionnaires-to-finish-off","style":"position:relative;"},"children":[{"type":"text","value":"Questionnaires to finish off"},{"type":"element","tagName":"a","properties":{"href":"#questionnaires-to-finish-off","ariaLabel":"questionnaires to finish off 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First, please respond to a quick questionnaire on this part of the course."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7"},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please also respond to the course feedback questionnaire. The questionnaire results help us improve the course."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"dc0405b3-392a-5811-9138-709848a26667"},"children":[]}]}],"data":{"quirksMode":false}},"html":"

    The last programming exercise

    \n

    The final programming exercise on this course is creating a small game of your own with pygame. The exercise template contains some images for you to use. Please don't use any other images in your game, or others will not be able to test your game based on the source code alone.

    \n

    The subject of the game is up to you, but it should be about as complicated as the Sokoban example in this part of the material. The game should contain the following features:

    \n
      \n
    • The game has a sprite the player can move in some way
    • \n
    • The game has some collectable items and/or enemies
    • \n
    • The player needs to be set a clear task in the game
    • \n
    • The game contains a counter which tells the player how they are doing in the game
    • \n
    • The source code for the game is divided into functions like in the Sokoban example
    • \n
    \n

    When the game is finished, submit it for peer revies with the following form. After this, familiarize yourself with the games of two of your coursemates, give the games a grade you deem appropriate,a dn write a short review of the game. Please take into account both how interesting and playable the game was, and how readable the program code is, when grading and reviewing the games.

    \n

    Submitting the game

    \n

    You should submit the source code of your game through the TMC Paste feature, and include the link you receive in the form below.

    \n

    With your solution open in the editor, click the TMC menu button in Visual Studio Code (next to the eye symbol). This opens a menu where you can look for the TMC Paste feature by typing in send:

    \n\n \n \n \n \n \n \"tmc\n \n \n \n

    Select the option Send Exercise to TMC Paste, and there should be a notification in the bottom right hand corner of the window telling you that the source code has been sent to the TMC server:

    \n\n \n \n \n \n \n \"tmc\n \n \n \n

    This is the link your should paste in the form below.

    \n

    How part 14 is graded

    \n

    The goal of this part is to build a small, working game. As this part contains only a single task, you get points for effort. Please submit your game even if you can't get it to work quite the way you wanted to.

    \n

    If your game doesn't work the way you intended, please include comments in your code about how it should work, or any features you think are missing but did not manage to implement.

    \n

    If a game you are reviewing is incomplete or doesn't work quite right, please include comments about what you think could have been done to make it work, if at all possible.

    \n

    Some game ideas

    \n

    It is completely up to you what your game will be about, but here are some ideas to help you get started. Remember the material in part 13; those examples can help you work on your ideas, too.

    \n

    A collecting game

    \n
      \n
    • The player moves the robot with the arrow keys.
    • \n
    • A coin appears in a random location on the screen. When the robot reaches it, the coin moves to a new location.
    • \n
    • There are also monsters on the screen, and the robot must avoid them.
    • \n
    \n

    A rain of coins

    \n
      \n
    • The player moves the robot to the left and right along the bottom of the screen.
    • \n
    • Coins rain from the sky. The robot must collect these.
    • \n
    • Also monsters rain from the sky. The robot must avoid these.
    • \n
    \n

    Peer review

    \n

    You should assess the game based on the following criteria:

    \n
      \n
    • What does the game look like?
    • \n
    • Is the game playable? Is it fun to play and easy to use?
    • \n
    • Is the game idea interesting?
    • \n
    • How well is it programmed? Is the code readable, and does it make appropriate use of classes and functions?
    • \n
    \n

    A good review usually points out both good features and some suggestions for improvements.

    \n
    \n

    Questionnaires to finish off

    \n

    First, please respond to a quick questionnaire on this part of the course.

    \n
    \n

    Please also respond to the course feedback questionnaire. The questionnaire results help us improve the course.

    \n
    ","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-14/4-your-own-game.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"h2","properties":{"id":"the-last-programming-exercise","style":"position:relative;"},"children":[{"type":"text","value":"The last programming exercise"},{"type":"element","tagName":"a","properties":{"href":"#the-last-programming-exercise","ariaLabel":"the last programming exercise 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The final programming exercise on this course is creating a small game of your own with pygame. The exercise template contains some images for you to use. Please don't use any other images in your game, or others will not be able to test your game based on the source code alone."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The subject of the game is up to you, but it should be about as complicated as the Sokoban example in this part of the material. The game should contain the following features:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The game has a sprite the player can move in some way"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The game has some collectable items and/or enemies"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The player needs to be set a clear task in the game"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The game contains a counter which tells the player how they are doing in the game"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The source code for the game is divided into functions like in the Sokoban example"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the game is finished, submit it for peer revies with the following form. After this, familiarize yourself with the games of two of your coursemates, give the games a grade you deem appropriate,a dn write a short review of the game. Please take into account both how interesting and playable the game was, and how readable the program code is, when grading and reviewing the games."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"submitting-the-game","style":"position:relative;"},"children":[{"type":"text","value":"Submitting the game"},{"type":"element","tagName":"a","properties":{"href":"#submitting-the-game","ariaLabel":"submitting the game 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You should submit the source code of your game through the TMC Paste feature, and include the link you receive in the form below."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With your solution open in the editor, click the TMC menu button in Visual Studio Code (next to the eye symbol). This opens a menu where you can look for the TMC Paste feature by typing in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"send"}]},{"type":"text","value":":"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 626px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/6bf3f5cce371044c253dc34c755ee566/af590/tmc_paste_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: 10.869565217391305%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAACCAIAAADXZGvcAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAWElEQVQI12PQsfGwDYy38o208Aw1tPcwsHM3tPcwtPfQtXbVwY307b2U1bUZhHXs+c1D+I19hY09lUwc5Y0c5IzslUwdhTUt+NXMBNSxIEF1M1lDeylFNQBUlRfSchJdXQAAAABJRU5ErkJggg=='); 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":["/static/6bf3f5cce371044c253dc34c755ee566/a0b58/tmc_paste_1.webp 230w","/static/6bf3f5cce371044c253dc34c755ee566/bc10c/tmc_paste_1.webp 460w","/static/6bf3f5cce371044c253dc34c755ee566/63b03/tmc_paste_1.webp 626w"],"sizes":"(max-width: 626px) 100vw, 626px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6bf3f5cce371044c253dc34c755ee566/81c8e/tmc_paste_1.png 230w","/static/6bf3f5cce371044c253dc34c755ee566/08a84/tmc_paste_1.png 460w","/static/6bf3f5cce371044c253dc34c755ee566/af590/tmc_paste_1.png 626w"],"sizes":"(max-width: 626px) 100vw, 626px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/6bf3f5cce371044c253dc34c755ee566/af590/tmc_paste_1.png","alt":"tmc paste 1","title":"tmc paste 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Select the option "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Send Exercise to TMC Paste"}]},{"type":"text","value":", and there should be a notification in the bottom right hand corner of the window telling you that the source code has been sent to the TMC server:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 463px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/8513d666220a4b7cb4b0fe987d121287/71ce0/tmc_paste_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: 26.521739130434778%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAIAAADKYVtkAAAACXBIWXMAAA7DAAAOwwHHb6hkAAAA40lEQVQY02XKW3KEIBQEUPeRivLwKggCFy4wTh46TrKG/GQXs4asJFloasxnuk519Uc3nQ7KZevROJwOxidQpuXQyaGTYyugFcMBWK8Y6NGiDcTU3HAdqb68v+2X7bK9btu6Xvf96bxEjBHRe59SSjGlRJnIGtML7t1cIvbaNm0/aqVjooDR++BcQIz3Zy6ZckqZ8rFLrfXkneMTMkPdFDuNDUzhVAgxLsu5llJzrqVmIgAQQkgppRBS/EUKxvjnl7392Nv3w/rRtDCBsr0yoCzo+eg7Mej/eK9YeOb5yml/NPkXc5s2qFqAD9IAAAAASUVORK5CYII='); 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":["/static/8513d666220a4b7cb4b0fe987d121287/a0b58/tmc_paste_2.webp 230w","/static/8513d666220a4b7cb4b0fe987d121287/bc10c/tmc_paste_2.webp 460w","/static/8513d666220a4b7cb4b0fe987d121287/a7d21/tmc_paste_2.webp 463w"],"sizes":"(max-width: 463px) 100vw, 463px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8513d666220a4b7cb4b0fe987d121287/81c8e/tmc_paste_2.png 230w","/static/8513d666220a4b7cb4b0fe987d121287/08a84/tmc_paste_2.png 460w","/static/8513d666220a4b7cb4b0fe987d121287/71ce0/tmc_paste_2.png 463w"],"sizes":"(max-width: 463px) 100vw, 463px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/8513d666220a4b7cb4b0fe987d121287/71ce0/tmc_paste_2.png","alt":"tmc paste 2","title":"tmc paste 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can copy the link by clicking on the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Open URL"}]},{"type":"text","value":" button in the notification. A popup should appear, and it should contain the option to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Copy"}]},{"type":"text","value":":"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 463px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/9de4d62e51a1cf0025cc5500310d1edf/71ce0/tmc_paste_3.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: 31.304347826086953%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAAA7DAAAOwwHHb6hkAAABHUlEQVQY002Ly07CQBhG+8A+lQuXunGLISyUpAO4oUiAkrbQmfnn2s6FztRSA4nRLydfchYnqfKdrMsiP56KgjE2fMcYuj6GGG//j7v2IYbOex9jnEwmyWwLT0vpXFBtZ1zUJjY2qjYY17e2b13f2Nje6LUJXHXG9dfrMI5jmqbJ4/z88HLyPhDhhL5Q6YV2VFiuPIaWcINpQ7ipaUOFxdy1NgzDb+ycG8fRaCk5tEoqKSRnQIngjAGVQmgpgBJSny8XP973FzPGStBZXu8qWhDJOQcApZSUEgA4F0prxhgAUABCCMaYEGKtnU6nCRD8ecDPb+nrbJluq2N+QAiVZVmVxQItsmyDMc7W6/n7x2q5WiCEUoTSdLP52u/3P8PDR9k+xN7gAAAAAElFTkSuQmCC'); 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":["/static/9de4d62e51a1cf0025cc5500310d1edf/a0b58/tmc_paste_3.webp 230w","/static/9de4d62e51a1cf0025cc5500310d1edf/bc10c/tmc_paste_3.webp 460w","/static/9de4d62e51a1cf0025cc5500310d1edf/a7d21/tmc_paste_3.webp 463w"],"sizes":"(max-width: 463px) 100vw, 463px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9de4d62e51a1cf0025cc5500310d1edf/81c8e/tmc_paste_3.png 230w","/static/9de4d62e51a1cf0025cc5500310d1edf/08a84/tmc_paste_3.png 460w","/static/9de4d62e51a1cf0025cc5500310d1edf/71ce0/tmc_paste_3.png 463w"],"sizes":"(max-width: 463px) 100vw, 463px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/9de4d62e51a1cf0025cc5500310d1edf/71ce0/tmc_paste_3.png","alt":"tmc paste 3","title":"tmc paste 3","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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is the link your should paste in the form below."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"how-part-14-is-graded","style":"position:relative;"},"children":[{"type":"text","value":"How part 14 is graded"},{"type":"element","tagName":"a","properties":{"href":"#how-part-14-is-graded","ariaLabel":"how part 14 is graded 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The goal of this part is to build a small, working game. As this part contains only a single task, "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"you get points for effort"}]},{"type":"text","value":". Please submit your game even if you can't get it to work quite the way you wanted to."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If your game doesn't work the way you intended, please include comments in your code about how it "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"should"}]},{"type":"text","value":" work, or any features you think are missing but did not manage to implement."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If a game you are reviewing is incomplete or doesn't work quite right, please include comments about what you think could have been done to make it work, if at all possible."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"some-game-ideas","style":"position:relative;"},"children":[{"type":"text","value":"Some game ideas"},{"type":"element","tagName":"a","properties":{"href":"#some-game-ideas","ariaLabel":"some game ideas 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is completely up to you what your game will be about, but here are some ideas to help you get started. Remember the material in part 13; those examples can help you work on your ideas, too."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"a-collecting-game","style":"position:relative;"},"children":[{"type":"text","value":"A collecting game"},{"type":"element","tagName":"a","properties":{"href":"#a-collecting-game","ariaLabel":"a collecting game 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":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The player moves the robot with the arrow keys."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"A coin appears in a random location on the screen. When the robot reaches it, the coin moves to a new location."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"There are also monsters on the screen, and the robot must avoid them."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h3","properties":{"id":"a-rain-of-coins","style":"position:relative;"},"children":[{"type":"text","value":"A rain of coins"},{"type":"element","tagName":"a","properties":{"href":"#a-rain-of-coins","ariaLabel":"a rain of coins 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":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The player moves the robot to the left and right along the bottom of the screen."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Coins rain from the sky. The robot must collect these."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Also monsters rain from the sky. The robot must avoid these."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"peer-review","style":"position:relative;"},"children":[{"type":"text","value":"Peer review"},{"type":"element","tagName":"a","properties":{"href":"#peer-review","ariaLabel":"peer review 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You should assess the game based on the following criteria:"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"What does the game look like?"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Is the game "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"playable"}]},{"type":"text","value":"? Is it fun to play and easy to use?"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Is the game idea interesting?"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"How well is it programmed? Is the code readable, and does it make appropriate use of classes and functions?"}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A good review usually points out both good features and some suggestions for improvements."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050"},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"h2","properties":{"id":"questionnaires-to-finish-off","style":"position:relative;"},"children":[{"type":"text","value":"Questionnaires to finish off"},{"type":"element","tagName":"a","properties":{"href":"#questionnaires-to-finish-off","ariaLabel":"questionnaires to finish off 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":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First, please respond to a quick questionnaire on this part of the course."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7"},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please also respond to the course feedback questionnaire. The questionnaire results help us improve the course."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"dc0405b3-392a-5811-9138-709848a26667"},"children":[]}]}],"data":{"quirksMode":false}},"html":"

    The last programming exercise

    \n

    The final programming exercise on this course is creating a small game of your own with pygame. The exercise template contains some images for you to use. Please don't use any other images in your game, or others will not be able to test your game based on the source code alone.

    \n

    The subject of the game is up to you, but it should be about as complicated as the Sokoban example in this part of the material. The game should contain the following features:

    \n
      \n
    • The game has a sprite the player can move in some way
    • \n
    • The game has some collectable items and/or enemies
    • \n
    • The player needs to be set a clear task in the game
    • \n
    • The game contains a counter which tells the player how they are doing in the game
    • \n
    • The source code for the game is divided into functions like in the Sokoban example
    • \n
    \n

    When the game is finished, submit it for peer revies with the following form. After this, familiarize yourself with the games of two of your coursemates, give the games a grade you deem appropriate,a dn write a short review of the game. Please take into account both how interesting and playable the game was, and how readable the program code is, when grading and reviewing the games.

    \n

    Submitting the game

    \n

    You should submit the source code of your game through the TMC Paste feature, and include the link you receive in the form below.

    \n

    With your solution open in the editor, click the TMC menu button in Visual Studio Code (next to the eye symbol). This opens a menu where you can look for the TMC Paste feature by typing in send:

    \n\n \n \n \n \n \n \"tmc\n \n \n \n

    Select the option Send Exercise to TMC Paste, and there should be a notification in the bottom right hand corner of the window telling you that the source code has been sent to the TMC server:

    \n\n \n \n \n \n \n \"tmc\n \n \n \n

    This is the link your should paste in the form below.

    \n

    How part 14 is graded

    \n

    The goal of this part is to build a small, working game. As this part contains only a single task, you get points for effort. Please submit your game even if you can't get it to work quite the way you wanted to.

    \n

    If your game doesn't work the way you intended, please include comments in your code about how it should work, or any features you think are missing but did not manage to implement.

    \n

    If a game you are reviewing is incomplete or doesn't work quite right, please include comments about what you think could have been done to make it work, if at all possible.

    \n

    Some game ideas

    \n

    It is completely up to you what your game will be about, but here are some ideas to help you get started. Remember the material in part 13; those examples can help you work on your ideas, too.

    \n

    A collecting game

    \n
      \n
    • The player moves the robot with the arrow keys.
    • \n
    • A coin appears in a random location on the screen. When the robot reaches it, the coin moves to a new location.
    • \n
    • There are also monsters on the screen, and the robot must avoid them.
    • \n
    \n

    A rain of coins

    \n
      \n
    • The player moves the robot to the left and right along the bottom of the screen.
    • \n
    • Coins rain from the sky. The robot must collect these.
    • \n
    • Also monsters rain from the sky. The robot must avoid these.
    • \n
    \n

    Peer review

    \n

    You should assess the game based on the following criteria:

    \n
      \n
    • What does the game look like?
    • \n
    • Is the game playable? Is it fun to play and easy to use?
    • \n
    • Is the game idea interesting?
    • \n
    • How well is it programmed? Is the code readable, and does it make appropriate use of classes and functions?
    • \n
    \n

    A good review usually points out both good features and some suggestions for improvements.

    \n
    \n

    Questionnaires to finish off

    \n

    First, please respond to a quick questionnaire on this part of the course.

    \n
    \n

    Please also respond to the course feedback questionnaire. The questionnaire results help us improve the course.

    \n
    ","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-14/4-your-own-game.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-14/page-data.json b/page-data/part-14/page-data.json index 8fea5e649..06a50f71d 100644 --- a/page-data/part-14/page-data.json +++ b/page-data/part-14/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/part-14", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-14","title":"Part 14"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-14/index.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-14","title":"Part 14"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-14/index.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-2/1-programming-terminology/page-data.json b/page-data/part-2/1-programming-terminology/page-data.json index 5ef0d0b59..bad994cc5 100644 --- a/page-data/part-2/1-programming-terminology/page-data.json +++ b/page-data/part-2/1-programming-terminology/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-2/1-programming-terminology", - "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 some essential terminology in programming"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know the difference between a statement and an expression"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to find out the data type of an evaluated expression"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will have learnt to use debugging methods to find mistakes in your code"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the first part of this course we didn't pay much attention to terminology, so let's have a look at some central concepts in programming."}]},{"type":"element","tagName":"h2","properties":{"id":"statement","style":"position:relative;"},"children":[{"type":"text","value":"Statement"},{"type":"element","tagName":"a","properties":{"href":"#statement","ariaLabel":"statement 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":"A "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"statement"}]},{"type":"text","value":" is a part of the program which executes something. It often, but not always, refers to a single command."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print(\"Hi!\")"}]},{"type":"text","value":" is a statement which prints out a line of text. Likewise, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number = 2"}]},{"type":"text","value":" is a statement which assigns a value to a variable."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A statement can also be more complicated. It can, for instance, contain other statements. The following statement spans three lines:"}]},{"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":"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":"\"Anna\""}]},{"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":"\"Hi!\""}]},{"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","number"]},"children":[{"type":"text","value":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the above case there are two statements (a print statement and an assignment statement) within a conditional statement."}]},{"type":"element","tagName":"h2","properties":{"id":"block","style":"position:relative;"},"children":[{"type":"text","value":"Block"},{"type":"element","tagName":"a","properties":{"href":"#block","ariaLabel":"block 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":"A "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"block"}]},{"type":"text","value":" is a group of consecutive statements that are at the same level in the structure of the program. For example, the block of a conditional statement contains those statements which are executed only if the condition is true."}]},{"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":"if"}]},{"type":"text","value":" age "},{"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":"17"}]},{"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":"# beginning of the conditional block"}]},{"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":"\"You are of age!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" age "},{"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":"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":"\"You are now one year older...\""}]},{"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":"# end of the conditional block"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This here belongs to another block\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In Python blocks are expressed by indenting all code in the block by the same amount of whitespace."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the main block of a Python program must always be at the leftmost edge of the file, without indentation:"}]},{"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":"# this program will not work because it is not written at the leftmost egde of the file"}]},{"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":"\"hello world\""}]},{"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":"\"this program is not very good...\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"expression","style":"position:relative;"},"children":[{"type":"text","value":"Expression"},{"type":"element","tagName":"a","properties":{"href":"#expression","ariaLabel":"expression 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":"An "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"expression"}]},{"type":"text","value":" is a bit of code that results in a determined data type. When the program is executed, the expression is evaluated so that it has a value that can then be used in the program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here are a few examples of expressions:"}]},{"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\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":{},"children":[{"type":"text","value":"Expression"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Value"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Type"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Python data type"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 + 4 + 3"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"9"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"integer"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"abc\" + \"de\""}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"abcde\""}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"string"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"11 / 2"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"5.5"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"floating point number"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 * 5 > 9"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Boolean value"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Because all expressions have a type, they can be assigned to 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","comment"]},"children":[{"type":"text","value":"# the variable x is assigned the value of the expression 1 + 2"}]},{"type":"text","value":"\nx "},{"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":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Simple expressions can be assembled together to form more complicated expressions, for example with arithmetic operations:"}]},{"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":"# the variable y is assigned the value of the expression '3 times x plus x squared'"}]},{"type":"text","value":"\ny "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"function","style":"position:relative;"},"children":[{"type":"text","value":"Function"},{"type":"element","tagName":"a","properties":{"href":"#function","ariaLabel":"function 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":"A "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" executes some functionality. Functions can also take one or more "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"arguments"}]},{"type":"text","value":", which are data that can be fed to and processed by the function. Arguments are sometimes also referred to as "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"parameters"}]},{"type":"text","value":". There is a technical distinction between an argument and a parameter, but the words are often used interchangeably. For now it should suffice to remember that both terms refer to the idea of some data passed to the function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A function is executed when it is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"called"}]},{"type":"text","value":". That is, when the function (and its arguments, if any) is mentioned in the code. The following statement calls the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" function with the argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"this is an argument\""}]},{"type":"text","value":":"}]},{"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":"\"this is an argument\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Another function you've already used often is the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" function, which asks the user for input. The argument of this function is the message that is shown to the user:"}]},{"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":"name "},{"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 your name: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this case the function also "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"returns"}]},{"type":"text","value":" a value. After the function has been executed, the section of code where it was called is replaced by the value it returns; it is another expression that has now been evaluated. The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" returns a string value containing whatever the user typed in at the prompt. The value a function returns is often stored in a variable so that it can be used in the program later on."}]},{"type":"element","tagName":"h2","properties":{"id":"data-type","style":"position:relative;"},"children":[{"type":"text","value":"Data type"},{"type":"element","tagName":"a","properties":{"href":"#data-type","ariaLabel":"data type 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":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Data type"}]},{"type":"text","value":" refers to the characteristics of any value present in the program. In the following bit of code the data type of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" is string or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":", and the data type of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"result"}]},{"type":"text","value":" is integer or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":":"}]},{"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":"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":"\"Anna\""}]},{"type":"text","value":"\nresult "},{"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":"100"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can use the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" to find out the data type of any expression. An example of its use:"}]},{"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","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anna\""}]},{"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","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"syntax","style":"position:relative;"},"children":[{"type":"text","value":"Syntax"},{"type":"element","tagName":"a","properties":{"href":"#syntax","ariaLabel":"syntax 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":"Similarly to natural languages, the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"syntax"}]},{"type":"text","value":" of a programming language determines how the code of a program should be written. Each programming language has its own specific syntax."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The syntax of Python specifies, among other things, that the first line of an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statement should end in a colon character, and the block of the statement should be indented:"}]},{"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":"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":"\"Anna\""}]},{"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":"\"Hi!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the syntactic rules of the programming language are not followed, there will be an error:"}]},{"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":"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":"\"Anna\""}]},{"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":"\"Hi!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":" File \"test.py\", line 1\n if name == \"Anna\"\n ^\nSyntaxError: invalid syntax\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"debugging","style":"position:relative;"},"children":[{"type":"text","value":"Debugging"},{"type":"element","tagName":"a","properties":{"href":"#debugging","ariaLabel":"debugging 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":"If the syntax of the program is correct but the program still doesn't function as intended, there is a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"bug"}]},{"type":"text","value":" in the program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bugs manifest in different ways. Some bugs cause an error during execution. For example, the following program"}]},{"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":"x "},{"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":"text","value":"\ny "},{"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":"text","value":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" y\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":"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":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" divided by "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" 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":"result"},{"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":"p","properties":{},"children":[{"type":"text","value":"causes this error:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"ZeroDivisionError: integer division or modulo by zero on line 3\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The problem here is mathematical in nature: division by zero is not allowed, and this halts the execution of the program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Errors during execution are usually rather easy to fix, because the error message states the line of code causing the error. Of course the actual reason for the bug might be somewhere quite different than the line of code causing the error."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sometimes a bug in the program is revealed because the result the code produces is wrong. Discovering and locating this type of bug can be challenging. In the programming exercises on this course the tests are usually intended to reveal bugs of this type. Before a bug can be fixed, its cause must first be located."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programming jargon refers to discovering the causes of bugs as "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"debugging"}]},{"type":"text","value":". It is an extremely important skill in any programmer's toolbox. Professional programmers often spend more time debugging than writing fresh code."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A simple yet effective way of debugging a program is adding debugging print statements to your code. Verifying the results of your code with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" commands gives a quick confirmation the code does what you want it to do."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following is an attempt to solve one of the exercises from the "},{"type":"element","tagName":"a","properties":{"href":"/part-1/5-conditional-statements"},"children":[{"type":"text","value":"previous section"}]},{"type":"text","value":":"}]},{"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":"hourly_wage "},{"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":"float"}]},{"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"]},"children":[{"type":"text","value":"\"Hourly wage: \""}]},{"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":"\nhours "},{"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"]},"children":[{"type":"text","value":"\"Hours worked: \""}]},{"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":"\nday "},{"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":"\"Day of the week: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ndaily_wages "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" hourly_wage "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" hours\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" day "},{"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":"\"sunday\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n daily_wages "},{"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":"2"}]},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Daily wages: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"daily_wages"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euros\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program doesn't work quite right. Executing the tests prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"FAIL: PythonEditorTest: test_sunday_1\n\nWith input 20.0,6,Sunday correct wage 240.0 is not found in output Daily wages: 120.0 euros\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When debugging the exercises on this course, the first step is often checking how the program behaves with the input specified in the test that failed. Indeed the result isn't what was expected:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Daily wages: 120.0 euros"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Debugging usually means running the program multiple times. It can come in handy to temporarily \"hard-code\" the problematic input, instead of asking the user for input each time. In this case hard-coding 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","comment"]},"children":[{"type":"text","value":"# hourly_wage = float(input(\"Hourly wage: \"))"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# hours = int(input(\"Hours worked: \"))"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# day = input(\"Day of the week: \")"}]},{"type":"text","value":"\nhourly_wage "},{"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":"20.0"}]},{"type":"text","value":"\nhours "},{"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":"6"}]},{"type":"text","value":"\nday "},{"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":"\"Sunday\""}]},{"type":"text","value":"\n\ndaily_wages "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" hourly_wage "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" hours\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" day "},{"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":"\"sunday\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n daily_wages "},{"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":"2"}]},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Daily wages: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"daily_wages"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euros\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The next step could be adding "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"debugging print statements"}]},{"type":"text","value":". The problematic part of the code is in the section dealing with Sundays, so let's add "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" commands before and after the line that should double the daily wages on Sundays:"}]},{"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":"# ..."}]},{"type":"text","value":"\n\ndaily_wages "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" hourly_wage "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" hours\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" day "},{"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":"\"sunday\""}]},{"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":"\"wages before:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" daily_wages"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n daily_wages "},{"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":"2"}]},{"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":"\"wages after doubling:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" daily_wages"},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Daily wages: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"daily_wages"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euros\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Running the code now reveals nothing - the debugging print statements aren't printed at all. It seems that the contents of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" block are never executed, so there must be a problem with the conditional statement. Let's try printing out the value of the Boolean expression:"}]},{"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":"# ..."}]},{"type":"text","value":"\n\ndaily_wages "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" hourly_wage "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" hours\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":"\"condition:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" day "},{"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":"\"sunday\""}]},{"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":"if"}]},{"type":"text","value":" day "},{"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":"\"sunday\""}]},{"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":"\"wages before:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" daily_wages"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n daily_wages "},{"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":"2"}]},{"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":"\"wages after doubling:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" daily_wages"},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Daily wages: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"daily_wages"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euros\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Indeed, the value is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":", so the contents of the if block are never executed:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"condition: False\nDaily wages: 120.0 euros"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The issue must then lie within the condition of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statement. As in so many situations in programming, the case of letters matters also in comparisons. Notice how the \"sunday\" in the Boolean expression has not been capitalized, but in the input it was. Let's fix this (in both the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command and the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"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","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\ndaily_wages "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" hourly_wage "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" hours\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":"\"condition:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" day "},{"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":"\"Sunday\""}]},{"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":"if"}]},{"type":"text","value":" day "},{"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":"\"Sunday\""}]},{"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":"\"wages before:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" daily_wages"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n daily_wages "},{"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":"2"}]},{"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":"\"wages after doubling:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" daily_wages"},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Daily wages: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"daily_wages"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euros\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Running this prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"condition: True\nwages before: 120\nwages after doubling: 120\nDaily wages: 120.0 euros"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It seems the value stored in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"daily_wages"}]},{"type":"text","value":" is correct at first: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hourly_wage = 20.0"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hours = 6"}]},{"type":"text","value":", and 20.0 * 6 = 120.0. The command which is supposed to double the figure doesn't do so, however, so there must be a problem with the command. And indeed the command"}]},{"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":"daily_wages "},{"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":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"does double the value, but it doesn't store the new value anywhere. Let's change it so it also stores the new value:"}]},{"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":"daily_wages "},{"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":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Running the program again reveals that the printout at the end is now also correct:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"condition: True\nwages before: 120\nwages after doubling: 240\nDaily wages: 240.0 euros"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the program has been fixed, remember to remove all debugging print statements and other code added for debugging purposes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This example was quite simple, and in such a short program one could probably figure out the bugs just by reading the code carefully. However, using debugging print statements is often a quick way to get a feeling for where the problem might lie. Print statements can be used to figure out which parts of the program seem to work correctly, so bug tracking efforts can be concentrated on the sections of code which are the most likely culprits."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Debugging print statements are only one tool for debugging programs. We will come back to this subject later on during this course. You should now get into the habit of using debugging print statements to look for mistakes in your code. Programming professionals cannot get by without using them, so it is a very useful tool for beginners as well."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fix the syntax","tmcname":"part02-01_fix_syntax","height":"400px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program contains several "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"syntactic errors"}]},{"type":"text","value":". Please fix the program so that the syntax is in order and the program works as specified by the examples below."}]},{"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","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 number: \""}]},{"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":"if"}]},{"type":"text","value":" number"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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 number was greater than one hundred\""}]},{"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","number"]},"children":[{"type":"text","value":"100"}]},{"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":"\"Now its value has decreased by one hundred"},{"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":"\"Its value is now\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number"},{"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","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" must be my lucky number!\""}]},{"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":"\"Have a nice day!"},{"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 number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"13"}]},{"type":"text","value":"\n13 must be my lucky number!\nHave a nice day!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"101"}]},{"type":"text","value":"\nThe number was greater than one hundred\nNow its value has decreased by one hundred\nIts value is now 1\n1 must be my lucky number!\nHave a nice day!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Number of characters","tmcname":"part02-02_number_of_characters"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" can be used to find out the length of a string, among other things. The function returns the number of characters in a string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of how this works:"}]},{"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":"word "},{"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":"\"abcd\""}]},{"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","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"word"},{"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":"print"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hi there\""}]},{"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\nword2 "},{"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":"\"howdydoody\""}]},{"type":"text","value":"\nlength "},{"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":"word2"},{"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":"length"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nempty_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":"text","value":"\nlength "},{"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":"empty_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":"length"},{"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\n8\n10\n0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a word and then prints out the number of characters, if there was more than one typed in."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hey"}]},{"type":"text","value":"\nThere are 3 letters in the word hey\nThank you!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"banana"}]},{"type":"text","value":"\nThere are 6 letters in the word banana\nThank you!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"b"}]},{"type":"text","value":"\nThank you!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Typecasting","tmcname":"part02-03_typecasting"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When programming in Python, often we need to change the data type of a value. For example, a floating point number can be converted into an integer with the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":":"}]},{"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":"\ntemperature "},{"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":"float"}]},{"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"]},"children":[{"type":"text","value":"\"Please type in a temperature: \""}]},{"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":"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 temperature is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" temperature"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"...and rounded down it is\""}]},{"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":"text","value":"temperature"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a temperature: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5.15"}]},{"type":"text","value":"\nThe temperature is 5.15\n...and rounded down it is 5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice the function always rounds down, and not according to the rounding rules in mathematics. This is an example of a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"floor function"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a temperature: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8.99"}]},{"type":"text","value":"\nThe temperature is 8.99\n...and rounded down it is 8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a floating point number and then prints out the integer part and the decimal part separately. Use the Python "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":" function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can assume the number given by the user is always greater than zero."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1.34"}]},{"type":"text","value":"\nInteger part: 1\nDecimal part: 0.34"}]}]}]},{"type":"comment","value":"\n\nA quiz to review the contents of this section:\n\n\n\n\n"}]},"html":"

    After this section

      \n
    • You will be familiar with some essential terminology in programming
    • \n
    • You will know the difference between a statement and an expression
    • \n
    • You will be able to find out the data type of an evaluated expression
    • \n
    • You will have learnt to use debugging methods to find mistakes in your code
    • \n

    In the first part of this course we didn't pay much attention to terminology, so let's have a look at some central concepts in programming.

    Statement

    A statement is a part of the program which executes something. It often, but not always, refers to a single command.

    For example, print(\"Hi!\") is a statement which prints out a line of text. Likewise, number = 2 is a statement which assigns a value to a variable.

    A statement can also be more complicated. It can, for instance, contain other statements. The following statement spans three lines:

    if name == \"Anna\":\n    print(\"Hi!\")\n    number = 2

    In the above case there are two statements (a print statement and an assignment statement) within a conditional statement.

    Block

    A block is a group of consecutive statements that are at the same level in the structure of the program. For example, the block of a conditional statement contains those statements which are executed only if the condition is true.

    if age > 17:\n    # beginning of the conditional block\n    print(\"You are of age!\")\n    age = age + 1\n    print(\"You are now one year older...\")\n    # end of the conditional block\n\nprint(\"This here belongs to another block\")

    In Python blocks are expressed by indenting all code in the block by the same amount of whitespace.

    NB: the main block of a Python program must always be at the leftmost edge of the file, without indentation:

    # this program will not work because it is not written at the leftmost egde of the file\n  print(\"hello world\")\n  print(\"this program is not very good...\")

    Expression

    An expression is a bit of code that results in a determined data type. When the program is executed, the expression is evaluated so that it has a value that can then be used in the program.

    Here are a few examples of expressions:

    \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\n\n
    ExpressionValueTypePython data type
    2 + 4 + 39integerint
    \"abc\" + \"de\"\"abcde\"stringstr
    11 / 25.5floating point numberfloat
    2 * 5 > 9TrueBoolean valuebool

    Because all expressions have a type, they can be assigned to variables:

    # the variable x is assigned the value of the expression 1 + 2\nx = 1 + 2

    Simple expressions can be assembled together to form more complicated expressions, for example with arithmetic operations:

    # the variable y is assigned the value of the expression '3 times x plus x squared'\ny = 3 * x + x**2

    Function

    A function executes some functionality. Functions can also take one or more arguments, which are data that can be fed to and processed by the function. Arguments are sometimes also referred to as parameters. There is a technical distinction between an argument and a parameter, but the words are often used interchangeably. For now it should suffice to remember that both terms refer to the idea of some data passed to the function.

    A function is executed when it is called. That is, when the function (and its arguments, if any) is mentioned in the code. The following statement calls the print function with the argument \"this is an argument\":

    print(\"this is an argument\")

    Another function you've already used often is the input function, which asks the user for input. The argument of this function is the message that is shown to the user:

    name = input(\"Please type in your name: \")

    In this case the function also returns a value. After the function has been executed, the section of code where it was called is replaced by the value it returns; it is another expression that has now been evaluated. The function input returns a string value containing whatever the user typed in at the prompt. The value a function returns is often stored in a variable so that it can be used in the program later on.

    Data type

    Data type refers to the characteristics of any value present in the program. In the following bit of code the data type of the variable name is string or str, and the data type of the variable result is integer or int:

    name = \"Anna\"\nresult = 100

    You can use the function type to find out the data type of any expression. An example of its use:

    print(type(\"Anna\"))\nprint(type(100))

    <class 'str'>\n<class 'int'>

    Syntax

    Similarly to natural languages, the syntax of a programming language determines how the code of a program should be written. Each programming language has its own specific syntax.

    The syntax of Python specifies, among other things, that the first line of an if statement should end in a colon character, and the block of the statement should be indented:

    if name == \"Anna\":\n    print(\"Hi!\")

    If the syntactic rules of the programming language are not followed, there will be an error:

    if name == \"Anna\"\n    print(\"Hi!\")
    \n  File \"test.py\", line 1\n    if name == \"Anna\"\n                    ^\nSyntaxError: invalid syntax\n

    Debugging

    If the syntax of the program is correct but the program still doesn't function as intended, there is a bug in the program.

    Bugs manifest in different ways. Some bugs cause an error during execution. For example, the following program

    x = 10\ny = 0\nresult = x / y\n\nprint(f\"{x} divided by {y} is {result}\")

    causes this error:

    \nZeroDivisionError: integer division or modulo by zero on line 3\n

    The problem here is mathematical in nature: division by zero is not allowed, and this halts the execution of the program.

    Errors during execution are usually rather easy to fix, because the error message states the line of code causing the error. Of course the actual reason for the bug might be somewhere quite different than the line of code causing the error.

    Sometimes a bug in the program is revealed because the result the code produces is wrong. Discovering and locating this type of bug can be challenging. In the programming exercises on this course the tests are usually intended to reveal bugs of this type. Before a bug can be fixed, its cause must first be located.

    Programming jargon refers to discovering the causes of bugs as debugging. It is an extremely important skill in any programmer's toolbox. Professional programmers often spend more time debugging than writing fresh code.

    A simple yet effective way of debugging a program is adding debugging print statements to your code. Verifying the results of your code with print commands gives a quick confirmation the code does what you want it to do.

    The following is an attempt to solve one of the exercises from the previous section:

    hourly_wage = float(input(\"Hourly wage: \"))\nhours = int(input(\"Hours worked: \"))\nday = input(\"Day of the week: \")\n\ndaily_wages = hourly_wage * hours\nif day == \"sunday\":\n    daily_wages * 2\n\nprint(f\"Daily wages: {daily_wages} euros\")

    The program doesn't work quite right. Executing the tests prints out the following:

    \nFAIL: PythonEditorTest: test_sunday_1\n\nWith input 20.0,6,Sunday correct wage 240.0 is not found in output Daily wages: 120.0 euros\n

    When debugging the exercises on this course, the first step is often checking how the program behaves with the input specified in the test that failed. Indeed the result isn't what was expected:

    Daily wages: 120.0 euros

    Debugging usually means running the program multiple times. It can come in handy to temporarily \"hard-code\" the problematic input, instead of asking the user for input each time. In this case hard-coding could look like this:

    # hourly_wage = float(input(\"Hourly wage: \"))\n# hours = int(input(\"Hours worked: \"))\n# day = input(\"Day of the week: \")\nhourly_wage = 20.0\nhours = 6\nday = \"Sunday\"\n\ndaily_wages = hourly_wage * hours\nif day == \"sunday\":\n    daily_wages * 2\n\nprint(f\"Daily wages: {daily_wages} euros\")

    The next step could be adding debugging print statements. The problematic part of the code is in the section dealing with Sundays, so let's add print commands before and after the line that should double the daily wages on Sundays:

    # ...\n\ndaily_wages = hourly_wage * hours\nif day == \"sunday\":\n    print(\"wages before:\", daily_wages)\n    daily_wages * 2\n    print(\"wages after doubling:\", daily_wages)\n\nprint(f\"Daily wages: {daily_wages} euros\")

    Running the code now reveals nothing - the debugging print statements aren't printed at all. It seems that the contents of the if block are never executed, so there must be a problem with the conditional statement. Let's try printing out the value of the Boolean expression:

    # ...\n\ndaily_wages = hourly_wage * hours\nprint(\"condition:\", day == \"sunday\")\nif day == \"sunday\":\n    print(\"wages before:\", daily_wages)\n    daily_wages * 2\n    print(\"wages after doubling:\", daily_wages)\n\nprint(f\"Daily wages: {daily_wages} euros\")

    Indeed, the value is False, so the contents of the if block are never executed:

    condition: False\nDaily wages: 120.0 euros

    The issue must then lie within the condition of the if statement. As in so many situations in programming, the case of letters matters also in comparisons. Notice how the \"sunday\" in the Boolean expression has not been capitalized, but in the input it was. Let's fix this (in both the print command and the if statement):

    # ...\n\ndaily_wages = hourly_wage * hours\nprint(\"condition:\", day == \"Sunday\")\nif day == \"Sunday\":\n    print(\"wages before:\", daily_wages)\n    daily_wages * 2\n    print(\"wages after doubling:\", daily_wages)\n\nprint(f\"Daily wages: {daily_wages} euros\")

    Running this prints out the following:

    condition: True\nwages before: 120\nwages after doubling: 120\nDaily wages: 120.0 euros

    It seems the value stored in daily_wages is correct at first: hourly_wage = 20.0 and hours = 6, and 20.0 * 6 = 120.0. The command which is supposed to double the figure doesn't do so, however, so there must be a problem with the command. And indeed the command

    daily_wages * 2

    does double the value, but it doesn't store the new value anywhere. Let's change it so it also stores the new value:

    daily_wages *= 2

    Running the program again reveals that the printout at the end is now also correct:

    condition: True\nwages before: 120\nwages after doubling: 240\nDaily wages: 240.0 euros

    When the program has been fixed, remember to remove all debugging print statements and other code added for debugging purposes.

    This example was quite simple, and in such a short program one could probably figure out the bugs just by reading the code carefully. However, using debugging print statements is often a quick way to get a feeling for where the problem might lie. Print statements can be used to figure out which parts of the program seem to work correctly, so bug tracking efforts can be concentrated on the sections of code which are the most likely culprits.

    Debugging print statements are only one tool for debugging programs. We will come back to this subject later on during this course. You should now get into the habit of using debugging print statements to look for mistakes in your code. Programming professionals cannot get by without using them, so it is a very useful tool for beginners as well.

    The following program contains several syntactic errors. Please fix the program so that the syntax is in order and the program works as specified by the examples below.

      number = input(\"Please type in a number: \")\n  if number>100\n    print(\"The number was greater than one hundred\")\n    number - 100\n    print(\"Now its value has decreased by one hundred)\n     print(\"Its value is now\"+ number)\n print(number + \" must be my lucky number!\")\n print(\"Have a nice day!)

    Please type in a number: 13\n13 must be my lucky number!\nHave a nice day!

    Please type in a number: 101\nThe number was greater than one hundred\nNow its value has decreased by one hundred\nIts value is now 1\n1 must be my lucky number!\nHave a nice day!

    The function len can be used to find out the length of a string, among other things. The function returns the number of characters in a string.

    Some examples of how this works:

    word = \"abcd\"\nprint(len(word))\n\nprint(len(\"hi there\"))\n\nword2 = \"howdydoody\"\nlength = len(word2)\nprint(length)\n\nempty_string = \"\"\nlength = len(empty_string)\nprint(length)

    4\n8\n10\n0

    Please write a program which asks the user for a word and then prints out the number of characters, if there was more than one typed in.

    Examples of expected behaviour:

    Please type in a word: hey\nThere are 3 letters in the word hey\nThank you!

    Please type in a word: banana\nThere are 6 letters in the word banana\nThank you!

    Please type in a word: b\nThank you!

    When programming in Python, often we need to change the data type of a value. For example, a floating point number can be converted into an integer with the function int:

    \ntemperature = float(input(\"Please type in a temperature: \"))\n\nprint(\"The temperature is\", temperature)\n\nprint(\"...and rounded down it is\", int(temperature))\n

    Please type in a temperature: 5.15\nThe temperature is 5.15\n...and rounded down it is 5

    Notice the function always rounds down, and not according to the rounding rules in mathematics. This is an example of a floor function.

    Please type in a temperature: 8.99\nThe temperature is 8.99\n...and rounded down it is 8

    Please write a program which asks the user for a floating point number and then prints out the integer part and the decimal part separately. Use the Python int function.

    You can assume the number given by the user is always greater than zero.

    An example of expected behaviour:

    Please type in a number: 1.34\nInteger part: 1\nDecimal part: 0.34

    ","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-2/1-programming_terminology.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 some essential terminology in programming"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know the difference between a statement and an expression"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to find out the data type of an evaluated expression"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will have learnt to use debugging methods to find mistakes in your code"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the first part of this course we didn't pay much attention to terminology, so let's have a look at some central concepts in programming."}]},{"type":"element","tagName":"h2","properties":{"id":"statement","style":"position:relative;"},"children":[{"type":"text","value":"Statement"},{"type":"element","tagName":"a","properties":{"href":"#statement","ariaLabel":"statement 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":"A "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"statement"}]},{"type":"text","value":" is a part of the program which executes something. It often, but not always, refers to a single command."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print(\"Hi!\")"}]},{"type":"text","value":" is a statement which prints out a line of text. Likewise, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number = 2"}]},{"type":"text","value":" is a statement which assigns a value to a variable."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A statement can also be more complicated. It can, for instance, contain other statements. The following statement spans three lines:"}]},{"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":"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":"\"Anna\""}]},{"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":"\"Hi!\""}]},{"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","number"]},"children":[{"type":"text","value":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the above case there are two statements (a print statement and an assignment statement) within a conditional statement."}]},{"type":"element","tagName":"h2","properties":{"id":"block","style":"position:relative;"},"children":[{"type":"text","value":"Block"},{"type":"element","tagName":"a","properties":{"href":"#block","ariaLabel":"block 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":"A "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"block"}]},{"type":"text","value":" is a group of consecutive statements that are at the same level in the structure of the program. For example, the block of a conditional statement contains those statements which are executed only if the condition is true."}]},{"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":"if"}]},{"type":"text","value":" age "},{"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":"17"}]},{"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":"# beginning of the conditional block"}]},{"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":"\"You are of age!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" age "},{"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":"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":"\"You are now one year older...\""}]},{"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":"# end of the conditional block"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This here belongs to another block\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In Python blocks are expressed by indenting all code in the block by the same amount of whitespace."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the main block of a Python program must always be at the leftmost edge of the file, without indentation:"}]},{"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":"# this program will not work because it is not written at the leftmost egde of the file"}]},{"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":"\"hello world\""}]},{"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":"\"this program is not very good...\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"expression","style":"position:relative;"},"children":[{"type":"text","value":"Expression"},{"type":"element","tagName":"a","properties":{"href":"#expression","ariaLabel":"expression 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":"An "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"expression"}]},{"type":"text","value":" is a bit of code that results in a determined data type. When the program is executed, the expression is evaluated so that it has a value that can then be used in the program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here are a few examples of expressions:"}]},{"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\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":{},"children":[{"type":"text","value":"Expression"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Value"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Type"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Python data type"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 + 4 + 3"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"9"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"integer"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"abc\" + \"de\""}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"abcde\""}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"string"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"11 / 2"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"5.5"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"floating point number"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"2 * 5 > 9"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"text","value":"Boolean value"}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Because all expressions have a type, they can be assigned to 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","comment"]},"children":[{"type":"text","value":"# the variable x is assigned the value of the expression 1 + 2"}]},{"type":"text","value":"\nx "},{"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":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Simple expressions can be assembled together to form more complicated expressions, for example with arithmetic operations:"}]},{"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":"# the variable y is assigned the value of the expression '3 times x plus x squared'"}]},{"type":"text","value":"\ny "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"function","style":"position:relative;"},"children":[{"type":"text","value":"Function"},{"type":"element","tagName":"a","properties":{"href":"#function","ariaLabel":"function 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":"A "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"function"}]},{"type":"text","value":" executes some functionality. Functions can also take one or more "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"arguments"}]},{"type":"text","value":", which are data that can be fed to and processed by the function. Arguments are sometimes also referred to as "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"parameters"}]},{"type":"text","value":". There is a technical distinction between an argument and a parameter, but the words are often used interchangeably. For now it should suffice to remember that both terms refer to the idea of some data passed to the function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A function is executed when it is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"called"}]},{"type":"text","value":". That is, when the function (and its arguments, if any) is mentioned in the code. The following statement calls the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" function with the argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"this is an argument\""}]},{"type":"text","value":":"}]},{"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":"\"this is an argument\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Another function you've already used often is the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" function, which asks the user for input. The argument of this function is the message that is shown to the user:"}]},{"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":"name "},{"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 your name: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this case the function also "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"returns"}]},{"type":"text","value":" a value. After the function has been executed, the section of code where it was called is replaced by the value it returns; it is another expression that has now been evaluated. The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" returns a string value containing whatever the user typed in at the prompt. The value a function returns is often stored in a variable so that it can be used in the program later on."}]},{"type":"element","tagName":"h2","properties":{"id":"data-type","style":"position:relative;"},"children":[{"type":"text","value":"Data type"},{"type":"element","tagName":"a","properties":{"href":"#data-type","ariaLabel":"data type 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":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Data type"}]},{"type":"text","value":" refers to the characteristics of any value present in the program. In the following bit of code the data type of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" is string or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":", and the data type of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"result"}]},{"type":"text","value":" is integer or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":":"}]},{"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":"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":"\"Anna\""}]},{"type":"text","value":"\nresult "},{"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":"100"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can use the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"type"}]},{"type":"text","value":" to find out the data type of any expression. An example of its use:"}]},{"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","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anna\""}]},{"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","builtin"]},"children":[{"type":"text","value":"type"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"syntax","style":"position:relative;"},"children":[{"type":"text","value":"Syntax"},{"type":"element","tagName":"a","properties":{"href":"#syntax","ariaLabel":"syntax 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":"Similarly to natural languages, the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"syntax"}]},{"type":"text","value":" of a programming language determines how the code of a program should be written. Each programming language has its own specific syntax."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The syntax of Python specifies, among other things, that the first line of an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statement should end in a colon character, and the block of the statement should be indented:"}]},{"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":"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":"\"Anna\""}]},{"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":"\"Hi!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the syntactic rules of the programming language are not followed, there will be an error:"}]},{"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":"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":"\"Anna\""}]},{"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":"\"Hi!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":" File \"test.py\", line 1\n if name == \"Anna\"\n ^\nSyntaxError: invalid syntax\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"debugging","style":"position:relative;"},"children":[{"type":"text","value":"Debugging"},{"type":"element","tagName":"a","properties":{"href":"#debugging","ariaLabel":"debugging 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":"If the syntax of the program is correct but the program still doesn't function as intended, there is a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"bug"}]},{"type":"text","value":" in the program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Bugs manifest in different ways. Some bugs cause an error during execution. For example, the following program"}]},{"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":"x "},{"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":"text","value":"\ny "},{"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":"text","value":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"/"}]},{"type":"text","value":" y\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":"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":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" divided by "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" 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":"result"},{"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":"p","properties":{},"children":[{"type":"text","value":"causes this error:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"ZeroDivisionError: integer division or modulo by zero on line 3\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The problem here is mathematical in nature: division by zero is not allowed, and this halts the execution of the program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Errors during execution are usually rather easy to fix, because the error message states the line of code causing the error. Of course the actual reason for the bug might be somewhere quite different than the line of code causing the error."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sometimes a bug in the program is revealed because the result the code produces is wrong. Discovering and locating this type of bug can be challenging. In the programming exercises on this course the tests are usually intended to reveal bugs of this type. Before a bug can be fixed, its cause must first be located."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Programming jargon refers to discovering the causes of bugs as "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"debugging"}]},{"type":"text","value":". It is an extremely important skill in any programmer's toolbox. Professional programmers often spend more time debugging than writing fresh code."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A simple yet effective way of debugging a program is adding debugging print statements to your code. Verifying the results of your code with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" commands gives a quick confirmation the code does what you want it to do."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following is an attempt to solve one of the exercises from the "},{"type":"element","tagName":"a","properties":{"href":"/part-1/5-conditional-statements"},"children":[{"type":"text","value":"previous section"}]},{"type":"text","value":":"}]},{"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":"hourly_wage "},{"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":"float"}]},{"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"]},"children":[{"type":"text","value":"\"Hourly wage: \""}]},{"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":"\nhours "},{"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"]},"children":[{"type":"text","value":"\"Hours worked: \""}]},{"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":"\nday "},{"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":"\"Day of the week: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ndaily_wages "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" hourly_wage "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" hours\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" day "},{"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":"\"sunday\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n daily_wages "},{"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":"2"}]},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Daily wages: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"daily_wages"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euros\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program doesn't work quite right. Executing the tests prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"FAIL: PythonEditorTest: test_sunday_1\n\nWith input 20.0,6,Sunday correct wage 240.0 is not found in output Daily wages: 120.0 euros\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When debugging the exercises on this course, the first step is often checking how the program behaves with the input specified in the test that failed. Indeed the result isn't what was expected:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Daily wages: 120.0 euros"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Debugging usually means running the program multiple times. It can come in handy to temporarily \"hard-code\" the problematic input, instead of asking the user for input each time. In this case hard-coding 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","comment"]},"children":[{"type":"text","value":"# hourly_wage = float(input(\"Hourly wage: \"))"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# hours = int(input(\"Hours worked: \"))"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# day = input(\"Day of the week: \")"}]},{"type":"text","value":"\nhourly_wage "},{"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":"20.0"}]},{"type":"text","value":"\nhours "},{"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":"6"}]},{"type":"text","value":"\nday "},{"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":"\"Sunday\""}]},{"type":"text","value":"\n\ndaily_wages "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" hourly_wage "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" hours\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" day "},{"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":"\"sunday\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n daily_wages "},{"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":"2"}]},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Daily wages: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"daily_wages"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euros\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The next step could be adding "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"debugging print statements"}]},{"type":"text","value":". The problematic part of the code is in the section dealing with Sundays, so let's add "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" commands before and after the line that should double the daily wages on Sundays:"}]},{"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":"# ..."}]},{"type":"text","value":"\n\ndaily_wages "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" hourly_wage "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" hours\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" day "},{"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":"\"sunday\""}]},{"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":"\"wages before:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" daily_wages"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n daily_wages "},{"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":"2"}]},{"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":"\"wages after doubling:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" daily_wages"},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Daily wages: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"daily_wages"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euros\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Running the code now reveals nothing - the debugging print statements aren't printed at all. It seems that the contents of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" block are never executed, so there must be a problem with the conditional statement. Let's try printing out the value of the Boolean expression:"}]},{"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":"# ..."}]},{"type":"text","value":"\n\ndaily_wages "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" hourly_wage "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" hours\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":"\"condition:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" day "},{"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":"\"sunday\""}]},{"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":"if"}]},{"type":"text","value":" day "},{"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":"\"sunday\""}]},{"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":"\"wages before:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" daily_wages"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n daily_wages "},{"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":"2"}]},{"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":"\"wages after doubling:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" daily_wages"},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Daily wages: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"daily_wages"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euros\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Indeed, the value is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":", so the contents of the if block are never executed:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"condition: False\nDaily wages: 120.0 euros"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The issue must then lie within the condition of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statement. As in so many situations in programming, the case of letters matters also in comparisons. Notice how the \"sunday\" in the Boolean expression has not been capitalized, but in the input it was. Let's fix this (in both the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command and the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"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","comment"]},"children":[{"type":"text","value":"# ..."}]},{"type":"text","value":"\n\ndaily_wages "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" hourly_wage "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" hours\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":"\"condition:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" day "},{"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":"\"Sunday\""}]},{"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":"if"}]},{"type":"text","value":" day "},{"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":"\"Sunday\""}]},{"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":"\"wages before:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" daily_wages"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n daily_wages "},{"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":"2"}]},{"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":"\"wages after doubling:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" daily_wages"},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Daily wages: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"daily_wages"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euros\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Running this prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"condition: True\nwages before: 120\nwages after doubling: 120\nDaily wages: 120.0 euros"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It seems the value stored in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"daily_wages"}]},{"type":"text","value":" is correct at first: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hourly_wage = 20.0"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hours = 6"}]},{"type":"text","value":", and 20.0 * 6 = 120.0. The command which is supposed to double the figure doesn't do so, however, so there must be a problem with the command. And indeed the command"}]},{"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":"daily_wages "},{"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":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"does double the value, but it doesn't store the new value anywhere. Let's change it so it also stores the new value:"}]},{"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":"daily_wages "},{"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":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Running the program again reveals that the printout at the end is now also correct:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"condition: True\nwages before: 120\nwages after doubling: 240\nDaily wages: 240.0 euros"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the program has been fixed, remember to remove all debugging print statements and other code added for debugging purposes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This example was quite simple, and in such a short program one could probably figure out the bugs just by reading the code carefully. However, using debugging print statements is often a quick way to get a feeling for where the problem might lie. Print statements can be used to figure out which parts of the program seem to work correctly, so bug tracking efforts can be concentrated on the sections of code which are the most likely culprits."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Debugging print statements are only one tool for debugging programs. We will come back to this subject later on during this course. You should now get into the habit of using debugging print statements to look for mistakes in your code. Programming professionals cannot get by without using them, so it is a very useful tool for beginners as well."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fix the syntax","tmcname":"part02-01_fix_syntax","height":"400px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program contains several "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"syntactic errors"}]},{"type":"text","value":". Please fix the program so that the syntax is in order and the program works as specified by the examples below."}]},{"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","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 number: \""}]},{"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":"if"}]},{"type":"text","value":" number"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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 number was greater than one hundred\""}]},{"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","number"]},"children":[{"type":"text","value":"100"}]},{"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":"\"Now its value has decreased by one hundred"},{"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":"\"Its value is now\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" number"},{"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","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" must be my lucky number!\""}]},{"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":"\"Have a nice day!"},{"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 number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"13"}]},{"type":"text","value":"\n13 must be my lucky number!\nHave a nice day!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"101"}]},{"type":"text","value":"\nThe number was greater than one hundred\nNow its value has decreased by one hundred\nIts value is now 1\n1 must be my lucky number!\nHave a nice day!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Number of characters","tmcname":"part02-02_number_of_characters"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" can be used to find out the length of a string, among other things. The function returns the number of characters in a string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of how this works:"}]},{"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":"word "},{"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":"\"abcd\""}]},{"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","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"word"},{"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":"print"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"hi there\""}]},{"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\nword2 "},{"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":"\"howdydoody\""}]},{"type":"text","value":"\nlength "},{"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":"word2"},{"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":"length"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nempty_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":"text","value":"\nlength "},{"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":"empty_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":"length"},{"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\n8\n10\n0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a word and then prints out the number of characters, if there was more than one typed in."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hey"}]},{"type":"text","value":"\nThere are 3 letters in the word hey\nThank you!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"banana"}]},{"type":"text","value":"\nThere are 6 letters in the word banana\nThank you!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"b"}]},{"type":"text","value":"\nThank you!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Typecasting","tmcname":"part02-03_typecasting"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When programming in Python, often we need to change the data type of a value. For example, a floating point number can be converted into an integer with the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":":"}]},{"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":"\ntemperature "},{"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":"float"}]},{"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"]},"children":[{"type":"text","value":"\"Please type in a temperature: \""}]},{"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":"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 temperature is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" temperature"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"...and rounded down it is\""}]},{"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":"text","value":"temperature"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a temperature: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5.15"}]},{"type":"text","value":"\nThe temperature is 5.15\n...and rounded down it is 5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice the function always rounds down, and not according to the rounding rules in mathematics. This is an example of a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"floor function"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a temperature: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8.99"}]},{"type":"text","value":"\nThe temperature is 8.99\n...and rounded down it is 8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a floating point number and then prints out the integer part and the decimal part separately. Use the Python "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":" function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can assume the number given by the user is always greater than zero."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1.34"}]},{"type":"text","value":"\nInteger part: 1\nDecimal part: 0.34"}]}]}]},{"type":"comment","value":"\n\nA quiz to review the contents of this section:\n\n\n\n\n"}]},"html":"

    After this section

      \n
    • You will be familiar with some essential terminology in programming
    • \n
    • You will know the difference between a statement and an expression
    • \n
    • You will be able to find out the data type of an evaluated expression
    • \n
    • You will have learnt to use debugging methods to find mistakes in your code
    • \n

    In the first part of this course we didn't pay much attention to terminology, so let's have a look at some central concepts in programming.

    Statement

    A statement is a part of the program which executes something. It often, but not always, refers to a single command.

    For example, print(\"Hi!\") is a statement which prints out a line of text. Likewise, number = 2 is a statement which assigns a value to a variable.

    A statement can also be more complicated. It can, for instance, contain other statements. The following statement spans three lines:

    if name == \"Anna\":\n    print(\"Hi!\")\n    number = 2

    In the above case there are two statements (a print statement and an assignment statement) within a conditional statement.

    Block

    A block is a group of consecutive statements that are at the same level in the structure of the program. For example, the block of a conditional statement contains those statements which are executed only if the condition is true.

    if age > 17:\n    # beginning of the conditional block\n    print(\"You are of age!\")\n    age = age + 1\n    print(\"You are now one year older...\")\n    # end of the conditional block\n\nprint(\"This here belongs to another block\")

    In Python blocks are expressed by indenting all code in the block by the same amount of whitespace.

    NB: the main block of a Python program must always be at the leftmost edge of the file, without indentation:

    # this program will not work because it is not written at the leftmost egde of the file\n  print(\"hello world\")\n  print(\"this program is not very good...\")

    Expression

    An expression is a bit of code that results in a determined data type. When the program is executed, the expression is evaluated so that it has a value that can then be used in the program.

    Here are a few examples of expressions:

    \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\n\n
    ExpressionValueTypePython data type
    2 + 4 + 39integerint
    \"abc\" + \"de\"\"abcde\"stringstr
    11 / 25.5floating point numberfloat
    2 * 5 > 9TrueBoolean valuebool

    Because all expressions have a type, they can be assigned to variables:

    # the variable x is assigned the value of the expression 1 + 2\nx = 1 + 2

    Simple expressions can be assembled together to form more complicated expressions, for example with arithmetic operations:

    # the variable y is assigned the value of the expression '3 times x plus x squared'\ny = 3 * x + x**2

    Function

    A function executes some functionality. Functions can also take one or more arguments, which are data that can be fed to and processed by the function. Arguments are sometimes also referred to as parameters. There is a technical distinction between an argument and a parameter, but the words are often used interchangeably. For now it should suffice to remember that both terms refer to the idea of some data passed to the function.

    A function is executed when it is called. That is, when the function (and its arguments, if any) is mentioned in the code. The following statement calls the print function with the argument \"this is an argument\":

    print(\"this is an argument\")

    Another function you've already used often is the input function, which asks the user for input. The argument of this function is the message that is shown to the user:

    name = input(\"Please type in your name: \")

    In this case the function also returns a value. After the function has been executed, the section of code where it was called is replaced by the value it returns; it is another expression that has now been evaluated. The function input returns a string value containing whatever the user typed in at the prompt. The value a function returns is often stored in a variable so that it can be used in the program later on.

    Data type

    Data type refers to the characteristics of any value present in the program. In the following bit of code the data type of the variable name is string or str, and the data type of the variable result is integer or int:

    name = \"Anna\"\nresult = 100

    You can use the function type to find out the data type of any expression. An example of its use:

    print(type(\"Anna\"))\nprint(type(100))

    <class 'str'>\n<class 'int'>

    Syntax

    Similarly to natural languages, the syntax of a programming language determines how the code of a program should be written. Each programming language has its own specific syntax.

    The syntax of Python specifies, among other things, that the first line of an if statement should end in a colon character, and the block of the statement should be indented:

    if name == \"Anna\":\n    print(\"Hi!\")

    If the syntactic rules of the programming language are not followed, there will be an error:

    if name == \"Anna\"\n    print(\"Hi!\")
    \n  File \"test.py\", line 1\n    if name == \"Anna\"\n                    ^\nSyntaxError: invalid syntax\n

    Debugging

    If the syntax of the program is correct but the program still doesn't function as intended, there is a bug in the program.

    Bugs manifest in different ways. Some bugs cause an error during execution. For example, the following program

    x = 10\ny = 0\nresult = x / y\n\nprint(f\"{x} divided by {y} is {result}\")

    causes this error:

    \nZeroDivisionError: integer division or modulo by zero on line 3\n

    The problem here is mathematical in nature: division by zero is not allowed, and this halts the execution of the program.

    Errors during execution are usually rather easy to fix, because the error message states the line of code causing the error. Of course the actual reason for the bug might be somewhere quite different than the line of code causing the error.

    Sometimes a bug in the program is revealed because the result the code produces is wrong. Discovering and locating this type of bug can be challenging. In the programming exercises on this course the tests are usually intended to reveal bugs of this type. Before a bug can be fixed, its cause must first be located.

    Programming jargon refers to discovering the causes of bugs as debugging. It is an extremely important skill in any programmer's toolbox. Professional programmers often spend more time debugging than writing fresh code.

    A simple yet effective way of debugging a program is adding debugging print statements to your code. Verifying the results of your code with print commands gives a quick confirmation the code does what you want it to do.

    The following is an attempt to solve one of the exercises from the previous section:

    hourly_wage = float(input(\"Hourly wage: \"))\nhours = int(input(\"Hours worked: \"))\nday = input(\"Day of the week: \")\n\ndaily_wages = hourly_wage * hours\nif day == \"sunday\":\n    daily_wages * 2\n\nprint(f\"Daily wages: {daily_wages} euros\")

    The program doesn't work quite right. Executing the tests prints out the following:

    \nFAIL: PythonEditorTest: test_sunday_1\n\nWith input 20.0,6,Sunday correct wage 240.0 is not found in output Daily wages: 120.0 euros\n

    When debugging the exercises on this course, the first step is often checking how the program behaves with the input specified in the test that failed. Indeed the result isn't what was expected:

    Daily wages: 120.0 euros

    Debugging usually means running the program multiple times. It can come in handy to temporarily \"hard-code\" the problematic input, instead of asking the user for input each time. In this case hard-coding could look like this:

    # hourly_wage = float(input(\"Hourly wage: \"))\n# hours = int(input(\"Hours worked: \"))\n# day = input(\"Day of the week: \")\nhourly_wage = 20.0\nhours = 6\nday = \"Sunday\"\n\ndaily_wages = hourly_wage * hours\nif day == \"sunday\":\n    daily_wages * 2\n\nprint(f\"Daily wages: {daily_wages} euros\")

    The next step could be adding debugging print statements. The problematic part of the code is in the section dealing with Sundays, so let's add print commands before and after the line that should double the daily wages on Sundays:

    # ...\n\ndaily_wages = hourly_wage * hours\nif day == \"sunday\":\n    print(\"wages before:\", daily_wages)\n    daily_wages * 2\n    print(\"wages after doubling:\", daily_wages)\n\nprint(f\"Daily wages: {daily_wages} euros\")

    Running the code now reveals nothing - the debugging print statements aren't printed at all. It seems that the contents of the if block are never executed, so there must be a problem with the conditional statement. Let's try printing out the value of the Boolean expression:

    # ...\n\ndaily_wages = hourly_wage * hours\nprint(\"condition:\", day == \"sunday\")\nif day == \"sunday\":\n    print(\"wages before:\", daily_wages)\n    daily_wages * 2\n    print(\"wages after doubling:\", daily_wages)\n\nprint(f\"Daily wages: {daily_wages} euros\")

    Indeed, the value is False, so the contents of the if block are never executed:

    condition: False\nDaily wages: 120.0 euros

    The issue must then lie within the condition of the if statement. As in so many situations in programming, the case of letters matters also in comparisons. Notice how the \"sunday\" in the Boolean expression has not been capitalized, but in the input it was. Let's fix this (in both the print command and the if statement):

    # ...\n\ndaily_wages = hourly_wage * hours\nprint(\"condition:\", day == \"Sunday\")\nif day == \"Sunday\":\n    print(\"wages before:\", daily_wages)\n    daily_wages * 2\n    print(\"wages after doubling:\", daily_wages)\n\nprint(f\"Daily wages: {daily_wages} euros\")

    Running this prints out the following:

    condition: True\nwages before: 120\nwages after doubling: 120\nDaily wages: 120.0 euros

    It seems the value stored in daily_wages is correct at first: hourly_wage = 20.0 and hours = 6, and 20.0 * 6 = 120.0. The command which is supposed to double the figure doesn't do so, however, so there must be a problem with the command. And indeed the command

    daily_wages * 2

    does double the value, but it doesn't store the new value anywhere. Let's change it so it also stores the new value:

    daily_wages *= 2

    Running the program again reveals that the printout at the end is now also correct:

    condition: True\nwages before: 120\nwages after doubling: 240\nDaily wages: 240.0 euros

    When the program has been fixed, remember to remove all debugging print statements and other code added for debugging purposes.

    This example was quite simple, and in such a short program one could probably figure out the bugs just by reading the code carefully. However, using debugging print statements is often a quick way to get a feeling for where the problem might lie. Print statements can be used to figure out which parts of the program seem to work correctly, so bug tracking efforts can be concentrated on the sections of code which are the most likely culprits.

    Debugging print statements are only one tool for debugging programs. We will come back to this subject later on during this course. You should now get into the habit of using debugging print statements to look for mistakes in your code. Programming professionals cannot get by without using them, so it is a very useful tool for beginners as well.

    The following program contains several syntactic errors. Please fix the program so that the syntax is in order and the program works as specified by the examples below.

      number = input(\"Please type in a number: \")\n  if number>100\n    print(\"The number was greater than one hundred\")\n    number - 100\n    print(\"Now its value has decreased by one hundred)\n     print(\"Its value is now\"+ number)\n print(number + \" must be my lucky number!\")\n print(\"Have a nice day!)

    Please type in a number: 13\n13 must be my lucky number!\nHave a nice day!

    Please type in a number: 101\nThe number was greater than one hundred\nNow its value has decreased by one hundred\nIts value is now 1\n1 must be my lucky number!\nHave a nice day!

    The function len can be used to find out the length of a string, among other things. The function returns the number of characters in a string.

    Some examples of how this works:

    word = \"abcd\"\nprint(len(word))\n\nprint(len(\"hi there\"))\n\nword2 = \"howdydoody\"\nlength = len(word2)\nprint(length)\n\nempty_string = \"\"\nlength = len(empty_string)\nprint(length)

    4\n8\n10\n0

    Please write a program which asks the user for a word and then prints out the number of characters, if there was more than one typed in.

    Examples of expected behaviour:

    Please type in a word: hey\nThere are 3 letters in the word hey\nThank you!

    Please type in a word: banana\nThere are 6 letters in the word banana\nThank you!

    Please type in a word: b\nThank you!

    When programming in Python, often we need to change the data type of a value. For example, a floating point number can be converted into an integer with the function int:

    \ntemperature = float(input(\"Please type in a temperature: \"))\n\nprint(\"The temperature is\", temperature)\n\nprint(\"...and rounded down it is\", int(temperature))\n

    Please type in a temperature: 5.15\nThe temperature is 5.15\n...and rounded down it is 5

    Notice the function always rounds down, and not according to the rounding rules in mathematics. This is an example of a floor function.

    Please type in a temperature: 8.99\nThe temperature is 8.99\n...and rounded down it is 8

    Please write a program which asks the user for a floating point number and then prints out the integer part and the decimal part separately. Use the Python int function.

    You can assume the number given by the user is always greater than zero.

    An example of expected behaviour:

    Please type in a number: 1.34\nInteger part: 1\nDecimal part: 0.34

    ","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-2/1-programming_terminology.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-2/2-else-elif/page-data.json b/page-data/part-2/2-else-elif/page-data.json index 19ed1908c..6e354759e 100644 --- a/page-data/part-2/2-else-elif/page-data.json +++ b/page-data/part-2/2-else-elif/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-2/2-else-elif", - "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 know how to create multiple branches within conditional statements"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will understand the purpose of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" statements within a conditional statement"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use the modulo operation "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%"}]},{"type":"text","value":" in Boolean expressions"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at a program which asks the user to input a number, and then prints out different messages based on whether the number is negative, positive, or equal to zero:"}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is negative\""}]},{"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":"if"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is positive or zero\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This looks a bit clumsy and repetitive. We only ever want to execute one of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" blocks, because the input will always be either below zero, or zero or above. That is, either "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number < 0"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number >= 0"}]},{"type":"text","value":" is true, but never both at the same time. So, the first conditional statement actually contains all we need here. If it is true, the number is negative. If it is false, the number equals zero or is positive."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Instead of creating a whole another conditional statement, as in the example above, it is possible to create another branch of the same conditional statement to cover all cases "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"where the condition was false"}]},{"type":"text","value":". This is called the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" statement."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The previous example rewritten:"}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is negative\""}]},{"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":"else"}]},{"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 number is positive or zero\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When using an if-else construction, one and exactly one of the branches will always be executed. The following picture illustrates the structure:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 538px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/c5e369c48eb7d985dab911b91e99c09e/9516f/2_2_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: 129.56521739130434%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAaCAYAAAC3g3x9AAAACXBIWXMAAAsTAAALEwEAmpwYAAACGklEQVRIx5WV56oqQRCE9/3fyj+CGFHBhJgw55ztw9fcXuaOs3pOw7DrhNrqrpo2kn/xer30eblcZDqd/jf3eDxku91Kv9+XdrstnU5H97DXj8gHPJ/PetidA2gwGEipVJJ8Pi/ZbFZ6vZ6O4XAoq9VKJpOJbDabMOB6vY7nbByPR2UGOM/ZbCb3+12ez6eun04nWS6X3wH9AHC3273NX69XPRe5LJIYEjDhUCqVkkajob/92pN65H+JhRDgfr/XeepLrRaLhYrlnlNAXsifjbyTjiuKAR4OBwVh3G43BYexm7ICUuBcLifpdFqKxaLU6/UgQwNETQj4gDFDlMlkMlKpVKRcLqsVSMkXhZTn83mcDT4kZdvDGuejkFouINbgIPNYB99xmHlfFOZVZRQz1SwdghTYRBYME8S1mgHGtvnkQ8xrwQeZZwBOCT7aJgQIO7OGy450OewGyv8K0EpB/cbjsTSbzbhRMKy+MA5ePbeGLqD50zoMXadWq+k8zABj7Q2QdFgk8J3LEKvwEZ720cT25YLa0wU0RamjNQe3D9iIQmBJgAR9MXQ1ExtsEiC1hR3Xk+sKS79TvaXsg1MvCwBR2To6HsUq/hX9CAhDig+bQqEg1WpV/wJ4B9y/fl8BSRd1sUO321V1ASf1PzEMtX8MjN9Go5G2sCRhPjL0LWF9kXp+VfkbU1O71WrFxvd9+yvAkJVchv5N+QG65eiteVQ1KAAAAABJRU5ErkJggg=='); 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":["/static/c5e369c48eb7d985dab911b91e99c09e/a0b58/2_2_1.webp 230w","/static/c5e369c48eb7d985dab911b91e99c09e/bc10c/2_2_1.webp 460w","/static/c5e369c48eb7d985dab911b91e99c09e/b52d2/2_2_1.webp 538w"],"sizes":"(max-width: 538px) 100vw, 538px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/c5e369c48eb7d985dab911b91e99c09e/81c8e/2_2_1.png 230w","/static/c5e369c48eb7d985dab911b91e99c09e/08a84/2_2_1.png 460w","/static/c5e369c48eb7d985dab911b91e99c09e/9516f/2_2_1.png 538w"],"sizes":"(max-width: 538px) 100vw, 538px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/c5e369c48eb7d985dab911b91e99c09e/9516f/2_2_1.png","alt":"2 2 1","title":"2 2 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":"p","properties":{},"children":[{"type":"text","value":"NB: there can never be an else branch without an if branch before it. The if-else construction as a whole forms a single "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"conditional statement"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following example checks whether a number given by the user is even or not. Parity can be checked with the modulo operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%"}]},{"type":"text","value":", which produces the remainder of an integer division operation. When divided by two, if the remainder is zero, the number is even. Otherwise the number is odd."}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"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":"2"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is even\""}]},{"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":"else"}]},{"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 number is odd\""}]},{"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 number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nThe number is odd"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Another example with string comparison:"}]},{"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":"correct "},{"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":"\"kittycat\""}]},{"type":"text","value":"\npassword "},{"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 the password: \""}]},{"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":"if"}]},{"type":"text","value":" password "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" correct"},{"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":"\"Welcome\""}]},{"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":"else"}]},{"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":"\"No admittance\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With two different inputs this should print out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in the password: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"kittycat"}]},{"type":"text","value":"\nWelcome"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in the password: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"monkey"}]},{"type":"text","value":"\nNo admittance"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Age of maturity","tmcname":"part02-04_age_of_maturity","height":"400px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for their age. The program should then print out a message based on whether the user is of age or not, using 18 as the age of maturity."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How old are you? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"12"}]},{"type":"text","value":"\nYou are not of age!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How old are you? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"32"}]},{"type":"text","value":"\nYou are of age!"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"alternative-branches-using-the-elif-statement","style":"position:relative;"},"children":[{"type":"text","value":"Alternative branches using the elif statement"},{"type":"element","tagName":"a","properties":{"href":"#alternative-branches-using-the-elif-statement","ariaLabel":"alternative branches using the elif statement 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":"Often there are more than two options the program should account for. For example, the result of a football match could go three ways: home wins, away wins, or there is a tie."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A conditional statement can be added to with an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" branch. It is short for the words \"else if\", which means the branch will contain an alternative to the original condition. Importantly, an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" statement is executed only if none of the preceding branches is executed."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 668px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/62ebffc483e387b1f45eac73a9e0d285/74866/2_2_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: 156.52173913043478%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAfCAYAAADnTu3OAAAACXBIWXMAAAsTAAALEwEAmpwYAAACV0lEQVRIx5WW2aoCMRBE/f/v8kVBEHxQcd/3fV/7cgIlMWa8Y0PIMpNKdXWnZzJmZs/n0zVst9vZ4/GwcP1+v9vxeLTZbGbNZtPq9bpNp1M7nU7umSyjjeqXy+XbXOPJZOLAFouFlUolKxaL7l3m/X7fjbfb7Sfger3+AFN/Pp/dczbTGMMQu16vtlqtPgFZDNf8Xmxh5RsyccBXwFiDSafTsVqt5sa+xqkYyjSfz+fOXYLH2H/OWiJgjCWGW7hMADabzWt9v99bu91Oz1AGkMD8wwnOcDiMA2p+u93cRgGoHwwGLl185gTFpU3olg/IC7gC8OVycQ3xlfi+LKy/ouxbCEgkfQOUjbynHHxLG6gjZq/Xc1ELAUlmic5NQadCoWC5XM6NWet2u+65S5tqtWrZbNby+bzLL//qASg3iSz6cShJDQHdmNFoZIfDwUmT4WXcotfVC13mGWM/gQECILRUGgICO9l4PHYySTs/qP9GGUCYqGigFS4DiJvhTfqah9IQFgBhuEogmFMP6f39/wLCDiatVusNkGe610qfVIC4TOoQSQwt1SgGVHFaakC5zOaY4fJPgGGUYSuGjJHjZ0B0RC8luFgR+Z8Z4jJzxkSVm0Wvj1IioEB4UdXEv8t+4guEcdRln6VfnnxA1rROxCkI+o5HAWNfuFj5YjNVhtvCYeTgV4bhXwTCK9eIbLlcdjry51CpVBxbdI4yDMFxEY2kE4z5DYEhBaLRaLzS6VUcLMGSPlK4SApx/XxXE8tXzH01BQXGYhb+WCUyjB2ig/hs4HK4DsM/YKlzPPfbHa4AAAAASUVORK5CYII='); 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":["/static/62ebffc483e387b1f45eac73a9e0d285/a0b58/2_2_2.webp 230w","/static/62ebffc483e387b1f45eac73a9e0d285/bc10c/2_2_2.webp 460w","/static/62ebffc483e387b1f45eac73a9e0d285/7c056/2_2_2.webp 668w"],"sizes":"(max-width: 668px) 100vw, 668px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/62ebffc483e387b1f45eac73a9e0d285/81c8e/2_2_2.png 230w","/static/62ebffc483e387b1f45eac73a9e0d285/08a84/2_2_2.png 460w","/static/62ebffc483e387b1f45eac73a9e0d285/74866/2_2_2.png 668w"],"sizes":"(max-width: 668px) 100vw, 668px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/62ebffc483e387b1f45eac73a9e0d285/74866/2_2_2.png","alt":"2 2 2","title":"2 2 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":"Let's have a look at a program which determines the winner of a match:"}]},{"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":"goals_home "},{"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"]},"children":[{"type":"text","value":"\"Home goals scored: \""}]},{"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":"\ngoals_away "},{"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"]},"children":[{"type":"text","value":"\"Away goals scored: \""}]},{"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":"if"}]},{"type":"text","value":" goals_home "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" goals_away"},{"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 home team won!\""}]},{"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":"elif"}]},{"type":"text","value":" goals_away "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" goals_home"},{"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 away team won!\""}]},{"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":"else"}]},{"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":"\"It's a tie!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program could print out three different statements given different inputs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Home goals scored: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nAway goals scored: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nThe home team won!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Home goals scored: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nAway goals scored: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nThe away team won!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Home goals scored: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nAway goals scored: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nIt's a tie!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the above example there are three alternative branches, exactly one of which will always be executed. However, there is no limit to the number of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" branches a conditional statement can contain, and the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" branch is not mandatory."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is also a valid conditional 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":"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":"\"Holiday calendar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndate "},{"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":"\"What is the date today? \""}]},{"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":"if"}]},{"type":"text","value":" date "},{"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":"\"Dec 26\""}]},{"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":"\"It's Boxing Day\""}]},{"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":"elif"}]},{"type":"text","value":" date "},{"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":"\"Dec 31\""}]},{"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":"\"It's Hogmanay\""}]},{"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":"elif"}]},{"type":"text","value":" date "},{"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":"\"Jan 1\""}]},{"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":"\"It's New Year's Day\""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Thanks and bye.\""}]},{"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":"Holiday calendar\nWhat is the date today? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Dec 31"}]},{"type":"text","value":"\nIt's Hogmanay\nThanks and bye."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice the previous example has no "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" branch. If the user inputs a date which is not mentioned in any of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" branches, or inputs a date in a different format, none of the three branches of the conditional statement is executed."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Holiday calendar\nWhat is the date today? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Dec 25"}]},{"type":"text","value":"\nThanks and bye."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Greater than or equal to","tmcname":"part02-05_greater_or_equal","height":"400px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for two integer numbers. The program should then print out whichever is greater. If the numbers are equal, the program should print a different message."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in the first number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nPlease type in another number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nThe greater number was: 5"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in the first number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nPlease type in another number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\nThe greater number was: 8"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in the first number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nPlease type in another number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nThe numbers are equal!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"The elder","tmcname":"part02-06_elder","height":"550px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for the names and ages of two persons. The program should then print out the name of the elder."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Person 1:\nName: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Alan"}]},{"type":"text","value":"\nAge: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"26"}]},{"type":"text","value":"\nPerson 2:\nName: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Ada"}]},{"type":"text","value":"\nAge: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"27"}]},{"type":"text","value":"\nThe elder is Ada"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Person 1:\nName: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Bill"}]},{"type":"text","value":"\nAge: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nPerson 2:\nName: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Jean"}]},{"type":"text","value":"\nAge: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nBill and Jean are the same age"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Alphabetically last","tmcname":"part02-07_alphabetically_last","height":"500px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python comparison operators can also be used on strings. String "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" is smaller than string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" if it comes alphabetically before "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":". Notice however that the comparison is only reliable if"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the characters compared are of the same case, i.e. both UPPERCASE or both lowercase"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"only the standard English alphabet of a to z, or A to Z, is used."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for two words. The program should then print out whichever of the two comes alphabetically last."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can assume all words will be typed in lowercase entirely."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in the 1st word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"car"}]},{"type":"text","value":"\nPlease type in the 2nd word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"scooter"}]},{"type":"text","value":"\nscooter comes alphabetically last."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in the 1st word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"zorro"}]},{"type":"text","value":"\nPlease type in the 2nd word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"batman"}]},{"type":"text","value":"\nzorro comes alphabetically last."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in the 1st word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]},{"type":"text","value":"\nPlease type in the 2nd word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]},{"type":"text","value":"\nYou gave the same word twice."}]}]}]},{"type":"comment","value":"\n\nA quiz to review the contents of this section:\n\n\n\n"}]},"html":"

    After this section

      \n
    • You will know how to create multiple branches within conditional statements
    • \n
    • You will understand the purpose of if, elif and else statements within a conditional statement
    • \n
    • You will be able to use the modulo operation % in Boolean expressions
    • \n

    Let's have a look at a program which asks the user to input a number, and then prints out different messages based on whether the number is negative, positive, or equal to zero:

    number = int(input(\"Please type in a number: \"))\n\nif number < 0:\n    print(\"The number is negative\")\n\nif number >= 0:\n    print(\"The number is positive or zero\")

    This looks a bit clumsy and repetitive. We only ever want to execute one of the if blocks, because the input will always be either below zero, or zero or above. That is, either number < 0 or number >= 0 is true, but never both at the same time. So, the first conditional statement actually contains all we need here. If it is true, the number is negative. If it is false, the number equals zero or is positive.

    Instead of creating a whole another conditional statement, as in the example above, it is possible to create another branch of the same conditional statement to cover all cases where the condition was false. This is called the else statement.

    The previous example rewritten:

    number = int(input(\"Please type in a number: \"))\n\nif number < 0:\n    print(\"The number is negative\")\nelse:\n    print(\"The number is positive or zero\")

    When using an if-else construction, one and exactly one of the branches will always be executed. The following picture illustrates the structure:

    \n \n \n \n \n \n \"2\n \n \n

    NB: there can never be an else branch without an if branch before it. The if-else construction as a whole forms a single conditional statement.

    The following example checks whether a number given by the user is even or not. Parity can be checked with the modulo operator %, which produces the remainder of an integer division operation. When divided by two, if the remainder is zero, the number is even. Otherwise the number is odd.

    number = int(input(\"Please type in a number: \"))\n\nif number % 2 == 0:\n    print(\"The number is even\")\nelse:\n    print(\"The number is odd\")

    Please type in a number: 5\nThe number is odd

    Another example with string comparison:

    correct = \"kittycat\"\npassword = input(\"Please type in the password: \")\n\nif password == correct:\n    print(\"Welcome\")\nelse:\n    print(\"No admittance\")

    With two different inputs this should print out:

    Please type in the password: kittycat\nWelcome

    Please type in the password: monkey\nNo admittance

    Please write a program which asks the user for their age. The program should then print out a message based on whether the user is of age or not, using 18 as the age of maturity.

    Some examples of expected behaviour:

    How old are you? 12\nYou are not of age!

    How old are you? 32\nYou are of age!

    Alternative branches using the elif statement

    Often there are more than two options the program should account for. For example, the result of a football match could go three ways: home wins, away wins, or there is a tie.

    A conditional statement can be added to with an elif branch. It is short for the words \"else if\", which means the branch will contain an alternative to the original condition. Importantly, an elif statement is executed only if none of the preceding branches is executed.

    \n \n
    goals_home = int(input(\"Home goals scored: \"))\ngoals_away = int(input(\"Away goals scored: \"))\n\nif goals_home > goals_away:\n    print(\"The home team won!\")\nelif goals_away > goals_home:\n    print(\"The away team won!\")\nelse:\n    print(\"It's a tie!\")

    This program could print out three different statements given different inputs:

    Home goals scored: 4\nAway goals scored: 2\nThe home team won!

    Home goals scored: 0\nAway goals scored: 6\nThe away team won!

    Home goals scored: 3\nAway goals scored: 3\nIt's a tie!

    In the above example there are three alternative branches, exactly one of which will always be executed. However, there is no limit to the number of elif branches a conditional statement can contain, and the else branch is not mandatory.

    This is also a valid conditional statement:

    print(\"Holiday calendar\")\ndate = input(\"What is the date today? \")\n\nif date == \"Dec 26\":\n    print(\"It's Boxing Day\")\nelif date == \"Dec 31\":\n    print(\"It's Hogmanay\")\nelif date == \"Jan 1\":\n    print(\"It's New Year's Day\")\n\nprint(\"Thanks and bye.\")

    Holiday calendar\nWhat is the date today? Dec 31\nIt's Hogmanay\nThanks and bye.

    Notice the previous example has no else branch. If the user inputs a date which is not mentioned in any of the if or elif branches, or inputs a date in a different format, none of the three branches of the conditional statement is executed.

    Holiday calendar\nWhat is the date today? Dec 25\nThanks and bye.

    Please write a program which asks for two integer numbers. The program should then print out whichever is greater. If the numbers are equal, the program should print a different message.

    Some examples of expected behaviour:

    Please type in the first number: 5\nPlease type in another number: 3\nThe greater number was: 5

    Please type in the first number: 5\nPlease type in another number: 8\nThe greater number was: 8

    Please type in the first number: 5\nPlease type in another number: 5\nThe numbers are equal!

    Please write a program which asks for the names and ages of two persons. The program should then print out the name of the elder.

    Some examples of expected behaviour:

    Person 1:\nName: Alan\nAge: 26\nPerson 2:\nName: Ada\nAge: 27\nThe elder is Ada

    Person 1:\nName: Bill\nAge: 1\nPerson 2:\nName: Jean\nAge: 1\nBill and Jean are the same age

    Python comparison operators can also be used on strings. String a is smaller than string b if it comes alphabetically before b. Notice however that the comparison is only reliable if

      \n
    • the characters compared are of the same case, i.e. both UPPERCASE or both lowercase
    • \n
    • only the standard English alphabet of a to z, or A to Z, is used.
    • \n

    Please write a program which asks the user for two words. The program should then print out whichever of the two comes alphabetically last.

    You can assume all words will be typed in lowercase entirely.

    Some examples of expected behaviour:

    Please type in the 1st word: car\nPlease type in the 2nd word: scooter\nscooter comes alphabetically last.

    Please type in the 1st word: zorro\nPlease type in the 2nd word: batman\nzorro comes alphabetically last.

    Please type in the 1st word: python\nPlease type in the 2nd word: python\nYou gave the same word twice.

    ","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-2/2-else-elif.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know how to create multiple branches within conditional statements"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will understand the purpose of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" statements within a conditional statement"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use the modulo operation "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%"}]},{"type":"text","value":" in Boolean expressions"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at a program which asks the user to input a number, and then prints out different messages based on whether the number is negative, positive, or equal to zero:"}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is negative\""}]},{"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":"if"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is positive or zero\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This looks a bit clumsy and repetitive. We only ever want to execute one of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" blocks, because the input will always be either below zero, or zero or above. That is, either "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number < 0"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number >= 0"}]},{"type":"text","value":" is true, but never both at the same time. So, the first conditional statement actually contains all we need here. If it is true, the number is negative. If it is false, the number equals zero or is positive."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Instead of creating a whole another conditional statement, as in the example above, it is possible to create another branch of the same conditional statement to cover all cases "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"where the condition was false"}]},{"type":"text","value":". This is called the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" statement."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The previous example rewritten:"}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is negative\""}]},{"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":"else"}]},{"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 number is positive or zero\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When using an if-else construction, one and exactly one of the branches will always be executed. The following picture illustrates the structure:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 538px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/c5e369c48eb7d985dab911b91e99c09e/9516f/2_2_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: 129.56521739130434%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAaCAYAAAC3g3x9AAAACXBIWXMAAAsTAAALEwEAmpwYAAACGklEQVRIx5WV56oqQRCE9/3fyj+CGFHBhJgw55ztw9fcXuaOs3pOw7DrhNrqrpo2kn/xer30eblcZDqd/jf3eDxku91Kv9+XdrstnU5H97DXj8gHPJ/PetidA2gwGEipVJJ8Pi/ZbFZ6vZ6O4XAoq9VKJpOJbDabMOB6vY7nbByPR2UGOM/ZbCb3+12ez6eun04nWS6X3wH9AHC3273NX69XPRe5LJIYEjDhUCqVkkajob/92pN65H+JhRDgfr/XeepLrRaLhYrlnlNAXsifjbyTjiuKAR4OBwVh3G43BYexm7ICUuBcLifpdFqKxaLU6/UgQwNETQj4gDFDlMlkMlKpVKRcLqsVSMkXhZTn83mcDT4kZdvDGuejkFouINbgIPNYB99xmHlfFOZVZRQz1SwdghTYRBYME8S1mgHGtvnkQ8xrwQeZZwBOCT7aJgQIO7OGy450OewGyv8K0EpB/cbjsTSbzbhRMKy+MA5ePbeGLqD50zoMXadWq+k8zABj7Q2QdFgk8J3LEKvwEZ720cT25YLa0wU0RamjNQe3D9iIQmBJgAR9MXQ1ExtsEiC1hR3Xk+sKS79TvaXsg1MvCwBR2To6HsUq/hX9CAhDig+bQqEg1WpV/wJ4B9y/fl8BSRd1sUO321V1ASf1PzEMtX8MjN9Go5G2sCRhPjL0LWF9kXp+VfkbU1O71WrFxvd9+yvAkJVchv5N+QG65eiteVQ1KAAAAABJRU5ErkJggg=='); 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":["/static/c5e369c48eb7d985dab911b91e99c09e/a0b58/2_2_1.webp 230w","/static/c5e369c48eb7d985dab911b91e99c09e/bc10c/2_2_1.webp 460w","/static/c5e369c48eb7d985dab911b91e99c09e/b52d2/2_2_1.webp 538w"],"sizes":"(max-width: 538px) 100vw, 538px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/c5e369c48eb7d985dab911b91e99c09e/81c8e/2_2_1.png 230w","/static/c5e369c48eb7d985dab911b91e99c09e/08a84/2_2_1.png 460w","/static/c5e369c48eb7d985dab911b91e99c09e/9516f/2_2_1.png 538w"],"sizes":"(max-width: 538px) 100vw, 538px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/c5e369c48eb7d985dab911b91e99c09e/9516f/2_2_1.png","alt":"2 2 1","title":"2 2 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":"p","properties":{},"children":[{"type":"text","value":"NB: there can never be an else branch without an if branch before it. The if-else construction as a whole forms a single "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"conditional statement"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following example checks whether a number given by the user is even or not. Parity can be checked with the modulo operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%"}]},{"type":"text","value":", which produces the remainder of an integer division operation. When divided by two, if the remainder is zero, the number is even. Otherwise the number is odd."}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"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":"2"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is even\""}]},{"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":"else"}]},{"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 number is odd\""}]},{"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 number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nThe number is odd"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Another example with string comparison:"}]},{"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":"correct "},{"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":"\"kittycat\""}]},{"type":"text","value":"\npassword "},{"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 the password: \""}]},{"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":"if"}]},{"type":"text","value":" password "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" correct"},{"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":"\"Welcome\""}]},{"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":"else"}]},{"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":"\"No admittance\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With two different inputs this should print out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in the password: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"kittycat"}]},{"type":"text","value":"\nWelcome"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in the password: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"monkey"}]},{"type":"text","value":"\nNo admittance"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Age of maturity","tmcname":"part02-04_age_of_maturity","height":"400px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for their age. The program should then print out a message based on whether the user is of age or not, using 18 as the age of maturity."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How old are you? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"12"}]},{"type":"text","value":"\nYou are not of age!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How old are you? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"32"}]},{"type":"text","value":"\nYou are of age!"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"alternative-branches-using-the-elif-statement","style":"position:relative;"},"children":[{"type":"text","value":"Alternative branches using the elif statement"},{"type":"element","tagName":"a","properties":{"href":"#alternative-branches-using-the-elif-statement","ariaLabel":"alternative branches using the elif statement 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":"Often there are more than two options the program should account for. For example, the result of a football match could go three ways: home wins, away wins, or there is a tie."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A conditional statement can be added to with an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" branch. It is short for the words \"else if\", which means the branch will contain an alternative to the original condition. Importantly, an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" statement is executed only if none of the preceding branches is executed."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 668px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/62ebffc483e387b1f45eac73a9e0d285/74866/2_2_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: 156.52173913043478%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAfCAYAAADnTu3OAAAACXBIWXMAAAsTAAALEwEAmpwYAAACV0lEQVRIx5WW2aoCMRBE/f/v8kVBEHxQcd/3fV/7cgIlMWa8Y0PIMpNKdXWnZzJmZs/n0zVst9vZ4/GwcP1+v9vxeLTZbGbNZtPq9bpNp1M7nU7umSyjjeqXy+XbXOPJZOLAFouFlUolKxaL7l3m/X7fjbfb7Sfger3+AFN/Pp/dczbTGMMQu16vtlqtPgFZDNf8Xmxh5RsyccBXwFiDSafTsVqt5sa+xqkYyjSfz+fOXYLH2H/OWiJgjCWGW7hMADabzWt9v99bu91Oz1AGkMD8wwnOcDiMA2p+u93cRgGoHwwGLl185gTFpU3olg/IC7gC8OVycQ3xlfi+LKy/ouxbCEgkfQOUjbynHHxLG6gjZq/Xc1ELAUlmic5NQadCoWC5XM6NWet2u+65S5tqtWrZbNby+bzLL//qASg3iSz6cShJDQHdmNFoZIfDwUmT4WXcotfVC13mGWM/gQECILRUGgICO9l4PHYySTs/qP9GGUCYqGigFS4DiJvhTfqah9IQFgBhuEogmFMP6f39/wLCDiatVusNkGe610qfVIC4TOoQSQwt1SgGVHFaakC5zOaY4fJPgGGUYSuGjJHjZ0B0RC8luFgR+Z8Z4jJzxkSVm0Wvj1IioEB4UdXEv8t+4guEcdRln6VfnnxA1rROxCkI+o5HAWNfuFj5YjNVhtvCYeTgV4bhXwTCK9eIbLlcdjry51CpVBxbdI4yDMFxEY2kE4z5DYEhBaLRaLzS6VUcLMGSPlK4SApx/XxXE8tXzH01BQXGYhb+WCUyjB2ig/hs4HK4DsM/YKlzPPfbHa4AAAAASUVORK5CYII='); 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":["/static/62ebffc483e387b1f45eac73a9e0d285/a0b58/2_2_2.webp 230w","/static/62ebffc483e387b1f45eac73a9e0d285/bc10c/2_2_2.webp 460w","/static/62ebffc483e387b1f45eac73a9e0d285/7c056/2_2_2.webp 668w"],"sizes":"(max-width: 668px) 100vw, 668px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/62ebffc483e387b1f45eac73a9e0d285/81c8e/2_2_2.png 230w","/static/62ebffc483e387b1f45eac73a9e0d285/08a84/2_2_2.png 460w","/static/62ebffc483e387b1f45eac73a9e0d285/74866/2_2_2.png 668w"],"sizes":"(max-width: 668px) 100vw, 668px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/62ebffc483e387b1f45eac73a9e0d285/74866/2_2_2.png","alt":"2 2 2","title":"2 2 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":"Let's have a look at a program which determines the winner of a match:"}]},{"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":"goals_home "},{"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"]},"children":[{"type":"text","value":"\"Home goals scored: \""}]},{"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":"\ngoals_away "},{"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"]},"children":[{"type":"text","value":"\"Away goals scored: \""}]},{"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":"if"}]},{"type":"text","value":" goals_home "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" goals_away"},{"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 home team won!\""}]},{"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":"elif"}]},{"type":"text","value":" goals_away "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" goals_home"},{"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 away team won!\""}]},{"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":"else"}]},{"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":"\"It's a tie!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program could print out three different statements given different inputs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Home goals scored: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nAway goals scored: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nThe home team won!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Home goals scored: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nAway goals scored: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\nThe away team won!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Home goals scored: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nAway goals scored: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nIt's a tie!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the above example there are three alternative branches, exactly one of which will always be executed. However, there is no limit to the number of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" branches a conditional statement can contain, and the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" branch is not mandatory."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is also a valid conditional 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":"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":"\"Holiday calendar\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndate "},{"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":"\"What is the date today? \""}]},{"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":"if"}]},{"type":"text","value":" date "},{"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":"\"Dec 26\""}]},{"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":"\"It's Boxing Day\""}]},{"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":"elif"}]},{"type":"text","value":" date "},{"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":"\"Dec 31\""}]},{"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":"\"It's Hogmanay\""}]},{"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":"elif"}]},{"type":"text","value":" date "},{"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":"\"Jan 1\""}]},{"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":"\"It's New Year's Day\""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Thanks and bye.\""}]},{"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":"Holiday calendar\nWhat is the date today? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Dec 31"}]},{"type":"text","value":"\nIt's Hogmanay\nThanks and bye."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice the previous example has no "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" branch. If the user inputs a date which is not mentioned in any of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" branches, or inputs a date in a different format, none of the three branches of the conditional statement is executed."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Holiday calendar\nWhat is the date today? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Dec 25"}]},{"type":"text","value":"\nThanks and bye."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Greater than or equal to","tmcname":"part02-05_greater_or_equal","height":"400px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for two integer numbers. The program should then print out whichever is greater. If the numbers are equal, the program should print a different message."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in the first number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nPlease type in another number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nThe greater number was: 5"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in the first number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nPlease type in another number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\nThe greater number was: 8"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in the first number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nPlease type in another number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nThe numbers are equal!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"The elder","tmcname":"part02-06_elder","height":"550px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for the names and ages of two persons. The program should then print out the name of the elder."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Person 1:\nName: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Alan"}]},{"type":"text","value":"\nAge: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"26"}]},{"type":"text","value":"\nPerson 2:\nName: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Ada"}]},{"type":"text","value":"\nAge: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"27"}]},{"type":"text","value":"\nThe elder is Ada"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Person 1:\nName: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Bill"}]},{"type":"text","value":"\nAge: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nPerson 2:\nName: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Jean"}]},{"type":"text","value":"\nAge: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nBill and Jean are the same age"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Alphabetically last","tmcname":"part02-07_alphabetically_last","height":"500px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python comparison operators can also be used on strings. String "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" is smaller than string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" if it comes alphabetically before "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":". Notice however that the comparison is only reliable if"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the characters compared are of the same case, i.e. both UPPERCASE or both lowercase"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"only the standard English alphabet of a to z, or A to Z, is used."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for two words. The program should then print out whichever of the two comes alphabetically last."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can assume all words will be typed in lowercase entirely."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in the 1st word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"car"}]},{"type":"text","value":"\nPlease type in the 2nd word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"scooter"}]},{"type":"text","value":"\nscooter comes alphabetically last."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in the 1st word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"zorro"}]},{"type":"text","value":"\nPlease type in the 2nd word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"batman"}]},{"type":"text","value":"\nzorro comes alphabetically last."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in the 1st word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]},{"type":"text","value":"\nPlease type in the 2nd word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]},{"type":"text","value":"\nYou gave the same word twice."}]}]}]},{"type":"comment","value":"\n\nA quiz to review the contents of this section:\n\n\n\n"}]},"html":"

    After this section

      \n
    • You will know how to create multiple branches within conditional statements
    • \n
    • You will understand the purpose of if, elif and else statements within a conditional statement
    • \n
    • You will be able to use the modulo operation % in Boolean expressions
    • \n

    Let's have a look at a program which asks the user to input a number, and then prints out different messages based on whether the number is negative, positive, or equal to zero:

    number = int(input(\"Please type in a number: \"))\n\nif number < 0:\n    print(\"The number is negative\")\n\nif number >= 0:\n    print(\"The number is positive or zero\")

    This looks a bit clumsy and repetitive. We only ever want to execute one of the if blocks, because the input will always be either below zero, or zero or above. That is, either number < 0 or number >= 0 is true, but never both at the same time. So, the first conditional statement actually contains all we need here. If it is true, the number is negative. If it is false, the number equals zero or is positive.

    Instead of creating a whole another conditional statement, as in the example above, it is possible to create another branch of the same conditional statement to cover all cases where the condition was false. This is called the else statement.

    The previous example rewritten:

    number = int(input(\"Please type in a number: \"))\n\nif number < 0:\n    print(\"The number is negative\")\nelse:\n    print(\"The number is positive or zero\")

    When using an if-else construction, one and exactly one of the branches will always be executed. The following picture illustrates the structure:

    \n
    \n \n \n \n \n \"2\n \n \n

    NB: there can never be an else branch without an if branch before it. The if-else construction as a whole forms a single conditional statement.

    The following example checks whether a number given by the user is even or not. Parity can be checked with the modulo operator %, which produces the remainder of an integer division operation. When divided by two, if the remainder is zero, the number is even. Otherwise the number is odd.

    number = int(input(\"Please type in a number: \"))\n\nif number % 2 == 0:\n    print(\"The number is even\")\nelse:\n    print(\"The number is odd\")

    Please type in a number: 5\nThe number is odd

    Another example with string comparison:

    correct = \"kittycat\"\npassword = input(\"Please type in the password: \")\n\nif password == correct:\n    print(\"Welcome\")\nelse:\n    print(\"No admittance\")

    With two different inputs this should print out:

    Please type in the password: kittycat\nWelcome

    Please type in the password: monkey\nNo admittance

    Please write a program which asks the user for their age. The program should then print out a message based on whether the user is of age or not, using 18 as the age of maturity.

    Some examples of expected behaviour:

    How old are you? 12\nYou are not of age!

    How old are you? 32\nYou are of age!

    Alternative branches using the elif statement

    Often there are more than two options the program should account for. For example, the result of a football match could go three ways: home wins, away wins, or there is a tie.

    A conditional statement can be added to with an elif branch. It is short for the words \"else if\", which means the branch will contain an alternative to the original condition. Importantly, an elif statement is executed only if none of the preceding branches is executed.

    \n \n
    goals_home = int(input(\"Home goals scored: \"))\ngoals_away = int(input(\"Away goals scored: \"))\n\nif goals_home > goals_away:\n    print(\"The home team won!\")\nelif goals_away > goals_home:\n    print(\"The away team won!\")\nelse:\n    print(\"It's a tie!\")

    This program could print out three different statements given different inputs:

    Home goals scored: 4\nAway goals scored: 2\nThe home team won!

    Home goals scored: 0\nAway goals scored: 6\nThe away team won!

    Home goals scored: 3\nAway goals scored: 3\nIt's a tie!

    In the above example there are three alternative branches, exactly one of which will always be executed. However, there is no limit to the number of elif branches a conditional statement can contain, and the else branch is not mandatory.

    This is also a valid conditional statement:

    print(\"Holiday calendar\")\ndate = input(\"What is the date today? \")\n\nif date == \"Dec 26\":\n    print(\"It's Boxing Day\")\nelif date == \"Dec 31\":\n    print(\"It's Hogmanay\")\nelif date == \"Jan 1\":\n    print(\"It's New Year's Day\")\n\nprint(\"Thanks and bye.\")

    Holiday calendar\nWhat is the date today? Dec 31\nIt's Hogmanay\nThanks and bye.

    Notice the previous example has no else branch. If the user inputs a date which is not mentioned in any of the if or elif branches, or inputs a date in a different format, none of the three branches of the conditional statement is executed.

    Holiday calendar\nWhat is the date today? Dec 25\nThanks and bye.

    Please write a program which asks for two integer numbers. The program should then print out whichever is greater. If the numbers are equal, the program should print a different message.

    Some examples of expected behaviour:

    Please type in the first number: 5\nPlease type in another number: 3\nThe greater number was: 5

    Please type in the first number: 5\nPlease type in another number: 8\nThe greater number was: 8

    Please type in the first number: 5\nPlease type in another number: 5\nThe numbers are equal!

    Please write a program which asks for the names and ages of two persons. The program should then print out the name of the elder.

    Some examples of expected behaviour:

    Person 1:\nName: Alan\nAge: 26\nPerson 2:\nName: Ada\nAge: 27\nThe elder is Ada

    Person 1:\nName: Bill\nAge: 1\nPerson 2:\nName: Jean\nAge: 1\nBill and Jean are the same age

    Python comparison operators can also be used on strings. String a is smaller than string b if it comes alphabetically before b. Notice however that the comparison is only reliable if

      \n
    • the characters compared are of the same case, i.e. both UPPERCASE or both lowercase
    • \n
    • only the standard English alphabet of a to z, or A to Z, is used.
    • \n

    Please write a program which asks the user for two words. The program should then print out whichever of the two comes alphabetically last.

    You can assume all words will be typed in lowercase entirely.

    Some examples of expected behaviour:

    Please type in the 1st word: car\nPlease type in the 2nd word: scooter\nscooter comes alphabetically last.

    Please type in the 1st word: zorro\nPlease type in the 2nd word: batman\nzorro comes alphabetically last.

    Please type in the 1st word: python\nPlease type in the 2nd word: python\nYou gave the same word twice.

    ","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-2/2-else-elif.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-2/3-combining-conditions/page-data.json b/page-data/part-2/3-combining-conditions/page-data.json index 191108a4d..83eba5295 100644 --- a/page-data/part-2/3-combining-conditions/page-data.json +++ b/page-data/part-2/3-combining-conditions/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-2/3-combining-conditions", - "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 know how to use the operators "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"or"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" in conditions"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to write nested conditionals"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"logical-operators","style":"position:relative;"},"children":[{"type":"text","value":"Logical operators"},{"type":"element","tagName":"a","properties":{"href":"#logical-operators","ariaLabel":"logical operators 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":"You can combine conditions with the logical operators "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"or"}]},{"type":"text","value":". The operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" specifies that all the given conditions must be true at the same time. The operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"or"}]},{"type":"text","value":" specifies that at least one of the given conditions must be true."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, the condition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number >= 5 and number <= 8"}]},{"type":"text","value":" determines that "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" must simultaneously be at least 5 and at most 8. That is, it must be between 5 and 8."}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"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":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is between 5 and 8\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Meanwhile, the condition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number < 5 or number > 8"}]},{"type":"text","value":" determines that "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" must be either less than 5 or greater than 8. That is, it must not be within the range of 5 to 8."}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"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":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"or"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is not within the range of 5 to 8\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following truth table contains the behaviour of these operators in different situations:"}]},{"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\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":"a"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"b"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"a and b"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"a or b"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sometimes it is necessary to know if something is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" true. The operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" negates a condition:"}]},{"type":"text","value":"\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":"a"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"not a"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The above example with the range of 5 to 8 "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"excluded"}]},{"type":"text","value":" could also be programmed 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":"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"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":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"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":"8"}]},{"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 number is not within the range of 5 to 8\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Especially in programming, logical operators are often called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Boolean operators"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Simplified combined conditions"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The condition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x >= a and x <= b"}]},{"type":"text","value":" is a very common way of checking whether the number "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" falls within the range of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":". An expression with this structure works the same way in most programming languages."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python also allows a simplified notation for combining conditions: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a <= x <= b"}]},{"type":"text","value":" achieves the same result as the longer version using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":". This shorter notation might be more familiar from mathematics, but it is not very widely used in Python programming, possibly because very few other programming languages have a similar shorthand."}]}]},{"type":"element","tagName":"h2","properties":{"id":"combining-and-chaining-conditions","style":"position:relative;"},"children":[{"type":"text","value":"Combining and chaining conditions"},{"type":"element","tagName":"a","properties":{"href":"#combining-and-chaining-conditions","ariaLabel":"combining and chaining conditions 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 following program asks the user to type in four numbers. It then works out which of the four is the greatest, with the help of some conditions:"}]},{"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":"n1 "},{"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"]},"children":[{"type":"text","value":"\"Number 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":"text","value":"\nn2 "},{"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"]},"children":[{"type":"text","value":"\"Number 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":"\nn3 "},{"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"]},"children":[{"type":"text","value":"\"Number 3: \""}]},{"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":"\nn4 "},{"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"]},"children":[{"type":"text","value":"\"Number 4: \""}]},{"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":"if"}]},{"type":"text","value":" n1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" n2 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" n1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" n3 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" n1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" n4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n greatest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" n1\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" n2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" n3 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" n2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" n4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n greatest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" n2\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" n3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" n4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n greatest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" n3\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n greatest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" n4\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":"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":"greatest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" is the greatest of the numbers.\""}]}]},{"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":"Number 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nNumber 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nNumber 3: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nNumber 4: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n4 is the greatest of the numbers."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the above example the first condition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n1 > n2 and n1 > n3 and n1 > n4"}]},{"type":"text","value":" is true only if all three conditions within are true."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Age check","tmcname":"part02-08_age_check"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for the user's age. If the age is not plausible, that is, it is under 5 or something that can't be an actual human age, the program should print out a comment."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Have a look at the examples of expected behaviour below to figure out which comment is applicable in each case."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your age? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"13"}]},{"type":"text","value":"\nOk, you're 13 years old"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your age? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nI suspect you can't write quite yet..."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your age? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-4"}]},{"type":"text","value":"\nThat must be a mistake"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Nephews","tmcname":"part02-09_nephews"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for the user's name. If the name is Huey, Dewey or Louie, the program should recognise the user as one of Donald Duck's nephews."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In a similar fashion, if the name is Morty or Ferdie, the program should recognise the user as one of Mickey Mouse's nephews."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in your name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Morty"}]},{"type":"text","value":"\nI think you might be one of Mickey Mouse's nephews."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in your name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Huey"}]},{"type":"text","value":"\nI think you might be one of Donald Duck's nephews."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in your name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Ken"}]},{"type":"text","value":"\nYou're not a nephew of any character I know of."}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Grades and points","tmcname":"part02-10_grades_and_points"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The table below outlines the grade boundaries on a certain university course. Please write a program which asks for the amount of points received and then prints out the grade attained according to the 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\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":"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"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"impossible!"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"0-49"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"fail"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"50-59"}]},{"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":"60-69"}]},{"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":"70-79"}]},{"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":"80-89"}]},{"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":"90-100"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"> 100"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"impossible!"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many points [0-100]: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"37"}]},{"type":"text","value":"\nGrade: fail"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many points [0-100]: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"76"}]},{"type":"text","value":"\nGrade: 3"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many points [0-100]: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-3"}]},{"type":"text","value":"\nGrade: impossible!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"FizzBuzz","tmcname":"part02-11_fizzbuzz"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for an integer number. If the number is divisible by three, the program should print out Fizz. If the number is divisible by five, the program should print out Buzz. If the number is divisible by both three and five, the program should print out FizzBuzz."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9"}]},{"type":"text","value":"\nFizz"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"20"}]},{"type":"text","value":"\nBuzz"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"45"}]},{"type":"text","value":"\nFizzBuzz"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"nested-conditionals","style":"position:relative;"},"children":[{"type":"text","value":"Nested conditionals"},{"type":"element","tagName":"a","properties":{"href":"#nested-conditionals","ariaLabel":"nested conditionals 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":"Conditional statements can also be nested within other conditional statements. For example, the following program checks whether a number is above zero, and then whether it is odd or even:"}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"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":"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":"if"}]},{"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":"2"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is even\""}]},{"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":"else"}]},{"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 number is odd\""}]},{"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":"else"}]},{"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 number is negative or zero\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of how this program behaves:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nThe number is odd"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"18"}]},{"type":"text","value":"\nThe number is even"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-4"}]},{"type":"text","value":"\nThe number is negative or zero"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With nested conditional statements it is crucial to get the indentations right. Indentations determine which branches are linked together. For example, an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" branch and an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" branch with the same amount of whitespace are determined to be branches of the same conditional statement."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The same result can often be achieved using either nested conditional statements or conditions combined with logical operators. The example below is functionally no different from the example above, in the sense that it will print out the exactly same things with the same inputs:"}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"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":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"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":"2"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is even\""}]},{"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":"elif"}]},{"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":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"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":"2"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is odd\""}]},{"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":"else"}]},{"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 number is negative or zero\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Neither approach is intrinsically better than the other, but in different situations one or the other may seem more logical. In this particular example most people tend to find the first version with nesting to be more intuitive."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Leap year","tmcname":"part02-12_leap_year"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Generally, any year that is divisible by four is a leap year. However, if the year is additionally divisible by 100, it is a leap year only if it also divisible by 400."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a year, and then prints out whether that year is a leap year or not."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a year: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2011"}]},{"type":"text","value":"\nThat year is not a leap year."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a year: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2020"}]},{"type":"text","value":"\nThat year is a leap year."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a year: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1800"}]},{"type":"text","value":"\nThat year is not a leap year."}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Alphabetically in the middle","tmcname":"part02-13_alphabetically_in_the_middle"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for three letters. The program should then print out whichever of the three letters would be in the middle if the letters were in alphabetical order."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume the letters will be either all uppercase, or all lowercase."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1st letter: x\n2nd letter: c\n3rd letter: p\nThe letter in the middle is p"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1st letter: C\n2nd letter: B\n3rd letter: A\nThe letter in the middle is B"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Gift tax calculator","tmcname":"part02-14_gift_tax_calculator","height":"500px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some say paying taxes makes Finns happy, so let's see if the secret of happiness lies in one of the taxes set out in Finnish tax code."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://www.vero.fi/en/individuals/property/gifts/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"According to the Finnish Tax Administration"}]},{"type":"text","value":", a gift is a transfer of property to another person against no compensation or payment. If the total value of the gifts you receive from the same donor in the course of 3 years is €5,000 or more, you must pay gift tax."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the gift is received from a close relative or a family member, the amount of tax to be paid is determined by the following table, which is also available on "},{"type":"element","tagName":"a","properties":{"href":"https://www.vero.fi/en/individuals/property/gifts/gift-tax-calculator/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"this website"}]},{"type":"text","value":":"}]},{"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\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":"Value of gift"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Tax at the lower limit"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Tax rate for the exceeding part (%)"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"5 000 — 25 000"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"8"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"25 000 — 55 000"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"1 700"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"10"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"55 000 — 200 000"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"4 700"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"12"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"200 000 — 1 000 000"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"22 100"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"15"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"1 000 000 —"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"142 100"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"17"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, for a gift of 6 000 euros the recipient pays a tax of 180 euros (100 + (6 000 - 5 000) * 0.08). Similarly, for a gift of 75 000 euros the recipient pays a tax of 7 100 euros (4 700 + (75 000 - 55 000) * 0.12)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which calculates the correct amount of tax for a gift from a close relative. Have a look at the examples below to see what is expected. Notice the lack of thousands separators in the input values - you may assume there will be no spaces or other thousands separators in the numbers in the input, as we haven't yet covered dealing with these."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Value of gift: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3500"}]},{"type":"text","value":"\nNo tax!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Value of gift: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5000"}]},{"type":"text","value":"\nAmount of tax: 100.0 euros"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Value of gift: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"27500"}]},{"type":"text","value":"\nAmount of tax: 1950.0 euros"}]}]}]},{"type":"comment","value":"\n\nA quiz to review the contents of this section:\n\n\n\n"}]},"html":"

    After this section

      \n
    • You will know how to use the operators and, or and not in conditions
    • \n
    • You will be able to write nested conditionals
    • \n

    Logical operators

    You can combine conditions with the logical operators and and or. The operator and specifies that all the given conditions must be true at the same time. The operator or specifies that at least one of the given conditions must be true.

    For example, the condition number >= 5 and number <= 8 determines that number must simultaneously be at least 5 and at most 8. That is, it must be between 5 and 8.

    number = int(input(\"Please type in a number: \"))\nif number >= 5 and number <= 8:\n    print(\"The number is between 5 and 8\")

    Meanwhile, the condition number < 5 or number > 8 determines that number must be either less than 5 or greater than 8. That is, it must not be within the range of 5 to 8.

    number = int(input(\"Please type in a number: \"))\nif number < 5 or number > 8:\n    print(\"The number is not within the range of 5 to 8\")

    The following truth table contains the behaviour of these operators in different situations:

    \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\n\n
    aba and ba or b
    FalseFalseFalseFalse
    TrueFalseFalseTrue
    FalseTrueFalseTrue
    TrueTrueTrueTrue

    Sometimes it is necessary to know if something is not true. The operator not negates a condition:

    \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
    anot a
    TrueFalse
    FalseTrue

    The above example with the range of 5 to 8 excluded could also be programmed like this:

    number = int(input(\"Please type in a number: \"))\nif not (number >= 5 and number <= 8):\n    print(\"The number is not within the range of 5 to 8\")

    Especially in programming, logical operators are often called Boolean operators.

    The condition x >= a and x <= b is a very common way of checking whether the number x falls within the range of a to b. An expression with this structure works the same way in most programming languages.

    Python also allows a simplified notation for combining conditions: a <= x <= b achieves the same result as the longer version using and. This shorter notation might be more familiar from mathematics, but it is not very widely used in Python programming, possibly because very few other programming languages have a similar shorthand.

    Combining and chaining conditions

    The following program asks the user to type in four numbers. It then works out which of the four is the greatest, with the help of some conditions:

    n1 = int(input(\"Number 1: \"))\nn2 = int(input(\"Number 2: \"))\nn3 = int(input(\"Number 3: \"))\nn4 = int(input(\"Number 4: \"))\n\nif n1 > n2 and n1 > n3 and n1 > n4:\n    greatest = n1\nelif n2 > n3 and n2 > n4:\n    greatest = n2\nelif n3 > n4:\n    greatest = n3\nelse:\n    greatest = n4\n\nprint(f\" {greatest} is the greatest of the numbers.\")

    Number 1: 2\nNumber 2: 4\nNumber 3: 1\nNumber 4: 1\n4 is the greatest of the numbers.

    In the above example the first condition n1 > n2 and n1 > n3 and n1 > n4 is true only if all three conditions within are true.

    Please write a program which asks for the user's age. If the age is not plausible, that is, it is under 5 or something that can't be an actual human age, the program should print out a comment.

    Have a look at the examples of expected behaviour below to figure out which comment is applicable in each case.

    What is your age? 13\nOk, you're 13 years old

    What is your age? 2\nI suspect you can't write quite yet...

    What is your age? -4\nThat must be a mistake

    Please write a program which asks for the user's name. If the name is Huey, Dewey or Louie, the program should recognise the user as one of Donald Duck's nephews.

    In a similar fashion, if the name is Morty or Ferdie, the program should recognise the user as one of Mickey Mouse's nephews.

    Some examples:

    Please type in your name: Morty\nI think you might be one of Mickey Mouse's nephews.

    Please type in your name: Huey\nI think you might be one of Donald Duck's nephews.

    Please type in your name: Ken\nYou're not a nephew of any character I know of.

    The table below outlines the grade boundaries on a certain university course. Please write a program which asks for the amount of points received and then prints out the grade attained according to the table.

    \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\n\n\n\n\n\n\n\n
    pointsgrade
    < 0impossible!
    0-49fail
    50-591
    60-692
    70-793
    80-894
    90-1005
    > 100impossible!

    Some examples:

    How many points [0-100]: 37\nGrade: fail

    How many points [0-100]: 76\nGrade: 3

    How many points [0-100]: -3\nGrade: impossible!

    Please write a program which asks the user for an integer number. If the number is divisible by three, the program should print out Fizz. If the number is divisible by five, the program should print out Buzz. If the number is divisible by both three and five, the program should print out FizzBuzz.

    Some examples of expected behaviour:

    Number: 9\nFizz

    Number: 7

    Number: 20\nBuzz

    Number: 45\nFizzBuzz

    Nested conditionals

    Conditional statements can also be nested within other conditional statements. For example, the following program checks whether a number is above zero, and then whether it is odd or even:

    number = int(input(\"Please type in a number: \"))\n\nif number > 0:\n    if number % 2 == 0:\n        print(\"The number is even\")\n    else:\n        print(\"The number is odd\")\nelse:\n    print(\"The number is negative or zero\")

    Some examples of how this program behaves:

    Please type in a number: 3\nThe number is odd

    Please type in a number: 18\nThe number is even

    Please type in a number: -4\nThe number is negative or zero

    With nested conditional statements it is crucial to get the indentations right. Indentations determine which branches are linked together. For example, an if branch and an else branch with the same amount of whitespace are determined to be branches of the same conditional statement.

    The same result can often be achieved using either nested conditional statements or conditions combined with logical operators. The example below is functionally no different from the example above, in the sense that it will print out the exactly same things with the same inputs:

    number = int(input(\"Please type in a number: \"))\n\nif number > 0 and number % 2 == 0:\n    print(\"The number is even\")\nelif number > 0 and number % 2 != 0:\n    print(\"The number is odd\")\nelse:\n    print(\"The number is negative or zero\")

    Neither approach is intrinsically better than the other, but in different situations one or the other may seem more logical. In this particular example most people tend to find the first version with nesting to be more intuitive.

    Generally, any year that is divisible by four is a leap year. However, if the year is additionally divisible by 100, it is a leap year only if it also divisible by 400.

    Please write a program which asks the user for a year, and then prints out whether that year is a leap year or not.

    Some examples:

    Please type in a year: 2011\nThat year is not a leap year.

    Please type in a year: 2020\nThat year is a leap year.

    Please type in a year: 1800\nThat year is not a leap year.

    Please write a program which asks the user for three letters. The program should then print out whichever of the three letters would be in the middle if the letters were in alphabetical order.

    You may assume the letters will be either all uppercase, or all lowercase.

    Some examples of expected behaviour:

    1st letter: x\n2nd letter: c\n3rd letter: p\nThe letter in the middle is p

    1st letter: C\n2nd letter: B\n3rd letter: A\nThe letter in the middle is B

    Some say paying taxes makes Finns happy, so let's see if the secret of happiness lies in one of the taxes set out in Finnish tax code.

    According to the Finnish Tax Administration, a gift is a transfer of property to another person against no compensation or payment. If the total value of the gifts you receive from the same donor in the course of 3 years is €5,000 or more, you must pay gift tax.

    When the gift is received from a close relative or a family member, the amount of tax to be paid is determined by the following table, which is also available on this website:

    \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\n\n
    Value of giftTax at the lower limitTax rate for the exceeding part (%)
    5 000 — 25 0001008
    25 000 — 55 0001 70010
    55 000 — 200 0004 70012
    200 000 — 1 000 00022 10015
    1 000 000 —142 10017

    So, for a gift of 6 000 euros the recipient pays a tax of 180 euros (100 + (6 000 - 5 000) * 0.08). Similarly, for a gift of 75 000 euros the recipient pays a tax of 7 100 euros (4 700 + (75 000 - 55 000) * 0.12).

    Please write a program which calculates the correct amount of tax for a gift from a close relative. Have a look at the examples below to see what is expected. Notice the lack of thousands separators in the input values - you may assume there will be no spaces or other thousands separators in the numbers in the input, as we haven't yet covered dealing with these.

    Value of gift: 3500\nNo tax!

    Value of gift: 5000\nAmount of tax: 100.0 euros

    Value of gift: 27500\nAmount of tax: 1950.0 euros

    ","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-2/3-combining-conditions.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know how to use the operators "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"or"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" in conditions"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to write nested conditionals"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"logical-operators","style":"position:relative;"},"children":[{"type":"text","value":"Logical operators"},{"type":"element","tagName":"a","properties":{"href":"#logical-operators","ariaLabel":"logical operators 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":"You can combine conditions with the logical operators "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"or"}]},{"type":"text","value":". The operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" specifies that all the given conditions must be true at the same time. The operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"or"}]},{"type":"text","value":" specifies that at least one of the given conditions must be true."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, the condition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number >= 5 and number <= 8"}]},{"type":"text","value":" determines that "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" must simultaneously be at least 5 and at most 8. That is, it must be between 5 and 8."}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"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":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is between 5 and 8\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Meanwhile, the condition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number < 5 or number > 8"}]},{"type":"text","value":" determines that "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" must be either less than 5 or greater than 8. That is, it must not be within the range of 5 to 8."}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"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":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"or"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is not within the range of 5 to 8\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following truth table contains the behaviour of these operators in different situations:"}]},{"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\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":"a"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"b"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"a and b"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"a or b"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sometimes it is necessary to know if something is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" true. The operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" negates a condition:"}]},{"type":"text","value":"\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":"a"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"not a"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"False"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"True"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The above example with the range of 5 to 8 "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"excluded"}]},{"type":"text","value":" could also be programmed 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":"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"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":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"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":"8"}]},{"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 number is not within the range of 5 to 8\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Especially in programming, logical operators are often called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Boolean operators"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Simplified combined conditions"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The condition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x >= a and x <= b"}]},{"type":"text","value":" is a very common way of checking whether the number "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" falls within the range of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":". An expression with this structure works the same way in most programming languages."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python also allows a simplified notation for combining conditions: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a <= x <= b"}]},{"type":"text","value":" achieves the same result as the longer version using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":". This shorter notation might be more familiar from mathematics, but it is not very widely used in Python programming, possibly because very few other programming languages have a similar shorthand."}]}]},{"type":"element","tagName":"h2","properties":{"id":"combining-and-chaining-conditions","style":"position:relative;"},"children":[{"type":"text","value":"Combining and chaining conditions"},{"type":"element","tagName":"a","properties":{"href":"#combining-and-chaining-conditions","ariaLabel":"combining and chaining conditions 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 following program asks the user to type in four numbers. It then works out which of the four is the greatest, with the help of some conditions:"}]},{"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":"n1 "},{"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"]},"children":[{"type":"text","value":"\"Number 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":"text","value":"\nn2 "},{"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"]},"children":[{"type":"text","value":"\"Number 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":"\nn3 "},{"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"]},"children":[{"type":"text","value":"\"Number 3: \""}]},{"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":"\nn4 "},{"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"]},"children":[{"type":"text","value":"\"Number 4: \""}]},{"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":"if"}]},{"type":"text","value":" n1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" n2 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" n1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" n3 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" n1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" n4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n greatest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" n1\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" n2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" n3 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" n2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" n4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n greatest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" n2\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" n3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" n4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n greatest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" n3\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n greatest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" n4\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":"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":"greatest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" is the greatest of the numbers.\""}]}]},{"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":"Number 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nNumber 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nNumber 3: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nNumber 4: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n4 is the greatest of the numbers."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the above example the first condition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n1 > n2 and n1 > n3 and n1 > n4"}]},{"type":"text","value":" is true only if all three conditions within are true."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Age check","tmcname":"part02-08_age_check"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for the user's age. If the age is not plausible, that is, it is under 5 or something that can't be an actual human age, the program should print out a comment."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Have a look at the examples of expected behaviour below to figure out which comment is applicable in each case."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your age? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"13"}]},{"type":"text","value":"\nOk, you're 13 years old"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your age? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nI suspect you can't write quite yet..."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What is your age? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-4"}]},{"type":"text","value":"\nThat must be a mistake"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Nephews","tmcname":"part02-09_nephews"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for the user's name. If the name is Huey, Dewey or Louie, the program should recognise the user as one of Donald Duck's nephews."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In a similar fashion, if the name is Morty or Ferdie, the program should recognise the user as one of Mickey Mouse's nephews."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in your name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Morty"}]},{"type":"text","value":"\nI think you might be one of Mickey Mouse's nephews."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in your name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Huey"}]},{"type":"text","value":"\nI think you might be one of Donald Duck's nephews."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in your name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Ken"}]},{"type":"text","value":"\nYou're not a nephew of any character I know of."}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Grades and points","tmcname":"part02-10_grades_and_points"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The table below outlines the grade boundaries on a certain university course. Please write a program which asks for the amount of points received and then prints out the grade attained according to the 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\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":"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"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"impossible!"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"0-49"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"fail"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"50-59"}]},{"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":"60-69"}]},{"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":"70-79"}]},{"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":"80-89"}]},{"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":"90-100"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"> 100"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"impossible!"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many points [0-100]: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"37"}]},{"type":"text","value":"\nGrade: fail"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many points [0-100]: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"76"}]},{"type":"text","value":"\nGrade: 3"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many points [0-100]: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-3"}]},{"type":"text","value":"\nGrade: impossible!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"FizzBuzz","tmcname":"part02-11_fizzbuzz"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for an integer number. If the number is divisible by three, the program should print out Fizz. If the number is divisible by five, the program should print out Buzz. If the number is divisible by both three and five, the program should print out FizzBuzz."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9"}]},{"type":"text","value":"\nFizz"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"20"}]},{"type":"text","value":"\nBuzz"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"45"}]},{"type":"text","value":"\nFizzBuzz"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"nested-conditionals","style":"position:relative;"},"children":[{"type":"text","value":"Nested conditionals"},{"type":"element","tagName":"a","properties":{"href":"#nested-conditionals","ariaLabel":"nested conditionals 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":"Conditional statements can also be nested within other conditional statements. For example, the following program checks whether a number is above zero, and then whether it is odd or even:"}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"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":"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":"if"}]},{"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":"2"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is even\""}]},{"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":"else"}]},{"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 number is odd\""}]},{"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":"else"}]},{"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 number is negative or zero\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of how this program behaves:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nThe number is odd"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"18"}]},{"type":"text","value":"\nThe number is even"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-4"}]},{"type":"text","value":"\nThe number is negative or zero"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With nested conditional statements it is crucial to get the indentations right. Indentations determine which branches are linked together. For example, an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" branch and an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" branch with the same amount of whitespace are determined to be branches of the same conditional statement."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The same result can often be achieved using either nested conditional statements or conditions combined with logical operators. The example below is functionally no different from the example above, in the sense that it will print out the exactly same things with the same inputs:"}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"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":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"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":"2"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is even\""}]},{"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":"elif"}]},{"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":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"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":"2"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The number is odd\""}]},{"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":"else"}]},{"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 number is negative or zero\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Neither approach is intrinsically better than the other, but in different situations one or the other may seem more logical. In this particular example most people tend to find the first version with nesting to be more intuitive."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Leap year","tmcname":"part02-12_leap_year"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Generally, any year that is divisible by four is a leap year. However, if the year is additionally divisible by 100, it is a leap year only if it also divisible by 400."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a year, and then prints out whether that year is a leap year or not."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a year: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2011"}]},{"type":"text","value":"\nThat year is not a leap year."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a year: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2020"}]},{"type":"text","value":"\nThat year is a leap year."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a year: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1800"}]},{"type":"text","value":"\nThat year is not a leap year."}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Alphabetically in the middle","tmcname":"part02-13_alphabetically_in_the_middle"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for three letters. The program should then print out whichever of the three letters would be in the middle if the letters were in alphabetical order."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume the letters will be either all uppercase, or all lowercase."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1st letter: x\n2nd letter: c\n3rd letter: p\nThe letter in the middle is p"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1st letter: C\n2nd letter: B\n3rd letter: A\nThe letter in the middle is B"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Gift tax calculator","tmcname":"part02-14_gift_tax_calculator","height":"500px"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some say paying taxes makes Finns happy, so let's see if the secret of happiness lies in one of the taxes set out in Finnish tax code."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://www.vero.fi/en/individuals/property/gifts/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"According to the Finnish Tax Administration"}]},{"type":"text","value":", a gift is a transfer of property to another person against no compensation or payment. If the total value of the gifts you receive from the same donor in the course of 3 years is €5,000 or more, you must pay gift tax."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the gift is received from a close relative or a family member, the amount of tax to be paid is determined by the following table, which is also available on "},{"type":"element","tagName":"a","properties":{"href":"https://www.vero.fi/en/individuals/property/gifts/gift-tax-calculator/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"this website"}]},{"type":"text","value":":"}]},{"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\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":"Value of gift"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Tax at the lower limit"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Tax rate for the exceeding part (%)"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"5 000 — 25 000"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"8"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"25 000 — 55 000"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"1 700"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"10"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"55 000 — 200 000"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"4 700"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"12"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"200 000 — 1 000 000"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"22 100"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"15"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"1 000 000 —"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"142 100"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"17"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, for a gift of 6 000 euros the recipient pays a tax of 180 euros (100 + (6 000 - 5 000) * 0.08). Similarly, for a gift of 75 000 euros the recipient pays a tax of 7 100 euros (4 700 + (75 000 - 55 000) * 0.12)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which calculates the correct amount of tax for a gift from a close relative. Have a look at the examples below to see what is expected. Notice the lack of thousands separators in the input values - you may assume there will be no spaces or other thousands separators in the numbers in the input, as we haven't yet covered dealing with these."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Value of gift: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3500"}]},{"type":"text","value":"\nNo tax!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Value of gift: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5000"}]},{"type":"text","value":"\nAmount of tax: 100.0 euros"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Value of gift: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"27500"}]},{"type":"text","value":"\nAmount of tax: 1950.0 euros"}]}]}]},{"type":"comment","value":"\n\nA quiz to review the contents of this section:\n\n\n\n"}]},"html":"

    After this section

      \n
    • You will know how to use the operators and, or and not in conditions
    • \n
    • You will be able to write nested conditionals
    • \n

    Logical operators

    You can combine conditions with the logical operators and and or. The operator and specifies that all the given conditions must be true at the same time. The operator or specifies that at least one of the given conditions must be true.

    For example, the condition number >= 5 and number <= 8 determines that number must simultaneously be at least 5 and at most 8. That is, it must be between 5 and 8.

    number = int(input(\"Please type in a number: \"))\nif number >= 5 and number <= 8:\n    print(\"The number is between 5 and 8\")

    Meanwhile, the condition number < 5 or number > 8 determines that number must be either less than 5 or greater than 8. That is, it must not be within the range of 5 to 8.

    number = int(input(\"Please type in a number: \"))\nif number < 5 or number > 8:\n    print(\"The number is not within the range of 5 to 8\")

    The following truth table contains the behaviour of these operators in different situations:

    \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\n\n
    aba and ba or b
    FalseFalseFalseFalse
    TrueFalseFalseTrue
    FalseTrueFalseTrue
    TrueTrueTrueTrue

    Sometimes it is necessary to know if something is not true. The operator not negates a condition:

    \n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n
    anot a
    TrueFalse
    FalseTrue

    The above example with the range of 5 to 8 excluded could also be programmed like this:

    number = int(input(\"Please type in a number: \"))\nif not (number >= 5 and number <= 8):\n    print(\"The number is not within the range of 5 to 8\")

    Especially in programming, logical operators are often called Boolean operators.

    The condition x >= a and x <= b is a very common way of checking whether the number x falls within the range of a to b. An expression with this structure works the same way in most programming languages.

    Python also allows a simplified notation for combining conditions: a <= x <= b achieves the same result as the longer version using and. This shorter notation might be more familiar from mathematics, but it is not very widely used in Python programming, possibly because very few other programming languages have a similar shorthand.

    Combining and chaining conditions

    The following program asks the user to type in four numbers. It then works out which of the four is the greatest, with the help of some conditions:

    n1 = int(input(\"Number 1: \"))\nn2 = int(input(\"Number 2: \"))\nn3 = int(input(\"Number 3: \"))\nn4 = int(input(\"Number 4: \"))\n\nif n1 > n2 and n1 > n3 and n1 > n4:\n    greatest = n1\nelif n2 > n3 and n2 > n4:\n    greatest = n2\nelif n3 > n4:\n    greatest = n3\nelse:\n    greatest = n4\n\nprint(f\" {greatest} is the greatest of the numbers.\")

    Number 1: 2\nNumber 2: 4\nNumber 3: 1\nNumber 4: 1\n4 is the greatest of the numbers.

    In the above example the first condition n1 > n2 and n1 > n3 and n1 > n4 is true only if all three conditions within are true.

    Please write a program which asks for the user's age. If the age is not plausible, that is, it is under 5 or something that can't be an actual human age, the program should print out a comment.

    Have a look at the examples of expected behaviour below to figure out which comment is applicable in each case.

    What is your age? 13\nOk, you're 13 years old

    What is your age? 2\nI suspect you can't write quite yet...

    What is your age? -4\nThat must be a mistake

    Please write a program which asks for the user's name. If the name is Huey, Dewey or Louie, the program should recognise the user as one of Donald Duck's nephews.

    In a similar fashion, if the name is Morty or Ferdie, the program should recognise the user as one of Mickey Mouse's nephews.

    Some examples:

    Please type in your name: Morty\nI think you might be one of Mickey Mouse's nephews.

    Please type in your name: Huey\nI think you might be one of Donald Duck's nephews.

    Please type in your name: Ken\nYou're not a nephew of any character I know of.

    The table below outlines the grade boundaries on a certain university course. Please write a program which asks for the amount of points received and then prints out the grade attained according to the table.

    \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\n\n\n\n\n\n\n\n
    pointsgrade
    < 0impossible!
    0-49fail
    50-591
    60-692
    70-793
    80-894
    90-1005
    > 100impossible!

    Some examples:

    How many points [0-100]: 37\nGrade: fail

    How many points [0-100]: 76\nGrade: 3

    How many points [0-100]: -3\nGrade: impossible!

    Please write a program which asks the user for an integer number. If the number is divisible by three, the program should print out Fizz. If the number is divisible by five, the program should print out Buzz. If the number is divisible by both three and five, the program should print out FizzBuzz.

    Some examples of expected behaviour:

    Number: 9\nFizz

    Number: 7

    Number: 20\nBuzz

    Number: 45\nFizzBuzz

    Nested conditionals

    Conditional statements can also be nested within other conditional statements. For example, the following program checks whether a number is above zero, and then whether it is odd or even:

    number = int(input(\"Please type in a number: \"))\n\nif number > 0:\n    if number % 2 == 0:\n        print(\"The number is even\")\n    else:\n        print(\"The number is odd\")\nelse:\n    print(\"The number is negative or zero\")

    Some examples of how this program behaves:

    Please type in a number: 3\nThe number is odd

    Please type in a number: 18\nThe number is even

    Please type in a number: -4\nThe number is negative or zero

    With nested conditional statements it is crucial to get the indentations right. Indentations determine which branches are linked together. For example, an if branch and an else branch with the same amount of whitespace are determined to be branches of the same conditional statement.

    The same result can often be achieved using either nested conditional statements or conditions combined with logical operators. The example below is functionally no different from the example above, in the sense that it will print out the exactly same things with the same inputs:

    number = int(input(\"Please type in a number: \"))\n\nif number > 0 and number % 2 == 0:\n    print(\"The number is even\")\nelif number > 0 and number % 2 != 0:\n    print(\"The number is odd\")\nelse:\n    print(\"The number is negative or zero\")

    Neither approach is intrinsically better than the other, but in different situations one or the other may seem more logical. In this particular example most people tend to find the first version with nesting to be more intuitive.

    Generally, any year that is divisible by four is a leap year. However, if the year is additionally divisible by 100, it is a leap year only if it also divisible by 400.

    Please write a program which asks the user for a year, and then prints out whether that year is a leap year or not.

    Some examples:

    Please type in a year: 2011\nThat year is not a leap year.

    Please type in a year: 2020\nThat year is a leap year.

    Please type in a year: 1800\nThat year is not a leap year.

    Please write a program which asks the user for three letters. The program should then print out whichever of the three letters would be in the middle if the letters were in alphabetical order.

    You may assume the letters will be either all uppercase, or all lowercase.

    Some examples of expected behaviour:

    1st letter: x\n2nd letter: c\n3rd letter: p\nThe letter in the middle is p

    1st letter: C\n2nd letter: B\n3rd letter: A\nThe letter in the middle is B

    Some say paying taxes makes Finns happy, so let's see if the secret of happiness lies in one of the taxes set out in Finnish tax code.

    According to the Finnish Tax Administration, a gift is a transfer of property to another person against no compensation or payment. If the total value of the gifts you receive from the same donor in the course of 3 years is €5,000 or more, you must pay gift tax.

    When the gift is received from a close relative or a family member, the amount of tax to be paid is determined by the following table, which is also available on this website:

    \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\n\n
    Value of giftTax at the lower limitTax rate for the exceeding part (%)
    5 000 — 25 0001008
    25 000 — 55 0001 70010
    55 000 — 200 0004 70012
    200 000 — 1 000 00022 10015
    1 000 000 —142 10017

    So, for a gift of 6 000 euros the recipient pays a tax of 180 euros (100 + (6 000 - 5 000) * 0.08). Similarly, for a gift of 75 000 euros the recipient pays a tax of 7 100 euros (4 700 + (75 000 - 55 000) * 0.12).

    Please write a program which calculates the correct amount of tax for a gift from a close relative. Have a look at the examples below to see what is expected. Notice the lack of thousands separators in the input values - you may assume there will be no spaces or other thousands separators in the numbers in the input, as we haven't yet covered dealing with these.

    Value of gift: 3500\nNo tax!

    Value of gift: 5000\nAmount of tax: 100.0 euros

    Value of gift: 27500\nAmount of tax: 1950.0 euros

    ","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-2/3-combining-conditions.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-2/4-simple-loops/page-data.json b/page-data/part-2/4-simple-loops/page-data.json index 5a661331b..1390443da 100644 --- a/page-data/part-2/4-simple-loops/page-data.json +++ b/page-data/part-2/4-simple-loops/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-2/4-simple-loops", - "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 know what a loop means in programming"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while True"}]},{"type":"text","value":" loop in your programs"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":" command to break out of a loop"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We have now covered conditional structures in some detail. Another central technique in programming is repetition, or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"iteration"}]},{"type":"text","value":". Together these form the fundamental control structures any programmer must master. They are called control structures because essentially they allow you to control which lines of code get executed when. While conditional structures allow you to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"choose between"}]},{"type":"text","value":" sections of code, iteration structures allow you to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"repeat"}]},{"type":"text","value":" sections of code. They are often called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"loops"}]},{"type":"text","value":" because they allow the program to \"loop back\" to some line that was already executed before. The process of executing one repetition of a loop is also referred to as an iteration of the loop."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This section introduces a simple "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop. Its structure is similar to the conditional statements we already covered. In the next part we will delve into some more sophisticated examples."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at a program which asks the user to type in a number and then prints out the number squared. This continues until the user types in -1."}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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"]},"children":[{"type":"text","value":"\"Please type in a number, -1 to quit: \""}]},{"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":"if"}]},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"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":"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":"2"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Thanks and bye!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Running the program could look like this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number, -1 to quit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n4\nPlease type in a number, -1 to quit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n16\nPlease type in a number, -1 to quit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n100\nPlease type in a number, -1 to quit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nThanks and bye!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, the program asks for several numbers, thanks to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" statement in the program. When the user types in -1, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":" command is executed, which exits the loop and execution continues from the first line after the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" block."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With loops, it is crucial that there is always a way to exit the loop at some point in the code, otherwise the repetition could go on forever. To illustrate this, let's change the above example a little:"}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number, -1 to quit: \""}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"if"}]},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"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":"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":"2"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Thanks and bye!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this version the program asks the user to type in a number "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"outside the loop"}]},{"type":"text","value":". If the user types in any other number than -1, the loop is never exited from. This forms an "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"infinite loop"}]},{"type":"text","value":", which means the block of code within the loop is repeated endlessly:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number, -1 to quit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n4\n4\n4\n4\n4\n4\n4\n4\n(continued ad infinitum...)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program has a similar structure to the example above the infinite loop, but the user experience is quite different. This program allows the user to proceed only if they type in the correct PIN "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":":"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n code "},{"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 your PIN: \""}]},{"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":"if"}]},{"type":"text","value":" code "},{"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":"\"1234\""}]},{"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":"break"}]},{"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":"\"Incorrect...try again\""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Correct PIN entered!\""}]},{"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 your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0000"}]},{"type":"text","value":"\nIncorrect...try again\nPlease type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9999"}]},{"type":"text","value":"\nIncorrect...try again\nPlease type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":"\nCorrect PIN entered!"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Shall we continue?","tmcname":"part02-15_shall_we_continue"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's create a program along the lines of the example above. This program should print out the message \"hi\" and then ask \"Shall we continue?\" until the user inputs \"no\". Then the program should print out \"okay then\" and finish. Please have a look at the example below."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"hi\nShall we continue? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"yes"}]},{"type":"text","value":"\nhi\nShall we continue? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"oui"}]},{"type":"text","value":"\nhi\nShall we continue? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jawohl"}]},{"type":"text","value":"\nhi\nShall we continue? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"no"}]},{"type":"text","value":"\nokay then"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Input validation","tmcname":"part02-16_input_validation"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for integer numbers."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the number is below zero, the program should print out the message \"Invalid number\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the number is above zero, the program should print out the square root of the number using the Python "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":" function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In either case, the program should then ask for another number."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the user inputs the number zero, the program should stop asking for numbers and exit the loop."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Below you'll find a reminder of how the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":" function is used. Remember to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" it in the beginning of the program."}]},{"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":"# sqrt function will not work without this line in the beginning of the program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" math "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" sqrt\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":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"3.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected behaviour of your program:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"16"}]},{"type":"text","value":"\n4.0\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n2.0\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-3"}]},{"type":"text","value":"\nInvalid number\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n1.0\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nExiting..."}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fix the code: Countdown","tmcname":"part02-17_countdown"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program should print out a countdown. The code is as follows:"}]},{"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":"5"}]},{"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":"\"Countdown!\""}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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 number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"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":"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":"break"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Now!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This should print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Countdown!\n5\n4\n3\n2\n1\nNow!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"However, the program doesn't quite work. Please fix it."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Repeat password","tmcname":"part02-18_repeat_password"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a password. The program should then ask the user to type in the password again. If the user types in something else than the first password, the program should keep on asking until the user types the first password again correctly."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Have a look at the expected behaviour below:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Password: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sekred"}]},{"type":"text","value":"\nRepeat password: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"secret"}]},{"type":"text","value":"\nThey do not match!\nRepeat password: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"cantremember"}]},{"type":"text","value":"\nThey do not match!\nRepeat password: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sekred"}]},{"type":"text","value":"\nUser account created!"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"loops-and-helper-variables","style":"position:relative;"},"children":[{"type":"text","value":"Loops and helper variables"},{"type":"element","tagName":"a","properties":{"href":"#loops-and-helper-variables","ariaLabel":"loops and helper variables 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":"Let's make the PIN checking example a bit more realistic. This version gives the user only three attempts at typing in a PIN."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program uses two helper variables. The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"attempts"}]},{"type":"text","value":" keeps track of how many times the user has typed in a PIN. The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"success"}]},{"type":"text","value":" is set to either "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" based on whether the user is successful in signing in."}]},{"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":"attempts "},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n code "},{"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 your PIN: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n attempts "},{"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","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" code "},{"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":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n success "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" attempts "},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n success "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# this is printed if the code was incorrect AND there have been less than three attempts"}]},{"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":"\"Incorrect...try again\""}]},{"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":"if"}]},{"type":"text","value":" success"},{"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":"\"Correct PIN entered!\""}]},{"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":"else"}]},{"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":"\"Too many attempts...\""}]},{"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 your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0000"}]},{"type":"text","value":"\nIncorrect...try again\nPlease type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":"\nCorrect PIN entered!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0000"}]},{"type":"text","value":"\nIncorrect...try again\nPlease type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9999"}]},{"type":"text","value":"\nIncorrect...try again\nPlease type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4321"}]},{"type":"text","value":"\nToo many attempts..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The loop is exited "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"either"}]},{"type":"text","value":" when the user types the correct PIN "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"or"}]},{"type":"text","value":" if there have been too many attempts. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statement after the loop checks the value of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"success"}]},{"type":"text","value":" and prints out a message accordingly."}]},{"type":"element","tagName":"h2","properties":{"id":"debugging-print-statements-in-loops","style":"position:relative;"},"children":[{"type":"text","value":"Debugging print statements in loops"},{"type":"element","tagName":"a","properties":{"href":"#debugging-print-statements-in-loops","ariaLabel":"debugging print statements in loops 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":"Adding loops to programs also adds to the potential sources of bugs. It becomes even more important to master the use of debugging print statements as introduced in the "},{"type":"element","tagName":"a","properties":{"href":"/part-2/1-programming-terminology"},"children":[{"type":"text","value":"first section of this part"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at a program almost identical to the previous example, but with one crucial difference:"}]},{"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":"attempts "},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n code "},{"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 your PIN: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n attempts "},{"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","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" attempts "},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n success "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" code "},{"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":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n success "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Incorrect...try again\""}]},{"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":"if"}]},{"type":"text","value":" success"},{"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":"\"Correct PIN entered!\""}]},{"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":"else"}]},{"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":"\"Too many attempts...\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This version acts strangely when the user types in the correct code on the third attempt:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0000"}]},{"type":"text","value":"\nIncorrect...try again\nPlease type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9999"}]},{"type":"text","value":"\nIncorrect...try again\nPlease type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":"\nToo many attempts..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, let's try and find the cause by adding some strategic debugging print statements inside the loop:"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"\"beginning of the while block:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n code "},{"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 your PIN: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n attempts "},{"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","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":"\"attempts:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" attempts"},{"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":"\"condition1:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" attempts "},{"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":"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":"if"}]},{"type":"text","value":" attempts "},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n success "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"code:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" code"},{"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":"\"condition2:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" code "},{"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":"\"1234\""}]},{"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":"if"}]},{"type":"text","value":" code "},{"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":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n success "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Incorrect...try again\""}]},{"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":"beginning of the while block:\nPlease type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2233"}]},{"type":"text","value":"\nattempts: 1\ncondition1: False\ncode: 2233\ncondition2: False\nIncorrect...try again\nbeginning of the while block:\nPlease type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4545"}]},{"type":"text","value":"\nattempts: 2\ncondition1: False\ncode: 4545\ncondition2: False\nIncorrect...try again\nbeginning of the while block:\nPlease type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":"\nattempts: 3\ncondition1: True\nToo many attempts..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"From the above printouts we can see that during the third iteration of the loop the condition of the first "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statement is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":", and the loop is exited. This iteration never gets to the second "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statement, which checks whether the code was typed in correctly:"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"# ...."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# this block is executed too early"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" attempts "},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n success "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the third iteration never gets this far"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" code "},{"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":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n success "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The order of conditional statements, or of different branches within a conditional statement, is a common cause for bugs, especially in loops. Debugging print statements are often the simplest way of finding their cause."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"PIN and number of attempts","tmcname":"part02-19_pin_and_number_of_attempts"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which keeps asking the user for a PIN code until they type in the correct one, which is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"4321"}]},{"type":"text","value":". The program should then print out the number of times the user tried different codes."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3245"}]},{"type":"text","value":"\nWrong\nPIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":"\nWrong\nPIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0000"}]},{"type":"text","value":"\nWrong\nPIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4321"}]},{"type":"text","value":"\nCorrect! It took you 4 attempts"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the user gets it right on the first try, the program should print out something a bit different:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4321"}]},{"type":"text","value":"\nCorrect! It only took you one single attempt!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"The next leap year","tmcname":"part02-20_next_leap_year"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a year, and prints out the next leap year."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Year: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2023"}]},{"type":"text","value":"\nThe next leap year after 2023 is 2024"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the user inputs a year which is a leap year (such as 2024), the program should print out the following leap year:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Year: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2024"}]},{"type":"text","value":"\nThe next leap year after 2024 is 2028"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"concatenating-strings-with-the--operator","style":"position:relative;"},"children":[{"type":"text","value":"Concatenating strings with the + operator"},{"type":"element","tagName":"a","properties":{"href":"#concatenating-strings-with-the--operator","ariaLabel":"concatenating strings with the operator 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 above example with PIN checking used a helper variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"attempts"}]},{"type":"text","value":" to keep track of how many times the user had tried to type in a code:"}]},{"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":"attempts "},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n code "},{"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 your PIN: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n attempts "},{"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","comment"]},"children":[{"type":"text","value":"# ..."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The variable is set to zero outside the loop, and each iteration increases its value by one."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A similar idea of incrementation works with string variables as well. The program could, for instance, keep track of all the PIN codes the user typed in:"}]},{"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":"\ncodes "},{"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":"text","value":"\nattempts "},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n code "},{"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 your PIN: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n attempts "},{"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 codes "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" code "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The helper variable is initialized to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"an empty string"}]},{"type":"text","value":", that is, a string with no characters in it:"}]},{"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":"codes "},{"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":"p","properties":{},"children":[{"type":"text","value":"With each iteration the string gets longer, as the code the user typed in is added, along with a comma:"}]},{"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":" code "},{"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 your PIN: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n codes "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" code "},{"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":"p","properties":{},"children":[{"type":"text","value":"If the user types in the codes "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"1111 2222 1234"}]},{"type":"text","value":", at the end of the program's execution the value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"codes"}]},{"type":"text","value":" would be"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1111, 2222, 1234,"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Story","tmcname":"part02-21_story"},"children":[{"type":"element","tagName":"h3","properties":{"id":"part-1","style":"position:relative;"},"children":[{"type":"text","value":"Part 1"},{"type":"element","tagName":"a","properties":{"href":"#part-1","ariaLabel":"part 1 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":"Please write a program which keeps asking the user for words. If the user types in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"end"}]},{"type":"text","value":", the program should print out the story the words formed, and finish."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Once"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"upon"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"time"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"there"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"was"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"girl"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"end"}]},{"type":"text","value":"\nOnce upon a time there was a girl"}]}]},{"type":"element","tagName":"h3","properties":{"id":"part-2","style":"position:relative;"},"children":[{"type":"text","value":"Part 2"},{"type":"element","tagName":"a","properties":{"href":"#part-2","ariaLabel":"part 2 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":"Change the program so that the loop ends also if the user types in the same word twice in a row."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"It"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"was"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"dark"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"and"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"stormy"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"night"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"night"}]},{"type":"text","value":"\nIt was a dark and stormy night"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Working with numbers","tmcname":"part02-22_working_with_numbers"},"children":[{"type":"element","tagName":"h3","properties":{"id":"pre-task","style":"position:relative;"},"children":[{"type":"text","value":"Pre-task"},{"type":"element","tagName":"a","properties":{"href":"#pre-task","ariaLabel":"pre task 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":"Please write a program which asks the user for integer numbers. The program should keep asking for numbers until the user types in zero."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in integer numbers. Type in 0 to finish.\nNumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nNumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"22"}]},{"type":"text","value":"\nNumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9"}]},{"type":"text","value":"\nNumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-2"}]},{"type":"text","value":"\nNumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"h3","properties":{"id":"part-1-count","style":"position:relative;"},"children":[{"type":"text","value":"Part 1: Count"},{"type":"element","tagName":"a","properties":{"href":"#part-1-count","ariaLabel":"part 1 count 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":"After reading in the numbers the program should print out how many numbers were typed in. The zero at the end should not be included in the count."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You will need a new variable here to keep track of the numbers typed in."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"... the program asks for numbers\nNumbers typed in 4"}]}]},{"type":"element","tagName":"h3","properties":{"id":"part-2-sum","style":"position:relative;"},"children":[{"type":"text","value":"Part 2: Sum"},{"type":"element","tagName":"a","properties":{"href":"#part-2-sum","ariaLabel":"part 2 sum 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 program should also print out the sum of all the numbers typed in. The zero at the end should not be included in the calculation."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should now print out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"... the program asks for numbers\nNumbers typed in 4\nThe sum of the numbers is 34"}]}]},{"type":"element","tagName":"h3","properties":{"id":"part-3-mean","style":"position:relative;"},"children":[{"type":"text","value":"Part 3: Mean"},{"type":"element","tagName":"a","properties":{"href":"#part-3-mean","ariaLabel":"part 3 mean 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 program should also print out the mean of the numbers. The zero at the end should not be included in the calculation. You may assume the user will always type in at least one valid non-zero number."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"... the program asks for numbers\nNumbers typed in 4\nThe sum of the numbers is 34\nThe mean of the numbers is 8.5"}]}]},{"type":"element","tagName":"h4","properties":{"id":"part-4-positives-and-negatives","style":"position:relative;"},"children":[{"type":"text","value":"Part 4: Positives and negatives"},{"type":"element","tagName":"a","properties":{"href":"#part-4-positives-and-negatives","ariaLabel":"part 4 positives and negatives 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 program should also print out statistics on how many of the numbers were positive and how many were negative. The zero at the end should not be included in the calculation."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"... the program asks for numbers\nNumbers typed in 4\nThe sum of the numbers is 34\nThe mean of the numbers is 8.5\nPositive numbers 3\nNegative numbers 1"}]}]}]},{"type":"comment","value":"\n\nA quiz to review the contents of this section:\n\n\n\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this week's materials."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will know what a loop means in programming
    • \n
    • You will be able to use a while True loop in your programs
    • \n
    • You will know how to use the break command to break out of a loop
    • \n

    We have now covered conditional structures in some detail. Another central technique in programming is repetition, or iteration. Together these form the fundamental control structures any programmer must master. They are called control structures because essentially they allow you to control which lines of code get executed when. While conditional structures allow you to choose between sections of code, iteration structures allow you to repeat sections of code. They are often called loops because they allow the program to \"loop back\" to some line that was already executed before. The process of executing one repetition of a loop is also referred to as an iteration of the loop.

    This section introduces a simple while loop. Its structure is similar to the conditional statements we already covered. In the next part we will delve into some more sophisticated examples.

    Let's have a look at a program which asks the user to type in a number and then prints out the number squared. This continues until the user types in -1.

    while True:\n    number = int(input(\"Please type in a number, -1 to quit: \"))\n\n    if number == -1:\n        break\n\n    print(number ** 2)\n\nprint(\"Thanks and bye!\")

    Running the program could look like this:

    Please type in a number, -1 to quit: 2\n4\nPlease type in a number, -1 to quit: 4\n16\nPlease type in a number, -1 to quit: 10\n100\nPlease type in a number, -1 to quit: -1\nThanks and bye!

    As you can see above, the program asks for several numbers, thanks to the while statement in the program. When the user types in -1, the break command is executed, which exits the loop and execution continues from the first line after the while block.

    With loops, it is crucial that there is always a way to exit the loop at some point in the code, otherwise the repetition could go on forever. To illustrate this, let's change the above example a little:

    number = int(input(\"Please type in a number, -1 to quit: \"))\nwhile True:\n    if number == -1:\n        break\n\n    print(number ** 2)\n\nprint(\"Thanks and bye!\")

    In this version the program asks the user to type in a number outside the loop. If the user types in any other number than -1, the loop is never exited from. This forms an infinite loop, which means the block of code within the loop is repeated endlessly:

    Please type in a number, -1 to quit: 2\n4\n4\n4\n4\n4\n4\n4\n4\n(continued ad infinitum...)

    The following program has a similar structure to the example above the infinite loop, but the user experience is quite different. This program allows the user to proceed only if they type in the correct PIN 1234:

    while True:\n    code = input(\"Please type in your PIN: \")\n    if code == \"1234\":\n        break\n    print(\"Incorrect...try again\")\n\nprint(\"Correct PIN entered!\")

    Please type in your PIN: 0000\nIncorrect...try again\nPlease type in your PIN: 9999\nIncorrect...try again\nPlease type in your PIN: 1234\nCorrect PIN entered!

    Let's create a program along the lines of the example above. This program should print out the message \"hi\" and then ask \"Shall we continue?\" until the user inputs \"no\". Then the program should print out \"okay then\" and finish. Please have a look at the example below.

    hi\nShall we continue? yes\nhi\nShall we continue? oui\nhi\nShall we continue? jawohl\nhi\nShall we continue? no\nokay then

    Please write a program which asks the user for integer numbers.

    If the number is below zero, the program should print out the message \"Invalid number\".

    If the number is above zero, the program should print out the square root of the number using the Python sqrt function.

    In either case, the program should then ask for another number.

    If the user inputs the number zero, the program should stop asking for numbers and exit the loop.

    Below you'll find a reminder of how the sqrt function is used. Remember to import it in the beginning of the program.

    # sqrt function will not work without this line in the beginning of the program\nfrom math import sqrt\n\nprint(sqrt(9))

    3.0

    An example of expected behaviour of your program:

    Please type in a number: 16\n4.0\nPlease type in a number: 4\n2.0\nPlease type in a number: -3\nInvalid number\nPlease type in a number: 1\n1.0\nPlease type in a number: 0\nExiting...

    This program should print out a countdown. The code is as follows:

    number = 5\nprint(\"Countdown!\")\nwhile True:\n  print(number)\n  number = number - 1\n  if number > 0:\n    break\n\nprint(\"Now!\")

    This should print out

    Countdown!\n5\n4\n3\n2\n1\nNow!

    However, the program doesn't quite work. Please fix it.

    Please write a program which asks the user for a password. The program should then ask the user to type in the password again. If the user types in something else than the first password, the program should keep on asking until the user types the first password again correctly.

    Have a look at the expected behaviour below:

    Password: sekred\nRepeat password: secret\nThey do not match!\nRepeat password: cantremember\nThey do not match!\nRepeat password: sekred\nUser account created!

    Loops and helper variables

    Let's make the PIN checking example a bit more realistic. This version gives the user only three attempts at typing in a PIN.

    The program uses two helper variables. The variable attempts keeps track of how many times the user has typed in a PIN. The variable success is set to either True or False based on whether the user is successful in signing in.

    attempts = 0\n\nwhile True:\n    code = input(\"Please type in your PIN: \")\n    attempts += 1\n\n    if code == \"1234\":\n        success = True\n        break\n\n    if attempts == 3:\n        success = False\n        break\n\n    # this is printed if the code was incorrect AND there have been less than three attempts\n    print(\"Incorrect...try again\")\n\nif success:\n    print(\"Correct PIN entered!\")\nelse:\n    print(\"Too many attempts...\")

    Please type in your PIN: 0000\nIncorrect...try again\nPlease type in your PIN: 1234\nCorrect PIN entered!

    Please type in your PIN: 0000\nIncorrect...try again\nPlease type in your PIN: 9999\nIncorrect...try again\nPlease type in your PIN: 4321\nToo many attempts...

    The loop is exited either when the user types the correct PIN or if there have been too many attempts. The if statement after the loop checks the value of the variable success and prints out a message accordingly.

    Debugging print statements in loops

    Adding loops to programs also adds to the potential sources of bugs. It becomes even more important to master the use of debugging print statements as introduced in the first section of this part.

    Let's have a look at a program almost identical to the previous example, but with one crucial difference:

    attempts = 0\n\nwhile True:\n    code = input(\"Please type in your PIN: \")\n    attempts += 1\n\n    if attempts == 3:\n        success = False\n        break\n\n    if code == \"1234\":\n        success = True\n        break\n\n    print(\"Incorrect...try again\")\n\nif success:\n    print(\"Correct PIN entered!\")\nelse:\n    print(\"Too many attempts...\")

    This version acts strangely when the user types in the correct code on the third attempt:

    Please type in your PIN: 0000\nIncorrect...try again\nPlease type in your PIN: 9999\nIncorrect...try again\nPlease type in your PIN: 1234\nToo many attempts...

    So, let's try and find the cause by adding some strategic debugging print statements inside the loop:

    while True:\n    print(\"beginning of the while block:\")\n    code = input(\"Please type in your PIN: \")\n    attempts += 1\n\n    print(\"attempts:\", attempts)\n    print(\"condition1:\", attempts == 3)\n    if attempts == 3:\n        success = False\n        break\n\n    print(\"code:\", code)\n    print(\"condition2:\", code == \"1234\")\n    if code == \"1234\":\n        success = True\n        break\n\n    print(\"Incorrect...try again\")

    beginning of the while block:\nPlease type in your PIN: 2233\nattempts: 1\ncondition1: False\ncode: 2233\ncondition2: False\nIncorrect...try again\nbeginning of the while block:\nPlease type in your PIN: 4545\nattempts: 2\ncondition1: False\ncode: 4545\ncondition2: False\nIncorrect...try again\nbeginning of the while block:\nPlease type in your PIN: 1234\nattempts: 3\ncondition1: True\nToo many attempts...

    From the above printouts we can see that during the third iteration of the loop the condition of the first if statement is True, and the loop is exited. This iteration never gets to the second if statement, which checks whether the code was typed in correctly:

      while True:\n    # ....\n\n    # this block is executed too early\n    if attempts == 3:\n        success = False\n        break\n\n    # the third iteration never gets this far\n    if code == \"1234\":\n        success = True\n        break

    The order of conditional statements, or of different branches within a conditional statement, is a common cause for bugs, especially in loops. Debugging print statements are often the simplest way of finding their cause.

    Please write a program which keeps asking the user for a PIN code until they type in the correct one, which is 4321. The program should then print out the number of times the user tried different codes.

    PIN: 3245\nWrong\nPIN: 1234\nWrong\nPIN: 0000\nWrong\nPIN: 4321\nCorrect! It took you 4 attempts

    If the user gets it right on the first try, the program should print out something a bit different:

    PIN: 4321\nCorrect! It only took you one single attempt!

    Please write a program which asks the user for a year, and prints out the next leap year.

    Year: 2023\nThe next leap year after 2023 is 2024

    If the user inputs a year which is a leap year (such as 2024), the program should print out the following leap year:

    Year: 2024\nThe next leap year after 2024 is 2028

    Concatenating strings with the + operator

    The above example with PIN checking used a helper variable attempts to keep track of how many times the user had tried to type in a code:

    attempts = 0\n\nwhile True:\n    code = input(\"Please type in your PIN: \")\n    attempts += 1\n    # ...

    The variable is set to zero outside the loop, and each iteration increases its value by one.

    A similar idea of incrementation works with string variables as well. The program could, for instance, keep track of all the PIN codes the user typed in:

    \ncodes = \"\"\nattempts = 0\n\nwhile True:\n    code = input(\"Please type in your PIN: \")\n    attempts += 1\n    codes += code + \", \"\n    # ...

    The helper variable is initialized to an empty string, that is, a string with no characters in it:

    codes = \"\"

    With each iteration the string gets longer, as the code the user typed in is added, along with a comma:

        code = input(\"Please type in your PIN: \")\n    codes += code + \", \"

    If the user types in the codes 1111 2222 1234, at the end of the program's execution the value of codes would be

    1111, 2222, 1234,

    Part 1

    Please write a program which keeps asking the user for words. If the user types in end, the program should print out the story the words formed, and finish.

    Please type in a word: Once\nPlease type in a word: upon\nPlease type in a word: a\nPlease type in a word: time\nPlease type in a word: there\nPlease type in a word: was\nPlease type in a word: a\nPlease type in a word: girl\nPlease type in a word: end\nOnce upon a time there was a girl

    Part 2

    Change the program so that the loop ends also if the user types in the same word twice in a row.

    Please type in a word: It\nPlease type in a word: was\nPlease type in a word: a\nPlease type in a word: dark\nPlease type in a word: and\nPlease type in a word: stormy\nPlease type in a word: night\nPlease type in a word: night\nIt was a dark and stormy night

    Pre-task

    Please write a program which asks the user for integer numbers. The program should keep asking for numbers until the user types in zero.

    Please type in integer numbers. Type in 0 to finish.\nNumber: 5\nNumber: 22\nNumber: 9\nNumber: -2\nNumber: 0

    Part 1: Count

    After reading in the numbers the program should print out how many numbers were typed in. The zero at the end should not be included in the count.

    You will need a new variable here to keep track of the numbers typed in.

    ... the program asks for numbers\nNumbers typed in 4

    Part 2: Sum

    The program should also print out the sum of all the numbers typed in. The zero at the end should not be included in the calculation.

    The program should now print out the following:

    ... the program asks for numbers\nNumbers typed in 4\nThe sum of the numbers is 34

    Part 3: Mean

    The program should also print out the mean of the numbers. The zero at the end should not be included in the calculation. You may assume the user will always type in at least one valid non-zero number.

    ... the program asks for numbers\nNumbers typed in 4\nThe sum of the numbers is 34\nThe mean of the numbers is 8.5

    Part 4: Positives and negatives

    The program should also print out statistics on how many of the numbers were positive and how many were negative. The zero at the end should not be included in the calculation.

    ... the program asks for numbers\nNumbers typed in 4\nThe sum of the numbers is 34\nThe mean of the numbers is 8.5\nPositive numbers 3\nNegative numbers 1

    Please respond to a quick questionnaire on this week's materials.

    ","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-2/4-simple-loops.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know what a loop means in programming"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while True"}]},{"type":"text","value":" loop in your programs"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":" command to break out of a loop"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We have now covered conditional structures in some detail. Another central technique in programming is repetition, or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"iteration"}]},{"type":"text","value":". Together these form the fundamental control structures any programmer must master. They are called control structures because essentially they allow you to control which lines of code get executed when. While conditional structures allow you to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"choose between"}]},{"type":"text","value":" sections of code, iteration structures allow you to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"repeat"}]},{"type":"text","value":" sections of code. They are often called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"loops"}]},{"type":"text","value":" because they allow the program to \"loop back\" to some line that was already executed before. The process of executing one repetition of a loop is also referred to as an iteration of the loop."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This section introduces a simple "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop. Its structure is similar to the conditional statements we already covered. In the next part we will delve into some more sophisticated examples."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at a program which asks the user to type in a number and then prints out the number squared. This continues until the user types in -1."}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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"]},"children":[{"type":"text","value":"\"Please type in a number, -1 to quit: \""}]},{"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":"if"}]},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"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":"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":"2"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Thanks and bye!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Running the program could look like this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number, -1 to quit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n4\nPlease type in a number, -1 to quit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n16\nPlease type in a number, -1 to quit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n100\nPlease type in a number, -1 to quit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nThanks and bye!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, the program asks for several numbers, thanks to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" statement in the program. When the user types in -1, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":" command is executed, which exits the loop and execution continues from the first line after the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" block."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With loops, it is crucial that there is always a way to exit the loop at some point in the code, otherwise the repetition could go on forever. To illustrate this, let's change the above example a little:"}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number, -1 to quit: \""}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"if"}]},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"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":"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":"2"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Thanks and bye!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this version the program asks the user to type in a number "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"outside the loop"}]},{"type":"text","value":". If the user types in any other number than -1, the loop is never exited from. This forms an "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"infinite loop"}]},{"type":"text","value":", which means the block of code within the loop is repeated endlessly:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number, -1 to quit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n4\n4\n4\n4\n4\n4\n4\n4\n(continued ad infinitum...)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program has a similar structure to the example above the infinite loop, but the user experience is quite different. This program allows the user to proceed only if they type in the correct PIN "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":":"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n code "},{"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 your PIN: \""}]},{"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":"if"}]},{"type":"text","value":" code "},{"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":"\"1234\""}]},{"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":"break"}]},{"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":"\"Incorrect...try again\""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Correct PIN entered!\""}]},{"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 your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0000"}]},{"type":"text","value":"\nIncorrect...try again\nPlease type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9999"}]},{"type":"text","value":"\nIncorrect...try again\nPlease type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":"\nCorrect PIN entered!"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Shall we continue?","tmcname":"part02-15_shall_we_continue"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's create a program along the lines of the example above. This program should print out the message \"hi\" and then ask \"Shall we continue?\" until the user inputs \"no\". Then the program should print out \"okay then\" and finish. Please have a look at the example below."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"hi\nShall we continue? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"yes"}]},{"type":"text","value":"\nhi\nShall we continue? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"oui"}]},{"type":"text","value":"\nhi\nShall we continue? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"jawohl"}]},{"type":"text","value":"\nhi\nShall we continue? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"no"}]},{"type":"text","value":"\nokay then"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Input validation","tmcname":"part02-16_input_validation"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for integer numbers."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the number is below zero, the program should print out the message \"Invalid number\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the number is above zero, the program should print out the square root of the number using the Python "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":" function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In either case, the program should then ask for another number."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the user inputs the number zero, the program should stop asking for numbers and exit the loop."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Below you'll find a reminder of how the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":" function is used. Remember to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" it in the beginning of the program."}]},{"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":"# sqrt function will not work without this line in the beginning of the program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" math "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" sqrt\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":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"3.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected behaviour of your program:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"16"}]},{"type":"text","value":"\n4.0\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n2.0\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-3"}]},{"type":"text","value":"\nInvalid number\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n1.0\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nExiting..."}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fix the code: Countdown","tmcname":"part02-17_countdown"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program should print out a countdown. The code is as follows:"}]},{"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":"5"}]},{"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":"\"Countdown!\""}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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 number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"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":"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":"break"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Now!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This should print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Countdown!\n5\n4\n3\n2\n1\nNow!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"However, the program doesn't quite work. Please fix it."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Repeat password","tmcname":"part02-18_repeat_password"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a password. The program should then ask the user to type in the password again. If the user types in something else than the first password, the program should keep on asking until the user types the first password again correctly."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Have a look at the expected behaviour below:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Password: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sekred"}]},{"type":"text","value":"\nRepeat password: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"secret"}]},{"type":"text","value":"\nThey do not match!\nRepeat password: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"cantremember"}]},{"type":"text","value":"\nThey do not match!\nRepeat password: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"sekred"}]},{"type":"text","value":"\nUser account created!"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"loops-and-helper-variables","style":"position:relative;"},"children":[{"type":"text","value":"Loops and helper variables"},{"type":"element","tagName":"a","properties":{"href":"#loops-and-helper-variables","ariaLabel":"loops and helper variables 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":"Let's make the PIN checking example a bit more realistic. This version gives the user only three attempts at typing in a PIN."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program uses two helper variables. The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"attempts"}]},{"type":"text","value":" keeps track of how many times the user has typed in a PIN. The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"success"}]},{"type":"text","value":" is set to either "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" based on whether the user is successful in signing in."}]},{"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":"attempts "},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n code "},{"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 your PIN: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n attempts "},{"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","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" code "},{"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":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n success "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" attempts "},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n success "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# this is printed if the code was incorrect AND there have been less than three attempts"}]},{"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":"\"Incorrect...try again\""}]},{"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":"if"}]},{"type":"text","value":" success"},{"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":"\"Correct PIN entered!\""}]},{"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":"else"}]},{"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":"\"Too many attempts...\""}]},{"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 your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0000"}]},{"type":"text","value":"\nIncorrect...try again\nPlease type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":"\nCorrect PIN entered!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0000"}]},{"type":"text","value":"\nIncorrect...try again\nPlease type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9999"}]},{"type":"text","value":"\nIncorrect...try again\nPlease type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4321"}]},{"type":"text","value":"\nToo many attempts..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The loop is exited "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"either"}]},{"type":"text","value":" when the user types the correct PIN "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"or"}]},{"type":"text","value":" if there have been too many attempts. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statement after the loop checks the value of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"success"}]},{"type":"text","value":" and prints out a message accordingly."}]},{"type":"element","tagName":"h2","properties":{"id":"debugging-print-statements-in-loops","style":"position:relative;"},"children":[{"type":"text","value":"Debugging print statements in loops"},{"type":"element","tagName":"a","properties":{"href":"#debugging-print-statements-in-loops","ariaLabel":"debugging print statements in loops 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":"Adding loops to programs also adds to the potential sources of bugs. It becomes even more important to master the use of debugging print statements as introduced in the "},{"type":"element","tagName":"a","properties":{"href":"/part-2/1-programming-terminology"},"children":[{"type":"text","value":"first section of this part"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at a program almost identical to the previous example, but with one crucial difference:"}]},{"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":"attempts "},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n code "},{"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 your PIN: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n attempts "},{"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","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" attempts "},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n success "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" code "},{"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":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n success "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Incorrect...try again\""}]},{"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":"if"}]},{"type":"text","value":" success"},{"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":"\"Correct PIN entered!\""}]},{"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":"else"}]},{"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":"\"Too many attempts...\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This version acts strangely when the user types in the correct code on the third attempt:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0000"}]},{"type":"text","value":"\nIncorrect...try again\nPlease type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9999"}]},{"type":"text","value":"\nIncorrect...try again\nPlease type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":"\nToo many attempts..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, let's try and find the cause by adding some strategic debugging print statements inside the loop:"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"\"beginning of the while block:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n code "},{"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 your PIN: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n attempts "},{"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","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":"\"attempts:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" attempts"},{"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":"\"condition1:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" attempts "},{"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":"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":"if"}]},{"type":"text","value":" attempts "},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n success "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"code:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" code"},{"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":"\"condition2:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" code "},{"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":"\"1234\""}]},{"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":"if"}]},{"type":"text","value":" code "},{"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":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n success "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Incorrect...try again\""}]},{"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":"beginning of the while block:\nPlease type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2233"}]},{"type":"text","value":"\nattempts: 1\ncondition1: False\ncode: 2233\ncondition2: False\nIncorrect...try again\nbeginning of the while block:\nPlease type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4545"}]},{"type":"text","value":"\nattempts: 2\ncondition1: False\ncode: 4545\ncondition2: False\nIncorrect...try again\nbeginning of the while block:\nPlease type in your PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":"\nattempts: 3\ncondition1: True\nToo many attempts..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"From the above printouts we can see that during the third iteration of the loop the condition of the first "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statement is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":", and the loop is exited. This iteration never gets to the second "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statement, which checks whether the code was typed in correctly:"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"# ...."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# this block is executed too early"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" attempts "},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n success "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the third iteration never gets this far"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" code "},{"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":"\"1234\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n success "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The order of conditional statements, or of different branches within a conditional statement, is a common cause for bugs, especially in loops. Debugging print statements are often the simplest way of finding their cause."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"PIN and number of attempts","tmcname":"part02-19_pin_and_number_of_attempts"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which keeps asking the user for a PIN code until they type in the correct one, which is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"4321"}]},{"type":"text","value":". The program should then print out the number of times the user tried different codes."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3245"}]},{"type":"text","value":"\nWrong\nPIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234"}]},{"type":"text","value":"\nWrong\nPIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0000"}]},{"type":"text","value":"\nWrong\nPIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4321"}]},{"type":"text","value":"\nCorrect! It took you 4 attempts"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the user gets it right on the first try, the program should print out something a bit different:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"PIN: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4321"}]},{"type":"text","value":"\nCorrect! It only took you one single attempt!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"The next leap year","tmcname":"part02-20_next_leap_year"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a year, and prints out the next leap year."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Year: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2023"}]},{"type":"text","value":"\nThe next leap year after 2023 is 2024"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the user inputs a year which is a leap year (such as 2024), the program should print out the following leap year:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Year: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2024"}]},{"type":"text","value":"\nThe next leap year after 2024 is 2028"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"concatenating-strings-with-the--operator","style":"position:relative;"},"children":[{"type":"text","value":"Concatenating strings with the + operator"},{"type":"element","tagName":"a","properties":{"href":"#concatenating-strings-with-the--operator","ariaLabel":"concatenating strings with the operator 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 above example with PIN checking used a helper variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"attempts"}]},{"type":"text","value":" to keep track of how many times the user had tried to type in a code:"}]},{"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":"attempts "},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n code "},{"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 your PIN: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n attempts "},{"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","comment"]},"children":[{"type":"text","value":"# ..."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The variable is set to zero outside the loop, and each iteration increases its value by one."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A similar idea of incrementation works with string variables as well. The program could, for instance, keep track of all the PIN codes the user typed in:"}]},{"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":"\ncodes "},{"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":"text","value":"\nattempts "},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n code "},{"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 your PIN: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n attempts "},{"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 codes "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" code "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ..."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The helper variable is initialized to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"an empty string"}]},{"type":"text","value":", that is, a string with no characters in it:"}]},{"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":"codes "},{"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":"p","properties":{},"children":[{"type":"text","value":"With each iteration the string gets longer, as the code the user typed in is added, along with a comma:"}]},{"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":" code "},{"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 your PIN: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n codes "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" code "},{"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":"p","properties":{},"children":[{"type":"text","value":"If the user types in the codes "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"1111 2222 1234"}]},{"type":"text","value":", at the end of the program's execution the value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"codes"}]},{"type":"text","value":" would be"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1111, 2222, 1234,"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Story","tmcname":"part02-21_story"},"children":[{"type":"element","tagName":"h3","properties":{"id":"part-1","style":"position:relative;"},"children":[{"type":"text","value":"Part 1"},{"type":"element","tagName":"a","properties":{"href":"#part-1","ariaLabel":"part 1 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":"Please write a program which keeps asking the user for words. If the user types in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"end"}]},{"type":"text","value":", the program should print out the story the words formed, and finish."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Once"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"upon"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"time"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"there"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"was"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"girl"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"end"}]},{"type":"text","value":"\nOnce upon a time there was a girl"}]}]},{"type":"element","tagName":"h3","properties":{"id":"part-2","style":"position:relative;"},"children":[{"type":"text","value":"Part 2"},{"type":"element","tagName":"a","properties":{"href":"#part-2","ariaLabel":"part 2 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":"Change the program so that the loop ends also if the user types in the same word twice in a row."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"It"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"was"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"dark"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"and"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"stormy"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"night"}]},{"type":"text","value":"\nPlease type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"night"}]},{"type":"text","value":"\nIt was a dark and stormy night"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Working with numbers","tmcname":"part02-22_working_with_numbers"},"children":[{"type":"element","tagName":"h3","properties":{"id":"pre-task","style":"position:relative;"},"children":[{"type":"text","value":"Pre-task"},{"type":"element","tagName":"a","properties":{"href":"#pre-task","ariaLabel":"pre task 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":"Please write a program which asks the user for integer numbers. The program should keep asking for numbers until the user types in zero."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in integer numbers. Type in 0 to finish.\nNumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nNumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"22"}]},{"type":"text","value":"\nNumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9"}]},{"type":"text","value":"\nNumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-2"}]},{"type":"text","value":"\nNumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]}]}]},{"type":"element","tagName":"h3","properties":{"id":"part-1-count","style":"position:relative;"},"children":[{"type":"text","value":"Part 1: Count"},{"type":"element","tagName":"a","properties":{"href":"#part-1-count","ariaLabel":"part 1 count 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":"After reading in the numbers the program should print out how many numbers were typed in. The zero at the end should not be included in the count."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You will need a new variable here to keep track of the numbers typed in."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"... the program asks for numbers\nNumbers typed in 4"}]}]},{"type":"element","tagName":"h3","properties":{"id":"part-2-sum","style":"position:relative;"},"children":[{"type":"text","value":"Part 2: Sum"},{"type":"element","tagName":"a","properties":{"href":"#part-2-sum","ariaLabel":"part 2 sum 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 program should also print out the sum of all the numbers typed in. The zero at the end should not be included in the calculation."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should now print out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"... the program asks for numbers\nNumbers typed in 4\nThe sum of the numbers is 34"}]}]},{"type":"element","tagName":"h3","properties":{"id":"part-3-mean","style":"position:relative;"},"children":[{"type":"text","value":"Part 3: Mean"},{"type":"element","tagName":"a","properties":{"href":"#part-3-mean","ariaLabel":"part 3 mean 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 program should also print out the mean of the numbers. The zero at the end should not be included in the calculation. You may assume the user will always type in at least one valid non-zero number."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"... the program asks for numbers\nNumbers typed in 4\nThe sum of the numbers is 34\nThe mean of the numbers is 8.5"}]}]},{"type":"element","tagName":"h4","properties":{"id":"part-4-positives-and-negatives","style":"position:relative;"},"children":[{"type":"text","value":"Part 4: Positives and negatives"},{"type":"element","tagName":"a","properties":{"href":"#part-4-positives-and-negatives","ariaLabel":"part 4 positives and negatives 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 program should also print out statistics on how many of the numbers were positive and how many were negative. The zero at the end should not be included in the calculation."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"... the program asks for numbers\nNumbers typed in 4\nThe sum of the numbers is 34\nThe mean of the numbers is 8.5\nPositive numbers 3\nNegative numbers 1"}]}]}]},{"type":"comment","value":"\n\nA quiz to review the contents of this section:\n\n\n\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this week's materials."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will know what a loop means in programming
    • \n
    • You will be able to use a while True loop in your programs
    • \n
    • You will know how to use the break command to break out of a loop
    • \n

    We have now covered conditional structures in some detail. Another central technique in programming is repetition, or iteration. Together these form the fundamental control structures any programmer must master. They are called control structures because essentially they allow you to control which lines of code get executed when. While conditional structures allow you to choose between sections of code, iteration structures allow you to repeat sections of code. They are often called loops because they allow the program to \"loop back\" to some line that was already executed before. The process of executing one repetition of a loop is also referred to as an iteration of the loop.

    This section introduces a simple while loop. Its structure is similar to the conditional statements we already covered. In the next part we will delve into some more sophisticated examples.

    Let's have a look at a program which asks the user to type in a number and then prints out the number squared. This continues until the user types in -1.

    while True:\n    number = int(input(\"Please type in a number, -1 to quit: \"))\n\n    if number == -1:\n        break\n\n    print(number ** 2)\n\nprint(\"Thanks and bye!\")

    Running the program could look like this:

    Please type in a number, -1 to quit: 2\n4\nPlease type in a number, -1 to quit: 4\n16\nPlease type in a number, -1 to quit: 10\n100\nPlease type in a number, -1 to quit: -1\nThanks and bye!

    As you can see above, the program asks for several numbers, thanks to the while statement in the program. When the user types in -1, the break command is executed, which exits the loop and execution continues from the first line after the while block.

    With loops, it is crucial that there is always a way to exit the loop at some point in the code, otherwise the repetition could go on forever. To illustrate this, let's change the above example a little:

    number = int(input(\"Please type in a number, -1 to quit: \"))\nwhile True:\n    if number == -1:\n        break\n\n    print(number ** 2)\n\nprint(\"Thanks and bye!\")

    In this version the program asks the user to type in a number outside the loop. If the user types in any other number than -1, the loop is never exited from. This forms an infinite loop, which means the block of code within the loop is repeated endlessly:

    Please type in a number, -1 to quit: 2\n4\n4\n4\n4\n4\n4\n4\n4\n(continued ad infinitum...)

    The following program has a similar structure to the example above the infinite loop, but the user experience is quite different. This program allows the user to proceed only if they type in the correct PIN 1234:

    while True:\n    code = input(\"Please type in your PIN: \")\n    if code == \"1234\":\n        break\n    print(\"Incorrect...try again\")\n\nprint(\"Correct PIN entered!\")

    Please type in your PIN: 0000\nIncorrect...try again\nPlease type in your PIN: 9999\nIncorrect...try again\nPlease type in your PIN: 1234\nCorrect PIN entered!

    Let's create a program along the lines of the example above. This program should print out the message \"hi\" and then ask \"Shall we continue?\" until the user inputs \"no\". Then the program should print out \"okay then\" and finish. Please have a look at the example below.

    hi\nShall we continue? yes\nhi\nShall we continue? oui\nhi\nShall we continue? jawohl\nhi\nShall we continue? no\nokay then

    Please write a program which asks the user for integer numbers.

    If the number is below zero, the program should print out the message \"Invalid number\".

    If the number is above zero, the program should print out the square root of the number using the Python sqrt function.

    In either case, the program should then ask for another number.

    If the user inputs the number zero, the program should stop asking for numbers and exit the loop.

    Below you'll find a reminder of how the sqrt function is used. Remember to import it in the beginning of the program.

    # sqrt function will not work without this line in the beginning of the program\nfrom math import sqrt\n\nprint(sqrt(9))

    3.0

    An example of expected behaviour of your program:

    Please type in a number: 16\n4.0\nPlease type in a number: 4\n2.0\nPlease type in a number: -3\nInvalid number\nPlease type in a number: 1\n1.0\nPlease type in a number: 0\nExiting...

    This program should print out a countdown. The code is as follows:

    number = 5\nprint(\"Countdown!\")\nwhile True:\n  print(number)\n  number = number - 1\n  if number > 0:\n    break\n\nprint(\"Now!\")

    This should print out

    Countdown!\n5\n4\n3\n2\n1\nNow!

    However, the program doesn't quite work. Please fix it.

    Please write a program which asks the user for a password. The program should then ask the user to type in the password again. If the user types in something else than the first password, the program should keep on asking until the user types the first password again correctly.

    Have a look at the expected behaviour below:

    Password: sekred\nRepeat password: secret\nThey do not match!\nRepeat password: cantremember\nThey do not match!\nRepeat password: sekred\nUser account created!

    Loops and helper variables

    Let's make the PIN checking example a bit more realistic. This version gives the user only three attempts at typing in a PIN.

    The program uses two helper variables. The variable attempts keeps track of how many times the user has typed in a PIN. The variable success is set to either True or False based on whether the user is successful in signing in.

    attempts = 0\n\nwhile True:\n    code = input(\"Please type in your PIN: \")\n    attempts += 1\n\n    if code == \"1234\":\n        success = True\n        break\n\n    if attempts == 3:\n        success = False\n        break\n\n    # this is printed if the code was incorrect AND there have been less than three attempts\n    print(\"Incorrect...try again\")\n\nif success:\n    print(\"Correct PIN entered!\")\nelse:\n    print(\"Too many attempts...\")

    Please type in your PIN: 0000\nIncorrect...try again\nPlease type in your PIN: 1234\nCorrect PIN entered!

    Please type in your PIN: 0000\nIncorrect...try again\nPlease type in your PIN: 9999\nIncorrect...try again\nPlease type in your PIN: 4321\nToo many attempts...

    The loop is exited either when the user types the correct PIN or if there have been too many attempts. The if statement after the loop checks the value of the variable success and prints out a message accordingly.

    Debugging print statements in loops

    Adding loops to programs also adds to the potential sources of bugs. It becomes even more important to master the use of debugging print statements as introduced in the first section of this part.

    Let's have a look at a program almost identical to the previous example, but with one crucial difference:

    attempts = 0\n\nwhile True:\n    code = input(\"Please type in your PIN: \")\n    attempts += 1\n\n    if attempts == 3:\n        success = False\n        break\n\n    if code == \"1234\":\n        success = True\n        break\n\n    print(\"Incorrect...try again\")\n\nif success:\n    print(\"Correct PIN entered!\")\nelse:\n    print(\"Too many attempts...\")

    This version acts strangely when the user types in the correct code on the third attempt:

    Please type in your PIN: 0000\nIncorrect...try again\nPlease type in your PIN: 9999\nIncorrect...try again\nPlease type in your PIN: 1234\nToo many attempts...

    So, let's try and find the cause by adding some strategic debugging print statements inside the loop:

    while True:\n    print(\"beginning of the while block:\")\n    code = input(\"Please type in your PIN: \")\n    attempts += 1\n\n    print(\"attempts:\", attempts)\n    print(\"condition1:\", attempts == 3)\n    if attempts == 3:\n        success = False\n        break\n\n    print(\"code:\", code)\n    print(\"condition2:\", code == \"1234\")\n    if code == \"1234\":\n        success = True\n        break\n\n    print(\"Incorrect...try again\")

    beginning of the while block:\nPlease type in your PIN: 2233\nattempts: 1\ncondition1: False\ncode: 2233\ncondition2: False\nIncorrect...try again\nbeginning of the while block:\nPlease type in your PIN: 4545\nattempts: 2\ncondition1: False\ncode: 4545\ncondition2: False\nIncorrect...try again\nbeginning of the while block:\nPlease type in your PIN: 1234\nattempts: 3\ncondition1: True\nToo many attempts...

    From the above printouts we can see that during the third iteration of the loop the condition of the first if statement is True, and the loop is exited. This iteration never gets to the second if statement, which checks whether the code was typed in correctly:

      while True:\n    # ....\n\n    # this block is executed too early\n    if attempts == 3:\n        success = False\n        break\n\n    # the third iteration never gets this far\n    if code == \"1234\":\n        success = True\n        break

    The order of conditional statements, or of different branches within a conditional statement, is a common cause for bugs, especially in loops. Debugging print statements are often the simplest way of finding their cause.

    Please write a program which keeps asking the user for a PIN code until they type in the correct one, which is 4321. The program should then print out the number of times the user tried different codes.

    PIN: 3245\nWrong\nPIN: 1234\nWrong\nPIN: 0000\nWrong\nPIN: 4321\nCorrect! It took you 4 attempts

    If the user gets it right on the first try, the program should print out something a bit different:

    PIN: 4321\nCorrect! It only took you one single attempt!

    Please write a program which asks the user for a year, and prints out the next leap year.

    Year: 2023\nThe next leap year after 2023 is 2024

    If the user inputs a year which is a leap year (such as 2024), the program should print out the following leap year:

    Year: 2024\nThe next leap year after 2024 is 2028

    Concatenating strings with the + operator

    The above example with PIN checking used a helper variable attempts to keep track of how many times the user had tried to type in a code:

    attempts = 0\n\nwhile True:\n    code = input(\"Please type in your PIN: \")\n    attempts += 1\n    # ...

    The variable is set to zero outside the loop, and each iteration increases its value by one.

    A similar idea of incrementation works with string variables as well. The program could, for instance, keep track of all the PIN codes the user typed in:

    \ncodes = \"\"\nattempts = 0\n\nwhile True:\n    code = input(\"Please type in your PIN: \")\n    attempts += 1\n    codes += code + \", \"\n    # ...

    The helper variable is initialized to an empty string, that is, a string with no characters in it:

    codes = \"\"

    With each iteration the string gets longer, as the code the user typed in is added, along with a comma:

        code = input(\"Please type in your PIN: \")\n    codes += code + \", \"

    If the user types in the codes 1111 2222 1234, at the end of the program's execution the value of codes would be

    1111, 2222, 1234,

    Part 1

    Please write a program which keeps asking the user for words. If the user types in end, the program should print out the story the words formed, and finish.

    Please type in a word: Once\nPlease type in a word: upon\nPlease type in a word: a\nPlease type in a word: time\nPlease type in a word: there\nPlease type in a word: was\nPlease type in a word: a\nPlease type in a word: girl\nPlease type in a word: end\nOnce upon a time there was a girl

    Part 2

    Change the program so that the loop ends also if the user types in the same word twice in a row.

    Please type in a word: It\nPlease type in a word: was\nPlease type in a word: a\nPlease type in a word: dark\nPlease type in a word: and\nPlease type in a word: stormy\nPlease type in a word: night\nPlease type in a word: night\nIt was a dark and stormy night

    Pre-task

    Please write a program which asks the user for integer numbers. The program should keep asking for numbers until the user types in zero.

    Please type in integer numbers. Type in 0 to finish.\nNumber: 5\nNumber: 22\nNumber: 9\nNumber: -2\nNumber: 0

    Part 1: Count

    After reading in the numbers the program should print out how many numbers were typed in. The zero at the end should not be included in the count.

    You will need a new variable here to keep track of the numbers typed in.

    ... the program asks for numbers\nNumbers typed in 4

    Part 2: Sum

    The program should also print out the sum of all the numbers typed in. The zero at the end should not be included in the calculation.

    The program should now print out the following:

    ... the program asks for numbers\nNumbers typed in 4\nThe sum of the numbers is 34

    Part 3: Mean

    The program should also print out the mean of the numbers. The zero at the end should not be included in the calculation. You may assume the user will always type in at least one valid non-zero number.

    ... the program asks for numbers\nNumbers typed in 4\nThe sum of the numbers is 34\nThe mean of the numbers is 8.5

    Part 4: Positives and negatives

    The program should also print out statistics on how many of the numbers were positive and how many were negative. The zero at the end should not be included in the calculation.

    ... the program asks for numbers\nNumbers typed in 4\nThe sum of the numbers is 34\nThe mean of the numbers is 8.5\nPositive numbers 3\nNegative numbers 1

    Please respond to a quick questionnaire on this week's materials.

    ","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-2/4-simple-loops.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-2/page-data.json b/page-data/part-2/page-data.json index be6849259..92ff40e46 100644 --- a/page-data/part-2/page-data.json +++ b/page-data/part-2/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/part-2", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-2","title":"Part 2"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-2/index.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-2","title":"Part 2"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-2/index.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-3/1-loops-with-conditions/page-data.json b/page-data/part-3/1-loops-with-conditions/page-data.json index 8b2f36acc..919bff95e 100644 --- a/page-data/part-3/1-loops-with-conditions/page-data.json +++ b/page-data/part-3/1-loops-with-conditions/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-3/1-loops-with-conditions", - "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 know how to create a while loop with a condition"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what roles initialisation, formulating a condition and updating variables perform in a loop"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to create loops with different kinds of conditions"}]},{"type":"text","value":"\n"}]}]},{"type":"comment","value":"the same text is in sections 3-1, 5-1 and 6-1, check them all if you're changing this"},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"About the exercises on this course"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Becoming a proficient programmer requires a lot of practice, sometimes even quite mechanical practice. It also involves developing problem solving skills and applying intuition. This is why there are a lot of exercises of different kinds on this course. Some of them ask you to quite straightforwardly apply what you have learnt in the material, but some of them are intentionally more challenging and open-ended."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some of the exercises might at first seem overwhelming, but this is nothing to worry about. None of the exercises is strictly mandatory, and in fact "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"you need only 25 % of the points from each part to pass the course."}]},{"type":"text","value":" You can find more details about passing the course on the "},{"type":"element","tagName":"a","properties":{"href":"/grading-and-exams"},"children":[{"type":"text","value":"page on grading"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"The exercises are not in any specific order of difficulty."}]},{"type":"text","value":" Each section usually introduces some new programming concepts, and these are then practised with both simpler and more complicated exercises. "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"If you come across an exercise that feels too difficult, move on to the next one."}]},{"type":"text","value":" You can always come back to the more difficult exercises if you have time later."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the going inevitably gets tough, a word of consolation: a task that seems impossibly difficult this week will likely feel rather easy in about four weeks' time."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the previous section we learnt to use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while True"}]},{"type":"text","value":" loop to repeat sections of code. In that construction the condition of the loop is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":", so the condition is fulfilled every time. We needed to explicitly break out from the loop each time to avoid an infinite loop. For example:"}]},{"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":"# Print numbers until the variable a equals 5"}]},{"type":"text","value":"\na "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a "},{"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":"if"}]},{"type":"text","value":" a "},{"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":"5"}]},{"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":"break"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1\n2\n3\n4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Of course, the condition doesn't always have to be "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":", but instead any Boolean expression can be used as the condition. The general structure of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" statement is as follows:"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"condition"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"block"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The idea here is that the execution goes back and forth, checking if the condition is true and executing the code within the block, over and over again. If the condition at any point is false, execution of the program continues from the line after the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" block."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 550px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/1566349634fe8f4b1710e005c46a3ba3/dd45a/3_1_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: 103.91304347826087%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAVCAYAAABG1c6oAAAACXBIWXMAAAsTAAALEwEAmpwYAAABx0lEQVQ4y5WV6Y4CIRCEef8X02jiHW/9o/G+77s3XydNGJzdVRIygFBdVd2gkw/b6/XS7+12k8vlklgLm4sP2NjmNrY5YLvdLvW3BGAMFK49n0/ttOl0KpPJRMeshaB0lwa2Xq/lfr+/yWGtXq9Lp9PxAd4kx7T5zmYzv+F4PEqz2dS1VqsllUpFqtWqrsF2u93K4XBQG7DDpZm/WCx8AA7BarVaKVi5XJZisags2WeAqCKoezweKoXsmYzlcukBN5uND3a9XmU0GmmQ2BL2c86BmslkJJfLSbvdFgKMx2O/kcjn89nPe72e9Pv9RFLMXwXEl2w2K6VSSWq1mtKfz+eJBMGeOfIajYZ6ud/vE94DiAUqmW6lEXvI+HQ6KUuYW2EzDmVzFnt+TYo1WMEYa2A7HA7VQ7IaAgJG5l1cmHQOxEUOKwqaL4wZh4CUV7fbTS9s/MGG8EoiCYYwho1l335n/6+S04IAGIKF9zmRlL8ehPCASaYWkYynYfA3wP+eLSRRVtRsPp+XQqGgNwYbAML3rwBhRoYt44xhGiv6GNAkGRtuRdpe981rbUzjBzYcf8zQvoPBQOVa5uOA7tv/FJ6xuAbDB/YHPJllO2xxVmIAAAAASUVORK5CYII='); 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":["/static/1566349634fe8f4b1710e005c46a3ba3/a0b58/3_1_1.webp 230w","/static/1566349634fe8f4b1710e005c46a3ba3/bc10c/3_1_1.webp 460w","/static/1566349634fe8f4b1710e005c46a3ba3/12b65/3_1_1.webp 550w"],"sizes":"(max-width: 550px) 100vw, 550px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/1566349634fe8f4b1710e005c46a3ba3/81c8e/3_1_1.png 230w","/static/1566349634fe8f4b1710e005c46a3ba3/08a84/3_1_1.png 460w","/static/1566349634fe8f4b1710e005c46a3ba3/dd45a/3_1_1.png 550w"],"sizes":"(max-width: 550px) 100vw, 550px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/1566349634fe8f4b1710e005c46a3ba3/dd45a/3_1_1.png","alt":"3 1 1","title":"3 1 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":"p","properties":{},"children":[{"type":"text","value":"In the following loop we have the condition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number < 10"}]},{"type":"text","value":". The block within the loop is executed only if the variable number is less than 10."}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"while"}]},{"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":"10"}]},{"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 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":"\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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Execution finished.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This could print out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n4\n5\n6\n7\n8\n9\nExecution finished."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this structure the condition is always checked before the block within the loop is executed. It may happen that the block never gets executed, like so:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"12"}]},{"type":"text","value":"\nExecution finished."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"12 is not less than 10, so the program doesn't print out a single number."}]},{"type":"element","tagName":"h2","properties":{"id":"initialisation-condition-and-update","style":"position:relative;"},"children":[{"type":"text","value":"Initialisation, condition and update"},{"type":"element","tagName":"a","properties":{"href":"#initialisation-condition-and-update","ariaLabel":"initialisation condition and update 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":"To create a loop you'll often need to include three distinct steps: initialisation, condition, and updating the iteration variables."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Initialisation"}]},{"type":"text","value":" refers to setting the initial value(s) of the variable(s) used within the condition of the loop. These are often called the iteration or iterator variables. This is performed before the loop is first entered. The "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"condition"}]},{"type":"text","value":" defines for how long the loop is to be executed. It is set out at the very beginning of the loop. Finally, within each repetition of the loop the variables involved in the condition are "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"updated"}]},{"type":"text","value":", so that each iteration brings the loop one step closer to its conclusion. The following image illustrates these steps:"}]},{"type":"comment","value":"- this is here in case the following image needs to be updated\n```python\n# Ask the user for a number\nnumber = int(input(\"Please type in a number: \"))\n\n# Repeat while the number is less than 10\nwhile number < 10:\n\n # Print out and increment\n print(number)\n number += 1\n\nprint(\"Execution finished.\")\n```\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 642px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/9fc35fe9541e12675e37086df542ad51/1bba8/3_1_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: 43.47826086956522%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAABhElEQVQoz5WRy6oiMRCG+/0fxaUv4NadoIgwR3GhjnPse3c6l6p0f4dkcDgMs5mCn1BF6qs/laJ8VVhrGaeRYRowdsD6iaav6U2PRIfGQJyFoJYQLcsyIyJUVcXz+eR+v+fz8XhQNFWNquRGJwNWerxaJulwOjJJS4gTMvtcS3kCGmM4HA6cz2d2ux3H45H9fk9hXcB7IQQleEUk5tzZQNQZlfinFkIkxoV3LMtC27Z0XZfVNA1FPTjauub584m3Fl+PLPOSp5VlSd3UlFVFWVY5r+oqNybQ7XbLDjebDdvt9rfDeZ6JqsQYSbNFFFXF+4BzHtXI90iuklKke865LO894zhSOC/0k6fvB8rPF8NoENUM+g58gxLrDUxmEiyEkJWgRWp6PH7RtR1D3SAu5MsfPz5om5a+7/Pz/lbaWfrZy+XCarVivV5zOp0oRNPCJ3QOLETirOgsDKYjiM8u/qXkMjlKe017vF6vvF4viskrXidMqDGhw8cJpwYjDSE6loX/ii8OE7QvuGlKpQAAAABJRU5ErkJggg=='); 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":["/static/9fc35fe9541e12675e37086df542ad51/a0b58/3_1_2.webp 230w","/static/9fc35fe9541e12675e37086df542ad51/bc10c/3_1_2.webp 460w","/static/9fc35fe9541e12675e37086df542ad51/7eb86/3_1_2.webp 642w"],"sizes":"(max-width: 642px) 100vw, 642px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9fc35fe9541e12675e37086df542ad51/81c8e/3_1_2.png 230w","/static/9fc35fe9541e12675e37086df542ad51/08a84/3_1_2.png 460w","/static/9fc35fe9541e12675e37086df542ad51/1bba8/3_1_2.png 642w"],"sizes":"(max-width: 642px) 100vw, 642px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/9fc35fe9541e12675e37086df542ad51/1bba8/3_1_2.png","alt":"3 1 2","title":"3 1 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":"If any one of these three components is missing, the loop will likely not function correctly. A typical error is omitting the update 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":"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":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"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":"10"}]},{"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":"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":"\"Execution finished.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here, the value of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" never changes. The program is stuck in an infinite loop, and the exact same bit of code is repeated over and over again until the user stops the execution, for example by pressing "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Control"}]},{"type":"text","value":" + "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"C"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1\n1\n1\n1\n1\n(continued ad infinitum...)"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Print numbers","tmcname":"part03-01_print_numbers"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which prints out all the even numbers between two and thirty, using a loop. Print each number on a separate line."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The beginning of your output should look like this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\n2\n4\n6\n8\netc...\n"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fix the code: Countdown","tmcname":"part03-02_countdown"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program below has some syntactic issues:"}]},{"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":"\"Are you ready?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnumber "},{"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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"while"}]},{"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":"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":"number"},{"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":"\"Now!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please fix it so that it prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Are you ready?\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n5\n4\n3\n2\n1\nNow!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This exercise is similar to the countdown exercise in the last section, but please don't use a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while True"}]},{"type":"text","value":" loop this time round!"}]}]},{"type":"element","tagName":"h2","properties":{"id":"writing-conditions","style":"position:relative;"},"children":[{"type":"text","value":"Writing conditions"},{"type":"element","tagName":"a","properties":{"href":"#writing-conditions","ariaLabel":"writing conditions 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":"Any Boolean expression or combination thereof is a valid condition in a loop. For example, the following program prints out every third number, but only as long as the number is less than 100 and not divisible by 5:"}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"while"}]},{"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":"100"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"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":"5"}]},{"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":"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":"number"},{"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","number"]},"children":[{"type":"text","value":"3"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Two examples of the program's execution with different inputs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"28"}]},{"type":"text","value":"\n28\n31\n34\n37"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"96"}]},{"type":"text","value":"\n96\n99"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the input is 28, the loop ends with the number 37, because the next number is 40, which is divisible by 5. When the input is 96, the loop ends with the number 99, because the next number is 102, which is not less than 100."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Whenever you write a loop you should make sure that the execution of the loop will always end at some point. The following program either finishes or doesn't, depending on the input:"}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"while"}]},{"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":"10"}]},{"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 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":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the input is an even number and equals 10 or less, the loop will terminate:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n4\n6\n8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In any other case the loop gets executed endlessly, as there is no way the variable could then ever equal 10. For example 3 or 12 are inputs that would end in an infinite loop."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Numbers","tmcname":"part03-03_numbers"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a number. The program then prints out all integer numbers greater than zero but smaller than the input."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Upper limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n1\n2\n3\n4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please don't use the value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" as the condition of your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop in this exercise!"}]}]},{"type":"element","tagName":"h2","properties":{"id":"debugging-tips","style":"position:relative;"},"children":[{"type":"text","value":"Debugging tips"},{"type":"element","tagName":"a","properties":{"href":"#debugging-tips","ariaLabel":"debugging tips 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":"Imagine you are writing some slightly more complicated program, such as the one in the next exercise, "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Powers of two"}]},{"type":"text","value":". The first efforts 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":"text","value":"limit "},{"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"]},"children":[{"type":"text","value":"\"Upper limit:\""}]},{"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":"\nnumber "},{"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":" number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" limit"},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here the program starts with reading the input, and continues with the outline of the loop and some attempt at a condition."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is likely the code will not work as desired on the first try. It might have to be tested dozens or even hundreds of times before it works correctly."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This bit of code always asks for input from the user, which makes testing it slow and cumbersome. Each time the program is tested, input must be typed in."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"One way around this is \"hard-coding\" the input while testing:"}]},{"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":"# let's hard-code the input value for testing"}]},{"type":"text","value":"\nlimit "},{"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":"8"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# int(input(\"Upper limit\"))"}]},{"type":"text","value":"\nnumber "},{"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":" number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" limit"},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the program works with the one hard-coded input, it is easy to test it with other hard-coded inputs as well. When it seems to work correctly all round, it can be tested with input from the user."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This trick works with many of the tests that the exercises on this course are graded with. If the test tells you that the program works incorrectly when the input is, say, 42, that input can be hard-coded into the program while you look for the source of the bug:"}]},{"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":"# the test said the program works incorrectly when the input is 42"}]},{"type":"text","value":"\nlimit "},{"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":"42"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# int(input(\"Upper limit\"))"}]},{"type":"text","value":"\nnumber "},{"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":" number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" limit"},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Print statement debugging was mentioned a few times in the "},{"type":"element","tagName":"a","properties":{"href":"/part-2"},"children":[{"type":"text","value":"previous part"}]},{"type":"text","value":" of the course. The programs you are asked to write will become more and more complex as the course advances. The amount of debugging you will have to do will likely increase accordingly. Common causes for bugs lie in the conditions that terminate loops; they may work correctly for some inputs and fail for others, and it is not always obvious why that is."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"That is why it is high time you included print statement debugging in your programming practices, if you haven't done so already. You can find debugging instructions in the "},{"type":"element","tagName":"a","properties":{"href":"/part-2/1-programming-terminology"},"children":[{"type":"text","value":"first"}]},{"type":"text","value":" and the "},{"type":"element","tagName":"a","properties":{"href":"/part-2/4-simple-loops"},"children":[{"type":"text","value":"fourth"}]},{"type":"text","value":" section of the previous part."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Besides print statements, there are many other tools that can be used for debugging. One of these is the "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":" on the "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Python Tutor"}]},{"type":"text","value":" website. The tool allows you to execute your code line by line, and also shows you the values stored in variables at each step."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The slightly broken code from the debugging example in the "},{"type":"element","tagName":"a","properties":{"href":"/part-2/4-simple-loops"},"children":[{"type":"text","value":"previous section"}]},{"type":"text","value":" is visualised with Python Tutor in the following image:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 896px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/1cef2f69bb7be251213fc205b34c9534/4c42d/3_1_3.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: 71.73913043478261%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAAsTAAALEwEAmpwYAAABqElEQVQ4y4WTy27jMAxF/f/fNUB33Qww28ZIm9hpLOtl661bkK7SpE0zBAgLMXN0SV53tVbklJFSgnMBxzeBw37CYj2AilIoCyiM1tjvegyHI+d4GDC9nxFjBHNyRkfFWimoWSL4gFrBSSAqaknh1hWzmGGNhdWGM4Z4qdmAtcA7h/N4gpYSCxUbi3VZ4FaHxVp+btANXGtBjGG7xDlWSMIYmLxHRd1USsUgekFJRSWXS8s+REjtMM0WQlr+bV1WHleLLjvP9wbv8bZ/hTWGFV5Ha3kSBn+e/uH5b4/D+/KjhrJLn0pCCAyKISDFdFPU2o2pQKgVZ2GhjbupaeeO5NKB5ngaRhhSaG8VEmxbVr2r/DoYSBtNMcIozUppft77z63XH2q/q7oGd/Rn2hIthbZJILqEFNO5tfsIcqMw58SzE+eJfUVeI+uQiUnhvRZ/gzGwlMxtrnZhkJaaTa6lwjaOzTbfFf4KpLZam7VUzMJhFitSzHe3+KhdBiqlIKVkx9M86SugbLO9GLx8GfwhsB3mWWA4Duh3PfqXHcZhwGk8odmqtf0/4AdY6Eqre/dLZQAAAABJRU5ErkJggg=='); 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":["/static/1cef2f69bb7be251213fc205b34c9534/a0b58/3_1_3.webp 230w","/static/1cef2f69bb7be251213fc205b34c9534/bc10c/3_1_3.webp 460w","/static/1cef2f69bb7be251213fc205b34c9534/c1a89/3_1_3.webp 896w"],"sizes":"(max-width: 896px) 100vw, 896px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/1cef2f69bb7be251213fc205b34c9534/81c8e/3_1_3.png 230w","/static/1cef2f69bb7be251213fc205b34c9534/08a84/3_1_3.png 460w","/static/1cef2f69bb7be251213fc205b34c9534/4c42d/3_1_3.png 896w"],"sizes":"(max-width: 896px) 100vw, 896px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/1cef2f69bb7be251213fc205b34c9534/4c42d/3_1_3.png","alt":"3 1 3","title":"3 1 3","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 red arrow points to where the execution of the program is at the moment. The tool displays what has been printed out so far, and also shows the value each variable has at each step. The execution moves forward line by line as you press "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Next"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"All you need to do to use the visualisation tool is to copy your code and paste it into the "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"code window"}]},{"type":"text","value":" of the tool. The tool does have some limitations compared to the version of Python used on this course. If you come across any cryptic error messages, it may be better to try some other debugging method."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"More experienced programmers are rarely heavy users of the visualisation tool, but for a beginner it can be a valuable aid. Programming as a discipline has little room for luck or chance. It is essential that a programmer understands what values are created by their code at any given moment in the execution. If the values stored in variables are not as expected, there is most likely a bug in the program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The visualisation tool and debugging print statements are both great ways for a programmer to see with their own eyes that a program does exactly what was expected of it."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Powers of two","tmcname":"part03-04_powers_of_two"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in an upper limit. The program then prints out numbers so that each subsequent number is the previous one doubled, starting from the number 1. That is, the program prints out powers of two in order."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The execution of the program finishes when the next number to be printed would be greater than the limit set by the user. No numbers greater than the limit should be printed."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Upper limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\n1\n2\n4\n8"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Upper limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"20"}]},{"type":"text","value":"\n1\n2\n4\n8\n16"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Upper limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"100"}]},{"type":"text","value":"\n1\n2\n4\n8\n16\n32\n64"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please don't use the value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" as the condition of your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop in this exercise!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"What are powers of two?"}]},{"type":"text","value":" The first power of two is the number 1. The next one is 1 times 2, which is 2. The next is 2 times 2, which is 4. The next is 4 times 2, which is 8, and so forth. Each power in the sequence is multiplied by two to produce the next one."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Powers of base n","tmcname":"part03-05_powers_of_base_n"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please change the program from the previous exercise so that the user gets to input also the base which is multiplied (in the previous program the base was always 2)."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Upper limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"27"}]},{"type":"text","value":"\nBase: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n1\n3\n9\n27"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Upper limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234567"}]},{"type":"text","value":"\nBase: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n1\n10\n100\n1000\n10000\n100000\n1000000"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please don't use the value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" as the condition of your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop in this exercise!"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"The sum of consecutive numbers, version 1","tmcname":"part03-06_consecutive_sum_v1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in a limit. The program then calculates the sum of consecutive numbers (1 + 2 + 3 + ...) until the sum is at least equal to the limit set by the user. The program should function as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n3"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n10"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"18"}]},{"type":"text","value":"\n21"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you have trouble understanding how the desired output is calculated, the sample outputs in the next exercise may help. You may assume the number typed in by the user is always equal to 2 or higher."}]}]},{"type":"element","tagName":"h2","properties":{"id":"building-strings","style":"position:relative;"},"children":[{"type":"text","value":"Building strings"},{"type":"element","tagName":"a","properties":{"href":"#building-strings","ariaLabel":"building 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":"In the very "},{"type":"element","tagName":"a","properties":{"href":"/part-1/2-information-from-the-user"},"children":[{"type":"text","value":"first week of the course"}]},{"type":"text","value":" we learnt that it is possible to \"build\" strings out of shorter strings with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" operator. For example, this is valid Python code:"}]},{"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":"words "},{"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":"\"pride\""}]},{"type":"text","value":"\nwords "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" words "},{"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":"\", prejudice\""}]},{"type":"text","value":"\nwords "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" words "},{"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":"\" and python\""}]},{"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":"words"},{"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":"pride, prejudice and python"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" operator allows us to write this a little more compactly:"}]},{"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":"words "},{"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":"\"pride\""}]},{"type":"text","value":"\nwords "},{"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":"\", prejudice\""}]},{"type":"text","value":"\nwords "},{"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":"\" and python\""}]},{"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":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This also applies to f-strings, which may come in handy if values stored in variables are needed as parts of the resulting string. For example this would 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":"course "},{"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":"\"Introduction to Programming\""}]},{"type":"text","value":"\ngrade "},{"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":"4"}]},{"type":"text","value":"\n\nverdict "},{"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":"\"You have received \""}]},{"type":"text","value":"\nverdict "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"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\"the grade "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"grade"},{"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":"\nverdict "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"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 the course "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"course"},{"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","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"verdict"},{"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":"You have received the grade 4 from the course Introduction to Programming"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the previous exercise you calculated the sum of consecutive numbers by always adding a new value inside a loop."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exact same idea applies to strings as well: you can add new parts to a string within a loop. This technique should be useful in the following exercise."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"The sum of consecutive numbers, version 2","tmcname":"part03-07_consecutive_sum_v2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a new version of the program in the previous exercise. In addition to the result it should also print out the calculation performed:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nThe consecutive sum: 1 + 2 = 3"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nThe consecutive sum: 1 + 2 + 3 + 4 = 10"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"18"}]},{"type":"text","value":"\nThe consecutive sum: 1 + 2 + 3 + 4 + 5 + 6 = 21"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume the number typed in by the user is always equal to 2 or higher."}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will know how to create a while loop with a condition
    • \n
    • You will know what roles initialisation, formulating a condition and updating variables perform in a loop
    • \n
    • You will be able to create loops with different kinds of conditions
    • \n

    Becoming a proficient programmer requires a lot of practice, sometimes even quite mechanical practice. It also involves developing problem solving skills and applying intuition. This is why there are a lot of exercises of different kinds on this course. Some of them ask you to quite straightforwardly apply what you have learnt in the material, but some of them are intentionally more challenging and open-ended.

    Some of the exercises might at first seem overwhelming, but this is nothing to worry about. None of the exercises is strictly mandatory, and in fact you need only 25 % of the points from each part to pass the course. You can find more details about passing the course on the page on grading.

    The exercises are not in any specific order of difficulty. Each section usually introduces some new programming concepts, and these are then practised with both simpler and more complicated exercises. If you come across an exercise that feels too difficult, move on to the next one. You can always come back to the more difficult exercises if you have time later.

    When the going inevitably gets tough, a word of consolation: a task that seems impossibly difficult this week will likely feel rather easy in about four weeks' time.

    In the previous section we learnt to use the while True loop to repeat sections of code. In that construction the condition of the loop is True, so the condition is fulfilled every time. We needed to explicitly break out from the loop each time to avoid an infinite loop. For example:

    # Print numbers until the variable a equals 5\na = 1\nwhile True:\n    print(a)\n    a += 1\n    if a == 5:\n        break

    1\n2\n3\n4

    Of course, the condition doesn't always have to be True, but instead any Boolean expression can be used as the condition. The general structure of the while statement is as follows:

    while <condition>:\n    <block>

    The idea here is that the execution goes back and forth, checking if the condition is true and executing the code within the block, over and over again. If the condition at any point is false, execution of the program continues from the line after the while block.

    \n \n

    Initialisation, condition and update

    To create a loop you'll often need to include three distinct steps: initialisation, condition, and updating the iteration variables.

    Initialisation refers to setting the initial value(s) of the variable(s) used within the condition of the loop. These are often called the iteration or iterator variables. This is performed before the loop is first entered. The condition defines for how long the loop is to be executed. It is set out at the very beginning of the loop. Finally, within each repetition of the loop the variables involved in the condition are updated, so that each iteration brings the loop one step closer to its conclusion. The following image illustrates these steps:

    \n \n \n \n \n \n \"3\n \n \n

    If any one of these three components is missing, the loop will likely not function correctly. A typical error is omitting the update step:

    number = 1\n\nwhile number < 10:\n    print(number)\n\nprint(\"Execution finished.\")

    Here, the value of the variable number never changes. The program is stuck in an infinite loop, and the exact same bit of code is repeated over and over again until the user stops the execution, for example by pressing Control + C:

    1\n1\n1\n1\n1\n(continued ad infinitum...)

    Please write a program which prints out all the even numbers between two and thirty, using a loop. Print each number on a separate line.

    The beginning of your output should look like this:

    \n2\n4\n6\n8\netc...\n

    The program below has some syntactic issues:

    print(\"Are you ready?\")\nnumber = int(input(\"Please type in a number: \"))\nwhile number = 0:\nprint(number)\nprint(\"Now!\")

    Please fix it so that it prints out the following:

    Are you ready?\nPlease type in a number: 5\n5\n4\n3\n2\n1\nNow!

    This exercise is similar to the countdown exercise in the last section, but please don't use a while True loop this time round!

    Writing conditions

    Any Boolean expression or combination thereof is a valid condition in a loop. For example, the following program prints out every third number, but only as long as the number is less than 100 and not divisible by 5:

    number = int(input(\"Please type in a number: \"))\n\nwhile number < 100 and number % 5 != 0:\n    print(number)\n    number += 3

    Two examples of the program's execution with different inputs:

    Please type in a number: 28\n28\n31\n34\n37

    Please type in a number: 96\n96\n99

    When the input is 28, the loop ends with the number 37, because the next number is 40, which is divisible by 5. When the input is 96, the loop ends with the number 99, because the next number is 102, which is not less than 100.

    Whenever you write a loop you should make sure that the execution of the loop will always end at some point. The following program either finishes or doesn't, depending on the input:

    number = int(input(\"Please type in a number: \"))\n\nwhile number != 10:\n    print(number)\n    number += 2

    If the input is an even number and equals 10 or less, the loop will terminate:

    Please type in a number: 4\n4\n6\n8

    In any other case the loop gets executed endlessly, as there is no way the variable could then ever equal 10. For example 3 or 12 are inputs that would end in an infinite loop.

    Please write a program which asks the user for a number. The program then prints out all integer numbers greater than zero but smaller than the input.

    Upper limit: 5\n1\n2\n3\n4

    Please don't use the value True as the condition of your while loop in this exercise!

    Debugging tips

    Imagine you are writing some slightly more complicated program, such as the one in the next exercise, Powers of two. The first efforts could look like this:

    limit = int(input(\"Upper limit:\"))\nnumber = 1\nwhile number == limit:\n   # more code

    Here the program starts with reading the input, and continues with the outline of the loop and some attempt at a condition.

    It is likely the code will not work as desired on the first try. It might have to be tested dozens or even hundreds of times before it works correctly.

    This bit of code always asks for input from the user, which makes testing it slow and cumbersome. Each time the program is tested, input must be typed in.

    One way around this is \"hard-coding\" the input while testing:

    # let's hard-code the input value for testing\nlimit = 8 # int(input(\"Upper limit\"))\nnumber = 1\nwhile number == limit:\n   # more code 

    When the program works with the one hard-coded input, it is easy to test it with other hard-coded inputs as well. When it seems to work correctly all round, it can be tested with input from the user.

    This trick works with many of the tests that the exercises on this course are graded with. If the test tells you that the program works incorrectly when the input is, say, 42, that input can be hard-coded into the program while you look for the source of the bug:

    # the test said the program works incorrectly when the input is 42\nlimit = 42 # int(input(\"Upper limit\"))\nnumber = 1\nwhile number == limit:\n   # more code

    Print statement debugging was mentioned a few times in the previous part of the course. The programs you are asked to write will become more and more complex as the course advances. The amount of debugging you will have to do will likely increase accordingly. Common causes for bugs lie in the conditions that terminate loops; they may work correctly for some inputs and fail for others, and it is not always obvious why that is.

    That is why it is high time you included print statement debugging in your programming practices, if you haven't done so already. You can find debugging instructions in the first and the fourth section of the previous part.

    Besides print statements, there are many other tools that can be used for debugging. One of these is the visualisation tool on the Python Tutor website. The tool allows you to execute your code line by line, and also shows you the values stored in variables at each step.

    The slightly broken code from the debugging example in the previous section is visualised with Python Tutor in the following image:

    \n \n \n \n \n \n \"3\n \n \n

    The red arrow points to where the execution of the program is at the moment. The tool displays what has been printed out so far, and also shows the value each variable has at each step. The execution moves forward line by line as you press Next.

    All you need to do to use the visualisation tool is to copy your code and paste it into the code window of the tool. The tool does have some limitations compared to the version of Python used on this course. If you come across any cryptic error messages, it may be better to try some other debugging method.

    More experienced programmers are rarely heavy users of the visualisation tool, but for a beginner it can be a valuable aid. Programming as a discipline has little room for luck or chance. It is essential that a programmer understands what values are created by their code at any given moment in the execution. If the values stored in variables are not as expected, there is most likely a bug in the program.

    The visualisation tool and debugging print statements are both great ways for a programmer to see with their own eyes that a program does exactly what was expected of it.

    Please write a program which asks the user to type in an upper limit. The program then prints out numbers so that each subsequent number is the previous one doubled, starting from the number 1. That is, the program prints out powers of two in order.

    The execution of the program finishes when the next number to be printed would be greater than the limit set by the user. No numbers greater than the limit should be printed.

    Upper limit: 8\n1\n2\n4\n8

    Upper limit: 20\n1\n2\n4\n8\n16

    Upper limit: 100\n1\n2\n4\n8\n16\n32\n64

    Please don't use the value True as the condition of your while loop in this exercise!

    What are powers of two? The first power of two is the number 1. The next one is 1 times 2, which is 2. The next is 2 times 2, which is 4. The next is 4 times 2, which is 8, and so forth. Each power in the sequence is multiplied by two to produce the next one.

    Please change the program from the previous exercise so that the user gets to input also the base which is multiplied (in the previous program the base was always 2).

    Upper limit: 27\nBase: 3\n1\n3\n9\n27

    Upper limit: 1234567\nBase: 10\n1\n10\n100\n1000\n10000\n100000\n1000000

    Please don't use the value True as the condition of your while loop in this exercise!

    Please write a program which asks the user to type in a limit. The program then calculates the sum of consecutive numbers (1 + 2 + 3 + ...) until the sum is at least equal to the limit set by the user. The program should function as follows:

    Limit: 2\n3

    Limit: 10\n10

    Limit: 18\n21

    If you have trouble understanding how the desired output is calculated, the sample outputs in the next exercise may help. You may assume the number typed in by the user is always equal to 2 or higher.

    Building strings

    In the very first week of the course we learnt that it is possible to \"build\" strings out of shorter strings with the + operator. For example, this is valid Python code:

    words = \"pride\"\nwords = words + \", prejudice\"\nwords = words + \" and python\"\n\nprint(words)

    pride, prejudice and python

    The += operator allows us to write this a little more compactly:

    words = \"pride\"\nwords += \", prejudice\"\nwords += \" and python\"\n\nprint(words)

    This also applies to f-strings, which may come in handy if values stored in variables are needed as parts of the resulting string. For example this would work:

    course = \"Introduction to Programming\"\ngrade = 4\n\nverdict = \"You have received \"\nverdict += f\"the grade {grade} \"\nverdict += f\"from the course {course}\"\n\nprint(verdict)

    You have received the grade 4 from the course Introduction to Programming

    In the previous exercise you calculated the sum of consecutive numbers by always adding a new value inside a loop.

    The exact same idea applies to strings as well: you can add new parts to a string within a loop. This technique should be useful in the following exercise.

    Please write a new version of the program in the previous exercise. In addition to the result it should also print out the calculation performed:

    Limit: 2\nThe consecutive sum: 1 + 2 = 3

    Limit: 10\nThe consecutive sum: 1 + 2 + 3 + 4 = 10

    Limit: 18\nThe consecutive sum: 1 + 2 + 3 + 4 + 5 + 6 = 21

    You may assume the number typed in by the user is always equal to 2 or higher.

    ","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-3/1-loops-with-conditions.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know how to create a while loop with a condition"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what roles initialisation, formulating a condition and updating variables perform in a loop"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to create loops with different kinds of conditions"}]},{"type":"text","value":"\n"}]}]},{"type":"comment","value":"the same text is in sections 3-1, 5-1 and 6-1, check them all if you're changing this"},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"About the exercises on this course"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Becoming a proficient programmer requires a lot of practice, sometimes even quite mechanical practice. It also involves developing problem solving skills and applying intuition. This is why there are a lot of exercises of different kinds on this course. Some of them ask you to quite straightforwardly apply what you have learnt in the material, but some of them are intentionally more challenging and open-ended."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some of the exercises might at first seem overwhelming, but this is nothing to worry about. None of the exercises is strictly mandatory, and in fact "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"you need only 25 % of the points from each part to pass the course."}]},{"type":"text","value":" You can find more details about passing the course on the "},{"type":"element","tagName":"a","properties":{"href":"/grading-and-exams"},"children":[{"type":"text","value":"page on grading"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"The exercises are not in any specific order of difficulty."}]},{"type":"text","value":" Each section usually introduces some new programming concepts, and these are then practised with both simpler and more complicated exercises. "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"If you come across an exercise that feels too difficult, move on to the next one."}]},{"type":"text","value":" You can always come back to the more difficult exercises if you have time later."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the going inevitably gets tough, a word of consolation: a task that seems impossibly difficult this week will likely feel rather easy in about four weeks' time."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the previous section we learnt to use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while True"}]},{"type":"text","value":" loop to repeat sections of code. In that construction the condition of the loop is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":", so the condition is fulfilled every time. We needed to explicitly break out from the loop each time to avoid an infinite loop. For example:"}]},{"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":"# Print numbers until the variable a equals 5"}]},{"type":"text","value":"\na "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n a "},{"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":"if"}]},{"type":"text","value":" a "},{"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":"5"}]},{"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":"break"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1\n2\n3\n4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Of course, the condition doesn't always have to be "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":", but instead any Boolean expression can be used as the condition. The general structure of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" statement is as follows:"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"condition"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"block"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The idea here is that the execution goes back and forth, checking if the condition is true and executing the code within the block, over and over again. If the condition at any point is false, execution of the program continues from the line after the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" block."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 550px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/1566349634fe8f4b1710e005c46a3ba3/dd45a/3_1_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: 103.91304347826087%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAVCAYAAABG1c6oAAAACXBIWXMAAAsTAAALEwEAmpwYAAABx0lEQVQ4y5WV6Y4CIRCEef8X02jiHW/9o/G+77s3XydNGJzdVRIygFBdVd2gkw/b6/XS7+12k8vlklgLm4sP2NjmNrY5YLvdLvW3BGAMFK49n0/ttOl0KpPJRMeshaB0lwa2Xq/lfr+/yWGtXq9Lp9PxAd4kx7T5zmYzv+F4PEqz2dS1VqsllUpFqtWqrsF2u93K4XBQG7DDpZm/WCx8AA7BarVaKVi5XJZisags2WeAqCKoezweKoXsmYzlcukBN5uND3a9XmU0GmmQ2BL2c86BmslkJJfLSbvdFgKMx2O/kcjn89nPe72e9Pv9RFLMXwXEl2w2K6VSSWq1mtKfz+eJBMGeOfIajYZ6ud/vE94DiAUqmW6lEXvI+HQ6KUuYW2EzDmVzFnt+TYo1WMEYa2A7HA7VQ7IaAgJG5l1cmHQOxEUOKwqaL4wZh4CUV7fbTS9s/MGG8EoiCYYwho1l335n/6+S04IAGIKF9zmRlL8ehPCASaYWkYynYfA3wP+eLSRRVtRsPp+XQqGgNwYbAML3rwBhRoYt44xhGiv6GNAkGRtuRdpe981rbUzjBzYcf8zQvoPBQOVa5uOA7tv/FJ6xuAbDB/YHPJllO2xxVmIAAAAASUVORK5CYII='); 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":["/static/1566349634fe8f4b1710e005c46a3ba3/a0b58/3_1_1.webp 230w","/static/1566349634fe8f4b1710e005c46a3ba3/bc10c/3_1_1.webp 460w","/static/1566349634fe8f4b1710e005c46a3ba3/12b65/3_1_1.webp 550w"],"sizes":"(max-width: 550px) 100vw, 550px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/1566349634fe8f4b1710e005c46a3ba3/81c8e/3_1_1.png 230w","/static/1566349634fe8f4b1710e005c46a3ba3/08a84/3_1_1.png 460w","/static/1566349634fe8f4b1710e005c46a3ba3/dd45a/3_1_1.png 550w"],"sizes":"(max-width: 550px) 100vw, 550px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/1566349634fe8f4b1710e005c46a3ba3/dd45a/3_1_1.png","alt":"3 1 1","title":"3 1 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":"p","properties":{},"children":[{"type":"text","value":"In the following loop we have the condition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number < 10"}]},{"type":"text","value":". The block within the loop is executed only if the variable number is less than 10."}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"while"}]},{"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":"10"}]},{"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 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":"\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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Execution finished.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This could print out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n4\n5\n6\n7\n8\n9\nExecution finished."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this structure the condition is always checked before the block within the loop is executed. It may happen that the block never gets executed, like so:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"12"}]},{"type":"text","value":"\nExecution finished."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"12 is not less than 10, so the program doesn't print out a single number."}]},{"type":"element","tagName":"h2","properties":{"id":"initialisation-condition-and-update","style":"position:relative;"},"children":[{"type":"text","value":"Initialisation, condition and update"},{"type":"element","tagName":"a","properties":{"href":"#initialisation-condition-and-update","ariaLabel":"initialisation condition and update 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":"To create a loop you'll often need to include three distinct steps: initialisation, condition, and updating the iteration variables."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Initialisation"}]},{"type":"text","value":" refers to setting the initial value(s) of the variable(s) used within the condition of the loop. These are often called the iteration or iterator variables. This is performed before the loop is first entered. The "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"condition"}]},{"type":"text","value":" defines for how long the loop is to be executed. It is set out at the very beginning of the loop. Finally, within each repetition of the loop the variables involved in the condition are "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"updated"}]},{"type":"text","value":", so that each iteration brings the loop one step closer to its conclusion. The following image illustrates these steps:"}]},{"type":"comment","value":"- this is here in case the following image needs to be updated\n```python\n# Ask the user for a number\nnumber = int(input(\"Please type in a number: \"))\n\n# Repeat while the number is less than 10\nwhile number < 10:\n\n # Print out and increment\n print(number)\n number += 1\n\nprint(\"Execution finished.\")\n```\n"},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 642px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/9fc35fe9541e12675e37086df542ad51/1bba8/3_1_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: 43.47826086956522%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAABhElEQVQoz5WRy6oiMRCG+/0fxaUv4NadoIgwR3GhjnPse3c6l6p0f4dkcDgMs5mCn1BF6qs/laJ8VVhrGaeRYRowdsD6iaav6U2PRIfGQJyFoJYQLcsyIyJUVcXz+eR+v+fz8XhQNFWNquRGJwNWerxaJulwOjJJS4gTMvtcS3kCGmM4HA6cz2d2ux3H45H9fk9hXcB7IQQleEUk5tzZQNQZlfinFkIkxoV3LMtC27Z0XZfVNA1FPTjauub584m3Fl+PLPOSp5VlSd3UlFVFWVY5r+oqNybQ7XbLDjebDdvt9rfDeZ6JqsQYSbNFFFXF+4BzHtXI90iuklKke865LO894zhSOC/0k6fvB8rPF8NoENUM+g58gxLrDUxmEiyEkJWgRWp6PH7RtR1D3SAu5MsfPz5om5a+7/Pz/lbaWfrZy+XCarVivV5zOp0oRNPCJ3QOLETirOgsDKYjiM8u/qXkMjlKe017vF6vvF4viskrXidMqDGhw8cJpwYjDSE6loX/ii8OE7QvuGlKpQAAAABJRU5ErkJggg=='); 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":["/static/9fc35fe9541e12675e37086df542ad51/a0b58/3_1_2.webp 230w","/static/9fc35fe9541e12675e37086df542ad51/bc10c/3_1_2.webp 460w","/static/9fc35fe9541e12675e37086df542ad51/7eb86/3_1_2.webp 642w"],"sizes":"(max-width: 642px) 100vw, 642px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9fc35fe9541e12675e37086df542ad51/81c8e/3_1_2.png 230w","/static/9fc35fe9541e12675e37086df542ad51/08a84/3_1_2.png 460w","/static/9fc35fe9541e12675e37086df542ad51/1bba8/3_1_2.png 642w"],"sizes":"(max-width: 642px) 100vw, 642px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/9fc35fe9541e12675e37086df542ad51/1bba8/3_1_2.png","alt":"3 1 2","title":"3 1 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":"If any one of these three components is missing, the loop will likely not function correctly. A typical error is omitting the update 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":"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":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"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":"10"}]},{"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":"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":"\"Execution finished.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here, the value of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" never changes. The program is stuck in an infinite loop, and the exact same bit of code is repeated over and over again until the user stops the execution, for example by pressing "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Control"}]},{"type":"text","value":" + "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"C"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1\n1\n1\n1\n1\n(continued ad infinitum...)"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Print numbers","tmcname":"part03-01_print_numbers"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which prints out all the even numbers between two and thirty, using a loop. Print each number on a separate line."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The beginning of your output should look like this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\n2\n4\n6\n8\netc...\n"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Fix the code: Countdown","tmcname":"part03-02_countdown"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program below has some syntactic issues:"}]},{"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":"\"Are you ready?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnumber "},{"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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"while"}]},{"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":"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":"number"},{"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":"\"Now!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please fix it so that it prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Are you ready?\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n5\n4\n3\n2\n1\nNow!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This exercise is similar to the countdown exercise in the last section, but please don't use a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while True"}]},{"type":"text","value":" loop this time round!"}]}]},{"type":"element","tagName":"h2","properties":{"id":"writing-conditions","style":"position:relative;"},"children":[{"type":"text","value":"Writing conditions"},{"type":"element","tagName":"a","properties":{"href":"#writing-conditions","ariaLabel":"writing conditions 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":"Any Boolean expression or combination thereof is a valid condition in a loop. For example, the following program prints out every third number, but only as long as the number is less than 100 and not divisible by 5:"}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"while"}]},{"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":"100"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"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":"5"}]},{"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":"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":"number"},{"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","number"]},"children":[{"type":"text","value":"3"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Two examples of the program's execution with different inputs:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"28"}]},{"type":"text","value":"\n28\n31\n34\n37"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"96"}]},{"type":"text","value":"\n96\n99"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the input is 28, the loop ends with the number 37, because the next number is 40, which is divisible by 5. When the input is 96, the loop ends with the number 99, because the next number is 102, which is not less than 100."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Whenever you write a loop you should make sure that the execution of the loop will always end at some point. The following program either finishes or doesn't, depending on the input:"}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"while"}]},{"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":"10"}]},{"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 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":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the input is an even number and equals 10 or less, the loop will terminate:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n4\n6\n8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In any other case the loop gets executed endlessly, as there is no way the variable could then ever equal 10. For example 3 or 12 are inputs that would end in an infinite loop."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Numbers","tmcname":"part03-03_numbers"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a number. The program then prints out all integer numbers greater than zero but smaller than the input."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Upper limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n1\n2\n3\n4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please don't use the value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" as the condition of your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop in this exercise!"}]}]},{"type":"element","tagName":"h2","properties":{"id":"debugging-tips","style":"position:relative;"},"children":[{"type":"text","value":"Debugging tips"},{"type":"element","tagName":"a","properties":{"href":"#debugging-tips","ariaLabel":"debugging tips 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":"Imagine you are writing some slightly more complicated program, such as the one in the next exercise, "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Powers of two"}]},{"type":"text","value":". The first efforts 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":"text","value":"limit "},{"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"]},"children":[{"type":"text","value":"\"Upper limit:\""}]},{"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":"\nnumber "},{"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":" number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" limit"},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here the program starts with reading the input, and continues with the outline of the loop and some attempt at a condition."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is likely the code will not work as desired on the first try. It might have to be tested dozens or even hundreds of times before it works correctly."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This bit of code always asks for input from the user, which makes testing it slow and cumbersome. Each time the program is tested, input must be typed in."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"One way around this is \"hard-coding\" the input while testing:"}]},{"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":"# let's hard-code the input value for testing"}]},{"type":"text","value":"\nlimit "},{"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":"8"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# int(input(\"Upper limit\"))"}]},{"type":"text","value":"\nnumber "},{"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":" number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" limit"},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the program works with the one hard-coded input, it is easy to test it with other hard-coded inputs as well. When it seems to work correctly all round, it can be tested with input from the user."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This trick works with many of the tests that the exercises on this course are graded with. If the test tells you that the program works incorrectly when the input is, say, 42, that input can be hard-coded into the program while you look for the source of the bug:"}]},{"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":"# the test said the program works incorrectly when the input is 42"}]},{"type":"text","value":"\nlimit "},{"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":"42"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# int(input(\"Upper limit\"))"}]},{"type":"text","value":"\nnumber "},{"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":" number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" limit"},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Print statement debugging was mentioned a few times in the "},{"type":"element","tagName":"a","properties":{"href":"/part-2"},"children":[{"type":"text","value":"previous part"}]},{"type":"text","value":" of the course. The programs you are asked to write will become more and more complex as the course advances. The amount of debugging you will have to do will likely increase accordingly. Common causes for bugs lie in the conditions that terminate loops; they may work correctly for some inputs and fail for others, and it is not always obvious why that is."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"That is why it is high time you included print statement debugging in your programming practices, if you haven't done so already. You can find debugging instructions in the "},{"type":"element","tagName":"a","properties":{"href":"/part-2/1-programming-terminology"},"children":[{"type":"text","value":"first"}]},{"type":"text","value":" and the "},{"type":"element","tagName":"a","properties":{"href":"/part-2/4-simple-loops"},"children":[{"type":"text","value":"fourth"}]},{"type":"text","value":" section of the previous part."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Besides print statements, there are many other tools that can be used for debugging. One of these is the "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":" on the "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Python Tutor"}]},{"type":"text","value":" website. The tool allows you to execute your code line by line, and also shows you the values stored in variables at each step."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The slightly broken code from the debugging example in the "},{"type":"element","tagName":"a","properties":{"href":"/part-2/4-simple-loops"},"children":[{"type":"text","value":"previous section"}]},{"type":"text","value":" is visualised with Python Tutor in the following image:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 896px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/1cef2f69bb7be251213fc205b34c9534/4c42d/3_1_3.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: 71.73913043478261%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAOCAYAAAAvxDzwAAAACXBIWXMAAAsTAAALEwEAmpwYAAABqElEQVQ4y4WTy27jMAxF/f/fNUB33Qww28ZIm9hpLOtl661bkK7SpE0zBAgLMXN0SV53tVbklJFSgnMBxzeBw37CYj2AilIoCyiM1tjvegyHI+d4GDC9nxFjBHNyRkfFWimoWSL4gFrBSSAqaknh1hWzmGGNhdWGM4Z4qdmAtcA7h/N4gpYSCxUbi3VZ4FaHxVp+btANXGtBjGG7xDlWSMIYmLxHRd1USsUgekFJRSWXS8s+REjtMM0WQlr+bV1WHleLLjvP9wbv8bZ/hTWGFV5Ha3kSBn+e/uH5b4/D+/KjhrJLn0pCCAyKISDFdFPU2o2pQKgVZ2GhjbupaeeO5NKB5ngaRhhSaG8VEmxbVr2r/DoYSBtNMcIozUppft77z63XH2q/q7oGd/Rn2hIthbZJILqEFNO5tfsIcqMw58SzE+eJfUVeI+uQiUnhvRZ/gzGwlMxtrnZhkJaaTa6lwjaOzTbfFf4KpLZam7VUzMJhFitSzHe3+KhdBiqlIKVkx9M86SugbLO9GLx8GfwhsB3mWWA4Duh3PfqXHcZhwGk8odmqtf0/4AdY6Eqre/dLZQAAAABJRU5ErkJggg=='); 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":["/static/1cef2f69bb7be251213fc205b34c9534/a0b58/3_1_3.webp 230w","/static/1cef2f69bb7be251213fc205b34c9534/bc10c/3_1_3.webp 460w","/static/1cef2f69bb7be251213fc205b34c9534/c1a89/3_1_3.webp 896w"],"sizes":"(max-width: 896px) 100vw, 896px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/1cef2f69bb7be251213fc205b34c9534/81c8e/3_1_3.png 230w","/static/1cef2f69bb7be251213fc205b34c9534/08a84/3_1_3.png 460w","/static/1cef2f69bb7be251213fc205b34c9534/4c42d/3_1_3.png 896w"],"sizes":"(max-width: 896px) 100vw, 896px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/1cef2f69bb7be251213fc205b34c9534/4c42d/3_1_3.png","alt":"3 1 3","title":"3 1 3","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 red arrow points to where the execution of the program is at the moment. The tool displays what has been printed out so far, and also shows the value each variable has at each step. The execution moves forward line by line as you press "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Next"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"All you need to do to use the visualisation tool is to copy your code and paste it into the "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"code window"}]},{"type":"text","value":" of the tool. The tool does have some limitations compared to the version of Python used on this course. If you come across any cryptic error messages, it may be better to try some other debugging method."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"More experienced programmers are rarely heavy users of the visualisation tool, but for a beginner it can be a valuable aid. Programming as a discipline has little room for luck or chance. It is essential that a programmer understands what values are created by their code at any given moment in the execution. If the values stored in variables are not as expected, there is most likely a bug in the program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The visualisation tool and debugging print statements are both great ways for a programmer to see with their own eyes that a program does exactly what was expected of it."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Powers of two","tmcname":"part03-04_powers_of_two"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in an upper limit. The program then prints out numbers so that each subsequent number is the previous one doubled, starting from the number 1. That is, the program prints out powers of two in order."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The execution of the program finishes when the next number to be printed would be greater than the limit set by the user. No numbers greater than the limit should be printed."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Upper limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\n1\n2\n4\n8"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Upper limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"20"}]},{"type":"text","value":"\n1\n2\n4\n8\n16"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Upper limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"100"}]},{"type":"text","value":"\n1\n2\n4\n8\n16\n32\n64"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please don't use the value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" as the condition of your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop in this exercise!"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"What are powers of two?"}]},{"type":"text","value":" The first power of two is the number 1. The next one is 1 times 2, which is 2. The next is 2 times 2, which is 4. The next is 4 times 2, which is 8, and so forth. Each power in the sequence is multiplied by two to produce the next one."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Powers of base n","tmcname":"part03-05_powers_of_base_n"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please change the program from the previous exercise so that the user gets to input also the base which is multiplied (in the previous program the base was always 2)."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Upper limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"27"}]},{"type":"text","value":"\nBase: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n1\n3\n9\n27"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Upper limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1234567"}]},{"type":"text","value":"\nBase: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n1\n10\n100\n1000\n10000\n100000\n1000000"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please don't use the value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" as the condition of your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop in this exercise!"}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"The sum of consecutive numbers, version 1","tmcname":"part03-06_consecutive_sum_v1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in a limit. The program then calculates the sum of consecutive numbers (1 + 2 + 3 + ...) until the sum is at least equal to the limit set by the user. The program should function as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n3"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n10"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"18"}]},{"type":"text","value":"\n21"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you have trouble understanding how the desired output is calculated, the sample outputs in the next exercise may help. You may assume the number typed in by the user is always equal to 2 or higher."}]}]},{"type":"element","tagName":"h2","properties":{"id":"building-strings","style":"position:relative;"},"children":[{"type":"text","value":"Building strings"},{"type":"element","tagName":"a","properties":{"href":"#building-strings","ariaLabel":"building 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":"In the very "},{"type":"element","tagName":"a","properties":{"href":"/part-1/2-information-from-the-user"},"children":[{"type":"text","value":"first week of the course"}]},{"type":"text","value":" we learnt that it is possible to \"build\" strings out of shorter strings with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" operator. For example, this is valid Python code:"}]},{"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":"words "},{"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":"\"pride\""}]},{"type":"text","value":"\nwords "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" words "},{"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":"\", prejudice\""}]},{"type":"text","value":"\nwords "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" words "},{"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":"\" and python\""}]},{"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":"words"},{"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":"pride, prejudice and python"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" operator allows us to write this a little more compactly:"}]},{"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":"words "},{"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":"\"pride\""}]},{"type":"text","value":"\nwords "},{"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":"\", prejudice\""}]},{"type":"text","value":"\nwords "},{"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":"\" and python\""}]},{"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":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This also applies to f-strings, which may come in handy if values stored in variables are needed as parts of the resulting string. For example this would 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":"course "},{"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":"\"Introduction to Programming\""}]},{"type":"text","value":"\ngrade "},{"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":"4"}]},{"type":"text","value":"\n\nverdict "},{"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":"\"You have received \""}]},{"type":"text","value":"\nverdict "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"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\"the grade "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"grade"},{"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":"\nverdict "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"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 the course "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"course"},{"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","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"verdict"},{"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":"You have received the grade 4 from the course Introduction to Programming"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the previous exercise you calculated the sum of consecutive numbers by always adding a new value inside a loop."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exact same idea applies to strings as well: you can add new parts to a string within a loop. This technique should be useful in the following exercise."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"The sum of consecutive numbers, version 2","tmcname":"part03-07_consecutive_sum_v2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a new version of the program in the previous exercise. In addition to the result it should also print out the calculation performed:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nThe consecutive sum: 1 + 2 = 3"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nThe consecutive sum: 1 + 2 + 3 + 4 = 10"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Limit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"18"}]},{"type":"text","value":"\nThe consecutive sum: 1 + 2 + 3 + 4 + 5 + 6 = 21"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume the number typed in by the user is always equal to 2 or higher."}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will know how to create a while loop with a condition
    • \n
    • You will know what roles initialisation, formulating a condition and updating variables perform in a loop
    • \n
    • You will be able to create loops with different kinds of conditions
    • \n

    Becoming a proficient programmer requires a lot of practice, sometimes even quite mechanical practice. It also involves developing problem solving skills and applying intuition. This is why there are a lot of exercises of different kinds on this course. Some of them ask you to quite straightforwardly apply what you have learnt in the material, but some of them are intentionally more challenging and open-ended.

    Some of the exercises might at first seem overwhelming, but this is nothing to worry about. None of the exercises is strictly mandatory, and in fact you need only 25 % of the points from each part to pass the course. You can find more details about passing the course on the page on grading.

    The exercises are not in any specific order of difficulty. Each section usually introduces some new programming concepts, and these are then practised with both simpler and more complicated exercises. If you come across an exercise that feels too difficult, move on to the next one. You can always come back to the more difficult exercises if you have time later.

    When the going inevitably gets tough, a word of consolation: a task that seems impossibly difficult this week will likely feel rather easy in about four weeks' time.

    In the previous section we learnt to use the while True loop to repeat sections of code. In that construction the condition of the loop is True, so the condition is fulfilled every time. We needed to explicitly break out from the loop each time to avoid an infinite loop. For example:

    # Print numbers until the variable a equals 5\na = 1\nwhile True:\n    print(a)\n    a += 1\n    if a == 5:\n        break

    1\n2\n3\n4

    Of course, the condition doesn't always have to be True, but instead any Boolean expression can be used as the condition. The general structure of the while statement is as follows:

    while <condition>:\n    <block>

    The idea here is that the execution goes back and forth, checking if the condition is true and executing the code within the block, over and over again. If the condition at any point is false, execution of the program continues from the line after the while block.

    \n \n

    Initialisation, condition and update

    To create a loop you'll often need to include three distinct steps: initialisation, condition, and updating the iteration variables.

    Initialisation refers to setting the initial value(s) of the variable(s) used within the condition of the loop. These are often called the iteration or iterator variables. This is performed before the loop is first entered. The condition defines for how long the loop is to be executed. It is set out at the very beginning of the loop. Finally, within each repetition of the loop the variables involved in the condition are updated, so that each iteration brings the loop one step closer to its conclusion. The following image illustrates these steps:

    \n \n \n \n \n \n \"3\n \n \n

    If any one of these three components is missing, the loop will likely not function correctly. A typical error is omitting the update step:

    number = 1\n\nwhile number < 10:\n    print(number)\n\nprint(\"Execution finished.\")

    Here, the value of the variable number never changes. The program is stuck in an infinite loop, and the exact same bit of code is repeated over and over again until the user stops the execution, for example by pressing Control + C:

    1\n1\n1\n1\n1\n(continued ad infinitum...)

    Please write a program which prints out all the even numbers between two and thirty, using a loop. Print each number on a separate line.

    The beginning of your output should look like this:

    \n2\n4\n6\n8\netc...\n

    The program below has some syntactic issues:

    print(\"Are you ready?\")\nnumber = int(input(\"Please type in a number: \"))\nwhile number = 0:\nprint(number)\nprint(\"Now!\")

    Please fix it so that it prints out the following:

    Are you ready?\nPlease type in a number: 5\n5\n4\n3\n2\n1\nNow!

    This exercise is similar to the countdown exercise in the last section, but please don't use a while True loop this time round!

    Writing conditions

    Any Boolean expression or combination thereof is a valid condition in a loop. For example, the following program prints out every third number, but only as long as the number is less than 100 and not divisible by 5:

    number = int(input(\"Please type in a number: \"))\n\nwhile number < 100 and number % 5 != 0:\n    print(number)\n    number += 3

    Two examples of the program's execution with different inputs:

    Please type in a number: 28\n28\n31\n34\n37

    Please type in a number: 96\n96\n99

    When the input is 28, the loop ends with the number 37, because the next number is 40, which is divisible by 5. When the input is 96, the loop ends with the number 99, because the next number is 102, which is not less than 100.

    Whenever you write a loop you should make sure that the execution of the loop will always end at some point. The following program either finishes or doesn't, depending on the input:

    number = int(input(\"Please type in a number: \"))\n\nwhile number != 10:\n    print(number)\n    number += 2

    If the input is an even number and equals 10 or less, the loop will terminate:

    Please type in a number: 4\n4\n6\n8

    In any other case the loop gets executed endlessly, as there is no way the variable could then ever equal 10. For example 3 or 12 are inputs that would end in an infinite loop.

    Please write a program which asks the user for a number. The program then prints out all integer numbers greater than zero but smaller than the input.

    Upper limit: 5\n1\n2\n3\n4

    Please don't use the value True as the condition of your while loop in this exercise!

    Debugging tips

    Imagine you are writing some slightly more complicated program, such as the one in the next exercise, Powers of two. The first efforts could look like this:

    limit = int(input(\"Upper limit:\"))\nnumber = 1\nwhile number == limit:\n   # more code

    Here the program starts with reading the input, and continues with the outline of the loop and some attempt at a condition.

    It is likely the code will not work as desired on the first try. It might have to be tested dozens or even hundreds of times before it works correctly.

    This bit of code always asks for input from the user, which makes testing it slow and cumbersome. Each time the program is tested, input must be typed in.

    One way around this is \"hard-coding\" the input while testing:

    # let's hard-code the input value for testing\nlimit = 8 # int(input(\"Upper limit\"))\nnumber = 1\nwhile number == limit:\n   # more code 

    When the program works with the one hard-coded input, it is easy to test it with other hard-coded inputs as well. When it seems to work correctly all round, it can be tested with input from the user.

    This trick works with many of the tests that the exercises on this course are graded with. If the test tells you that the program works incorrectly when the input is, say, 42, that input can be hard-coded into the program while you look for the source of the bug:

    # the test said the program works incorrectly when the input is 42\nlimit = 42 # int(input(\"Upper limit\"))\nnumber = 1\nwhile number == limit:\n   # more code

    Print statement debugging was mentioned a few times in the previous part of the course. The programs you are asked to write will become more and more complex as the course advances. The amount of debugging you will have to do will likely increase accordingly. Common causes for bugs lie in the conditions that terminate loops; they may work correctly for some inputs and fail for others, and it is not always obvious why that is.

    That is why it is high time you included print statement debugging in your programming practices, if you haven't done so already. You can find debugging instructions in the first and the fourth section of the previous part.

    Besides print statements, there are many other tools that can be used for debugging. One of these is the visualisation tool on the Python Tutor website. The tool allows you to execute your code line by line, and also shows you the values stored in variables at each step.

    The slightly broken code from the debugging example in the previous section is visualised with Python Tutor in the following image:

    \n \n \n \n \n \n \"3\n \n \n

    The red arrow points to where the execution of the program is at the moment. The tool displays what has been printed out so far, and also shows the value each variable has at each step. The execution moves forward line by line as you press Next.

    All you need to do to use the visualisation tool is to copy your code and paste it into the code window of the tool. The tool does have some limitations compared to the version of Python used on this course. If you come across any cryptic error messages, it may be better to try some other debugging method.

    More experienced programmers are rarely heavy users of the visualisation tool, but for a beginner it can be a valuable aid. Programming as a discipline has little room for luck or chance. It is essential that a programmer understands what values are created by their code at any given moment in the execution. If the values stored in variables are not as expected, there is most likely a bug in the program.

    The visualisation tool and debugging print statements are both great ways for a programmer to see with their own eyes that a program does exactly what was expected of it.

    Please write a program which asks the user to type in an upper limit. The program then prints out numbers so that each subsequent number is the previous one doubled, starting from the number 1. That is, the program prints out powers of two in order.

    The execution of the program finishes when the next number to be printed would be greater than the limit set by the user. No numbers greater than the limit should be printed.

    Upper limit: 8\n1\n2\n4\n8

    Upper limit: 20\n1\n2\n4\n8\n16

    Upper limit: 100\n1\n2\n4\n8\n16\n32\n64

    Please don't use the value True as the condition of your while loop in this exercise!

    What are powers of two? The first power of two is the number 1. The next one is 1 times 2, which is 2. The next is 2 times 2, which is 4. The next is 4 times 2, which is 8, and so forth. Each power in the sequence is multiplied by two to produce the next one.

    Please change the program from the previous exercise so that the user gets to input also the base which is multiplied (in the previous program the base was always 2).

    Upper limit: 27\nBase: 3\n1\n3\n9\n27

    Upper limit: 1234567\nBase: 10\n1\n10\n100\n1000\n10000\n100000\n1000000

    Please don't use the value True as the condition of your while loop in this exercise!

    Please write a program which asks the user to type in a limit. The program then calculates the sum of consecutive numbers (1 + 2 + 3 + ...) until the sum is at least equal to the limit set by the user. The program should function as follows:

    Limit: 2\n3

    Limit: 10\n10

    Limit: 18\n21

    If you have trouble understanding how the desired output is calculated, the sample outputs in the next exercise may help. You may assume the number typed in by the user is always equal to 2 or higher.

    Building strings

    In the very first week of the course we learnt that it is possible to \"build\" strings out of shorter strings with the + operator. For example, this is valid Python code:

    words = \"pride\"\nwords = words + \", prejudice\"\nwords = words + \" and python\"\n\nprint(words)

    pride, prejudice and python

    The += operator allows us to write this a little more compactly:

    words = \"pride\"\nwords += \", prejudice\"\nwords += \" and python\"\n\nprint(words)

    This also applies to f-strings, which may come in handy if values stored in variables are needed as parts of the resulting string. For example this would work:

    course = \"Introduction to Programming\"\ngrade = 4\n\nverdict = \"You have received \"\nverdict += f\"the grade {grade} \"\nverdict += f\"from the course {course}\"\n\nprint(verdict)

    You have received the grade 4 from the course Introduction to Programming

    In the previous exercise you calculated the sum of consecutive numbers by always adding a new value inside a loop.

    The exact same idea applies to strings as well: you can add new parts to a string within a loop. This technique should be useful in the following exercise.

    Please write a new version of the program in the previous exercise. In addition to the result it should also print out the calculation performed:

    Limit: 2\nThe consecutive sum: 1 + 2 = 3

    Limit: 10\nThe consecutive sum: 1 + 2 + 3 + 4 = 10

    Limit: 18\nThe consecutive sum: 1 + 2 + 3 + 4 + 5 + 6 = 21

    You may assume the number typed in by the user is always equal to 2 or higher.

    ","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-3/1-loops-with-conditions.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-3/2-working-with-strings/page-data.json b/page-data/part-3/2-working-with-strings/page-data.json index 136909e7e..be407a6f3 100644 --- a/page-data/part-3/2-working-with-strings/page-data.json +++ b/page-data/part-3/2-working-with-strings/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-3/2-working-with-strings", - "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 able to use the operators "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" with strings"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to find out the length of a string"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what is meant by string indexing"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to look for substrings within a string"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"string-operations","style":"position:relative;"},"children":[{"type":"text","value":"String operations"},{"type":"element","tagName":"a","properties":{"href":"#string-operations","ariaLabel":"string operations 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 can be combined, or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"concatenated"}]},{"type":"text","value":", with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" operator:"}]},{"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":"begin "},{"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":"\"ex\""}]},{"type":"text","value":"\nend "},{"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":"\"ample\""}]},{"type":"text","value":"\nword "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" begin"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"end\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":"word"},{"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":"example"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" operator can also be used with a string, when the other operand is an integer. The string operand is then repeated the number of times specified by the integer. For example this would 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":"word "},{"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":"\"banana\""}]},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"bananabananabanana"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Using string operations together with a loop we can write a program which draws a pyramid:"}]},{"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":"n "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# number of layers in the pyramid"}]},{"type":"text","value":"\nrow "},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" n "},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" row"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n row "},{"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":"text","value":"\n n "},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This prints out the following:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":" *\n ***\n *****\n *******\n *********\n ***********\n *************\n ***************\n *****************\n *******************"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command within the loop prints a line, which begins with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" spaces, followed by whatever is stored in the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"row"}]},{"type":"text","value":". Then two stars are added to the end of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"row"}]},{"type":"text","value":", and the value of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" is decreased by 1."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"String multiplied","tmcname":"part03-08_string_multiplied"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a string and an amount. The program then prints out the string as many times as specified by the amount. The printout should all be on one line, with no extra spaces or symbols added."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected behaviour:"}]},{"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":"hiya"}]},{"type":"text","value":"\nPlease type in an amount: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nhiyahiyahiyahiya"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-length-and-index-of-a-string","style":"position:relative;"},"children":[{"type":"text","value":"The length and index of a string"},{"type":"element","tagName":"a","properties":{"href":"#the-length-and-index-of-a-string","ariaLabel":"the length and index of a string 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 function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" returns the number of characters in a string, which is always an integer value. For example, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len(\"hey\")"}]},{"type":"text","value":" returns 3, because there are three characters in the string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hey"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program asks the user for a string and then prints it \"underlined\". The program prints a second line with as many "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" characters as is the length of the input:"}]},{"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":"input_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":"input_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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"-\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"input_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":"Please type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hi there!"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Hi there!\n---------\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The length of a string includes all the characters in the string, including whitespace. For example, the length of the string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bye bye"}]},{"type":"text","value":" is 7."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"The longer string","tmcname":"part03-09_longer_string"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for two strings and then prints out whichever is the longer of the two - that is, whichever has the more characters. If the strings are of equal length, the program should print out \"The strings are equally long\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in string 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hey"}]},{"type":"text","value":"\nPlease type in string 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hiya"}]},{"type":"text","value":"\nhiya is longer"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in string 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"howdy doody"}]},{"type":"text","value":"\nPlease type in string 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hola"}]},{"type":"text","value":"\nhowdy doody is longer"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in string 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hey"}]},{"type":"text","value":"\nPlease type in string 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"bye"}]},{"type":"text","value":"\nThe strings are equally long"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As strings are essentially sequences of characters, any single character in a string can also be retrieved. The operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]},{"type":"text","value":" finds the character with the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"index"}]},{"type":"text","value":" specified within the brackets."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The index refers to a position in the string, counting up from zero. The first character in the string has index 0, the second character has index 1, and so forth."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 646px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/23ff4a404e8e641ddf64851041396dde/27524/3_2_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: 24.782608695652172%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAApUlEQVQY012Q2QqFMAxE+///6HMFrVq7uGFzOYFIuYEhzWSazU3TJOu6yjzPEmOUbdsUy7IoSilSaxXvvWIcR+XR55wFa619cPd9C7iuSwk+n+cpz/Moh8fwx3FortdbQTNnj33fZRiGL0l3CvCRJgY4fEpJNWwFxyZs6qwAXRG876txCEEFNOrPgIfvz0NxYuD+R7aYG1EMMR7QxBqwOjlgEzPlD17sg2RMIZEkAAAAAElFTkSuQmCC'); 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":["/static/23ff4a404e8e641ddf64851041396dde/a0b58/3_2_1.webp 230w","/static/23ff4a404e8e641ddf64851041396dde/bc10c/3_2_1.webp 460w","/static/23ff4a404e8e641ddf64851041396dde/8c2f2/3_2_1.webp 646w"],"sizes":"(max-width: 646px) 100vw, 646px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/23ff4a404e8e641ddf64851041396dde/81c8e/3_2_1.png 230w","/static/23ff4a404e8e641ddf64851041396dde/08a84/3_2_1.png 460w","/static/23ff4a404e8e641ddf64851041396dde/27524/3_2_1.png 646w"],"sizes":"(max-width: 646px) 100vw, 646px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/23ff4a404e8e641ddf64851041396dde/27524/3_2_1.png","alt":"3 2 1","title":"3 2 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":"p","properties":{},"children":[{"type":"text","value":"For example, this program"}]},{"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":"\ninput_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":"input_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","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":"input_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":"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":"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":"input_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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"would print out this:"}]},{"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":"monkey"}]},{"type":"text","value":"\nm\no\nk"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Since the first character in a string has the index 0, the last character has the index "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"length - 1"}]},{"type":"text","value":". The following program prints out the first and the last characters 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":"input_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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"First character: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" input_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","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":"\"Last character: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" input_string"},{"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":"input_string"},{"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":"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":"testing"}]},{"type":"text","value":"\nFirst character: t\nLast character: g"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program loops through all the characters in a string from first to last:"}]},{"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":"input_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":"\nindex "},{"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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" index "},{"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":"input_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 "},{"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":"input_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"index"},{"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 index "},{"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":"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":"test"}]},{"type":"text","value":"\nt\ne\ns\nt"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can also use negative indexing to access characters counting from the end of the string. The last character in a string is at index -1, the second to last character is at index -2, and so forth. You can think of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input_string[-1]"}]},{"type":"text","value":" as shorthand for "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input_string[len(input_string) - 1]"}]},{"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: 616px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/373faf879dbcd0f2263ba34a06adccbb/40040/3_2_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: 26.08695652173913%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAsklEQVQY04WP2QqFMAxE+/+fZy1YRRR8cUPrioo6MoG8XbgDp8t0GhLT9z1CCOi6TmjbFtu2CXVdo6oqoWkaMHscB6j3fX9inufBdV1Q3fcNeiqe6THM/Z8MF3aTZZnsFLsYhgHTNAnrugp6n+cZ4zhi33csyyIT8i8zRrtiEe1AP2uIbyxCeKd/nqcU04x6hqOo9FwUBaIoQpqm8N4jSRI45xDHsUC/LEtYa+Utz3OZkHytf3+/GpjsnwAAAABJRU5ErkJggg=='); 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":["/static/373faf879dbcd0f2263ba34a06adccbb/a0b58/3_2_2.webp 230w","/static/373faf879dbcd0f2263ba34a06adccbb/bc10c/3_2_2.webp 460w","/static/373faf879dbcd0f2263ba34a06adccbb/26c8a/3_2_2.webp 616w"],"sizes":"(max-width: 616px) 100vw, 616px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/373faf879dbcd0f2263ba34a06adccbb/81c8e/3_2_2.png 230w","/static/373faf879dbcd0f2263ba34a06adccbb/08a84/3_2_2.png 460w","/static/373faf879dbcd0f2263ba34a06adccbb/40040/3_2_2.png 616w"],"sizes":"(max-width: 616px) 100vw, 616px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/373faf879dbcd0f2263ba34a06adccbb/40040/3_2_2.png","alt":"3 2 2","title":"3 2 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 example from above can be simplified with negative indexing:"}]},{"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":"input_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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"First character: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" input_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","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":"\"Last character: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" input_string"},{"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":"testing"}]},{"type":"text","value":"\nFirst character: t\nLast character: g"}]}]},{"type":"element","tagName":"h2","properties":{"id":"indexerror-string-index-out-of-range","style":"position:relative;"},"children":[{"type":"text","value":"IndexError: string index out of range"},{"type":"element","tagName":"a","properties":{"href":"#indexerror-string-index-out-of-range","ariaLabel":"indexerror string index out of range 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":"If you tried the above examples for yourself, you may already have come across the error message "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"IndexError: string index out of range"}]},{"type":"text","value":". This error appears if you try to access an index which is not present in the 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":"input_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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The tenth character: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" input_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":"9"}]},{"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":"introduction to programming"}]},{"type":"text","value":"\nThe tenth character: i"}]}]},{"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":"python"}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"Traceback (most recent call last):\nFile \""},{"type":"element","tagName":"stdin","properties":{},"children":[{"type":"text","value":"\", line 1, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nIndexError: string index out of range"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sometimes an indexing error is caused by a bug in the code. For example, it is quite common to index too far when trying to access the last character in 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":"input_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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Last character: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" input_string"},{"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":"input_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":"p","properties":{},"children":[{"type":"text","value":"Since string indexing begins at zero, the last character is at index "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len(input_string) - 1"}]},{"type":"text","value":", not at "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len(input_string)"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are situations where the program should prepare for errors caused by input from the user:"}]},{"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":"input_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":"if"}]},{"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":"input_string"},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"First character: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" input_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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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 input string is empty. There is no first character.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the example above, if the programmer hadn't included a check for the length of the input string, a string of length zero would have caused an error. A string of length zero is also called an empty string, and here it would be achieved by just pressing Enter at the input prompt."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"End to beginning","tmcname":"part03-10_end_to_beginning"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a string. The program then prints out the input string in reversed order, from end to beginning. Each character should be on a separate line."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected behaviour:"}]},{"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":"hiya"}]},{"type":"text","value":"\na\ny\ni\nh"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Second and second to last characters","tmcname":"part03-11_second_and_second_to_last"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a string. The program then prints out a message based on whether the second character and the second to last character are the same or not. See the examples below."}]},{"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":"python"}]},{"type":"text","value":"\nThe second and the second to last characters are different"}]}]},{"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":"pascal"}]},{"type":"text","value":"\nThe second and the second to last characters are a"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"A line of hashes","tmcname":"part03-12_line_of_hashes"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which prints out a line of hash characters, the width of which is chosen by the user."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Width: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"###\n"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Width: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"########\n"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"A rectangle of hashes","tmcname":"part03-13_rectangle_of_hashes"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please modify the previous program so that it also asks for the height, and prints out a rectangle of hash characters accordingly."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Width: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nHeight: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n##########\n##########\n##########"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Underlining","tmcname":"part03-14_underlining"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for strings using a loop. The program prints out each string underlined as shown in the examples below. The execution ends when the user inputs an empty string - that is, just presses Enter at the prompt."}]},{"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":"Hi there!"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Hi there!\n---------\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"This is a test"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"This is a test\n--------------\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"a"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"a\n-\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a string:"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Right-aligned","tmcname":"part03-15_right_aligned"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a string and then prints it out so that exactly 20 characters are displayed. If the input is shorter than 20 characters, the beginning of the line is filled in with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" characters."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume the input string is at most 20 characters long."}]},{"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":"python"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"**************python\n"}]}]},{"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":"alongerstring"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"*******alongerstring\n"}]}]},{"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":"averyverylongstring"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"*averyverylongstring\n"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"A framed word","tmcname":"part03-16_framed_word"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a string and then prints out a frame of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" characters with the word in the centre. The width of the frame should be 30 characters. You may assume the input string will always fit inside the frame."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the length of the input string is an odd number, you may print out the word in either of the two possible centre locations."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"testing"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"******************************\n* testing *\n******************************\n"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"******************************\n* python *\n******************************\n"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"substrings-and-slices","style":"position:relative;"},"children":[{"type":"text","value":"Substrings and slices"},{"type":"element","tagName":"a","properties":{"href":"#substrings-and-slices","ariaLabel":"substrings and 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":"A "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"substring"}]},{"type":"text","value":" of a string is a sequence of characters that forms a part of the string. For example, the string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"example"}]},{"type":"text","value":" contains the substrings "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exam"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"amp"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ple"}]},{"type":"text","value":", among others. In Python programming, the process of selecting substrings is usually called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"slicing"}]},{"type":"text","value":", and a substring is often referred to as a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"slice"}]},{"type":"text","value":" of the string. The two terms can often be used interchangeably."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you know the beginning and end indexes of the slice you wish to extract, you can do so with the notation "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[a:b]"}]},{"type":"text","value":". This means the slice begins at the index "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" and ends at the last character before index "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" - that is, including the first, but excluding the last. You can think of the indexes as separator lines drawn on the left side of the indexed character, as illustrated in the image below:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 581px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/2ff2c1c4d3ebbc4413f1f393bd3f8f0c/92d15/3_2_3.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: 33.04347826086957%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAAsTAAALEwEAmpwYAAABZUlEQVQoz02RW2vbQBCF9f//TENaKMS9pA9u3L7YahOKH5xidLF1XV1WXkmr1VdmIdADwxkNM0dnZ4KmaSiKgrZtyfOcYRgoy9J/C/d9j3MLVVWRpilJkvhQSpFlme9zziFY15VABKVRBuI4pq5rLpeLrwnneeaHhauy5Hq9ouoamRNBCV9TCmstgTGG2+3GOI50XedZa+2djeOEMRN9P9D1mnGaGSfLCsx28a6kT0yImCAQ5TdIUcTmeQbncN0rVv1maV5wzQtWPXs2xZ5VJ+h+wNrZz4mpaZoIjsejTwSyjyiKyIuCXjXoZMcQPWCSL5jkMzr+5PP2/JHb9RdxlPr9y65lPZIH/Af5iziWZy/WMjfPTMWWW7ZlKp+Yyh22/olTO0xzQmvje+VFy7L4XQZymbcryVG+PT6y3x8IwwPZ6YH2fEf1ekd7vqc8vaM9v8ekH/j75yvb7z84HPaEYehjs9nwD86xDxViuRttAAAAAElFTkSuQmCC'); 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":["/static/2ff2c1c4d3ebbc4413f1f393bd3f8f0c/a0b58/3_2_3.webp 230w","/static/2ff2c1c4d3ebbc4413f1f393bd3f8f0c/bc10c/3_2_3.webp 460w","/static/2ff2c1c4d3ebbc4413f1f393bd3f8f0c/39daa/3_2_3.webp 581w"],"sizes":"(max-width: 581px) 100vw, 581px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/2ff2c1c4d3ebbc4413f1f393bd3f8f0c/81c8e/3_2_3.png 230w","/static/2ff2c1c4d3ebbc4413f1f393bd3f8f0c/08a84/3_2_3.png 460w","/static/2ff2c1c4d3ebbc4413f1f393bd3f8f0c/92d15/3_2_3.png 581w"],"sizes":"(max-width: 581px) 100vw, 581px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/2ff2c1c4d3ebbc4413f1f393bd3f8f0c/92d15/3_2_3.png","alt":"3 2 3","title":"3 2 3","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":"Let's have a closer look at some sliced 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":"input_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":"\"presumptious\""}]},{"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":"input_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":"3"}]},{"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":"input_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":"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":"10"}]},{"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","comment"]},"children":[{"type":"text","value":"# if the beginning index is left out, it defaults to 0"}]},{"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":"input_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","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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# if the end index is left out, it defaults to the length of the string"}]},{"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":"input_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":"4"}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"pre\numptio\npre\numptious"}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Half open intervals"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In Python string processing the interval "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[a:b]"}]},{"type":"text","value":" is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"half open"}]},{"type":"text","value":", which in this case means that the character at the beginning index "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" is included in the interval, but the character at the end index "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" is left out. Why is that?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There is no profound reason for this feature. Rather it is a convention inherited from other programming languages."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Half open intervals may feel unintuitive, but in practice they do have some advantages. For example, you can easily calculate the length of a slice with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b-a"}]},{"type":"text","value":". On the other hand, you must always remember that the character at the end index "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" will not be included in the slice."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Substrings, part 1","tmcname":"part03-17_substrings_part_1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in a string. The program then prints out all the substrings which begin with the first character, from the shortest to the longest. Have a look at the example below."}]},{"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":"test"}]},{"type":"text","value":"\nt\nte\ntes\ntest"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Substrings, part 2","tmcname":"part03-18_substrings_part_2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in a string. The program then prints out all the substrings which end with the last character, from the shortest to the longest. Have a look at the example below."}]},{"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":"test"}]},{"type":"text","value":"\nt\nst\nest\ntest"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"searching-for-substrings","style":"position:relative;"},"children":[{"type":"text","value":"Searching for substrings"},{"type":"element","tagName":"a","properties":{"href":"#searching-for-substrings","ariaLabel":"searching for substrings 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 "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" operator can tell us if a string contains a particular substring. The Boolean expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a in b"}]},{"type":"text","value":" is true, if "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" contains the substring "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, this bit of code"}]},{"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":"input_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":"\"test\""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"t\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" input_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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"x\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" input_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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"es\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" input_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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ets\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" input_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nFalse\nTrue\nFalse"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program below lets the user search for substrings within a string hardcoded into the program:"}]},{"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":"input_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":"\"perpendicular\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n substring "},{"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":"\"What are you looking for? \""}]},{"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":"if"}]},{"type":"text","value":" substring "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" input_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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Found it\""}]},{"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":"else"}]},{"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":"\"Not found\""}]},{"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":"What are you looking for? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"perp"}]},{"type":"text","value":"\nFound it\nWhat are you looking for? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"abc"}]},{"type":"text","value":"\nNot found\nWhat are you looking for? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pen"}]},{"type":"text","value":"\nFound it\n..."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Does it contain vowels","tmcname":"part03-19_does_it_contain_vowels"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to input a string. The program then prints out different messages if the string contains any of the vowels a, e or o."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume the input will be in lowercase entirely. Have a look at the examples below."}]},{"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":"hello there"}]},{"type":"text","value":"\na not found\ne found\no found"}]}]},{"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":"hiya"}]},{"type":"text","value":"\na found\ne not found\no not found"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" returns a Boolean value, so it will only tell us "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" a substring exists in a string, but it will not be useful in finding out "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"where"}]},{"type":"text","value":" exactly it is. Instead, the Python string method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find"}]},{"type":"text","value":" can be used for this purpose. It takes the substring searched for as an argument, and returns either the first index where it is found, or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":" if the substring is not found within the string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The image below illustrates how it is used:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 441px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/9a030cb2cdef85d275a8320bb284d0dd/efc6e/3_2_4.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: 36.95652173913043%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAAsTAAALEwEAmpwYAAABLElEQVQoz32R227CQAxE+f8vIyJXAjThgUqkLBWQ2+ZGUjan2iWBp3Yky5Y1HnnsBcA4jn+GGkdN4Xw+Y1kWvu+z3+9xHIcsy5jn0XxgMU4D/6Lr4H6HYYC2fdd9/6wnMZ0XOvfDgKwqHkpRNw1N29J2HXXb0vc9jZTUdU1ZFJRSUhQFUkrSNEWWJaNSr02NYFOWfMYx5e3Gd5JwE4KrEHwdDlyEIMtzLpcLx+ORPE256jpJEKcTeZbxeDzegq22oBt9z4+uhwFV1y9LStuaMNsyVuczTNvNWGy3W3R4vo8fBHxEEUvLIopjHM/DcV1jTyOKItPfbDZ4rksQBKa32+1ML0mSp+WqqgjXa0MIw9AQbNs231RKMT9OW+u6zogsl0vzdXu1Qghhbq3jF4wJFCAuvsawAAAAAElFTkSuQmCC'); 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":["/static/9a030cb2cdef85d275a8320bb284d0dd/a0b58/3_2_4.webp 230w","/static/9a030cb2cdef85d275a8320bb284d0dd/24c94/3_2_4.webp 441w"],"sizes":"(max-width: 441px) 100vw, 441px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9a030cb2cdef85d275a8320bb284d0dd/81c8e/3_2_4.png 230w","/static/9a030cb2cdef85d275a8320bb284d0dd/efc6e/3_2_4.png 441w"],"sizes":"(max-width: 441px) 100vw, 441px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/9a030cb2cdef85d275a8320bb284d0dd/efc6e/3_2_4.png","alt":"3 2 4","title":"3 2 4","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":"Some examples of how "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find"}]},{"type":"text","value":" is used:"}]},{"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":"input_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":"\"test\""}]},{"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":"input_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"t\""}]},{"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":"input_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"x\""}]},{"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":"input_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"es\""}]},{"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":"input_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ets\""}]},{"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":"0\n-1\n1\n-1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The above substring search example implemented with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find"}]},{"type":"text","value":":"}]},{"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":"input_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":"\"perpendicular\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n substring "},{"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":"\"What are you looking for? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n index "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" input_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"substring"},{"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":"if"}]},{"type":"text","value":" index "},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Found it at the index "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"index"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"\"Not found\""}]},{"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":"What are you looking for? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"perp"}]},{"type":"text","value":"\nFound it at the index 0\nWhat are you looking for? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"abc"}]},{"type":"text","value":"\nNot found\nWhat are you looking for? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pen"}]},{"type":"text","value":"\nFound it at the index 3\n..."}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Methods"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Above we used the string "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"method"}]},{"type":"text","value":" "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find"}]},{"type":"text","value":". Methods work quite similarly to the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"functions"}]},{"type":"text","value":" covered in the previous part. What distinguishes them from functions is that methods are always attached to the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"object"}]},{"type":"text","value":" they are called on. The object is the entity named before the method in the method call. In the case of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find"}]},{"type":"text","value":" the object is the string where the method looks for the substring it has as an argument."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Find the first substring","tmcname":"part03-20_find_first_substring"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in a string and a single character. The program then prints the first three character slice which begins with the character specified by the user. You may assume the input string is at least three characters long. The program must print out three characters, or else nothing."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pay special attention to when there are less than two characters left in the string after the first occurrence of the character looked for. In that case nothing should be printed out, and there should not be any indexing errors when executing the program."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"mammoth"}]},{"type":"text","value":"\nPlease type in a character: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"m"}]},{"type":"text","value":"\nmam"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"banana"}]},{"type":"text","value":"\nPlease type in a character: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"n"}]},{"type":"text","value":"\nnan"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"tomato"}]},{"type":"text","value":"\nPlease type in a character: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"x"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]},{"type":"text","value":"\nPlease type in a character: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"n"}]}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Find all the substrings","tmcname":"part03-21_find_all_substrings"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please make an extended version of the previous program, which prints out "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"all the substrings which are at least three characters long"}]},{"type":"text","value":", and which begin with the character specified by the user. You may assume the input string is at least three characters long."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"mammoth"}]},{"type":"text","value":"\nPlease type in a character: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"m"}]},{"type":"text","value":"\nmam\nmmo\nmot"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"banana"}]},{"type":"text","value":"\nPlease type in a character: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"n"}]},{"type":"text","value":"\nnan"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint"}]},{"type":"text","value":" the following example may give you some inspiration as to how this exercise could be tackled:"}]},{"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":"word "},{"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":"\"Word: \""}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"if"}]},{"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":"word"},{"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":"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":"break"}]},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n word "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" word"},{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"mammoth"}]},{"type":"text","value":"\nmammoth\nmmoth\noth\nh"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"The second occurrence","tmcname":"part03-22_second_occurrence"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which finds the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"second"}]},{"type":"text","value":" occurrence of a substring. If there is no second (or first) occurrence, the program should print out a message accordingly."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise the occurrences "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"cannot"}]},{"type":"text","value":" overlap. For example, in the string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aaaa"}]},{"type":"text","value":" the second occurrence of the substring "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aa"}]},{"type":"text","value":" is at index 2."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"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":"abcabc"}]},{"type":"text","value":"\nPlease type in a substring: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ab"}]},{"type":"text","value":"\nThe second occurrence of the substring is at index 3."}]}]},{"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":"methodology"}]},{"type":"text","value":"\nPlease type in a substring: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"o"}]},{"type":"text","value":"\nThe second occurrence of the substring is at index 6."}]}]},{"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":"aybabtu"}]},{"type":"text","value":"\nPlease type in a substring: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ba"}]},{"type":"text","value":"\nThe substring does not occur twice in the string."}]}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will be able to use the operators + and * with strings
    • \n
    • You will know how to find out the length of a string
    • \n
    • You will know what is meant by string indexing
    • \n
    • You will know how to look for substrings within a string
    • \n

    String operations

    Strings can be combined, or concatenated, with the + operator:

    begin = \"ex\"\nend = \"ample\"\nword = begin+end\nprint(word)

    example

    The * operator can also be used with a string, when the other operand is an integer. The string operand is then repeated the number of times specified by the integer. For example this would work:

    word = \"banana\"\nprint(word*3)

    bananabananabanana

    Using string operations together with a loop we can write a program which draws a pyramid:

    n = 10 # number of layers in the pyramid\nrow = \"*\"\n\nwhile n > 0:\n    print(\" \" * n + row)\n    row += \"**\"\n    n -= 1

    This prints out the following:

              *\n         ***\n        *****\n       *******\n      *********\n     ***********\n    *************\n   ***************\n  *****************\n *******************

    The print command within the loop prints a line, which begins with n spaces, followed by whatever is stored in the variable row. Then two stars are added to the end of the variable row, and the value of the variable n is decreased by 1.

    Please write a program which asks the user for a string and an amount. The program then prints out the string as many times as specified by the amount. The printout should all be on one line, with no extra spaces or symbols added.

    An example of expected behaviour:

    Please type in a string: hiya\nPlease type in an amount: 4\nhiyahiyahiyahiya

    The length and index of a string

    The function len returns the number of characters in a string, which is always an integer value. For example, len(\"hey\") returns 3, because there are three characters in the string hey.

    The following program asks the user for a string and then prints it \"underlined\". The program prints a second line with as many - characters as is the length of the input:

    input_string = input(\"Please type in a string: \")\nprint(input_string)\nprint(\"-\"*len(input_string))

    Please type in a string: Hi there!

    \nHi there!\n---------\n

    The length of a string includes all the characters in the string, including whitespace. For example, the length of the string bye bye is 7.

    Please write a program which asks the user for two strings and then prints out whichever is the longer of the two - that is, whichever has the more characters. If the strings are of equal length, the program should print out \"The strings are equally long\".

    Some examples of expected behaviour:

    Please type in string 1: hey\nPlease type in string 2: hiya\nhiya is longer

    Please type in string 1: howdy doody\nPlease type in string 2: hola\nhowdy doody is longer

    Please type in string 1: hey\nPlease type in string 2: bye\nThe strings are equally long

    As strings are essentially sequences of characters, any single character in a string can also be retrieved. The operator [] finds the character with the index specified within the brackets.

    The index refers to a position in the string, counting up from zero. The first character in the string has index 0, the second character has index 1, and so forth.

    \n \n \n \n \n \n \"3\n \n \n

    For example, this program

    \ninput_string = input(\"Please type in a string: \")\nprint(input_string[0])\nprint(input_string[1])\nprint(input_string[3])\n

    would print out this:

    Please type in a string: monkey\nm\no\nk

    Since the first character in a string has the index 0, the last character has the index length - 1. The following program prints out the first and the last characters of a string:

    input_string = input(\"Please type in a string: \")\nprint(\"First character: \" + input_string[0])\nprint(\"Last character: \" + input_string[len(input_string) - 1])

    Please type in a string: testing\nFirst character: t\nLast character: g

    The following program loops through all the characters in a string from first to last:

    input_string = input(\"Please type in a string: \")\nindex = 0\nwhile index < len(input_string):\n    print(input_string[index])\n    index += 1

    Please type in a string: test\nt\ne\ns\nt

    You can also use negative indexing to access characters counting from the end of the string. The last character in a string is at index -1, the second to last character is at index -2, and so forth. You can think of input_string[-1] as shorthand for input_string[len(input_string) - 1].

    \n \n \n \n \n \n \"3\n \n \n

    The example from above can be simplified with negative indexing:

    input_string = input(\"Please type in a string: \")\nprint(\"First character: \" + input_string[0])\nprint(\"Last character: \" + input_string[-1])

    Please type in a string: testing\nFirst character: t\nLast character: g

    IndexError: string index out of range

    If you tried the above examples for yourself, you may already have come across the error message IndexError: string index out of range. This error appears if you try to access an index which is not present in the string.

    input_string = input(\"Please type in a string: \")\nprint(\"The tenth character: \" + input_string[9])

    Please type in a string: introduction to programming\nThe tenth character: i

    Please type in a string: python

    Traceback (most recent call last):\nFile \"\", line 1, in \nIndexError: string index out of range

    Sometimes an indexing error is caused by a bug in the code. For example, it is quite common to index too far when trying to access the last character in a string:

    input_string = input(\"Please type in a string: \")\nprint(\"Last character: \" + input_string[len(input_string)])

    Since string indexing begins at zero, the last character is at index len(input_string) - 1, not at len(input_string).

    There are situations where the program should prepare for errors caused by input from the user:

    input_string = input(\"Please type in a string: \")\nif len(input_string) > 0:\n    print(\"First character: \" + input_string[0])\nelse:\n    print(\"The input string is empty. There is no first character.\")

    In the example above, if the programmer hadn't included a check for the length of the input string, a string of length zero would have caused an error. A string of length zero is also called an empty string, and here it would be achieved by just pressing Enter at the input prompt.

    Please write a program which asks the user for a string. The program then prints out the input string in reversed order, from end to beginning. Each character should be on a separate line.

    An example of expected behaviour:

    Please type in a string: hiya\na\ny\ni\nh

    Please write a program which asks the user for a string. The program then prints out a message based on whether the second character and the second to last character are the same or not. See the examples below.

    Please type in a string: python\nThe second and the second to last characters are different

    Please type in a string: pascal\nThe second and the second to last characters are a

    Please write a program which prints out a line of hash characters, the width of which is chosen by the user.

    Width: 3

    \n###\n

    Width: 8

    \n########\n

    Please modify the previous program so that it also asks for the height, and prints out a rectangle of hash characters accordingly.

    Width: 10\nHeight: 3\n##########\n##########\n##########

    Please write a program which asks the user for strings using a loop. The program prints out each string underlined as shown in the examples below. The execution ends when the user inputs an empty string - that is, just presses Enter at the prompt.

    Please type in a string: Hi there!

    \nHi there!\n---------\n

    Please type in a string: This is a test

    \nThis is a test\n--------------\n

    Please type in a string: a

    \na\n-\n

    Please type in a string:

    Please write a program which asks the user for a string and then prints it out so that exactly 20 characters are displayed. If the input is shorter than 20 characters, the beginning of the line is filled in with * characters.

    You may assume the input string is at most 20 characters long.

    Please type in a string: python

    \n**************python\n

    Please type in a string: alongerstring

    \n*******alongerstring\n

    Please type in a string: averyverylongstring

    \n*averyverylongstring\n

    Please write a program which asks the user for a string and then prints out a frame of * characters with the word in the centre. The width of the frame should be 30 characters. You may assume the input string will always fit inside the frame.

    If the length of the input string is an odd number, you may print out the word in either of the two possible centre locations.

    Word: testing

    \n******************************\n*          testing           *\n******************************\n

    Word: python

    \n******************************\n*           python           *\n******************************\n

    Substrings and slices

    A substring of a string is a sequence of characters that forms a part of the string. For example, the string example contains the substrings exam, amp and ple, among others. In Python programming, the process of selecting substrings is usually called slicing, and a substring is often referred to as a slice of the string. The two terms can often be used interchangeably.

    If you know the beginning and end indexes of the slice you wish to extract, you can do so with the notation [a:b]. This means the slice begins at the index a and ends at the last character before index b - that is, including the first, but excluding the last. You can think of the indexes as separator lines drawn on the left side of the indexed character, as illustrated in the image below:

    \n \n \n \n \n \n \"3\n \n \n

    Let's have a closer look at some sliced strings:

    input_string = \"presumptious\"\n\nprint(input_string[0:3])\nprint(input_string[4:10])\n\n# if the beginning index is left out, it defaults to 0\nprint(input_string[:3])\n\n# if the end index is left out, it defaults to the length of the string\nprint(input_string[4:])

    pre\numptio\npre\numptious

    In Python string processing the interval [a:b] is half open, which in this case means that the character at the beginning index a is included in the interval, but the character at the end index b is left out. Why is that?

    There is no profound reason for this feature. Rather it is a convention inherited from other programming languages.

    Half open intervals may feel unintuitive, but in practice they do have some advantages. For example, you can easily calculate the length of a slice with b-a. On the other hand, you must always remember that the character at the end index b will not be included in the slice.

    Please write a program which asks the user to type in a string. The program then prints out all the substrings which begin with the first character, from the shortest to the longest. Have a look at the example below.

    Please type in a string: test\nt\nte\ntes\ntest

    Please write a program which asks the user to type in a string. The program then prints out all the substrings which end with the last character, from the shortest to the longest. Have a look at the example below.

    Please type in a string: test\nt\nst\nest\ntest

    Searching for substrings

    The in operator can tell us if a string contains a particular substring. The Boolean expression a in b is true, if b contains the substring a.

    For example, this bit of code

    input_string = \"test\"\n\nprint(\"t\" in input_string)\nprint(\"x\" in input_string)\nprint(\"es\" in input_string)\nprint(\"ets\" in input_string)

    prints out the following:

    True\nFalse\nTrue\nFalse

    The program below lets the user search for substrings within a string hardcoded into the program:

    input_string = \"perpendicular\"\n\nwhile True:\n    substring = input(\"What are you looking for? \")\n    if substring in input_string:\n        print(\"Found it\")\n    else:\n        print(\"Not found\")

    What are you looking for? perp\nFound it\nWhat are you looking for? abc\nNot found\nWhat are you looking for? pen\nFound it\n...

    Please write a program which asks the user to input a string. The program then prints out different messages if the string contains any of the vowels a, e or o.

    You may assume the input will be in lowercase entirely. Have a look at the examples below.

    Please type in a string: hello there\na not found\ne found\no found

    Please type in a string: hiya\na found\ne not found\no not found

    The operator in returns a Boolean value, so it will only tell us if a substring exists in a string, but it will not be useful in finding out where exactly it is. Instead, the Python string method find can be used for this purpose. It takes the substring searched for as an argument, and returns either the first index where it is found, or -1 if the substring is not found within the string.

    The image below illustrates how it is used:

    \n \n \n \n \n \n \"3\n \n \n

    Some examples of how find is used:

    input_string = \"test\"\n\nprint(input_string.find(\"t\"))\nprint(input_string.find(\"x\"))\nprint(input_string.find(\"es\"))\nprint(input_string.find(\"ets\"))

    0\n-1\n1\n-1

    The above substring search example implemented with find:

    input_string = \"perpendicular\"\n\nwhile True:\n    substring = input(\"What are you looking for? \")\n    index = input_string.find(substring)\n    if index >= 0:\n        print(f\"Found it at the index {index}\")\n    else:\n        print(\"Not found\")

    What are you looking for? perp\nFound it at the index 0\nWhat are you looking for? abc\nNot found\nWhat are you looking for? pen\nFound it at the index 3\n...

    Above we used the string method find. Methods work quite similarly to the functions covered in the previous part. What distinguishes them from functions is that methods are always attached to the object they are called on. The object is the entity named before the method in the method call. In the case of find the object is the string where the method looks for the substring it has as an argument.

    Please write a program which asks the user to type in a string and a single character. The program then prints the first three character slice which begins with the character specified by the user. You may assume the input string is at least three characters long. The program must print out three characters, or else nothing.

    Pay special attention to when there are less than two characters left in the string after the first occurrence of the character looked for. In that case nothing should be printed out, and there should not be any indexing errors when executing the program.

    Please type in a word: mammoth\nPlease type in a character: m\nmam

    Please type in a word: banana\nPlease type in a character: n\nnan

    Please type in a word: tomato\nPlease type in a character: x

    Please type in a word: python\nPlease type in a character: n

    Please make an extended version of the previous program, which prints out all the substrings which are at least three characters long, and which begin with the character specified by the user. You may assume the input string is at least three characters long.

    Please type in a word: mammoth\nPlease type in a character: m\nmam\nmmo\nmot

    Please type in a word: banana\nPlease type in a character: n\nnan

    Hint the following example may give you some inspiration as to how this exercise could be tackled:

    word = input(\"Word: \")\nwhile True:\n    if len(word) == 0:\n        break\n    print(word)\n    word = word[2:]

    Word: mammoth\nmammoth\nmmoth\noth\nh

    Please write a program which finds the second occurrence of a substring. If there is no second (or first) occurrence, the program should print out a message accordingly.

    In this exercise the occurrences cannot overlap. For example, in the string aaaa the second occurrence of the substring aa is at index 2.

    Some examples of expected behaviour:

    Please type in a string: abcabc\nPlease type in a substring: ab\nThe second occurrence of the substring is at index 3.

    Please type in a string: methodology\nPlease type in a substring: o\nThe second occurrence of the substring is at index 6.

    Please type in a string: aybabtu\nPlease type in a substring: ba\nThe substring does not occur twice in the string.

    ","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-3/2-working-with-strings.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 able to use the operators "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" with strings"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to find out the length of a string"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what is meant by string indexing"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to look for substrings within a string"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"string-operations","style":"position:relative;"},"children":[{"type":"text","value":"String operations"},{"type":"element","tagName":"a","properties":{"href":"#string-operations","ariaLabel":"string operations 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 can be combined, or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"concatenated"}]},{"type":"text","value":", with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" operator:"}]},{"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":"begin "},{"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":"\"ex\""}]},{"type":"text","value":"\nend "},{"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":"\"ample\""}]},{"type":"text","value":"\nword "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" begin"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"end\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":"word"},{"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":"example"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" operator can also be used with a string, when the other operand is an integer. The string operand is then repeated the number of times specified by the integer. For example this would 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":"word "},{"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":"\"banana\""}]},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"bananabananabanana"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Using string operations together with a loop we can write a program which draws a pyramid:"}]},{"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":"n "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# number of layers in the pyramid"}]},{"type":"text","value":"\nrow "},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" n "},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\" \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" n "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" row"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n row "},{"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":"text","value":"\n n "},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This prints out the following:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":" *\n ***\n *****\n *******\n *********\n ***********\n *************\n ***************\n *****************\n *******************"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command within the loop prints a line, which begins with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" spaces, followed by whatever is stored in the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"row"}]},{"type":"text","value":". Then two stars are added to the end of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"row"}]},{"type":"text","value":", and the value of the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" is decreased by 1."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"String multiplied","tmcname":"part03-08_string_multiplied"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a string and an amount. The program then prints out the string as many times as specified by the amount. The printout should all be on one line, with no extra spaces or symbols added."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected behaviour:"}]},{"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":"hiya"}]},{"type":"text","value":"\nPlease type in an amount: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nhiyahiyahiyahiya"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-length-and-index-of-a-string","style":"position:relative;"},"children":[{"type":"text","value":"The length and index of a string"},{"type":"element","tagName":"a","properties":{"href":"#the-length-and-index-of-a-string","ariaLabel":"the length and index of a string 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 function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" returns the number of characters in a string, which is always an integer value. For example, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len(\"hey\")"}]},{"type":"text","value":" returns 3, because there are three characters in the string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hey"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program asks the user for a string and then prints it \"underlined\". The program prints a second line with as many "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" characters as is the length of the input:"}]},{"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":"input_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":"input_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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"-\""}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"input_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":"Please type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hi there!"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Hi there!\n---------\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The length of a string includes all the characters in the string, including whitespace. For example, the length of the string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bye bye"}]},{"type":"text","value":" is 7."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"The longer string","tmcname":"part03-09_longer_string"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for two strings and then prints out whichever is the longer of the two - that is, whichever has the more characters. If the strings are of equal length, the program should print out \"The strings are equally long\"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in string 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hey"}]},{"type":"text","value":"\nPlease type in string 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hiya"}]},{"type":"text","value":"\nhiya is longer"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in string 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"howdy doody"}]},{"type":"text","value":"\nPlease type in string 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hola"}]},{"type":"text","value":"\nhowdy doody is longer"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in string 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"hey"}]},{"type":"text","value":"\nPlease type in string 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"bye"}]},{"type":"text","value":"\nThe strings are equally long"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As strings are essentially sequences of characters, any single character in a string can also be retrieved. The operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]},{"type":"text","value":" finds the character with the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"index"}]},{"type":"text","value":" specified within the brackets."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The index refers to a position in the string, counting up from zero. The first character in the string has index 0, the second character has index 1, and so forth."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 646px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/23ff4a404e8e641ddf64851041396dde/27524/3_2_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: 24.782608695652172%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAApUlEQVQY012Q2QqFMAxE+///6HMFrVq7uGFzOYFIuYEhzWSazU3TJOu6yjzPEmOUbdsUy7IoSilSaxXvvWIcR+XR55wFa619cPd9C7iuSwk+n+cpz/Moh8fwx3FortdbQTNnj33fZRiGL0l3CvCRJgY4fEpJNWwFxyZs6qwAXRG876txCEEFNOrPgIfvz0NxYuD+R7aYG1EMMR7QxBqwOjlgEzPlD17sg2RMIZEkAAAAAElFTkSuQmCC'); 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":["/static/23ff4a404e8e641ddf64851041396dde/a0b58/3_2_1.webp 230w","/static/23ff4a404e8e641ddf64851041396dde/bc10c/3_2_1.webp 460w","/static/23ff4a404e8e641ddf64851041396dde/8c2f2/3_2_1.webp 646w"],"sizes":"(max-width: 646px) 100vw, 646px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/23ff4a404e8e641ddf64851041396dde/81c8e/3_2_1.png 230w","/static/23ff4a404e8e641ddf64851041396dde/08a84/3_2_1.png 460w","/static/23ff4a404e8e641ddf64851041396dde/27524/3_2_1.png 646w"],"sizes":"(max-width: 646px) 100vw, 646px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/23ff4a404e8e641ddf64851041396dde/27524/3_2_1.png","alt":"3 2 1","title":"3 2 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":"p","properties":{},"children":[{"type":"text","value":"For example, this program"}]},{"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":"\ninput_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":"input_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","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":"input_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":"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":"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":"input_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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"would print out this:"}]},{"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":"monkey"}]},{"type":"text","value":"\nm\no\nk"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Since the first character in a string has the index 0, the last character has the index "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"length - 1"}]},{"type":"text","value":". The following program prints out the first and the last characters 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":"input_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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"First character: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" input_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","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":"\"Last character: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" input_string"},{"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":"input_string"},{"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":"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":"testing"}]},{"type":"text","value":"\nFirst character: t\nLast character: g"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program loops through all the characters in a string from first to last:"}]},{"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":"input_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":"\nindex "},{"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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" index "},{"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":"input_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 "},{"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":"input_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"index"},{"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 index "},{"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":"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":"test"}]},{"type":"text","value":"\nt\ne\ns\nt"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can also use negative indexing to access characters counting from the end of the string. The last character in a string is at index -1, the second to last character is at index -2, and so forth. You can think of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input_string[-1]"}]},{"type":"text","value":" as shorthand for "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input_string[len(input_string) - 1]"}]},{"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: 616px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/373faf879dbcd0f2263ba34a06adccbb/40040/3_2_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: 26.08695652173913%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAsklEQVQY04WP2QqFMAxE+/+fZy1YRRR8cUPrioo6MoG8XbgDp8t0GhLT9z1CCOi6TmjbFtu2CXVdo6oqoWkaMHscB6j3fX9inufBdV1Q3fcNeiqe6THM/Z8MF3aTZZnsFLsYhgHTNAnrugp6n+cZ4zhi33csyyIT8i8zRrtiEe1AP2uIbyxCeKd/nqcU04x6hqOo9FwUBaIoQpqm8N4jSRI45xDHsUC/LEtYa+Utz3OZkHytf3+/GpjsnwAAAABJRU5ErkJggg=='); 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":["/static/373faf879dbcd0f2263ba34a06adccbb/a0b58/3_2_2.webp 230w","/static/373faf879dbcd0f2263ba34a06adccbb/bc10c/3_2_2.webp 460w","/static/373faf879dbcd0f2263ba34a06adccbb/26c8a/3_2_2.webp 616w"],"sizes":"(max-width: 616px) 100vw, 616px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/373faf879dbcd0f2263ba34a06adccbb/81c8e/3_2_2.png 230w","/static/373faf879dbcd0f2263ba34a06adccbb/08a84/3_2_2.png 460w","/static/373faf879dbcd0f2263ba34a06adccbb/40040/3_2_2.png 616w"],"sizes":"(max-width: 616px) 100vw, 616px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/373faf879dbcd0f2263ba34a06adccbb/40040/3_2_2.png","alt":"3 2 2","title":"3 2 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 example from above can be simplified with negative indexing:"}]},{"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":"input_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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"First character: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" input_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","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":"\"Last character: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" input_string"},{"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":"testing"}]},{"type":"text","value":"\nFirst character: t\nLast character: g"}]}]},{"type":"element","tagName":"h2","properties":{"id":"indexerror-string-index-out-of-range","style":"position:relative;"},"children":[{"type":"text","value":"IndexError: string index out of range"},{"type":"element","tagName":"a","properties":{"href":"#indexerror-string-index-out-of-range","ariaLabel":"indexerror string index out of range 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":"If you tried the above examples for yourself, you may already have come across the error message "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"IndexError: string index out of range"}]},{"type":"text","value":". This error appears if you try to access an index which is not present in the 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":"input_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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The tenth character: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" input_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":"9"}]},{"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":"introduction to programming"}]},{"type":"text","value":"\nThe tenth character: i"}]}]},{"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":"python"}]}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"Traceback (most recent call last):\nFile \""},{"type":"element","tagName":"stdin","properties":{},"children":[{"type":"text","value":"\", line 1, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nIndexError: string index out of range"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sometimes an indexing error is caused by a bug in the code. For example, it is quite common to index too far when trying to access the last character in 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":"input_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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Last character: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" input_string"},{"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":"input_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":"p","properties":{},"children":[{"type":"text","value":"Since string indexing begins at zero, the last character is at index "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len(input_string) - 1"}]},{"type":"text","value":", not at "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len(input_string)"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are situations where the program should prepare for errors caused by input from the user:"}]},{"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":"input_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":"if"}]},{"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":"input_string"},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"First character: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" input_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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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 input string is empty. There is no first character.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the example above, if the programmer hadn't included a check for the length of the input string, a string of length zero would have caused an error. A string of length zero is also called an empty string, and here it would be achieved by just pressing Enter at the input prompt."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"End to beginning","tmcname":"part03-10_end_to_beginning"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a string. The program then prints out the input string in reversed order, from end to beginning. Each character should be on a separate line."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected behaviour:"}]},{"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":"hiya"}]},{"type":"text","value":"\na\ny\ni\nh"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Second and second to last characters","tmcname":"part03-11_second_and_second_to_last"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a string. The program then prints out a message based on whether the second character and the second to last character are the same or not. See the examples below."}]},{"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":"python"}]},{"type":"text","value":"\nThe second and the second to last characters are different"}]}]},{"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":"pascal"}]},{"type":"text","value":"\nThe second and the second to last characters are a"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"A line of hashes","tmcname":"part03-12_line_of_hashes"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which prints out a line of hash characters, the width of which is chosen by the user."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Width: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"###\n"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Width: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"########\n"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"A rectangle of hashes","tmcname":"part03-13_rectangle_of_hashes"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please modify the previous program so that it also asks for the height, and prints out a rectangle of hash characters accordingly."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Width: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nHeight: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n##########\n##########\n##########"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Underlining","tmcname":"part03-14_underlining"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for strings using a loop. The program prints out each string underlined as shown in the examples below. The execution ends when the user inputs an empty string - that is, just presses Enter at the prompt."}]},{"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":"Hi there!"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Hi there!\n---------\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"This is a test"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"This is a test\n--------------\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a string: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"a"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"a\n-\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a string:"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Right-aligned","tmcname":"part03-15_right_aligned"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a string and then prints it out so that exactly 20 characters are displayed. If the input is shorter than 20 characters, the beginning of the line is filled in with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" characters."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume the input string is at most 20 characters long."}]},{"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":"python"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"**************python\n"}]}]},{"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":"alongerstring"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"*******alongerstring\n"}]}]},{"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":"averyverylongstring"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"*averyverylongstring\n"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"A framed word","tmcname":"part03-16_framed_word"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a string and then prints out a frame of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" characters with the word in the centre. The width of the frame should be 30 characters. You may assume the input string will always fit inside the frame."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the length of the input string is an odd number, you may print out the word in either of the two possible centre locations."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"testing"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"******************************\n* testing *\n******************************\n"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"******************************\n* python *\n******************************\n"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"substrings-and-slices","style":"position:relative;"},"children":[{"type":"text","value":"Substrings and slices"},{"type":"element","tagName":"a","properties":{"href":"#substrings-and-slices","ariaLabel":"substrings and 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":"A "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"substring"}]},{"type":"text","value":" of a string is a sequence of characters that forms a part of the string. For example, the string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"example"}]},{"type":"text","value":" contains the substrings "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exam"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"amp"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ple"}]},{"type":"text","value":", among others. In Python programming, the process of selecting substrings is usually called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"slicing"}]},{"type":"text","value":", and a substring is often referred to as a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"slice"}]},{"type":"text","value":" of the string. The two terms can often be used interchangeably."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you know the beginning and end indexes of the slice you wish to extract, you can do so with the notation "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[a:b]"}]},{"type":"text","value":". This means the slice begins at the index "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" and ends at the last character before index "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" - that is, including the first, but excluding the last. You can think of the indexes as separator lines drawn on the left side of the indexed character, as illustrated in the image below:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 581px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/2ff2c1c4d3ebbc4413f1f393bd3f8f0c/92d15/3_2_3.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: 33.04347826086957%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAAsTAAALEwEAmpwYAAABZUlEQVQoz02RW2vbQBCF9f//TENaKMS9pA9u3L7YahOKH5xidLF1XV1WXkmr1VdmIdADwxkNM0dnZ4KmaSiKgrZtyfOcYRgoy9J/C/d9j3MLVVWRpilJkvhQSpFlme9zziFY15VABKVRBuI4pq5rLpeLrwnneeaHhauy5Hq9ouoamRNBCV9TCmstgTGG2+3GOI50XedZa+2djeOEMRN9P9D1mnGaGSfLCsx28a6kT0yImCAQ5TdIUcTmeQbncN0rVv1maV5wzQtWPXs2xZ5VJ+h+wNrZz4mpaZoIjsejTwSyjyiKyIuCXjXoZMcQPWCSL5jkMzr+5PP2/JHb9RdxlPr9y65lPZIH/Af5iziWZy/WMjfPTMWWW7ZlKp+Yyh22/olTO0xzQmvje+VFy7L4XQZymbcryVG+PT6y3x8IwwPZ6YH2fEf1ekd7vqc8vaM9v8ekH/j75yvb7z84HPaEYehjs9nwD86xDxViuRttAAAAAElFTkSuQmCC'); 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":["/static/2ff2c1c4d3ebbc4413f1f393bd3f8f0c/a0b58/3_2_3.webp 230w","/static/2ff2c1c4d3ebbc4413f1f393bd3f8f0c/bc10c/3_2_3.webp 460w","/static/2ff2c1c4d3ebbc4413f1f393bd3f8f0c/39daa/3_2_3.webp 581w"],"sizes":"(max-width: 581px) 100vw, 581px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/2ff2c1c4d3ebbc4413f1f393bd3f8f0c/81c8e/3_2_3.png 230w","/static/2ff2c1c4d3ebbc4413f1f393bd3f8f0c/08a84/3_2_3.png 460w","/static/2ff2c1c4d3ebbc4413f1f393bd3f8f0c/92d15/3_2_3.png 581w"],"sizes":"(max-width: 581px) 100vw, 581px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/2ff2c1c4d3ebbc4413f1f393bd3f8f0c/92d15/3_2_3.png","alt":"3 2 3","title":"3 2 3","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":"Let's have a closer look at some sliced 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":"input_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":"\"presumptious\""}]},{"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":"input_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":"3"}]},{"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":"input_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":"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":"10"}]},{"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","comment"]},"children":[{"type":"text","value":"# if the beginning index is left out, it defaults to 0"}]},{"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":"input_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","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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# if the end index is left out, it defaults to the length of the string"}]},{"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":"input_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":"4"}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"pre\numptio\npre\numptious"}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Half open intervals"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In Python string processing the interval "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[a:b]"}]},{"type":"text","value":" is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"half open"}]},{"type":"text","value":", which in this case means that the character at the beginning index "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" is included in the interval, but the character at the end index "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" is left out. Why is that?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There is no profound reason for this feature. Rather it is a convention inherited from other programming languages."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Half open intervals may feel unintuitive, but in practice they do have some advantages. For example, you can easily calculate the length of a slice with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b-a"}]},{"type":"text","value":". On the other hand, you must always remember that the character at the end index "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" will not be included in the slice."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Substrings, part 1","tmcname":"part03-17_substrings_part_1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in a string. The program then prints out all the substrings which begin with the first character, from the shortest to the longest. Have a look at the example below."}]},{"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":"test"}]},{"type":"text","value":"\nt\nte\ntes\ntest"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Substrings, part 2","tmcname":"part03-18_substrings_part_2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in a string. The program then prints out all the substrings which end with the last character, from the shortest to the longest. Have a look at the example below."}]},{"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":"test"}]},{"type":"text","value":"\nt\nst\nest\ntest"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"searching-for-substrings","style":"position:relative;"},"children":[{"type":"text","value":"Searching for substrings"},{"type":"element","tagName":"a","properties":{"href":"#searching-for-substrings","ariaLabel":"searching for substrings 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 "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" operator can tell us if a string contains a particular substring. The Boolean expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a in b"}]},{"type":"text","value":" is true, if "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" contains the substring "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, this bit of code"}]},{"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":"input_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":"\"test\""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"t\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" input_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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"x\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" input_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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"es\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" input_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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ets\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" input_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nFalse\nTrue\nFalse"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program below lets the user search for substrings within a string hardcoded into the program:"}]},{"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":"input_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":"\"perpendicular\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n substring "},{"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":"\"What are you looking for? \""}]},{"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":"if"}]},{"type":"text","value":" substring "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" input_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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Found it\""}]},{"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":"else"}]},{"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":"\"Not found\""}]},{"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":"What are you looking for? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"perp"}]},{"type":"text","value":"\nFound it\nWhat are you looking for? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"abc"}]},{"type":"text","value":"\nNot found\nWhat are you looking for? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pen"}]},{"type":"text","value":"\nFound it\n..."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Does it contain vowels","tmcname":"part03-19_does_it_contain_vowels"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to input a string. The program then prints out different messages if the string contains any of the vowels a, e or o."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume the input will be in lowercase entirely. Have a look at the examples below."}]},{"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":"hello there"}]},{"type":"text","value":"\na not found\ne found\no found"}]}]},{"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":"hiya"}]},{"type":"text","value":"\na found\ne not found\no not found"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" returns a Boolean value, so it will only tell us "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" a substring exists in a string, but it will not be useful in finding out "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"where"}]},{"type":"text","value":" exactly it is. Instead, the Python string method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find"}]},{"type":"text","value":" can be used for this purpose. It takes the substring searched for as an argument, and returns either the first index where it is found, or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":" if the substring is not found within the string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The image below illustrates how it is used:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 441px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/9a030cb2cdef85d275a8320bb284d0dd/efc6e/3_2_4.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: 36.95652173913043%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAAsTAAALEwEAmpwYAAABLElEQVQoz32R227CQAxE+f8vIyJXAjThgUqkLBWQ2+ZGUjan2iWBp3Yky5Y1HnnsBcA4jn+GGkdN4Xw+Y1kWvu+z3+9xHIcsy5jn0XxgMU4D/6Lr4H6HYYC2fdd9/6wnMZ0XOvfDgKwqHkpRNw1N29J2HXXb0vc9jZTUdU1ZFJRSUhQFUkrSNEWWJaNSr02NYFOWfMYx5e3Gd5JwE4KrEHwdDlyEIMtzLpcLx+ORPE256jpJEKcTeZbxeDzegq22oBt9z4+uhwFV1y9LStuaMNsyVuczTNvNWGy3W3R4vo8fBHxEEUvLIopjHM/DcV1jTyOKItPfbDZ4rksQBKa32+1ML0mSp+WqqgjXa0MIw9AQbNs231RKMT9OW+u6zogsl0vzdXu1Qghhbq3jF4wJFCAuvsawAAAAAElFTkSuQmCC'); 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":["/static/9a030cb2cdef85d275a8320bb284d0dd/a0b58/3_2_4.webp 230w","/static/9a030cb2cdef85d275a8320bb284d0dd/24c94/3_2_4.webp 441w"],"sizes":"(max-width: 441px) 100vw, 441px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/9a030cb2cdef85d275a8320bb284d0dd/81c8e/3_2_4.png 230w","/static/9a030cb2cdef85d275a8320bb284d0dd/efc6e/3_2_4.png 441w"],"sizes":"(max-width: 441px) 100vw, 441px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/9a030cb2cdef85d275a8320bb284d0dd/efc6e/3_2_4.png","alt":"3 2 4","title":"3 2 4","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":"Some examples of how "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find"}]},{"type":"text","value":" is used:"}]},{"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":"input_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":"\"test\""}]},{"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":"input_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"t\""}]},{"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":"input_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"x\""}]},{"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":"input_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"es\""}]},{"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":"input_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ets\""}]},{"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":"0\n-1\n1\n-1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The above substring search example implemented with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find"}]},{"type":"text","value":":"}]},{"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":"input_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":"\"perpendicular\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n substring "},{"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":"\"What are you looking for? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n index "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" input_string"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"substring"},{"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":"if"}]},{"type":"text","value":" index "},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Found it at the index "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"index"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"\"Not found\""}]},{"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":"What are you looking for? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"perp"}]},{"type":"text","value":"\nFound it at the index 0\nWhat are you looking for? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"abc"}]},{"type":"text","value":"\nNot found\nWhat are you looking for? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pen"}]},{"type":"text","value":"\nFound it at the index 3\n..."}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Methods"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Above we used the string "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"method"}]},{"type":"text","value":" "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find"}]},{"type":"text","value":". Methods work quite similarly to the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"functions"}]},{"type":"text","value":" covered in the previous part. What distinguishes them from functions is that methods are always attached to the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"object"}]},{"type":"text","value":" they are called on. The object is the entity named before the method in the method call. In the case of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find"}]},{"type":"text","value":" the object is the string where the method looks for the substring it has as an argument."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Find the first substring","tmcname":"part03-20_find_first_substring"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in a string and a single character. The program then prints the first three character slice which begins with the character specified by the user. You may assume the input string is at least three characters long. The program must print out three characters, or else nothing."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pay special attention to when there are less than two characters left in the string after the first occurrence of the character looked for. In that case nothing should be printed out, and there should not be any indexing errors when executing the program."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"mammoth"}]},{"type":"text","value":"\nPlease type in a character: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"m"}]},{"type":"text","value":"\nmam"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"banana"}]},{"type":"text","value":"\nPlease type in a character: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"n"}]},{"type":"text","value":"\nnan"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"tomato"}]},{"type":"text","value":"\nPlease type in a character: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"x"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]},{"type":"text","value":"\nPlease type in a character: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"n"}]}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Find all the substrings","tmcname":"part03-21_find_all_substrings"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please make an extended version of the previous program, which prints out "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"all the substrings which are at least three characters long"}]},{"type":"text","value":", and which begin with the character specified by the user. You may assume the input string is at least three characters long."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"mammoth"}]},{"type":"text","value":"\nPlease type in a character: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"m"}]},{"type":"text","value":"\nmam\nmmo\nmot"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"banana"}]},{"type":"text","value":"\nPlease type in a character: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"n"}]},{"type":"text","value":"\nnan"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint"}]},{"type":"text","value":" the following example may give you some inspiration as to how this exercise could be tackled:"}]},{"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":"word "},{"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":"\"Word: \""}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"if"}]},{"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":"word"},{"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":"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":"break"}]},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n word "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" word"},{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"mammoth"}]},{"type":"text","value":"\nmammoth\nmmoth\noth\nh"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"The second occurrence","tmcname":"part03-22_second_occurrence"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which finds the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"second"}]},{"type":"text","value":" occurrence of a substring. If there is no second (or first) occurrence, the program should print out a message accordingly."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise the occurrences "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"cannot"}]},{"type":"text","value":" overlap. For example, in the string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aaaa"}]},{"type":"text","value":" the second occurrence of the substring "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"aa"}]},{"type":"text","value":" is at index 2."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"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":"abcabc"}]},{"type":"text","value":"\nPlease type in a substring: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ab"}]},{"type":"text","value":"\nThe second occurrence of the substring is at index 3."}]}]},{"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":"methodology"}]},{"type":"text","value":"\nPlease type in a substring: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"o"}]},{"type":"text","value":"\nThe second occurrence of the substring is at index 6."}]}]},{"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":"aybabtu"}]},{"type":"text","value":"\nPlease type in a substring: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ba"}]},{"type":"text","value":"\nThe substring does not occur twice in the string."}]}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will be able to use the operators + and * with strings
    • \n
    • You will know how to find out the length of a string
    • \n
    • You will know what is meant by string indexing
    • \n
    • You will know how to look for substrings within a string
    • \n

    String operations

    Strings can be combined, or concatenated, with the + operator:

    begin = \"ex\"\nend = \"ample\"\nword = begin+end\nprint(word)

    example

    The * operator can also be used with a string, when the other operand is an integer. The string operand is then repeated the number of times specified by the integer. For example this would work:

    word = \"banana\"\nprint(word*3)

    bananabananabanana

    Using string operations together with a loop we can write a program which draws a pyramid:

    n = 10 # number of layers in the pyramid\nrow = \"*\"\n\nwhile n > 0:\n    print(\" \" * n + row)\n    row += \"**\"\n    n -= 1

    This prints out the following:

              *\n         ***\n        *****\n       *******\n      *********\n     ***********\n    *************\n   ***************\n  *****************\n *******************

    The print command within the loop prints a line, which begins with n spaces, followed by whatever is stored in the variable row. Then two stars are added to the end of the variable row, and the value of the variable n is decreased by 1.

    Please write a program which asks the user for a string and an amount. The program then prints out the string as many times as specified by the amount. The printout should all be on one line, with no extra spaces or symbols added.

    An example of expected behaviour:

    Please type in a string: hiya\nPlease type in an amount: 4\nhiyahiyahiyahiya

    The length and index of a string

    The function len returns the number of characters in a string, which is always an integer value. For example, len(\"hey\") returns 3, because there are three characters in the string hey.

    The following program asks the user for a string and then prints it \"underlined\". The program prints a second line with as many - characters as is the length of the input:

    input_string = input(\"Please type in a string: \")\nprint(input_string)\nprint(\"-\"*len(input_string))

    Please type in a string: Hi there!

    \nHi there!\n---------\n

    The length of a string includes all the characters in the string, including whitespace. For example, the length of the string bye bye is 7.

    Please write a program which asks the user for two strings and then prints out whichever is the longer of the two - that is, whichever has the more characters. If the strings are of equal length, the program should print out \"The strings are equally long\".

    Some examples of expected behaviour:

    Please type in string 1: hey\nPlease type in string 2: hiya\nhiya is longer

    Please type in string 1: howdy doody\nPlease type in string 2: hola\nhowdy doody is longer

    Please type in string 1: hey\nPlease type in string 2: bye\nThe strings are equally long

    As strings are essentially sequences of characters, any single character in a string can also be retrieved. The operator [] finds the character with the index specified within the brackets.

    The index refers to a position in the string, counting up from zero. The first character in the string has index 0, the second character has index 1, and so forth.

    \n \n \n \n \n \n \"3\n \n \n

    For example, this program

    \ninput_string = input(\"Please type in a string: \")\nprint(input_string[0])\nprint(input_string[1])\nprint(input_string[3])\n

    would print out this:

    Please type in a string: monkey\nm\no\nk

    Since the first character in a string has the index 0, the last character has the index length - 1. The following program prints out the first and the last characters of a string:

    input_string = input(\"Please type in a string: \")\nprint(\"First character: \" + input_string[0])\nprint(\"Last character: \" + input_string[len(input_string) - 1])

    Please type in a string: testing\nFirst character: t\nLast character: g

    The following program loops through all the characters in a string from first to last:

    input_string = input(\"Please type in a string: \")\nindex = 0\nwhile index < len(input_string):\n    print(input_string[index])\n    index += 1

    Please type in a string: test\nt\ne\ns\nt

    You can also use negative indexing to access characters counting from the end of the string. The last character in a string is at index -1, the second to last character is at index -2, and so forth. You can think of input_string[-1] as shorthand for input_string[len(input_string) - 1].

    \n \n \n \n \n \n \"3\n \n \n

    The example from above can be simplified with negative indexing:

    input_string = input(\"Please type in a string: \")\nprint(\"First character: \" + input_string[0])\nprint(\"Last character: \" + input_string[-1])

    Please type in a string: testing\nFirst character: t\nLast character: g

    IndexError: string index out of range

    If you tried the above examples for yourself, you may already have come across the error message IndexError: string index out of range. This error appears if you try to access an index which is not present in the string.

    input_string = input(\"Please type in a string: \")\nprint(\"The tenth character: \" + input_string[9])

    Please type in a string: introduction to programming\nThe tenth character: i

    Please type in a string: python

    Traceback (most recent call last):\nFile \"\", line 1, in \nIndexError: string index out of range

    Sometimes an indexing error is caused by a bug in the code. For example, it is quite common to index too far when trying to access the last character in a string:

    input_string = input(\"Please type in a string: \")\nprint(\"Last character: \" + input_string[len(input_string)])

    Since string indexing begins at zero, the last character is at index len(input_string) - 1, not at len(input_string).

    There are situations where the program should prepare for errors caused by input from the user:

    input_string = input(\"Please type in a string: \")\nif len(input_string) > 0:\n    print(\"First character: \" + input_string[0])\nelse:\n    print(\"The input string is empty. There is no first character.\")

    In the example above, if the programmer hadn't included a check for the length of the input string, a string of length zero would have caused an error. A string of length zero is also called an empty string, and here it would be achieved by just pressing Enter at the input prompt.

    Please write a program which asks the user for a string. The program then prints out the input string in reversed order, from end to beginning. Each character should be on a separate line.

    An example of expected behaviour:

    Please type in a string: hiya\na\ny\ni\nh

    Please write a program which asks the user for a string. The program then prints out a message based on whether the second character and the second to last character are the same or not. See the examples below.

    Please type in a string: python\nThe second and the second to last characters are different

    Please type in a string: pascal\nThe second and the second to last characters are a

    Please write a program which prints out a line of hash characters, the width of which is chosen by the user.

    Width: 3

    \n###\n

    Width: 8

    \n########\n

    Please modify the previous program so that it also asks for the height, and prints out a rectangle of hash characters accordingly.

    Width: 10\nHeight: 3\n##########\n##########\n##########

    Please write a program which asks the user for strings using a loop. The program prints out each string underlined as shown in the examples below. The execution ends when the user inputs an empty string - that is, just presses Enter at the prompt.

    Please type in a string: Hi there!

    \nHi there!\n---------\n

    Please type in a string: This is a test

    \nThis is a test\n--------------\n

    Please type in a string: a

    \na\n-\n

    Please type in a string:

    Please write a program which asks the user for a string and then prints it out so that exactly 20 characters are displayed. If the input is shorter than 20 characters, the beginning of the line is filled in with * characters.

    You may assume the input string is at most 20 characters long.

    Please type in a string: python

    \n**************python\n

    Please type in a string: alongerstring

    \n*******alongerstring\n

    Please type in a string: averyverylongstring

    \n*averyverylongstring\n

    Please write a program which asks the user for a string and then prints out a frame of * characters with the word in the centre. The width of the frame should be 30 characters. You may assume the input string will always fit inside the frame.

    If the length of the input string is an odd number, you may print out the word in either of the two possible centre locations.

    Word: testing

    \n******************************\n*          testing           *\n******************************\n

    Word: python

    \n******************************\n*           python           *\n******************************\n

    Substrings and slices

    A substring of a string is a sequence of characters that forms a part of the string. For example, the string example contains the substrings exam, amp and ple, among others. In Python programming, the process of selecting substrings is usually called slicing, and a substring is often referred to as a slice of the string. The two terms can often be used interchangeably.

    If you know the beginning and end indexes of the slice you wish to extract, you can do so with the notation [a:b]. This means the slice begins at the index a and ends at the last character before index b - that is, including the first, but excluding the last. You can think of the indexes as separator lines drawn on the left side of the indexed character, as illustrated in the image below:

    \n \n \n \n \n \n \"3\n \n \n

    Let's have a closer look at some sliced strings:

    input_string = \"presumptious\"\n\nprint(input_string[0:3])\nprint(input_string[4:10])\n\n# if the beginning index is left out, it defaults to 0\nprint(input_string[:3])\n\n# if the end index is left out, it defaults to the length of the string\nprint(input_string[4:])

    pre\numptio\npre\numptious

    In Python string processing the interval [a:b] is half open, which in this case means that the character at the beginning index a is included in the interval, but the character at the end index b is left out. Why is that?

    There is no profound reason for this feature. Rather it is a convention inherited from other programming languages.

    Half open intervals may feel unintuitive, but in practice they do have some advantages. For example, you can easily calculate the length of a slice with b-a. On the other hand, you must always remember that the character at the end index b will not be included in the slice.

    Please write a program which asks the user to type in a string. The program then prints out all the substrings which begin with the first character, from the shortest to the longest. Have a look at the example below.

    Please type in a string: test\nt\nte\ntes\ntest

    Please write a program which asks the user to type in a string. The program then prints out all the substrings which end with the last character, from the shortest to the longest. Have a look at the example below.

    Please type in a string: test\nt\nst\nest\ntest

    Searching for substrings

    The in operator can tell us if a string contains a particular substring. The Boolean expression a in b is true, if b contains the substring a.

    For example, this bit of code

    input_string = \"test\"\n\nprint(\"t\" in input_string)\nprint(\"x\" in input_string)\nprint(\"es\" in input_string)\nprint(\"ets\" in input_string)

    prints out the following:

    True\nFalse\nTrue\nFalse

    The program below lets the user search for substrings within a string hardcoded into the program:

    input_string = \"perpendicular\"\n\nwhile True:\n    substring = input(\"What are you looking for? \")\n    if substring in input_string:\n        print(\"Found it\")\n    else:\n        print(\"Not found\")

    What are you looking for? perp\nFound it\nWhat are you looking for? abc\nNot found\nWhat are you looking for? pen\nFound it\n...

    Please write a program which asks the user to input a string. The program then prints out different messages if the string contains any of the vowels a, e or o.

    You may assume the input will be in lowercase entirely. Have a look at the examples below.

    Please type in a string: hello there\na not found\ne found\no found

    Please type in a string: hiya\na found\ne not found\no not found

    The operator in returns a Boolean value, so it will only tell us if a substring exists in a string, but it will not be useful in finding out where exactly it is. Instead, the Python string method find can be used for this purpose. It takes the substring searched for as an argument, and returns either the first index where it is found, or -1 if the substring is not found within the string.

    The image below illustrates how it is used:

    \n \n \n \n \n \n \"3\n \n \n

    Some examples of how find is used:

    input_string = \"test\"\n\nprint(input_string.find(\"t\"))\nprint(input_string.find(\"x\"))\nprint(input_string.find(\"es\"))\nprint(input_string.find(\"ets\"))

    0\n-1\n1\n-1

    The above substring search example implemented with find:

    input_string = \"perpendicular\"\n\nwhile True:\n    substring = input(\"What are you looking for? \")\n    index = input_string.find(substring)\n    if index >= 0:\n        print(f\"Found it at the index {index}\")\n    else:\n        print(\"Not found\")

    What are you looking for? perp\nFound it at the index 0\nWhat are you looking for? abc\nNot found\nWhat are you looking for? pen\nFound it at the index 3\n...

    Above we used the string method find. Methods work quite similarly to the functions covered in the previous part. What distinguishes them from functions is that methods are always attached to the object they are called on. The object is the entity named before the method in the method call. In the case of find the object is the string where the method looks for the substring it has as an argument.

    Please write a program which asks the user to type in a string and a single character. The program then prints the first three character slice which begins with the character specified by the user. You may assume the input string is at least three characters long. The program must print out three characters, or else nothing.

    Pay special attention to when there are less than two characters left in the string after the first occurrence of the character looked for. In that case nothing should be printed out, and there should not be any indexing errors when executing the program.

    Please type in a word: mammoth\nPlease type in a character: m\nmam

    Please type in a word: banana\nPlease type in a character: n\nnan

    Please type in a word: tomato\nPlease type in a character: x

    Please type in a word: python\nPlease type in a character: n

    Please make an extended version of the previous program, which prints out all the substrings which are at least three characters long, and which begin with the character specified by the user. You may assume the input string is at least three characters long.

    Please type in a word: mammoth\nPlease type in a character: m\nmam\nmmo\nmot

    Please type in a word: banana\nPlease type in a character: n\nnan

    Hint the following example may give you some inspiration as to how this exercise could be tackled:

    word = input(\"Word: \")\nwhile True:\n    if len(word) == 0:\n        break\n    print(word)\n    word = word[2:]

    Word: mammoth\nmammoth\nmmoth\noth\nh

    Please write a program which finds the second occurrence of a substring. If there is no second (or first) occurrence, the program should print out a message accordingly.

    In this exercise the occurrences cannot overlap. For example, in the string aaaa the second occurrence of the substring aa is at index 2.

    Some examples of expected behaviour:

    Please type in a string: abcabc\nPlease type in a substring: ab\nThe second occurrence of the substring is at index 3.

    Please type in a string: methodology\nPlease type in a substring: o\nThe second occurrence of the substring is at index 6.

    Please type in a string: aybabtu\nPlease type in a substring: ba\nThe substring does not occur twice in the string.

    ","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-3/2-working-with-strings.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-3/3-more-loops/page-data.json b/page-data/part-3/3-more-loops/page-data.json index 16de25012..6627c9f7c 100644 --- a/page-data/part-3/3-more-loops/page-data.json +++ b/page-data/part-3/3-more-loops/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-3/3-more-loops", - "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 understand when the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":" command is needed to break out of loops"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":" command to move to the next iteration"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will understand how nested loops work"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-break-command","style":"position:relative;"},"children":[{"type":"text","value":"The break command"},{"type":"element","tagName":"a","properties":{"href":"#the-break-command","ariaLabel":"the break command 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":"You have already come across the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":" command. It can be used to stop the execution of a loop immediately. A typical example of where it is used is a situation where the program asks the user for input, and the execution ends only when a specific input is received."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The same functionality can be achieved without the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":" command, using a suitable condition. The two programs below both ask the user to type in numbers, and calculate the sum of the numbers until the user types in -1."}]},{"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":"# 1st version using the break command"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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"]},"children":[{"type":"text","value":"\"Please type in a number, -1 to exit: \""}]},{"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":"if"}]},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" number\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" "},{"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\"The sum 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":"sum"}]},{"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":"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":"# 2nd version without the break command"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"0"}]},{"type":"text","value":"\nnumber "},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"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","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 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"]},"children":[{"type":"text","value":"\"Please type in a number, -1 to exit: \""}]},{"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":"if"}]},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" number\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" "},{"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\"The sum 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":"sum"}]},{"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":"p","properties":{},"children":[{"type":"text","value":"Both programs print out the same thing with the same inputs, for example:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nThe sum is 14"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, the two programs are functionally practically identical. However, the first method is often easier, as the condition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number == -1"}]},{"type":"text","value":" appears only once, and the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" doesn't have to be initialised outside the loop."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":" command and a suitable condition can also be used together in a while loop. For example, the following loop is repeated as long as the sum of the numbers is at most 100, but it also stops if the user types in the number -1."}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"100"}]},{"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"]},"children":[{"type":"text","value":"\"Please type in a number, -1 to exit: \""}]},{"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":"if"}]},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" number\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" "},{"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\"The sum 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":"sum"}]},{"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":"p","properties":{},"children":[{"type":"text","value":"Some examples of the program's execution:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"15"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"21"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nThe sum is 44"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"15"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"21"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"45"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"17"}]},{"type":"text","value":"\nThe sum is 106"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the first example the execution of the loop stops because the user types in the number -1. In the second example it stops because the sum of the numbers exceeds 100."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As always in programming, there are many ways to reach the same functionality. The following program is functionally identical to the above:"}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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"]},"children":[{"type":"text","value":"\"Please type in a number, -1 to exit: \""}]},{"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":"if"}]},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" number\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"100"}]},{"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":"break"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" "},{"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\"The sum 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":"sum"}]},{"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":"h2","properties":{"id":"the-continue-command","style":"position:relative;"},"children":[{"type":"text","value":"The continue command"},{"type":"element","tagName":"a","properties":{"href":"#the-continue-command","ariaLabel":"the continue command 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":"Another way to change the way a loop is executed is the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":" command. It causes the execution of the loop to jump straight to the beginning of the loop, where the condition of the loop is. Then the execution continues normally with checking the condition:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 727px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/a248e448eca1ebe875046227836fc069/c54b3/3_3_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: 78.69565217391303%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB50lEQVQ4y41U23LaMBT0/39QXtOXPiQUQgZoXQYM2AYMFHy/yvZmVkGKif1QzWgkS+s9q3NWMtq2BVvTNMjzXM7VGsfuvDsWRYG6rh9w7IYCcNPzvB7gO7n6Pp1OqKqqT4h7S9MUy+USQgh8b4pENQZfrVaIoqiHNZSyyWSC19cXzGYzGZ1g3/cf0uA4Dna7HRaLBUajEcbjMfb7PcIwlD2O488jMx+u42C9XuN8PqMsC0lAMgLVCYIgkFjv5EksyYihKNbgdrt9HVk0LdaW9SCfOWJURViWpd7bWBvU4l4UfKaEAY1K1KiyGxJ3AvPlCfXVRCMyTZgkiSaMoxh5mWH3b4Mfv57x254jv2PZmCbDDyJkoYfCHeGy/Inq+IZWpBLA41yvV22rNEkRRD7+Hv7gbTXGzH5HUsZ6n+ky1DGUbI6qqFEcY7vd9qr8aIF7yoSQeTVUFYfMS1CWZYM+7AZRc6bHYJ4oN4pC+TMTqwslhPZa90bRTsRyT5lbF4WslmVJb83nc9kPh4OsKAFdQkVKU0+nU5imKee0EnmkbaiCBLZty5Hk3CAhVXTva9fgruvKkV5UWHJpH3KBJu56bejqqYtA5bRS7+p1Hwcq/N/H4Xg86uCDrw3l0kdDPw+tXS6XHiEVfgA5YdmRLsqPTAAAAABJRU5ErkJggg=='); 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":["/static/a248e448eca1ebe875046227836fc069/a0b58/3_3_1.webp 230w","/static/a248e448eca1ebe875046227836fc069/bc10c/3_3_1.webp 460w","/static/a248e448eca1ebe875046227836fc069/34975/3_3_1.webp 727w"],"sizes":"(max-width: 727px) 100vw, 727px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a248e448eca1ebe875046227836fc069/81c8e/3_3_1.png 230w","/static/a248e448eca1ebe875046227836fc069/08a84/3_3_1.png 460w","/static/a248e448eca1ebe875046227836fc069/c54b3/3_3_1.png 727w"],"sizes":"(max-width: 727px) 100vw, 727px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/a248e448eca1ebe875046227836fc069/c54b3/3_3_1.png","alt":"3 3 1","title":"3 3 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":"p","properties":{},"children":[{"type":"text","value":"For example, the following program sums up numbers from input, but it only includes the numbers which are smaller than 10. If the number is 10 or greater, the execution jumps to the beginning of the loop and the number is not added to the sum."}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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"]},"children":[{"type":"text","value":"\"Please type in a number, -1 to exit: \""}]},{"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":"if"}]},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"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":"10"}]},{"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":"continue"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" number\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" "},{"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\"The sum 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":"sum"}]},{"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":"Please type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"99"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nThe sum is 16"}]}]},{"type":"element","tagName":"h2","properties":{"id":"nested-loops","style":"position:relative;"},"children":[{"type":"text","value":"Nested loops"},{"type":"element","tagName":"a","properties":{"href":"#nested-loops","ariaLabel":"nested loops 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":"Just like "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statements, loops can also be placed inside other loops. For example, the following program uses a loop to ask the user to input numbers. It then uses another loop inside the first one to print a countdown from the given number down to 1:"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"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":"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":"number"},{"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","number"]},"children":[{"type":"text","value":"1"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n4\n3\n2\n1\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n3\n2\n1\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\n6\n5\n4\n3\n2\n1\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When there are nested loops, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":" commands only affect the innermost loop which they are a part of. The previous example could also be written 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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"if"}]},{"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":"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":"break"}]},{"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 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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here the latter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":" command only stops the innermost loop, which is used to print the numbers."}]},{"type":"element","tagName":"h2","properties":{"id":"more-helper-variables-with-loops","style":"position:relative;"},"children":[{"type":"text","value":"More helper variables with loops"},{"type":"element","tagName":"a","properties":{"href":"#more-helper-variables-with-loops","ariaLabel":"more helper variables with loops 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've already used helper variables, which increase or decrease with every iteration of a loop, many times before, so the following program should look quite familiar in structure. The program prints out all even numbers above zero until it reaches a limit set by the user:"}]},{"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":"limit "},{"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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"\ni "},{"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":"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":" limit"},{"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":"i"},{"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":"2"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\n0\n2\n4\n6"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The helper variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"i"}]},{"type":"text","value":" is set to 0 before the loop, and it increases by two with every iteration."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Using nested loops sometimes necessitates a separate helper variable for the inner loop. The program below prints out a \"number pyramid\" based on a number given by the user:"}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"while"}]},{"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":"0"}]},{"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":"0"}]},{"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":" number"},{"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\""}]},{"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","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":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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 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 "},{"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 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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n0 1 2 3 4\n0 1 2 3\n0 1 2\n0 1\n0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this program the outer loop uses the helper variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":", which decreases by 1 with each iteration until it reaches 0. The helper variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"i"}]},{"type":"text","value":" is set to 0 just before the inner loop is entered, each time the outer loop repeats."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The inner loop uses the helper variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"i"}]},{"type":"text","value":", which increases by 1 with each iteration of the inner loop. The inner loop repeats until "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"i"}]},{"type":"text","value":" is equal to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":", and prints out each value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"i"}]},{"type":"text","value":" on the same line, separated by a space character. When the inner loop finishes, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command in the outer loop starts a new line."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now remember that with each iteration of the outer loop the value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" decreases, so the amount of times the inner loop repeats also decreases. With each repetition the line of numbers gets shorter, and thus we get the pyramid shape."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nested loops can get confusing fast, but understanding the way they work is essential. You may well find the Python Tutor "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":" helpful in understanding how this example works. Copy the above code into the code window of the tool and follow the formation of the printout and the changing values of the helper variables as the execution progresses."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Multiplication","tmcname":"part03-23_multiplication"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a positive integer number. The program then prints out a list of multiplication operations until both operands reach the number given by the user. See the examples below for details:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: 2\n1 x 1 = 1\n1 x 2 = 2\n2 x 1 = 2\n2 x 2 = 4"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: 3\n1 x 1 = 1\n1 x 2 = 2\n1 x 3 = 3\n2 x 1 = 2\n2 x 2 = 4\n2 x 3 = 6\n3 x 1 = 3\n3 x 2 = 6\n3 x 3 = 9"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"First letters of words","tmcname":"part03-24_first_letters_of_words"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in a sentence. The program then prints out the first letter of each word in the sentence, each letter on a separate line."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a sentence: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Humpty Dumpty sat on a wall"}]},{"type":"text","value":"\nH\nD\ns\no\na\nw"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Factorial","tmcname":"part03-25_factorial"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in an integer number. If the user types in a number equal to or below 0, the execution ends. Otherwise the program prints out the factorial of the number."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The factorial of a number involves multiplying the number by all the positive integers smaller than itself. In other words, it is the product of all positive integers less than or equal to the number. For example, the factorial of 5 is 1 * 2 * 3 * 4 * 5 = 120."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nThe factorial of the number 3 is 6\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nThe factorial of the number 4 is 24\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nThanks and bye!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nThe factorial of the number 1 is 1\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nThanks and bye!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Flip the pairs","tmcname":"part03-26_flip_the_pairs"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in a number. The program then prints out all the positive integer values from 1 up to the number. However, the order of the numbers is changed so that each pair or numbers is flipped. That is, 2 comes before 1, 4 before 3 and so forth. See the examples below for details."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n2\n1\n4\n3\n5"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\n2\n1\n4\n3\n6\n5"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Taking turns","tmcname":"part03-27_taking_turns"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in a number. The program then prints out the positive integers between 1 and the number itself, alternating between the two ends of the range as in the examples below."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n1\n5\n2\n4\n3"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\n1\n6\n2\n5\n3\n4"}]}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will understand when the break command is needed to break out of loops
    • \n
    • You will be able to use the continue command to move to the next iteration
    • \n
    • You will understand how nested loops work
    • \n

    The break command

    You have already come across the break command. It can be used to stop the execution of a loop immediately. A typical example of where it is used is a situation where the program asks the user for input, and the execution ends only when a specific input is received.

    The same functionality can be achieved without the break command, using a suitable condition. The two programs below both ask the user to type in numbers, and calculate the sum of the numbers until the user types in -1.

    # 1st version using the break command\n\nsum = 0\n\nwhile True:\n    number = int(input(\"Please type in a number, -1 to exit: \"))\n    if number == -1:\n        break\n    sum += number\n\nprint (f\"The sum is {sum}\")
    # 2nd version without the break command\n\nsum = 0\nnumber = 0\n\nwhile number != -1:\n    number = int(input(\"Please type in a number, -1 to exit: \"))\n    if number != -1:\n        sum += number\n\nprint (f\"The sum is {sum}\")

    Both programs print out the same thing with the same inputs, for example:

    Please type in a number, -1 to exit: 2\nPlease type in a number, -1 to exit: 4\nPlease type in a number, -1 to exit: 5\nPlease type in a number, -1 to exit: 3\nPlease type in a number, -1 to exit: -1\nThe sum is 14

    So, the two programs are functionally practically identical. However, the first method is often easier, as the condition number == -1 appears only once, and the variable number doesn't have to be initialised outside the loop.

    The break command and a suitable condition can also be used together in a while loop. For example, the following loop is repeated as long as the sum of the numbers is at most 100, but it also stops if the user types in the number -1.

    sum = 0\n\nwhile sum <= 100:\n    number = int(input(\"Please type in a number, -1 to exit: \"))\n    if number == -1:\n        break\n    sum += number\n\nprint (f\"The sum is {sum}\")

    Some examples of the program's execution:

    Please type in a number, -1 to exit: 15\nPlease type in a number, -1 to exit: 8\nPlease type in a number, -1 to exit: 21\nPlease type in a number, -1 to exit: -1\nThe sum is 44

    Please type in a number, -1 to exit: 15\nPlease type in a number, -1 to exit: 8\nPlease type in a number, -1 to exit: 21\nPlease type in a number, -1 to exit: 45\nPlease type in a number, -1 to exit: 17\nThe sum is 106

    In the first example the execution of the loop stops because the user types in the number -1. In the second example it stops because the sum of the numbers exceeds 100.

    As always in programming, there are many ways to reach the same functionality. The following program is functionally identical to the above:

    sum = 0\n\nwhile True:\n    number = int(input(\"Please type in a number, -1 to exit: \"))\n    if number == -1:\n        break\n    sum += number\n    if sum > 100:\n        break\n\nprint (f\"The sum is {sum}\")

    The continue command

    Another way to change the way a loop is executed is the continue command. It causes the execution of the loop to jump straight to the beginning of the loop, where the condition of the loop is. Then the execution continues normally with checking the condition:

    \n \n \n \n \n \n \"3\n \n \n

    For example, the following program sums up numbers from input, but it only includes the numbers which are smaller than 10. If the number is 10 or greater, the execution jumps to the beginning of the loop and the number is not added to the sum.

    sum = 0\n\nwhile True:\n    number = int(input(\"Please type in a number, -1 to exit: \"))\n    if number == -1:\n        break\n    if number >= 10:\n        continue\n    sum += number\n\nprint (f\"The sum is {sum}\")

    Please type in a number, -1 to exit: 4\nPlease type in a number, -1 to exit: 7\nPlease type in a number, -1 to exit: 99\nPlease type in a number, -1 to exit: 5\nPlease type in a number, -1 to exit: -1\nThe sum is 16

    Nested loops

    Just like if statements, loops can also be placed inside other loops. For example, the following program uses a loop to ask the user to input numbers. It then uses another loop inside the first one to print a countdown from the given number down to 1:

    while True:\n    number = int(input(\"Please type in a number: \"))\n    if number == -1:\n        break\n    while number > 0:\n        print(number)\n        number -= 1

    Please type in a number: 4\n4\n3\n2\n1\nPlease type in a number: 3\n3\n2\n1\nPlease type in a number: 6\n6\n5\n4\n3\n2\n1\nPlease type in a number: -1

    When there are nested loops, break and continue commands only affect the innermost loop which they are a part of. The previous example could also be written like this:

    while True:\n    number = int(input(\"Please type in a number: \"))\n    if number == -1:\n        break\n    while True:\n        if number <= 0:\n            break\n        print(number)\n        number -= 1

    Here the latter break command only stops the innermost loop, which is used to print the numbers.

    More helper variables with loops

    We've already used helper variables, which increase or decrease with every iteration of a loop, many times before, so the following program should look quite familiar in structure. The program prints out all even numbers above zero until it reaches a limit set by the user:

    limit = int(input(\"Please type in a number: \"))\ni = 0\nwhile i < limit:\n    print(i)\n    i += 2

    Please type in a number: 8\n0\n2\n4\n6

    The helper variable i is set to 0 before the loop, and it increases by two with every iteration.

    Using nested loops sometimes necessitates a separate helper variable for the inner loop. The program below prints out a \"number pyramid\" based on a number given by the user:

    number = int(input(\"Please type in a number: \"))\nwhile number > 0:\n    i = 0\n    while i < number:\n        print(f\"{i} \", end=\"\")\n        i += 1\n    print()\n    number -= 1

    Please type in a number: 5\n0 1 2 3 4\n0 1 2 3\n0 1 2\n0 1\n0

    In this program the outer loop uses the helper variable number, which decreases by 1 with each iteration until it reaches 0. The helper variable i is set to 0 just before the inner loop is entered, each time the outer loop repeats.

    The inner loop uses the helper variable i, which increases by 1 with each iteration of the inner loop. The inner loop repeats until i is equal to number, and prints out each value of i on the same line, separated by a space character. When the inner loop finishes, the print command in the outer loop starts a new line.

    Now remember that with each iteration of the outer loop the value of number decreases, so the amount of times the inner loop repeats also decreases. With each repetition the line of numbers gets shorter, and thus we get the pyramid shape.

    Nested loops can get confusing fast, but understanding the way they work is essential. You may well find the Python Tutor visualisation tool helpful in understanding how this example works. Copy the above code into the code window of the tool and follow the formation of the printout and the changing values of the helper variables as the execution progresses.

    Please write a program which asks the user for a positive integer number. The program then prints out a list of multiplication operations until both operands reach the number given by the user. See the examples below for details:

    Please type in a number: 2\n1 x 1 = 1\n1 x 2 = 2\n2 x 1 = 2\n2 x 2 = 4

    Please type in a number: 3\n1 x 1 = 1\n1 x 2 = 2\n1 x 3 = 3\n2 x 1 = 2\n2 x 2 = 4\n2 x 3 = 6\n3 x 1 = 3\n3 x 2 = 6\n3 x 3 = 9

    Please write a program which asks the user to type in a sentence. The program then prints out the first letter of each word in the sentence, each letter on a separate line.

    An example of expected behaviour:

    Please type in a sentence: Humpty Dumpty sat on a wall\nH\nD\ns\no\na\nw

    Please write a program which asks the user to type in an integer number. If the user types in a number equal to or below 0, the execution ends. Otherwise the program prints out the factorial of the number.

    The factorial of a number involves multiplying the number by all the positive integers smaller than itself. In other words, it is the product of all positive integers less than or equal to the number. For example, the factorial of 5 is 1 * 2 * 3 * 4 * 5 = 120.

    Some examples of expected behaviour:

    Please type in a number: 3\nThe factorial of the number 3 is 6\nPlease type in a number: 4\nThe factorial of the number 4 is 24\nPlease type in a number: -1\nThanks and bye!

    Please type in a number: 1\nThe factorial of the number 1 is 1\nPlease type in a number: 0\nThanks and bye!

    Please write a program which asks the user to type in a number. The program then prints out all the positive integer values from 1 up to the number. However, the order of the numbers is changed so that each pair or numbers is flipped. That is, 2 comes before 1, 4 before 3 and so forth. See the examples below for details.

    Please type in a number: 5\n2\n1\n4\n3\n5

    Please type in a number: 6\n2\n1\n4\n3\n6\n5

    Please write a program which asks the user to type in a number. The program then prints out the positive integers between 1 and the number itself, alternating between the two ends of the range as in the examples below.

    Please type in a number: 5\n1\n5\n2\n4\n3

    Please type in a number: 6\n1\n6\n2\n5\n3\n4

    ","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-3/3-more-loops.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 understand when the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":" command is needed to break out of loops"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":" command to move to the next iteration"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will understand how nested loops work"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-break-command","style":"position:relative;"},"children":[{"type":"text","value":"The break command"},{"type":"element","tagName":"a","properties":{"href":"#the-break-command","ariaLabel":"the break command 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":"You have already come across the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":" command. It can be used to stop the execution of a loop immediately. A typical example of where it is used is a situation where the program asks the user for input, and the execution ends only when a specific input is received."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The same functionality can be achieved without the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":" command, using a suitable condition. The two programs below both ask the user to type in numbers, and calculate the sum of the numbers until the user types in -1."}]},{"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":"# 1st version using the break command"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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"]},"children":[{"type":"text","value":"\"Please type in a number, -1 to exit: \""}]},{"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":"if"}]},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" number\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" "},{"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\"The sum 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":"sum"}]},{"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":"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":"# 2nd version without the break command"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"0"}]},{"type":"text","value":"\nnumber "},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"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","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 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"]},"children":[{"type":"text","value":"\"Please type in a number, -1 to exit: \""}]},{"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":"if"}]},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" number\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" "},{"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\"The sum 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":"sum"}]},{"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":"p","properties":{},"children":[{"type":"text","value":"Both programs print out the same thing with the same inputs, for example:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nThe sum is 14"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, the two programs are functionally practically identical. However, the first method is often easier, as the condition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number == -1"}]},{"type":"text","value":" appears only once, and the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" doesn't have to be initialised outside the loop."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":" command and a suitable condition can also be used together in a while loop. For example, the following loop is repeated as long as the sum of the numbers is at most 100, but it also stops if the user types in the number -1."}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"100"}]},{"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"]},"children":[{"type":"text","value":"\"Please type in a number, -1 to exit: \""}]},{"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":"if"}]},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" number\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" "},{"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\"The sum 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":"sum"}]},{"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":"p","properties":{},"children":[{"type":"text","value":"Some examples of the program's execution:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"15"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"21"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nThe sum is 44"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"15"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"21"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"45"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"17"}]},{"type":"text","value":"\nThe sum is 106"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the first example the execution of the loop stops because the user types in the number -1. In the second example it stops because the sum of the numbers exceeds 100."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As always in programming, there are many ways to reach the same functionality. The following program is functionally identical to the above:"}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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"]},"children":[{"type":"text","value":"\"Please type in a number, -1 to exit: \""}]},{"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":"if"}]},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" number\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"100"}]},{"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":"break"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" "},{"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\"The sum 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":"sum"}]},{"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":"h2","properties":{"id":"the-continue-command","style":"position:relative;"},"children":[{"type":"text","value":"The continue command"},{"type":"element","tagName":"a","properties":{"href":"#the-continue-command","ariaLabel":"the continue command 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":"Another way to change the way a loop is executed is the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":" command. It causes the execution of the loop to jump straight to the beginning of the loop, where the condition of the loop is. Then the execution continues normally with checking the condition:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 727px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/a248e448eca1ebe875046227836fc069/c54b3/3_3_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: 78.69565217391303%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB50lEQVQ4y41U23LaMBT0/39QXtOXPiQUQgZoXQYM2AYMFHy/yvZmVkGKif1QzWgkS+s9q3NWMtq2BVvTNMjzXM7VGsfuvDsWRYG6rh9w7IYCcNPzvB7gO7n6Pp1OqKqqT4h7S9MUy+USQgh8b4pENQZfrVaIoqiHNZSyyWSC19cXzGYzGZ1g3/cf0uA4Dna7HRaLBUajEcbjMfb7PcIwlD2O488jMx+u42C9XuN8PqMsC0lAMgLVCYIgkFjv5EksyYihKNbgdrt9HVk0LdaW9SCfOWJURViWpd7bWBvU4l4UfKaEAY1K1KiyGxJ3AvPlCfXVRCMyTZgkiSaMoxh5mWH3b4Mfv57x254jv2PZmCbDDyJkoYfCHeGy/Inq+IZWpBLA41yvV22rNEkRRD7+Hv7gbTXGzH5HUsZ6n+ky1DGUbI6qqFEcY7vd9qr8aIF7yoSQeTVUFYfMS1CWZYM+7AZRc6bHYJ4oN4pC+TMTqwslhPZa90bRTsRyT5lbF4WslmVJb83nc9kPh4OsKAFdQkVKU0+nU5imKee0EnmkbaiCBLZty5Hk3CAhVXTva9fgruvKkV5UWHJpH3KBJu56bejqqYtA5bRS7+p1Hwcq/N/H4Xg86uCDrw3l0kdDPw+tXS6XHiEVfgA5YdmRLsqPTAAAAABJRU5ErkJggg=='); 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":["/static/a248e448eca1ebe875046227836fc069/a0b58/3_3_1.webp 230w","/static/a248e448eca1ebe875046227836fc069/bc10c/3_3_1.webp 460w","/static/a248e448eca1ebe875046227836fc069/34975/3_3_1.webp 727w"],"sizes":"(max-width: 727px) 100vw, 727px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a248e448eca1ebe875046227836fc069/81c8e/3_3_1.png 230w","/static/a248e448eca1ebe875046227836fc069/08a84/3_3_1.png 460w","/static/a248e448eca1ebe875046227836fc069/c54b3/3_3_1.png 727w"],"sizes":"(max-width: 727px) 100vw, 727px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/a248e448eca1ebe875046227836fc069/c54b3/3_3_1.png","alt":"3 3 1","title":"3 3 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":"p","properties":{},"children":[{"type":"text","value":"For example, the following program sums up numbers from input, but it only includes the numbers which are smaller than 10. If the number is 10 or greater, the execution jumps to the beginning of the loop and the number is not added to the sum."}]},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":"0"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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"]},"children":[{"type":"text","value":"\"Please type in a number, -1 to exit: \""}]},{"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":"if"}]},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"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":"10"}]},{"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":"continue"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" number\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" "},{"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\"The sum 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":"sum"}]},{"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":"Please type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"99"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nPlease type in a number, -1 to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nThe sum is 16"}]}]},{"type":"element","tagName":"h2","properties":{"id":"nested-loops","style":"position:relative;"},"children":[{"type":"text","value":"Nested loops"},{"type":"element","tagName":"a","properties":{"href":"#nested-loops","ariaLabel":"nested loops 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":"Just like "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statements, loops can also be placed inside other loops. For example, the following program uses a loop to ask the user to input numbers. It then uses another loop inside the first one to print a countdown from the given number down to 1:"}]},{"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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"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":"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":"number"},{"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","number"]},"children":[{"type":"text","value":"1"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n4\n3\n2\n1\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n3\n2\n1\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\n6\n5\n4\n3\n2\n1\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When there are nested loops, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":" commands only affect the innermost loop which they are a part of. The previous example could also be written 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":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"if"}]},{"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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"if"}]},{"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":"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":"break"}]},{"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 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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here the latter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":" command only stops the innermost loop, which is used to print the numbers."}]},{"type":"element","tagName":"h2","properties":{"id":"more-helper-variables-with-loops","style":"position:relative;"},"children":[{"type":"text","value":"More helper variables with loops"},{"type":"element","tagName":"a","properties":{"href":"#more-helper-variables-with-loops","ariaLabel":"more helper variables with loops 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've already used helper variables, which increase or decrease with every iteration of a loop, many times before, so the following program should look quite familiar in structure. The program prints out all even numbers above zero until it reaches a limit set by the user:"}]},{"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":"limit "},{"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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"\ni "},{"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":"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":" limit"},{"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":"i"},{"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":"2"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\n0\n2\n4\n6"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The helper variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"i"}]},{"type":"text","value":" is set to 0 before the loop, and it increases by two with every iteration."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Using nested loops sometimes necessitates a separate helper variable for the inner loop. The program below prints out a \"number pyramid\" based on a number given by the user:"}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in a number: \""}]},{"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":"while"}]},{"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":"0"}]},{"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":"0"}]},{"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":" number"},{"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\""}]},{"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","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":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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 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 "},{"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 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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n0 1 2 3 4\n0 1 2 3\n0 1 2\n0 1\n0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this program the outer loop uses the helper variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":", which decreases by 1 with each iteration until it reaches 0. The helper variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"i"}]},{"type":"text","value":" is set to 0 just before the inner loop is entered, each time the outer loop repeats."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The inner loop uses the helper variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"i"}]},{"type":"text","value":", which increases by 1 with each iteration of the inner loop. The inner loop repeats until "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"i"}]},{"type":"text","value":" is equal to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":", and prints out each value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"i"}]},{"type":"text","value":" on the same line, separated by a space character. When the inner loop finishes, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command in the outer loop starts a new line."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now remember that with each iteration of the outer loop the value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" decreases, so the amount of times the inner loop repeats also decreases. With each repetition the line of numbers gets shorter, and thus we get the pyramid shape."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Nested loops can get confusing fast, but understanding the way they work is essential. You may well find the Python Tutor "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":" helpful in understanding how this example works. Copy the above code into the code window of the tool and follow the formation of the printout and the changing values of the helper variables as the execution progresses."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Multiplication","tmcname":"part03-23_multiplication"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a positive integer number. The program then prints out a list of multiplication operations until both operands reach the number given by the user. See the examples below for details:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: 2\n1 x 1 = 1\n1 x 2 = 2\n2 x 1 = 2\n2 x 2 = 4"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: 3\n1 x 1 = 1\n1 x 2 = 2\n1 x 3 = 3\n2 x 1 = 2\n2 x 2 = 4\n2 x 3 = 6\n3 x 1 = 3\n3 x 2 = 6\n3 x 3 = 9"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"First letters of words","tmcname":"part03-24_first_letters_of_words"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in a sentence. The program then prints out the first letter of each word in the sentence, each letter on a separate line."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a sentence: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Humpty Dumpty sat on a wall"}]},{"type":"text","value":"\nH\nD\ns\no\na\nw"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Factorial","tmcname":"part03-25_factorial"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in an integer number. If the user types in a number equal to or below 0, the execution ends. Otherwise the program prints out the factorial of the number."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The factorial of a number involves multiplying the number by all the positive integers smaller than itself. In other words, it is the product of all positive integers less than or equal to the number. For example, the factorial of 5 is 1 * 2 * 3 * 4 * 5 = 120."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nThe factorial of the number 3 is 6\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nThe factorial of the number 4 is 24\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nThanks and bye!"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nThe factorial of the number 1 is 1\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nThanks and bye!"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Flip the pairs","tmcname":"part03-26_flip_the_pairs"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in a number. The program then prints out all the positive integer values from 1 up to the number. However, the order of the numbers is changed so that each pair or numbers is flipped. That is, 2 comes before 1, 4 before 3 and so forth. See the examples below for details."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n2\n1\n4\n3\n5"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\n2\n1\n4\n3\n6\n5"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Taking turns","tmcname":"part03-27_taking_turns"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in a number. The program then prints out the positive integers between 1 and the number itself, alternating between the two ends of the range as in the examples below."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n1\n5\n2\n4\n3"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\n1\n6\n2\n5\n3\n4"}]}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will understand when the break command is needed to break out of loops
    • \n
    • You will be able to use the continue command to move to the next iteration
    • \n
    • You will understand how nested loops work
    • \n

    The break command

    You have already come across the break command. It can be used to stop the execution of a loop immediately. A typical example of where it is used is a situation where the program asks the user for input, and the execution ends only when a specific input is received.

    The same functionality can be achieved without the break command, using a suitable condition. The two programs below both ask the user to type in numbers, and calculate the sum of the numbers until the user types in -1.

    # 1st version using the break command\n\nsum = 0\n\nwhile True:\n    number = int(input(\"Please type in a number, -1 to exit: \"))\n    if number == -1:\n        break\n    sum += number\n\nprint (f\"The sum is {sum}\")
    # 2nd version without the break command\n\nsum = 0\nnumber = 0\n\nwhile number != -1:\n    number = int(input(\"Please type in a number, -1 to exit: \"))\n    if number != -1:\n        sum += number\n\nprint (f\"The sum is {sum}\")

    Both programs print out the same thing with the same inputs, for example:

    Please type in a number, -1 to exit: 2\nPlease type in a number, -1 to exit: 4\nPlease type in a number, -1 to exit: 5\nPlease type in a number, -1 to exit: 3\nPlease type in a number, -1 to exit: -1\nThe sum is 14

    So, the two programs are functionally practically identical. However, the first method is often easier, as the condition number == -1 appears only once, and the variable number doesn't have to be initialised outside the loop.

    The break command and a suitable condition can also be used together in a while loop. For example, the following loop is repeated as long as the sum of the numbers is at most 100, but it also stops if the user types in the number -1.

    sum = 0\n\nwhile sum <= 100:\n    number = int(input(\"Please type in a number, -1 to exit: \"))\n    if number == -1:\n        break\n    sum += number\n\nprint (f\"The sum is {sum}\")

    Some examples of the program's execution:

    Please type in a number, -1 to exit: 15\nPlease type in a number, -1 to exit: 8\nPlease type in a number, -1 to exit: 21\nPlease type in a number, -1 to exit: -1\nThe sum is 44

    Please type in a number, -1 to exit: 15\nPlease type in a number, -1 to exit: 8\nPlease type in a number, -1 to exit: 21\nPlease type in a number, -1 to exit: 45\nPlease type in a number, -1 to exit: 17\nThe sum is 106

    In the first example the execution of the loop stops because the user types in the number -1. In the second example it stops because the sum of the numbers exceeds 100.

    As always in programming, there are many ways to reach the same functionality. The following program is functionally identical to the above:

    sum = 0\n\nwhile True:\n    number = int(input(\"Please type in a number, -1 to exit: \"))\n    if number == -1:\n        break\n    sum += number\n    if sum > 100:\n        break\n\nprint (f\"The sum is {sum}\")

    The continue command

    Another way to change the way a loop is executed is the continue command. It causes the execution of the loop to jump straight to the beginning of the loop, where the condition of the loop is. Then the execution continues normally with checking the condition:

    \n \n \n \n \n \n \"3\n \n \n

    For example, the following program sums up numbers from input, but it only includes the numbers which are smaller than 10. If the number is 10 or greater, the execution jumps to the beginning of the loop and the number is not added to the sum.

    sum = 0\n\nwhile True:\n    number = int(input(\"Please type in a number, -1 to exit: \"))\n    if number == -1:\n        break\n    if number >= 10:\n        continue\n    sum += number\n\nprint (f\"The sum is {sum}\")

    Please type in a number, -1 to exit: 4\nPlease type in a number, -1 to exit: 7\nPlease type in a number, -1 to exit: 99\nPlease type in a number, -1 to exit: 5\nPlease type in a number, -1 to exit: -1\nThe sum is 16

    Nested loops

    Just like if statements, loops can also be placed inside other loops. For example, the following program uses a loop to ask the user to input numbers. It then uses another loop inside the first one to print a countdown from the given number down to 1:

    while True:\n    number = int(input(\"Please type in a number: \"))\n    if number == -1:\n        break\n    while number > 0:\n        print(number)\n        number -= 1

    Please type in a number: 4\n4\n3\n2\n1\nPlease type in a number: 3\n3\n2\n1\nPlease type in a number: 6\n6\n5\n4\n3\n2\n1\nPlease type in a number: -1

    When there are nested loops, break and continue commands only affect the innermost loop which they are a part of. The previous example could also be written like this:

    while True:\n    number = int(input(\"Please type in a number: \"))\n    if number == -1:\n        break\n    while True:\n        if number <= 0:\n            break\n        print(number)\n        number -= 1

    Here the latter break command only stops the innermost loop, which is used to print the numbers.

    More helper variables with loops

    We've already used helper variables, which increase or decrease with every iteration of a loop, many times before, so the following program should look quite familiar in structure. The program prints out all even numbers above zero until it reaches a limit set by the user:

    limit = int(input(\"Please type in a number: \"))\ni = 0\nwhile i < limit:\n    print(i)\n    i += 2

    Please type in a number: 8\n0\n2\n4\n6

    The helper variable i is set to 0 before the loop, and it increases by two with every iteration.

    Using nested loops sometimes necessitates a separate helper variable for the inner loop. The program below prints out a \"number pyramid\" based on a number given by the user:

    number = int(input(\"Please type in a number: \"))\nwhile number > 0:\n    i = 0\n    while i < number:\n        print(f\"{i} \", end=\"\")\n        i += 1\n    print()\n    number -= 1

    Please type in a number: 5\n0 1 2 3 4\n0 1 2 3\n0 1 2\n0 1\n0

    In this program the outer loop uses the helper variable number, which decreases by 1 with each iteration until it reaches 0. The helper variable i is set to 0 just before the inner loop is entered, each time the outer loop repeats.

    The inner loop uses the helper variable i, which increases by 1 with each iteration of the inner loop. The inner loop repeats until i is equal to number, and prints out each value of i on the same line, separated by a space character. When the inner loop finishes, the print command in the outer loop starts a new line.

    Now remember that with each iteration of the outer loop the value of number decreases, so the amount of times the inner loop repeats also decreases. With each repetition the line of numbers gets shorter, and thus we get the pyramid shape.

    Nested loops can get confusing fast, but understanding the way they work is essential. You may well find the Python Tutor visualisation tool helpful in understanding how this example works. Copy the above code into the code window of the tool and follow the formation of the printout and the changing values of the helper variables as the execution progresses.

    Please write a program which asks the user for a positive integer number. The program then prints out a list of multiplication operations until both operands reach the number given by the user. See the examples below for details:

    Please type in a number: 2\n1 x 1 = 1\n1 x 2 = 2\n2 x 1 = 2\n2 x 2 = 4

    Please type in a number: 3\n1 x 1 = 1\n1 x 2 = 2\n1 x 3 = 3\n2 x 1 = 2\n2 x 2 = 4\n2 x 3 = 6\n3 x 1 = 3\n3 x 2 = 6\n3 x 3 = 9

    Please write a program which asks the user to type in a sentence. The program then prints out the first letter of each word in the sentence, each letter on a separate line.

    An example of expected behaviour:

    Please type in a sentence: Humpty Dumpty sat on a wall\nH\nD\ns\no\na\nw

    Please write a program which asks the user to type in an integer number. If the user types in a number equal to or below 0, the execution ends. Otherwise the program prints out the factorial of the number.

    The factorial of a number involves multiplying the number by all the positive integers smaller than itself. In other words, it is the product of all positive integers less than or equal to the number. For example, the factorial of 5 is 1 * 2 * 3 * 4 * 5 = 120.

    Some examples of expected behaviour:

    Please type in a number: 3\nThe factorial of the number 3 is 6\nPlease type in a number: 4\nThe factorial of the number 4 is 24\nPlease type in a number: -1\nThanks and bye!

    Please type in a number: 1\nThe factorial of the number 1 is 1\nPlease type in a number: 0\nThanks and bye!

    Please write a program which asks the user to type in a number. The program then prints out all the positive integer values from 1 up to the number. However, the order of the numbers is changed so that each pair or numbers is flipped. That is, 2 comes before 1, 4 before 3 and so forth. See the examples below for details.

    Please type in a number: 5\n2\n1\n4\n3\n5

    Please type in a number: 6\n2\n1\n4\n3\n6\n5

    Please write a program which asks the user to type in a number. The program then prints out the positive integers between 1 and the number itself, alternating between the two ends of the range as in the examples below.

    Please type in a number: 5\n1\n5\n2\n4\n3

    Please type in a number: 6\n1\n6\n2\n5\n3\n4

    ","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-3/3-more-loops.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-3/4-defining-functions/page-data.json b/page-data/part-3/4-defining-functions/page-data.json index ee4bcc800..8a23b4819 100644 --- a/page-data/part-3/4-defining-functions/page-data.json +++ b/page-data/part-3/4-defining-functions/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-3/4-defining-functions", - "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 know how to write and call your own functions"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will understand what is meant by the argument and the parameter of a function"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to define parameters in your own functions"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We have already used functions such as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" in our programs. These are functions built into Python, and so they are always ready at our disposal, no matter which environment we are programming in. However, it is also possible to define your own functions."}]},{"type":"element","tagName":"h2","properties":{"id":"the-function-definition","style":"position:relative;"},"children":[{"type":"text","value":"The function definition"},{"type":"element","tagName":"a","properties":{"href":"#the-function-definition","ariaLabel":"the function definition 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":"Before a function can be used, it must be defined. Any function definition begins with the keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":", short for "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"define"}]},{"type":"text","value":". Then comes the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" of the function, followed by parentheses and a colon character. This is called the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"header"}]},{"type":"text","value":" of the function. After this, indented just like "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" blocks, comes the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"body"}]},{"type":"text","value":" of the function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, the following code defines the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"message"}]},{"type":"text","value":":"}]},{"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":"message"}]},{"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 "},{"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":"\"This is my very own function!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the program above is executed, nothing seems to happen. This is because the code in the body of the function is only executed when the function is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"called"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Calling a function is as simple as mentioning its name in your code. Adding a function call to the end of the above program, like so"}]},{"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":"message"}]},{"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 "},{"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":"\"This is my very own function!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nmessage"},{"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":"results in a printed statement:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is my very own function!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When a function has been defined it can be called multiple times:"}]},{"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":"message"}]},{"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 "},{"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":"\"This is my very own function!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nmessage"},{"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":"\nmessage"},{"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":"\nmessage"},{"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":"This is my very own function!\nThis is my very own function!\nThis is my very own function!"}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Testing your own functions"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: from now on the majority of the exercises on this course will ask you to write your own function(s)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When a program consists of only functions, executing it doesn't seem to have any effect. The following code doesn't print out anything, even though there is a print 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":"greet"}]},{"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 "},{"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":"\"Hi!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The reason nothing is printed out is that the code within the body of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"greet"}]},{"type":"text","value":" function is only executed when the function is called."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The \"main\" program below the function should contain appropriate function calls, so that the program can be tested. In fact, Python treats all code that is not within function definitions as part of the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"main function"}]},{"type":"text","value":", which gets executed when the file itself is evaluated or executed. So, lets add a function call:"}]},{"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":"greet"}]},{"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 "},{"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":"\"Hi!\""}]},{"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":"# All code not within function definitions is part of"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the main function of the program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Calling our function:"}]},{"type":"text","value":"\n\ngreet"},{"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":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Important"}]},{"type":"text","value":": on this course the automatic tests that are run on the exercise files require an empty main function. No commands should be left in the main function of your solution. That is, any code that you yourself use for testing must be contained in a specially defined "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" block:"}]},{"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":"greet"}]},{"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 "},{"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":"\"Hi!\""}]},{"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":"# Write your main function within a block like this:"}]},{"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 greet"},{"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":"Any code left outside the above block causes an error:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 786px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/e2761f598b362de4b6ce89a0582da6da/321ea/3_4_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: 77.82608695652173%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB5UlEQVQ4y62Uy27UMBSG89QsESseAbHoA7BBiMuyUlmDgNKyoJrOhGkm90liJ3Ecx0k+lMxIo1IGMQhbnyInJ7/POb9lx1hDU1lEokmClNT3yeIEY1r6vqfv7Uk4puvQraHMFVkkyXyfpioZpzkOJ9F1BmfKwrQtpSxQqkJKQWfNHDAM/V8zxU9VOdPi4Rj/iUl0L3h6eb9j0vpPglOGe0FrJyePB59S8iy4WxwZ43h6yecXkhcvc6JIYaoUXUmUaijynGEY9rrHsjqYOI47nFdvtjw7S/C8kir5gdwmZFmGu1phrZ1Fh37YPX+htwPWDvc2djx/yWLxlS+Xn7l118RpShTHxGlCGEUHwnAmCALSNOTqesOTpysePXZ5fuayuPnGYrHEyVZ3ZEFMFkaUSYoqBErIA1LO75q6RjXN3A6tG4pCcXkl+PAp5/uNIM+3FEWB0769oLtdY9YbuoWLWXmYOx/jBejVGr1cY1yPVkhMZ2m1pm1bTKuB7oGPTvXuPXm6JQgjCiEQUuJ7G5I4QQiJEIKqrinyAt1olFI0TUOjJjTDMM5M/Zt66VSvzwncNVEYEWx8kihG5AWqVlRlRV3V88+lkHu3/3TKRhz98Zqubem6bmZ2dr/bPcYR29v5+zGmi+YnpeXZGTCOSMIAAAAASUVORK5CYII='); 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":["/static/e2761f598b362de4b6ce89a0582da6da/a0b58/3_4_1.webp 230w","/static/e2761f598b362de4b6ce89a0582da6da/bc10c/3_4_1.webp 460w","/static/e2761f598b362de4b6ce89a0582da6da/4cb1e/3_4_1.webp 786w"],"sizes":"(max-width: 786px) 100vw, 786px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/e2761f598b362de4b6ce89a0582da6da/81c8e/3_4_1.png 230w","/static/e2761f598b362de4b6ce89a0582da6da/08a84/3_4_1.png 460w","/static/e2761f598b362de4b6ce89a0582da6da/321ea/3_4_1.png 786w"],"sizes":"(max-width: 786px) 100vw, 786px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/e2761f598b362de4b6ce89a0582da6da/321ea/3_4_1.png","alt":"3 4 1","title":"3 4 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":"p","properties":{},"children":[{"type":"text","value":"The purpose of this is to make sure that your solution gets tested on a clean slate, as the tests often check what your functions print out. It is worth noting that the tests will not execute any code from within the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" block, so no code that is needed to fulfil the requirements of the exercise should be placed within the block."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Seven Brothers","tmcname":"part03-28_seven_brothers"},"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":"seven_brothers"}]},{"type":"text","value":". When the function is called, it should print out the names of the seven brothers in alphabetical order, as in the example below. See the "},{"type":"element","tagName":"a","properties":{"href":"/part-1/1-getting-started#programming-exercise-fix-the-code-seven-brothers"},"children":[{"type":"text","value":"similarly named exercise in part 1"}]},{"type":"text","value":" for more details about the brothers."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Aapo\nEero\nJuhani\nLauri\nSimeoni\nTimo\nTuomas"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"function-arguments","style":"position:relative;"},"children":[{"type":"text","value":"Function arguments"},{"type":"element","tagName":"a","properties":{"href":"#function-arguments","ariaLabel":"function arguments 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":"Functions often take one or more "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"arguments"}]},{"type":"text","value":", which may affect what the function does. For example, the built-in Python functions "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" take as argument(s) the text that is to be displayed:"}]},{"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":"\"Hi!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# argument is the string \"Hi!\""}]},{"type":"text","value":"\nname "},{"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":"\"What is your name? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# argument is the string \"What is your name? \""}]},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# argument is the value of the variable name"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It was mentioned before that the terms "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"argument"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"parameter"}]},{"type":"text","value":" are often used to refer to the same thing. The distinction is that while "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"argument"}]},{"type":"text","value":" is used with the data passed to the function when the function is called, "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inside"}]},{"type":"text","value":" the function the arguments are assigned to variables called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"parameters"}]},{"type":"text","value":". So, approximately, when the function is called, we call the passed bits of data arguments, but when we are defining the function, we call them parameters."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This may seem like a futile semantic distintion, and to make things even muddier, not all sources follow this definition. On this course we attempt to keep the distinction clear, however, as knowing the correct terminology will help you understand other sources besides this course material."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's define some functions that take arguments. In the function definition, the parameters are defined within the parentheses after the function name:"}]},{"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":"hello"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"\"Hello\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" target"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Calling this function twice, like so"}]},{"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":"hello"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhello"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"world!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"prints out two different greetings:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hello Emily\nHello world!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's take a closer look at the function definition:"}]},{"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":"hello"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"\"Hello\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" target"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"On the first line, in the function header, we defined that this function takes an argument, and assigns it to a parameter named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"target"}]},{"type":"text","value":". In the body of the function the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command uses the value stored in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"target"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the function is called, the parameter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"target"}]},{"type":"text","value":" has the value given as an argument in the function call. For example, the following function call"}]},{"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":"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":"\"Alan\""}]},{"type":"text","value":"\nhello"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"causes the parameter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"target"}]},{"type":"text","value":" to be set to the value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Alan\""}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The names of functions and their parameters follow the same principles as the names of variables. They should be descriptive, and contain primarily lowercase letters and underscore characters. Again, there are some exceptions to these guidelines, but we will ignore those for now."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"The first character","tmcname":"part03-29_first_character"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise contains the outline of the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"first_character"}]},{"type":"text","value":". Please complete it so that it prints out the first character of the string it takes as its argument."}]},{"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_character"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"text"},{"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":"# write your code here"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# testing the function:"}]},{"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 first_character"},{"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":"\n first_character"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'yellow'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n first_character"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'tomorrow'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n first_character"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'heliotrope'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n first_character"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'open'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n first_character"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'night'"}]},{"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":"p\ny\nt\nh\no\nn"}]}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Testing your functions with arguments"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Whenever your function takes one or more arguments, it is recommended to test it with various different arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pay special heed to \"special cases\" specific to the type of argument you are using. How will your function behave if the argument is zero or a negative number, or a floating point number instead of an integer? What happens if the argument is an empty string?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the exercise assignment doesn't explicitly tell you to include function calls, you may freely include your own within the main function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" block as explained above. The tests will ignore everything within the if block."}]}]},{"type":"element","tagName":"h2","properties":{"id":"more-examples","style":"position:relative;"},"children":[{"type":"text","value":"More examples"},{"type":"element","tagName":"a","properties":{"href":"#more-examples","ariaLabel":"more examples 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":"Let's have a look at some more examples of functions which take arguments. In the following function definition the parameter is a number:"}]},{"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":"squared"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"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\"The square of the 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":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" 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":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" x"},{"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\nsquared"},{"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":"\nsquared"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The square of the number 2 is 4\nThe square of the number 5 is 25"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Meanwhile, in this function definition there is an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statement within the body of the 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hello"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"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":"\"Emily\""}]},{"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":"\"Hello\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"else"}]},{"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":"\"Hi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nhello"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhello"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mark\""}]},{"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":"Hello Emily\nHi Mark"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This function takes two arguments:"}]},{"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":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"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 result "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" y\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\"The sum of the arguments "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"x"},{"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 "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" 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":"result"},{"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\n"},{"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":"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":" "},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"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":"The sum of the arguments 1 and 2 is 3\nThe sum of the arguments 5 and 24 is 29"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function also includes the helper variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"result"}]},{"type":"text","value":", which it uses to store the sum of its arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice how the names of the parameters within the function definition have no relation to any variables outside it. We might just as well call the above function 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":"text","value":"x "},{"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":"100"}]},{"type":"text","value":"\ny "},{"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":"30"}]},{"type":"text","value":"\n"},{"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":"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":" "},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" y"},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This should print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The sum of the arguments 1 and 2 is 3\nThe sum of the arguments 130 and 10 is 140"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the first function call the parameters are assigned the values "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x = 1"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y = 2"}]},{"type":"text","value":". In the second function call they are assigned the values "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x = 130"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y = 10"}]},{"type":"text","value":", regardless of the similarly named variables used in the function call."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We will come back to function definitions in the beginning of the next part of the course."}]},{"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":"In the examples above we saw that it is possible to assign new variables within function definitions. 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","comment"]},"children":[{"type":"text","value":"# this is a global variable"}]},{"type":"text","value":"\nname "},{"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":"\"Betty\""}]},{"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":"hello"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"given_name"},{"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 mistake"}]},{"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":"\"Hello\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nhello"},{"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":"\nhello"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betty\""}]},{"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":"Hello Betty\nHello Betty"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"No matter how many different arguments we call the function with, it will always print out the value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Betty\""}]},{"type":"text","value":" stored in the global variable."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Mean","tmcname":"part03-30_mean"},"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":"mean"}]},{"type":"text","value":", which takes three integer arguments. The function should print out the arithmetic mean of the three arguments."}]},{"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":"mean"},{"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":" "},{"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":" "},{"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":"\nmean"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"1"}]},{"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":"1"}]},{"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":"3.0\n4.0"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Print many times","tmcname":"part03-31_print_many_times"},"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":"print_many_times(text, times)"}]},{"type":"text","value":", which takes a string and an integer as arguments. The integer argument specifies how many times the string argument should be printed out:"}]},{"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":"print_many_times"},{"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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntext "},{"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":"\"All Pythons, except one, grow up\""}]},{"type":"text","value":"\ntimes "},{"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":"\nprint_many_times"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" times"},{"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":"hi\nhi\nhi\nhi\nhi"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"All Pythons, except one, grow up.\nAll Pythons, except one, grow up.\nAll Pythons, except one, grow up."}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"A square of hashes","tmcname":"part03-32_square_of_hashes"},"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":"hash_square(length)"}]},{"type":"text","value":", which takes an integer argument. The function prints out a square of hash characters, and the argument specifies the length of the side of the square."}]},{"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":"hash_square"},{"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":"\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":"\nhash_square"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"###\n###\n###\n\n#####\n#####\n#####\n#####\n#####\n"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Chessboard","tmcname":"part03-33_chessboard"},"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":"chessboard"}]},{"type":"text","value":", which prints out a chessboard made out of ones and zeroes. The function takes an integer argument, which specifies the length of the side of the board. See the examples below for details:"}]},{"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":"chessboard"},{"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":"\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":"\nchessboard"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"101\n010\n101\n\n101010\n010101\n101010\n010101\n101010\n010101\n"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"A word squared","tmcname":"part03-34_word_squared"},"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":"squared"}]},{"type":"text","value":", which takes a string argument and an integer argument, and prints out a square of characters as specified by the examples below."}]},{"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":"squared"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ab\""}]},{"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":"3"}]},{"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":"\nsquared"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"aybabtu\""}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"aba\nbab\naba\n\naybab\ntuayb\nabtua\nybabt\nuayba\n"}]}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this week's materials."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will know how to write and call your own functions
    • \n
    • You will understand what is meant by the argument and the parameter of a function
    • \n
    • You will be able to define parameters in your own functions
    • \n

    We have already used functions such as len, print and input in our programs. These are functions built into Python, and so they are always ready at our disposal, no matter which environment we are programming in. However, it is also possible to define your own functions.

    The function definition

    Before a function can be used, it must be defined. Any function definition begins with the keyword def, short for define. Then comes the name of the function, followed by parentheses and a colon character. This is called the header of the function. After this, indented just like while and if blocks, comes the body of the function.

    For example, the following code defines the function message:

    def message():\n    print(\"This is my very own function!\")

    If the program above is executed, nothing seems to happen. This is because the code in the body of the function is only executed when the function is called.

    Calling a function is as simple as mentioning its name in your code. Adding a function call to the end of the above program, like so

    def message():\n    print(\"This is my very own function!\")\n\nmessage()

    results in a printed statement:

    This is my very own function!

    When a function has been defined it can be called multiple times:

    def message():\n    print(\"This is my very own function!\")\n\nmessage()\nmessage()\nmessage()

    This is my very own function!\nThis is my very own function!\nThis is my very own function!

    NB: from now on the majority of the exercises on this course will ask you to write your own function(s).

    When a program consists of only functions, executing it doesn't seem to have any effect. The following code doesn't print out anything, even though there is a print statement:

    def greet():\n    print(\"Hi!\")

    The reason nothing is printed out is that the code within the body of the greet function is only executed when the function is called.

    The \"main\" program below the function should contain appropriate function calls, so that the program can be tested. In fact, Python treats all code that is not within function definitions as part of the main function, which gets executed when the file itself is evaluated or executed. So, lets add a function call:

    def greet():\n    print(\"Hi!\")\n\n# All code not within function definitions is part of\n# the main function of the program\n# Calling our function:\n\ngreet()

    Important: on this course the automatic tests that are run on the exercise files require an empty main function. No commands should be left in the main function of your solution. That is, any code that you yourself use for testing must be contained in a specially defined if block:

    def greet():\n    print(\"Hi!\")\n\n# Write your main function within a block like this:\nif __name__ == \"__main__\":\n    greet()

    Any code left outside the above block causes an error:

    \n \n
    def hello(target):\n    print(\"Hello\", target)

    Calling this function twice, like so

    hello(\"Emily\")\nhello(\"world!\")

    prints out two different greetings:

    Hello Emily\nHello world!

    Let's take a closer look at the function definition:

    def hello(target):\n    print(\"Hello\", target)

    On the first line, in the function header, we defined that this function takes an argument, and assigns it to a parameter named target. In the body of the function the print command uses the value stored in target.

    When the function is called, the parameter target has the value given as an argument in the function call. For example, the following function call

    name = \"Alan\"\nhello(name)

    causes the parameter target to be set to the value \"Alan\".

    The names of functions and their parameters follow the same principles as the names of variables. They should be descriptive, and contain primarily lowercase letters and underscore characters. Again, there are some exceptions to these guidelines, but we will ignore those for now.

    The exercise contains the outline of the function first_character. Please complete it so that it prints out the first character of the string it takes as its argument.

    def first_character(text):\n     # write your code here\n\n# testing the function:\nif __name__ == \"__main__\":\n    first_character('python')\n    first_character('yellow')\n    first_character('tomorrow')\n    first_character('heliotrope')\n    first_character('open')\n    first_character('night')

    p\ny\nt\nh\no\nn

    Whenever your function takes one or more arguments, it is recommended to test it with various different arguments.

    Pay special heed to \"special cases\" specific to the type of argument you are using. How will your function behave if the argument is zero or a negative number, or a floating point number instead of an integer? What happens if the argument is an empty string?

    If the exercise assignment doesn't explicitly tell you to include function calls, you may freely include your own within the main function if block as explained above. The tests will ignore everything within the if block.

    More examples

    Let's have a look at some more examples of functions which take arguments. In the following function definition the parameter is a number:

    def squared(x):\n    print(f\"The square of the number {x} is {x * x}\")\n\nsquared(2)\nsquared(5)

    The square of the number 2 is 4\nThe square of the number 5 is 25

    Meanwhile, in this function definition there is an if statement within the body of the function:

    def hello(name):\n    if name == \"Emily\":\n        print(\"Hello\", name)\n    else:\n        print(\"Hi\", name)\n\nhello(\"Emily\")\nhello(\"Mark\")

    Hello Emily\nHi Mark

    This function takes two arguments:

    def sum(x, y):\n    result = x + y\n    print(f\"The sum of the arguments {x} and {y} is {result}\")\n\nsum(1, 2)\nsum(5, 24)

    The sum of the arguments 1 and 2 is 3\nThe sum of the arguments 5 and 24 is 29

    The function also includes the helper variable result, which it uses to store the sum of its arguments.

    Notice how the names of the parameters within the function definition have no relation to any variables outside it. We might just as well call the above function like this:

    x = 100\ny = 30\nsum(1, 2)\nsum(x + y, 10)

    This should print out

    The sum of the arguments 1 and 2 is 3\nThe sum of the arguments 130 and 10 is 140

    In the first function call the parameters are assigned the values x = 1 and y = 2. In the second function call they are assigned the values x = 130 and y = 10, regardless of the similarly named variables used in the function call.

    We will come back to function definitions in the beginning of the next part of the course.

    Warning: using global variables within functions

    In the examples above we saw that it is possible to assign new variables within function definitions. The function can also see variables assigned outside it, in the main function. Such variables are called global variables.

    Using global variables from within functions is usually a bad idea. Among other issues, doing so may cause bugs which are difficult to trace.

    Below is an example of a function which uses a global variable \"by mistake\":

    # this is a global variable\nname = \"Betty\"\n\ndef hello(given_name):\n    # using the global variable instead of the parameter by mistake\n    print(\"Hello\", name)\n\nhello(\"Steve\")\nhello(\"Betty\")

    Hello Betty\nHello Betty

    No matter how many different arguments we call the function with, it will always print out the value \"Betty\" stored in the global variable.

    Please write a function named mean, which takes three integer arguments. The function should print out the arithmetic mean of the three arguments.

    mean(5, 3, 1)\nmean(10, 1, 1)

    3.0\n4.0

    Please write a function named print_many_times(text, times), which takes a string and an integer as arguments. The integer argument specifies how many times the string argument should be printed out:

    print_many_times(\"hi\", 5)\n\nprint()\n\ntext = \"All Pythons, except one, grow up\"\ntimes = 3\nprint_many_times(text, times)

    hi\nhi\nhi\nhi\nhi

    All Pythons, except one, grow up.\nAll Pythons, except one, grow up.\nAll Pythons, except one, grow up.

    Please write a function named hash_square(length), which takes an integer argument. The function prints out a square of hash characters, and the argument specifies the length of the side of the square.

    hash_square(3)\nprint()\nhash_square(5)
    \n###\n###\n###\n\n#####\n#####\n#####\n#####\n#####\n

    Please write a function named chessboard, which prints out a chessboard made out of ones and zeroes. The function takes an integer argument, which specifies the length of the side of the board. See the examples below for details:

    chessboard(3)\nprint()\nchessboard(6)
    \n101\n010\n101\n\n101010\n010101\n101010\n010101\n101010\n010101\n

    Please write a function named squared, which takes a string argument and an integer argument, and prints out a square of characters as specified by the examples below.

    squared(\"ab\", 3)\nprint()\nsquared(\"aybabtu\", 5)
    \naba\nbab\naba\n\naybab\ntuayb\nabtua\nybabt\nuayba\n

    Please respond to a quick questionnaire on this week's materials.

    ","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-3/4-defining-functions.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know how to write and call your own functions"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will understand what is meant by the argument and the parameter of a function"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to define parameters in your own functions"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We have already used functions such as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" in our programs. These are functions built into Python, and so they are always ready at our disposal, no matter which environment we are programming in. However, it is also possible to define your own functions."}]},{"type":"element","tagName":"h2","properties":{"id":"the-function-definition","style":"position:relative;"},"children":[{"type":"text","value":"The function definition"},{"type":"element","tagName":"a","properties":{"href":"#the-function-definition","ariaLabel":"the function definition 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":"Before a function can be used, it must be defined. Any function definition begins with the keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":", short for "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"define"}]},{"type":"text","value":". Then comes the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" of the function, followed by parentheses and a colon character. This is called the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"header"}]},{"type":"text","value":" of the function. After this, indented just like "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" blocks, comes the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"body"}]},{"type":"text","value":" of the function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, the following code defines the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"message"}]},{"type":"text","value":":"}]},{"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":"message"}]},{"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 "},{"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":"\"This is my very own function!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the program above is executed, nothing seems to happen. This is because the code in the body of the function is only executed when the function is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"called"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Calling a function is as simple as mentioning its name in your code. Adding a function call to the end of the above program, like so"}]},{"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":"message"}]},{"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 "},{"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":"\"This is my very own function!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nmessage"},{"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":"results in a printed statement:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is my very own function!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When a function has been defined it can be called multiple times:"}]},{"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":"message"}]},{"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 "},{"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":"\"This is my very own function!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nmessage"},{"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":"\nmessage"},{"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":"\nmessage"},{"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":"This is my very own function!\nThis is my very own function!\nThis is my very own function!"}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Testing your own functions"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: from now on the majority of the exercises on this course will ask you to write your own function(s)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When a program consists of only functions, executing it doesn't seem to have any effect. The following code doesn't print out anything, even though there is a print 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":"greet"}]},{"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 "},{"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":"\"Hi!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The reason nothing is printed out is that the code within the body of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"greet"}]},{"type":"text","value":" function is only executed when the function is called."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The \"main\" program below the function should contain appropriate function calls, so that the program can be tested. In fact, Python treats all code that is not within function definitions as part of the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"main function"}]},{"type":"text","value":", which gets executed when the file itself is evaluated or executed. So, lets add a function call:"}]},{"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":"greet"}]},{"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 "},{"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":"\"Hi!\""}]},{"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":"# All code not within function definitions is part of"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the main function of the program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Calling our function:"}]},{"type":"text","value":"\n\ngreet"},{"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":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Important"}]},{"type":"text","value":": on this course the automatic tests that are run on the exercise files require an empty main function. No commands should be left in the main function of your solution. That is, any code that you yourself use for testing must be contained in a specially defined "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" block:"}]},{"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":"greet"}]},{"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 "},{"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":"\"Hi!\""}]},{"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":"# Write your main function within a block like this:"}]},{"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 greet"},{"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":"Any code left outside the above block causes an error:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 786px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/e2761f598b362de4b6ce89a0582da6da/321ea/3_4_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: 77.82608695652173%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAQCAYAAAAWGF8bAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB5UlEQVQ4y62Uy27UMBSG89QsESseAbHoA7BBiMuyUlmDgNKyoJrOhGkm90liJ3Ecx0k+lMxIo1IGMQhbnyInJ7/POb9lx1hDU1lEokmClNT3yeIEY1r6vqfv7Uk4puvQraHMFVkkyXyfpioZpzkOJ9F1BmfKwrQtpSxQqkJKQWfNHDAM/V8zxU9VOdPi4Rj/iUl0L3h6eb9j0vpPglOGe0FrJyePB59S8iy4WxwZ43h6yecXkhcvc6JIYaoUXUmUaijynGEY9rrHsjqYOI47nFdvtjw7S/C8kir5gdwmZFmGu1phrZ1Fh37YPX+htwPWDvc2djx/yWLxlS+Xn7l118RpShTHxGlCGEUHwnAmCALSNOTqesOTpysePXZ5fuayuPnGYrHEyVZ3ZEFMFkaUSYoqBErIA1LO75q6RjXN3A6tG4pCcXkl+PAp5/uNIM+3FEWB0769oLtdY9YbuoWLWXmYOx/jBejVGr1cY1yPVkhMZ2m1pm1bTKuB7oGPTvXuPXm6JQgjCiEQUuJ7G5I4QQiJEIKqrinyAt1olFI0TUOjJjTDMM5M/Zt66VSvzwncNVEYEWx8kihG5AWqVlRlRV3V88+lkHu3/3TKRhz98Zqubem6bmZ2dr/bPcYR29v5+zGmi+YnpeXZGTCOSMIAAAAASUVORK5CYII='); 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":["/static/e2761f598b362de4b6ce89a0582da6da/a0b58/3_4_1.webp 230w","/static/e2761f598b362de4b6ce89a0582da6da/bc10c/3_4_1.webp 460w","/static/e2761f598b362de4b6ce89a0582da6da/4cb1e/3_4_1.webp 786w"],"sizes":"(max-width: 786px) 100vw, 786px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/e2761f598b362de4b6ce89a0582da6da/81c8e/3_4_1.png 230w","/static/e2761f598b362de4b6ce89a0582da6da/08a84/3_4_1.png 460w","/static/e2761f598b362de4b6ce89a0582da6da/321ea/3_4_1.png 786w"],"sizes":"(max-width: 786px) 100vw, 786px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/e2761f598b362de4b6ce89a0582da6da/321ea/3_4_1.png","alt":"3 4 1","title":"3 4 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":"p","properties":{},"children":[{"type":"text","value":"The purpose of this is to make sure that your solution gets tested on a clean slate, as the tests often check what your functions print out. It is worth noting that the tests will not execute any code from within the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" block, so no code that is needed to fulfil the requirements of the exercise should be placed within the block."}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Seven Brothers","tmcname":"part03-28_seven_brothers"},"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":"seven_brothers"}]},{"type":"text","value":". When the function is called, it should print out the names of the seven brothers in alphabetical order, as in the example below. See the "},{"type":"element","tagName":"a","properties":{"href":"/part-1/1-getting-started#programming-exercise-fix-the-code-seven-brothers"},"children":[{"type":"text","value":"similarly named exercise in part 1"}]},{"type":"text","value":" for more details about the brothers."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Aapo\nEero\nJuhani\nLauri\nSimeoni\nTimo\nTuomas"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"function-arguments","style":"position:relative;"},"children":[{"type":"text","value":"Function arguments"},{"type":"element","tagName":"a","properties":{"href":"#function-arguments","ariaLabel":"function arguments 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":"Functions often take one or more "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"arguments"}]},{"type":"text","value":", which may affect what the function does. For example, the built-in Python functions "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" take as argument(s) the text that is to be displayed:"}]},{"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":"\"Hi!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# argument is the string \"Hi!\""}]},{"type":"text","value":"\nname "},{"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":"\"What is your name? \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# argument is the string \"What is your name? \""}]},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# argument is the value of the variable name"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It was mentioned before that the terms "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"argument"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"parameter"}]},{"type":"text","value":" are often used to refer to the same thing. The distinction is that while "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"argument"}]},{"type":"text","value":" is used with the data passed to the function when the function is called, "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"inside"}]},{"type":"text","value":" the function the arguments are assigned to variables called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"parameters"}]},{"type":"text","value":". So, approximately, when the function is called, we call the passed bits of data arguments, but when we are defining the function, we call them parameters."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This may seem like a futile semantic distintion, and to make things even muddier, not all sources follow this definition. On this course we attempt to keep the distinction clear, however, as knowing the correct terminology will help you understand other sources besides this course material."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's define some functions that take arguments. In the function definition, the parameters are defined within the parentheses after the function name:"}]},{"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":"hello"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"\"Hello\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" target"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Calling this function twice, like so"}]},{"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":"hello"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhello"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"world!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"prints out two different greetings:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hello Emily\nHello world!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's take a closer look at the function definition:"}]},{"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":"hello"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"target"},{"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":"\"Hello\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" target"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"On the first line, in the function header, we defined that this function takes an argument, and assigns it to a parameter named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"target"}]},{"type":"text","value":". In the body of the function the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command uses the value stored in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"target"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the function is called, the parameter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"target"}]},{"type":"text","value":" has the value given as an argument in the function call. For example, the following function call"}]},{"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":"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":"\"Alan\""}]},{"type":"text","value":"\nhello"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"causes the parameter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"target"}]},{"type":"text","value":" to be set to the value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Alan\""}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The names of functions and their parameters follow the same principles as the names of variables. They should be descriptive, and contain primarily lowercase letters and underscore characters. Again, there are some exceptions to these guidelines, but we will ignore those for now."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"The first character","tmcname":"part03-29_first_character"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise contains the outline of the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"first_character"}]},{"type":"text","value":". Please complete it so that it prints out the first character of the string it takes as its argument."}]},{"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_character"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"text"},{"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":"# write your code here"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# testing the function:"}]},{"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 first_character"},{"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":"\n first_character"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'yellow'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n first_character"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'tomorrow'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n first_character"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'heliotrope'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n first_character"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'open'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n first_character"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'night'"}]},{"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":"p\ny\nt\nh\no\nn"}]}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Testing your functions with arguments"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Whenever your function takes one or more arguments, it is recommended to test it with various different arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Pay special heed to \"special cases\" specific to the type of argument you are using. How will your function behave if the argument is zero or a negative number, or a floating point number instead of an integer? What happens if the argument is an empty string?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the exercise assignment doesn't explicitly tell you to include function calls, you may freely include your own within the main function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" block as explained above. The tests will ignore everything within the if block."}]}]},{"type":"element","tagName":"h2","properties":{"id":"more-examples","style":"position:relative;"},"children":[{"type":"text","value":"More examples"},{"type":"element","tagName":"a","properties":{"href":"#more-examples","ariaLabel":"more examples 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":"Let's have a look at some more examples of functions which take arguments. In the following function definition the parameter is a number:"}]},{"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":"squared"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"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\"The square of the 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":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" 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":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" x"},{"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\nsquared"},{"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":"\nsquared"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The square of the number 2 is 4\nThe square of the number 5 is 25"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Meanwhile, in this function definition there is an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statement within the body of the 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"hello"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"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":"\"Emily\""}]},{"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":"\"Hello\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"else"}]},{"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":"\"Hi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nhello"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhello"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mark\""}]},{"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":"Hello Emily\nHi Mark"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This function takes two arguments:"}]},{"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":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"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 result "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" y\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\"The sum of the arguments "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"x"},{"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 "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" 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":"result"},{"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\n"},{"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":"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":" "},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"24"}]},{"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":"The sum of the arguments 1 and 2 is 3\nThe sum of the arguments 5 and 24 is 29"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function also includes the helper variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"result"}]},{"type":"text","value":", which it uses to store the sum of its arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice how the names of the parameters within the function definition have no relation to any variables outside it. We might just as well call the above function 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":"text","value":"x "},{"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":"100"}]},{"type":"text","value":"\ny "},{"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":"30"}]},{"type":"text","value":"\n"},{"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":"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":" "},{"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","builtin"]},"children":[{"type":"text","value":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" y"},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This should print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The sum of the arguments 1 and 2 is 3\nThe sum of the arguments 130 and 10 is 140"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the first function call the parameters are assigned the values "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x = 1"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y = 2"}]},{"type":"text","value":". In the second function call they are assigned the values "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x = 130"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y = 10"}]},{"type":"text","value":", regardless of the similarly named variables used in the function call."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We will come back to function definitions in the beginning of the next part of the course."}]},{"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":"In the examples above we saw that it is possible to assign new variables within function definitions. 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","comment"]},"children":[{"type":"text","value":"# this is a global variable"}]},{"type":"text","value":"\nname "},{"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":"\"Betty\""}]},{"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":"hello"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"given_name"},{"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 mistake"}]},{"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":"\"Hello\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nhello"},{"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":"\nhello"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betty\""}]},{"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":"Hello Betty\nHello Betty"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"No matter how many different arguments we call the function with, it will always print out the value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Betty\""}]},{"type":"text","value":" stored in the global variable."}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Mean","tmcname":"part03-30_mean"},"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":"mean"}]},{"type":"text","value":", which takes three integer arguments. The function should print out the arithmetic mean of the three arguments."}]},{"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":"mean"},{"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":" "},{"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":" "},{"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":"\nmean"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"1"}]},{"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":"1"}]},{"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":"3.0\n4.0"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Print many times","tmcname":"part03-31_print_many_times"},"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":"print_many_times(text, times)"}]},{"type":"text","value":", which takes a string and an integer as arguments. The integer argument specifies how many times the string argument should be printed out:"}]},{"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":"print_many_times"},{"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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntext "},{"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":"\"All Pythons, except one, grow up\""}]},{"type":"text","value":"\ntimes "},{"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":"\nprint_many_times"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" times"},{"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":"hi\nhi\nhi\nhi\nhi"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"All Pythons, except one, grow up.\nAll Pythons, except one, grow up.\nAll Pythons, except one, grow up."}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"A square of hashes","tmcname":"part03-32_square_of_hashes"},"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":"hash_square(length)"}]},{"type":"text","value":", which takes an integer argument. The function prints out a square of hash characters, and the argument specifies the length of the side of the square."}]},{"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":"hash_square"},{"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":"\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":"\nhash_square"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"###\n###\n###\n\n#####\n#####\n#####\n#####\n#####\n"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"Chessboard","tmcname":"part03-33_chessboard"},"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":"chessboard"}]},{"type":"text","value":", which prints out a chessboard made out of ones and zeroes. The function takes an integer argument, which specifies the length of the side of the board. See the examples below for details:"}]},{"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":"chessboard"},{"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":"\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":"\nchessboard"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"101\n010\n101\n\n101010\n010101\n101010\n010101\n101010\n010101\n"}]}]}]},{"type":"element","tagName":"in-browser-programming-exercise","properties":{"name":"A word squared","tmcname":"part03-34_word_squared"},"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":"squared"}]},{"type":"text","value":", which takes a string argument and an integer argument, and prints out a square of characters as specified by the examples below."}]},{"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":"squared"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ab\""}]},{"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":"3"}]},{"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":"\nsquared"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"aybabtu\""}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"aba\nbab\naba\n\naybab\ntuayb\nabtua\nybabt\nuayba\n"}]}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this week's materials."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will know how to write and call your own functions
    • \n
    • You will understand what is meant by the argument and the parameter of a function
    • \n
    • You will be able to define parameters in your own functions
    • \n

    We have already used functions such as len, print and input in our programs. These are functions built into Python, and so they are always ready at our disposal, no matter which environment we are programming in. However, it is also possible to define your own functions.

    The function definition

    Before a function can be used, it must be defined. Any function definition begins with the keyword def, short for define. Then comes the name of the function, followed by parentheses and a colon character. This is called the header of the function. After this, indented just like while and if blocks, comes the body of the function.

    For example, the following code defines the function message:

    def message():\n    print(\"This is my very own function!\")

    If the program above is executed, nothing seems to happen. This is because the code in the body of the function is only executed when the function is called.

    Calling a function is as simple as mentioning its name in your code. Adding a function call to the end of the above program, like so

    def message():\n    print(\"This is my very own function!\")\n\nmessage()

    results in a printed statement:

    This is my very own function!

    When a function has been defined it can be called multiple times:

    def message():\n    print(\"This is my very own function!\")\n\nmessage()\nmessage()\nmessage()

    This is my very own function!\nThis is my very own function!\nThis is my very own function!

    NB: from now on the majority of the exercises on this course will ask you to write your own function(s).

    When a program consists of only functions, executing it doesn't seem to have any effect. The following code doesn't print out anything, even though there is a print statement:

    def greet():\n    print(\"Hi!\")

    The reason nothing is printed out is that the code within the body of the greet function is only executed when the function is called.

    The \"main\" program below the function should contain appropriate function calls, so that the program can be tested. In fact, Python treats all code that is not within function definitions as part of the main function, which gets executed when the file itself is evaluated or executed. So, lets add a function call:

    def greet():\n    print(\"Hi!\")\n\n# All code not within function definitions is part of\n# the main function of the program\n# Calling our function:\n\ngreet()

    Important: on this course the automatic tests that are run on the exercise files require an empty main function. No commands should be left in the main function of your solution. That is, any code that you yourself use for testing must be contained in a specially defined if block:

    def greet():\n    print(\"Hi!\")\n\n# Write your main function within a block like this:\nif __name__ == \"__main__\":\n    greet()

    Any code left outside the above block causes an error:

    \n \n
    def hello(target):\n    print(\"Hello\", target)

    Calling this function twice, like so

    hello(\"Emily\")\nhello(\"world!\")

    prints out two different greetings:

    Hello Emily\nHello world!

    Let's take a closer look at the function definition:

    def hello(target):\n    print(\"Hello\", target)

    On the first line, in the function header, we defined that this function takes an argument, and assigns it to a parameter named target. In the body of the function the print command uses the value stored in target.

    When the function is called, the parameter target has the value given as an argument in the function call. For example, the following function call

    name = \"Alan\"\nhello(name)

    causes the parameter target to be set to the value \"Alan\".

    The names of functions and their parameters follow the same principles as the names of variables. They should be descriptive, and contain primarily lowercase letters and underscore characters. Again, there are some exceptions to these guidelines, but we will ignore those for now.

    The exercise contains the outline of the function first_character. Please complete it so that it prints out the first character of the string it takes as its argument.

    def first_character(text):\n     # write your code here\n\n# testing the function:\nif __name__ == \"__main__\":\n    first_character('python')\n    first_character('yellow')\n    first_character('tomorrow')\n    first_character('heliotrope')\n    first_character('open')\n    first_character('night')

    p\ny\nt\nh\no\nn

    Whenever your function takes one or more arguments, it is recommended to test it with various different arguments.

    Pay special heed to \"special cases\" specific to the type of argument you are using. How will your function behave if the argument is zero or a negative number, or a floating point number instead of an integer? What happens if the argument is an empty string?

    If the exercise assignment doesn't explicitly tell you to include function calls, you may freely include your own within the main function if block as explained above. The tests will ignore everything within the if block.

    More examples

    Let's have a look at some more examples of functions which take arguments. In the following function definition the parameter is a number:

    def squared(x):\n    print(f\"The square of the number {x} is {x * x}\")\n\nsquared(2)\nsquared(5)

    The square of the number 2 is 4\nThe square of the number 5 is 25

    Meanwhile, in this function definition there is an if statement within the body of the function:

    def hello(name):\n    if name == \"Emily\":\n        print(\"Hello\", name)\n    else:\n        print(\"Hi\", name)\n\nhello(\"Emily\")\nhello(\"Mark\")

    Hello Emily\nHi Mark

    This function takes two arguments:

    def sum(x, y):\n    result = x + y\n    print(f\"The sum of the arguments {x} and {y} is {result}\")\n\nsum(1, 2)\nsum(5, 24)

    The sum of the arguments 1 and 2 is 3\nThe sum of the arguments 5 and 24 is 29

    The function also includes the helper variable result, which it uses to store the sum of its arguments.

    Notice how the names of the parameters within the function definition have no relation to any variables outside it. We might just as well call the above function like this:

    x = 100\ny = 30\nsum(1, 2)\nsum(x + y, 10)

    This should print out

    The sum of the arguments 1 and 2 is 3\nThe sum of the arguments 130 and 10 is 140

    In the first function call the parameters are assigned the values x = 1 and y = 2. In the second function call they are assigned the values x = 130 and y = 10, regardless of the similarly named variables used in the function call.

    We will come back to function definitions in the beginning of the next part of the course.

    Warning: using global variables within functions

    In the examples above we saw that it is possible to assign new variables within function definitions. The function can also see variables assigned outside it, in the main function. Such variables are called global variables.

    Using global variables from within functions is usually a bad idea. Among other issues, doing so may cause bugs which are difficult to trace.

    Below is an example of a function which uses a global variable \"by mistake\":

    # this is a global variable\nname = \"Betty\"\n\ndef hello(given_name):\n    # using the global variable instead of the parameter by mistake\n    print(\"Hello\", name)\n\nhello(\"Steve\")\nhello(\"Betty\")

    Hello Betty\nHello Betty

    No matter how many different arguments we call the function with, it will always print out the value \"Betty\" stored in the global variable.

    Please write a function named mean, which takes three integer arguments. The function should print out the arithmetic mean of the three arguments.

    mean(5, 3, 1)\nmean(10, 1, 1)

    3.0\n4.0

    Please write a function named print_many_times(text, times), which takes a string and an integer as arguments. The integer argument specifies how many times the string argument should be printed out:

    print_many_times(\"hi\", 5)\n\nprint()\n\ntext = \"All Pythons, except one, grow up\"\ntimes = 3\nprint_many_times(text, times)

    hi\nhi\nhi\nhi\nhi

    All Pythons, except one, grow up.\nAll Pythons, except one, grow up.\nAll Pythons, except one, grow up.

    Please write a function named hash_square(length), which takes an integer argument. The function prints out a square of hash characters, and the argument specifies the length of the side of the square.

    hash_square(3)\nprint()\nhash_square(5)
    \n###\n###\n###\n\n#####\n#####\n#####\n#####\n#####\n

    Please write a function named chessboard, which prints out a chessboard made out of ones and zeroes. The function takes an integer argument, which specifies the length of the side of the board. See the examples below for details:

    chessboard(3)\nprint()\nchessboard(6)
    \n101\n010\n101\n\n101010\n010101\n101010\n010101\n101010\n010101\n

    Please write a function named squared, which takes a string argument and an integer argument, and prints out a square of characters as specified by the examples below.

    squared(\"ab\", 3)\nprint()\nsquared(\"aybabtu\", 5)
    \naba\nbab\naba\n\naybab\ntuayb\nabtua\nybabt\nuayba\n

    Please respond to a quick questionnaire on this week's materials.

    ","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-3/4-defining-functions.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-3/page-data.json b/page-data/part-3/page-data.json index 8132ffeac..88b720a10 100644 --- a/page-data/part-3/page-data.json +++ b/page-data/part-3/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/part-3", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-3","title":"Part 3"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-3/index.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-3","title":"Part 3"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-3/index.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-4/1-vscode/page-data.json b/page-data/part-4/1-vscode/page-data.json index 37c3f7bd1..cf8c438ff 100644 --- a/page-data/part-4/1-vscode/page-data.json +++ b/page-data/part-4/1-vscode/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-4/1-vscode", - "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 equipped to use the Visual Studio Code editor to complete exercises on this course"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be familiar with the interactive Python interpreter, and will be able to use it to run code"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Thus far all the exercises on this course have been completed directly on the course pages in embedded editor windows. Programming in the browser is very suitable for the very first steps in programming, but now it is time to start using a separate editor especially made for programming."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are dozens of different editors that are suited to programming. On this course we will use the "},{"type":"element","tagName":"a","properties":{"href":"https://code.visualstudio.com/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Visual Studio Code"}]},{"type":"text","value":" editor, which has been gaining traction in recent years."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please install the Visual Studio Code editor on your own computer now. You may also need to install Python and the Visual Studio Code plugin for Python. You will also need the TMC plugin, which will take care of running the tests that go with the exercises. In the TMC plugin, select "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"MOOC"}]},{"type":"text","value":" as the organization and "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Python Programming 2024"}]},{"type":"text","value":" as the course."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://www.mooc.fi/en/installation/vscode","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Here is a guide"}]},{"type":"text","value":" to installing and running all of these. Read the instructions on working on and submitting exercises, and then complete the task below:"}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Hello Visual Studio Code","tmcname":"part04-01_hello_visual_studio_code"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user which editor they are using. The program should keep on asking until the user types in "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Visual Studio Code"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Have a look at the example of expected behaviour below:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Editor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Emacs"}]},{"type":"text","value":"\nnot good\nEditor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Vim"}]},{"type":"text","value":"\nnot good\nEditor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Word"}]},{"type":"text","value":"\nawful\nEditor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Atom"}]},{"type":"text","value":"\nnot good\nEditor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Visual Studio Code"}]},{"type":"text","value":"\nan excellent choice!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the user types in Word or Notepad, the program counters with "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"awful"}]},{"type":"text","value":". Other unacceptable editor choices receive the reply "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not good"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should be case-insensitive in its reactions. That is, the same user input in lowercase, uppercase or mixed case should trigger the same reaction:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Editor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NOTEPAD"}]},{"type":"text","value":"\nawful\nEditor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"viSUal STudiO cODe"}]},{"type":"text","value":"\nan excellent choice!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hint: The simplest way to achieve this is converting all characters to the same case. The Python string method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lower"}]},{"type":"text","value":" converts a string to lowercase entirely. An example of its use:"}]},{"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":"mystring "},{"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":"\"Visual Studio CODE\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"visual studio code\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" mystring"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lower"},{"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 "},{"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":"\"this was the string I was looking for!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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 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. The same applies to any upcoming exercises that don't explicitly ask for functions."}]}]},{"type":"element","tagName":"h2","properties":{"id":"executing-code","style":"position:relative;"},"children":[{"type":"text","value":"Executing code"},{"type":"element","tagName":"a","properties":{"href":"#executing-code","ariaLabel":"executing code 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 Visual Studio Code, the easiest way to execute your code is clicking on the triangle in the upper right hand corner. Sometimes it happens that some program is left running in the background, maybe waiting for input from the user or stuck in an infinite loop, without you realising it. You might only notice this when trying to execute the next program, which simply will not run because the previous execution is still hogging up resources. A quick fix is pressing on the keys "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Control"}]},{"type":"text","value":"+"},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"C"}]},{"type":"text","value":" at the same time, which stops the execution of any ongoing process. The next program should now run fine."}]},{"type":"element","tagName":"h2","properties":{"id":"the-interactive-python-interpreter","style":"position:relative;"},"children":[{"type":"text","value":"The interactive Python interpreter"},{"type":"element","tagName":"a","properties":{"href":"#the-interactive-python-interpreter","ariaLabel":"the interactive python interpreter 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":"One of the most important tools for any Python programmer is the interactive Python interpreter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Starting up the interpreter may depend on your platform. In a Linux or Mac system, you can type in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"python3"}]},{"type":"text","value":" in the terminal. In Windows, the command on the command line might be "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"python"}]},{"type":"text","value":". Firing up the interpreter on a Mac looks like this:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/0f246/4_1_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: 17.82608695652174%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAABYlAAAWJQFJUiTwAAAArUlEQVQI13XLywqDMBCFYd//tbroMhiYZFppBG/YJAQTN+o4sVS77bc6HPiL2/Pe1E3ftc8HlmWJiFprKWVVVQAgpUT8/gCglBJCGGPatm1ORV2Zruve49s5F2O01nrvnXMppb7vQwjHfwU+cBxHRBRCAIB5GaUUIg7DoLUGgJRSjDGEME2T995aO8/zLyainDMzExEzH8ex7zsz55yvvW0bEW2ndV2XZSGiK/4AItbceE5Q2z8AAAAASUVORK5CYII='); 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":["/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/a0b58/4_1_1.webp 230w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/bc10c/4_1_1.webp 460w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/966d8/4_1_1.webp 920w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/34186/4_1_1.webp 1118w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/81c8e/4_1_1.png 230w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/08a84/4_1_1.png 460w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/c0255/4_1_1.png 920w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/0f246/4_1_1.png 1118w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/c0255/4_1_1.png","alt":"4 1 1","title":"4 1 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":"p","properties":{},"children":[{"type":"text","value":"It is also possible to start the interpreter inside Visual Studio Code. First you should execute a program by clicking on the triangle. This should open a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Terminal"}]},{"type":"text","value":" section on your screen, where you can now type in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"python3"}]},{"type":"text","value":" (or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"python"}]},{"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: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/895b071970fc467ecad71d69e6d70d5a/e431d/4_1_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: 49.130434782608695%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAABYlAAAWJQFJUiTwAAABF0lEQVQoz52RWW7DIBCGuUcLw2I2Ox42O7hZrtCXqve/SgVOokh5aNVPCM32s8wQjDHFlHIOIRhjnfP78r7ttgVuEWedMuZo58mNNak4e+JHnxYsBXPBcfbaaGOsbSKntZZqMM4111qplGfySyYL4h2AASeUUiG4EJxzAAD6BOs8uwr4G6MUGPQU6Wl2K/wNym4y9hD/GwIAfyyFO7vdxLajtUbEaZqsMcZZLsUDKSXnfBzHdV0Ph8M8z62kt5PUWtd1RcRSSs4ZQ9hy+UjLmkuKMYSwx2OMy7KkNtOcOqUUcr1eL5dLrXXbtvPpVOrxO54/h4jSDEoJIeD+yNdfkG3bEHE/MvarQgiD1koPzjml1GNmrw37AVG5Vxy3ozSFAAAAAElFTkSuQmCC'); 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":["/static/895b071970fc467ecad71d69e6d70d5a/a0b58/4_1_2.webp 230w","/static/895b071970fc467ecad71d69e6d70d5a/bc10c/4_1_2.webp 460w","/static/895b071970fc467ecad71d69e6d70d5a/966d8/4_1_2.webp 920w","/static/895b071970fc467ecad71d69e6d70d5a/445df/4_1_2.webp 1380w","/static/895b071970fc467ecad71d69e6d70d5a/78de1/4_1_2.webp 1840w","/static/895b071970fc467ecad71d69e6d70d5a/609ff/4_1_2.webp 1882w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/895b071970fc467ecad71d69e6d70d5a/81c8e/4_1_2.png 230w","/static/895b071970fc467ecad71d69e6d70d5a/08a84/4_1_2.png 460w","/static/895b071970fc467ecad71d69e6d70d5a/c0255/4_1_2.png 920w","/static/895b071970fc467ecad71d69e6d70d5a/b1001/4_1_2.png 1380w","/static/895b071970fc467ecad71d69e6d70d5a/161ec/4_1_2.png 1840w","/static/895b071970fc467ecad71d69e6d70d5a/e431d/4_1_2.png 1882w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/895b071970fc467ecad71d69e6d70d5a/c0255/4_1_2.png","alt":"4 1 2","title":"4 1 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":"You can also try a browser-based Python interpreter, such as "},{"type":"element","tagName":"a","properties":{"href":"https://www.python.org/shell/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://www.python.org/shell/"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The interpreter is a way of executing Python code line by line as soon as you write it. When you write a line of code and press "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Enter"}]},{"type":"text","value":", the interpreter executes the code right away and shows you the results:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/7977922a8e37fd253a7fd34e364e6d58/0f246/4_1_3.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: 19.565217391304348%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAkElEQVQI143LwQqDMAwAUP//53qQRq0zKmJALe0wB3HRZtDtOti7v6Kua0QchmEcRwCoqso5BwDOub7vjTFEpKoi8srO8xQRzYppmh4ZESHiuq7HcXjvY4zMHELw3jPzM4sxLsvCzN88z3PTNGVZtm2773tKSUTu+9Y/FETUdZ21FgAQcdu2kF3Xparph09+Az024L8JLoKAAAAAAElFTkSuQmCC'); 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":["/static/7977922a8e37fd253a7fd34e364e6d58/a0b58/4_1_3.webp 230w","/static/7977922a8e37fd253a7fd34e364e6d58/bc10c/4_1_3.webp 460w","/static/7977922a8e37fd253a7fd34e364e6d58/966d8/4_1_3.webp 920w","/static/7977922a8e37fd253a7fd34e364e6d58/34186/4_1_3.webp 1118w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7977922a8e37fd253a7fd34e364e6d58/81c8e/4_1_3.png 230w","/static/7977922a8e37fd253a7fd34e364e6d58/08a84/4_1_3.png 460w","/static/7977922a8e37fd253a7fd34e364e6d58/c0255/4_1_3.png 920w","/static/7977922a8e37fd253a7fd34e364e6d58/0f246/4_1_3.png 1118w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/7977922a8e37fd253a7fd34e364e6d58/c0255/4_1_3.png","alt":"4 1 3","title":"4 1 3","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":"Any Python code that can be written in a file can also be written in the interpreter. You can even assign variables and define methods:"}]},{"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","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" t "},{"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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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","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":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n"},{"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":" "},{"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"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"absolute_value"}]},{"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","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" number"},{"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","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":" number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"number\n"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" number\n"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" x "},{"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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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":"7"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" absolute_value"},{"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":"\nTraceback "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"most recent call 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":"text","value":"\n File "},{"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":" line "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"module"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\nNameError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'number'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" defined\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" absolute_value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" absolute_value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The interpreter is best suited to making small checks. For instance, you can try out functions and methods, or check if they exist at all:"}]},{"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","operator"]},"children":[{"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":"\"TextYteXt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"toupper"},{"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":"\nTraceback "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"most recent call 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":"text","value":"\n File "},{"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":" line "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"module"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\nAttributeError"},{"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":"'str'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"object"}]},{"type":"text","value":" has no attribute "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'toupper'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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":"\"TextYteXt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"upper"},{"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","string"]},"children":[{"type":"text","value":"'TEXTYTEXT'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If there is a method you need, and you can "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"almost"}]},{"type":"text","value":" remember its name, it's sometimes faster to skip Google and use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dir"}]},{"type":"text","value":" function in the interpreter. It tells you which methods are available to use on a given object:"}]},{"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","operator"]},"children":[{"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":"dir"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"this is 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","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__add__'"}]},{"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":"'__class__'"}]},{"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":"'__contains__'"}]},{"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":"'__delattr__'"}]},{"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":"'__dir__'"}]},{"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":"'__doc__'"}]},{"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":"'__eq__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__format__'"}]},{"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":"'__ge__'"}]},{"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":"'__getattribute__'"}]},{"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":"'__getitem__'"}]},{"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":"'__getnewargs__'"}]},{"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":"'__gt__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__hash__'"}]},{"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":"'__init__'"}]},{"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":"'__init_subclass__'"}]},{"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":"'__iter__'"}]},{"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":"'__le__'"}]},{"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":"'__len__'"}]},{"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":"'__lt__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__mod__'"}]},{"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":"'__mul__'"}]},{"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":"'__ne__'"}]},{"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":"'__new__'"}]},{"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":"'__reduce__'"}]},{"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":"'__reduce_ex__'"}]},{"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":"'__repr__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__rmod__'"}]},{"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":"'__rmul__'"}]},{"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":"'__setattr__'"}]},{"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":"'__sizeof__'"}]},{"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":"'__str__'"}]},{"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":"'__subclasshook__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'capitalize'"}]},{"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":"'casefold'"}]},{"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":"'center'"}]},{"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":"'count'"}]},{"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":"'encode'"}]},{"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":"'endswith'"}]},{"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":"'expandtabs'"}]},{"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":"'find'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'format'"}]},{"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":"'format_map'"}]},{"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":"'index'"}]},{"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":"'isalnum'"}]},{"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":"'isalpha'"}]},{"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":"'isascii'"}]},{"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":"'isdecimal'"}]},{"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":"'isdigit'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isidentifier'"}]},{"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":"'islower'"}]},{"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":"'isnumeric'"}]},{"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":"'isprintable'"}]},{"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":"'isspace'"}]},{"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":"'istitle'"}]},{"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":"'isupper'"}]},{"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":"'join'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'ljust'"}]},{"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":"'lstrip'"}]},{"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":"'maketrans'"}]},{"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":"'partition'"}]},{"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":"'replace'"}]},{"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":"'rfind'"}]},{"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":"'rindex'"}]},{"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":"'rjust'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'rpartition'"}]},{"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":"'rsplit'"}]},{"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":"'rstrip'"}]},{"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":"'split'"}]},{"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":"'splitlines'"}]},{"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":"'startswith'"}]},{"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":"'strip'"}]},{"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":"'swapcase'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'title'"}]},{"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":"'translate'"}]},{"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":"'zfill'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python strings have a lot of methods available, as you can see above. At this point it might be best to ignore any methods that have underscores in their names, but the rest may well prove useful. Some of them you can figure out by trying them out, and the rest you can search for online."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python lists don't have quite so many methods:"}]},{"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","operator"]},"children":[{"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":"dir"}]},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__add__'"}]},{"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":"'__class__'"}]},{"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":"'__contains__'"}]},{"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":"'__delattr__'"}]},{"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":"'__delitem__'"}]},{"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":"'__dir__'"}]},{"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":"'__doc__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__eq__'"}]},{"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":"'__format__'"}]},{"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":"'__ge__'"}]},{"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":"'__getattribute__'"}]},{"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":"'__getitem__'"}]},{"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":"'__gt__'"}]},{"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":"'__hash__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__iadd__'"}]},{"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":"'__imul__'"}]},{"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":"'__init__'"}]},{"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":"'__init_subclass__'"}]},{"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":"'__iter__'"}]},{"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":"'__le__'"}]},{"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":"'__len__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__lt__'"}]},{"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":"'__mul__'"}]},{"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":"'__ne__'"}]},{"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":"'__new__'"}]},{"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":"'__reduce__'"}]},{"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":"'__reduce_ex__'"}]},{"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":"'__repr__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__reversed__'"}]},{"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":"'__rmul__'"}]},{"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":"'__setattr__'"}]},{"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":"'__setitem__'"}]},{"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":"'__sizeof__'"}]},{"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":"'__str__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__subclasshook__'"}]},{"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":"'append'"}]},{"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":"'clear'"}]},{"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":"'copy'"}]},{"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":"'count'"}]},{"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":"'extend'"}]},{"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":"'index'"}]},{"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":"'insert'"}]},{"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":"'pop'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'remove'"}]},{"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":"'reverse'"}]},{"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":"'sort'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's try a few of them, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reverse"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"clear"}]},{"type":"text","value":" seem promising:"}]},{"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","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" 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","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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"reverse"},{"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","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" numbers\n"},{"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":" "},{"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":"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":" "},{"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":" "},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"clear"},{"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","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" numbers\n"},{"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":"As you can see, these methods do pretty much what you would expect based on their names."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice how the interpreter doesn't print out anything when you run the command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"numbers.reverse()"}]},{"type":"text","value":". This is because the interpreter prints something out only if the line of code has a value. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reverse()"}]},{"type":"text","value":" method here doesn't return any value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the above example we printed out the value of the list "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"numbers"}]},{"type":"text","value":" by typing in just the name of the variable. In fact, it is rarely necessary to explicitly type in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" commands in the interpreter. You can include them if you want to, however."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Remember to close the interpreter when you are finished. The commands "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"quit()"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exit()"}]},{"type":"text","value":" will close it, as will the key combo "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Control"}]},{"type":"text","value":"+"},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"D"}]},{"type":"text","value":" (Linux/Mac) or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Control"}]},{"type":"text","value":"+"},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Z"}]},{"type":"text","value":" (Windows). Especially in Visual Studio Code this is important, as trying to execute another Python program while the interpreter is still running results in a rather cryptic error message:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/030410f1be5c4a9d02209afb96fe30e4/966ce/4_1_4.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: 28.695652173913043%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA7klEQVQY023G0W6EIBCFYd9jBQZQgWEERdx1FTdbTd//lZqYthdNT76T/FXOkyMa0phzTikhekRy1nXGInrvyVqrlP4DVBNJVwbtOA8u9tA2QggAkFJKgO+4kv+MMc6uc8YAZKV0E4YQxwRaMyFqzm91fWOXK2rGfoGSoJSQwEEwwatnTFtM52MtcVrD+KA4O8rWz67PjiaDFz9Znwy+0v2dl1ea15BWjNVxnp/HeX4ceynPZdme615KWbfXvpdtC9QTInnfe+o92c5YYwjRIS4uVHcfHxgWGobGRt1FbaI2wyXqLsj2H6oj1S7KfgH1/DpLnHkC8gAAAABJRU5ErkJggg=='); 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":["/static/030410f1be5c4a9d02209afb96fe30e4/a0b58/4_1_4.webp 230w","/static/030410f1be5c4a9d02209afb96fe30e4/bc10c/4_1_4.webp 460w","/static/030410f1be5c4a9d02209afb96fe30e4/966d8/4_1_4.webp 920w","/static/030410f1be5c4a9d02209afb96fe30e4/445df/4_1_4.webp 1380w","/static/030410f1be5c4a9d02209afb96fe30e4/78de1/4_1_4.webp 1840w","/static/030410f1be5c4a9d02209afb96fe30e4/df82e/4_1_4.webp 2050w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/030410f1be5c4a9d02209afb96fe30e4/81c8e/4_1_4.png 230w","/static/030410f1be5c4a9d02209afb96fe30e4/08a84/4_1_4.png 460w","/static/030410f1be5c4a9d02209afb96fe30e4/c0255/4_1_4.png 920w","/static/030410f1be5c4a9d02209afb96fe30e4/b1001/4_1_4.png 1380w","/static/030410f1be5c4a9d02209afb96fe30e4/161ec/4_1_4.png 1840w","/static/030410f1be5c4a9d02209afb96fe30e4/966ce/4_1_4.png 2050w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/030410f1be5c4a9d02209afb96fe30e4/c0255/4_1_4.png","alt":"4 1 4","title":"4 1 4","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":"h2","properties":{"id":"the-built-in-debugger","style":"position:relative;"},"children":[{"type":"text","value":"The built-in debugger"},{"type":"element","tagName":"a","properties":{"href":"#the-built-in-debugger","ariaLabel":"the built in debugger 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 have already spent a fair amount of time and effort on developing debugging skills, mainly through print statement debugging. The Visual Studio Code editor has another tool at your disposal: a built-in "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"visual debugger"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To start debugging, first you have to define a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"breakpoint"}]},{"type":"text","value":" in your code. A breakpoint is a point in your program where the debugger halts execution. You can set a breakpoint by clicking on the left edge of any line in your program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following example is a slightly faulty attempt at solving the exercise "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"The sum of consecutive numbers"}]},{"type":"text","value":" in the "},{"type":"element","tagName":"a","properties":{"href":"/part-3/1-loops-with-conditions"},"children":[{"type":"text","value":"previous part"}]},{"type":"text","value":". There is a breakpoint on line 5:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 635px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/48f977029694dbc4538579f601adfc5e/1ddef/4_1_5.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: 49.565217391304344%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAAsTAAALEwEAmpwYAAABb0lEQVQoz4WSSY7bMBBFdQ9L4lAUZ1G23EaQ9CaHyP2v8gLRcQ9oN7L4YKFQ/Hg1DN57xCcu553r+cLWGue2kWNCxOHcglsCy7KwLP5NPf9EQ06lF5ScqbXR1o1cK62t1JLJMZLLyloKOWVCCMQQsdYyz/MXDQeF0RqtNKmeWdsF4x0xB1LyBCcs1mCNRc2KeZ76x2manmo4MNVhqDW57tTtRkwFoxXjaeR0OjGOYy9+RvSFMIgja411juYbL+dX9h+/2W+/KLWxXX9irfRipdR/1Qm1Un0m0zSS6oWyvVDWS89pfde9pe9b/dTy0a6IMKuZ5CttuxJi6VsLsaL1++y+02Mkg1iDMaafxThOqEmR6k5uN3LbSWXDGt0Nx/H0psPgY/wwHSTuGCOdMAQhpoC3Bm8VSSxRNEEsXiwuFHyqhNQQWVh8IeTW7/WdcP+D0q4T2H+0xwkdVGLur9W6b10bwdhDrtcYc4+PZTwM/wL3pRm/0skzmwAAAABJRU5ErkJggg=='); 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":["/static/48f977029694dbc4538579f601adfc5e/a0b58/4_1_5.webp 230w","/static/48f977029694dbc4538579f601adfc5e/bc10c/4_1_5.webp 460w","/static/48f977029694dbc4538579f601adfc5e/a4521/4_1_5.webp 635w"],"sizes":"(max-width: 635px) 100vw, 635px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/48f977029694dbc4538579f601adfc5e/81c8e/4_1_5.png 230w","/static/48f977029694dbc4538579f601adfc5e/08a84/4_1_5.png 460w","/static/48f977029694dbc4538579f601adfc5e/1ddef/4_1_5.png 635w"],"sizes":"(max-width: 635px) 100vw, 635px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/48f977029694dbc4538579f601adfc5e/1ddef/4_1_5.png","alt":"4 1 5","title":"4 1 5","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":"After the breakpoint has been set, choose "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Start debugging"}]},{"type":"text","value":" from the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Run"}]},{"type":"text","value":" menu. This opens a list of options, where you should select "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Python File"}]},{"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: 689px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/c4e9f7d2b99f512e3de82b450788ac99/0f79a/4_1_6.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: 50.43478260869565%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAAsTAAALEwEAmpwYAAABr0lEQVQoz22RWW7bMBBAdY9YC0mRlKzdkmwtCdDWiQP0JwtatEV7/2u8gpRRIEk/HmZGnHkYUkGW5czTxLIsG/Psa8f9+czl4cHzeLn4uC7L1j9P//rc3D7fo5QiaNqR+fyLu8c/rJffTF9+0t2+crh9pVtfPPX0RDU9+dgsL+94pl2/IdKSMNwRFGXL9Ok78+cf9HevtMsz3fpMf5W6mPVfkdX9RnmPeMMZWT0QyZwwvCGwxnIaetb5xHwcGA4Nh6aiq0u6pqKtC6xWaJWglbiSvEOQxBFRFBFUVc1xPDGOR7r2QN+PdF3v89bVh4E8L0hTg9aWVJsPuO9JkmxCrQ3LvHI6nrxk6AcvGYfxmvdkNiNNNdoP/584TgjDkMD95bpq6NqOsiipq5qyrCj2BXXdevyG2qBUilQKKSVSqje47bzQ2ozM5hhjfaPWGqVcTLHaYI0hMxbtRElMkgiEdGKNEIokkddcEEUhQdO0fjN3JSEkcRyj3MObFCUEWiRYJTAOKVBSkdo9qc5JU4tSGm0LL/dCdw2HewcndGs7drvdFt+xnd34861n5+ttLuIv6IsuDPMuDa0AAAAASUVORK5CYII='); 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":["/static/c4e9f7d2b99f512e3de82b450788ac99/a0b58/4_1_6.webp 230w","/static/c4e9f7d2b99f512e3de82b450788ac99/bc10c/4_1_6.webp 460w","/static/c4e9f7d2b99f512e3de82b450788ac99/2e76f/4_1_6.webp 689w"],"sizes":"(max-width: 689px) 100vw, 689px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/c4e9f7d2b99f512e3de82b450788ac99/81c8e/4_1_6.png 230w","/static/c4e9f7d2b99f512e3de82b450788ac99/08a84/4_1_6.png 460w","/static/c4e9f7d2b99f512e3de82b450788ac99/0f79a/4_1_6.png 689w"],"sizes":"(max-width: 689px) 100vw, 689px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/c4e9f7d2b99f512e3de82b450788ac99/0f79a/4_1_6.png","alt":"4 1 6","title":"4 1 6","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":"This starts the debugger, which executes your code as usual until the execution reaches a breakpoint and halts. If your code asks for input, remember to type that in in the terminal:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/2e80abc894afe89ee8b2a3444be1da55/58fee/4_1_7.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: 46.95652173913043%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAABjUlEQVQoz2WS227jIBRF/R2T2ObmgG0w4PgSt1E10vz/N60RbqfKqA9LcAQ6nM3e1bpuLMvKum2klAhhQpsO1w9MfsKHQJ5nYsqkGHHW0XU3pJQvKAancLajut9nwhjw1mONxZgOrTVtKxh8prMOpQSyaWiahrou1P9xvV6pG0ErJNXx/sFgPXlM3G72fL0zGqME2x55/llZ33rSpgn5FyFffjDNF2x/ob7WVG/P38SQiWM8pysNtTYIIc51DDO29yjd0TRlousL9Tn1K9W6LhhjUFp/yTUopVFKoVX5l4FhCPTWooRAKoOUGqk0QqhPuS/yq9OU+4IfA9a60xTnHFIKtFEoKXG3DlPq0kRpmlaeTYuact62LaIVtE1LlXNxLxOnxDh6cprp+wEVA+aYcVvGPzdCXMn3D0LcCfEgpnfm+5OUD2I+yPOTKR1UKeVvma+SU75zPA4e+866ruz7znEcJ2W/7xslIcuynPXj8TjvVSWD3gf86D8d/mKapjM+5aOLpELzHZ2fZvwz5S9qfAfjXY2XHQAAAABJRU5ErkJggg=='); 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":["/static/2e80abc894afe89ee8b2a3444be1da55/a0b58/4_1_7.webp 230w","/static/2e80abc894afe89ee8b2a3444be1da55/bc10c/4_1_7.webp 460w","/static/2e80abc894afe89ee8b2a3444be1da55/966d8/4_1_7.webp 920w","/static/2e80abc894afe89ee8b2a3444be1da55/42749/4_1_7.webp 1051w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/2e80abc894afe89ee8b2a3444be1da55/81c8e/4_1_7.png 230w","/static/2e80abc894afe89ee8b2a3444be1da55/08a84/4_1_7.png 460w","/static/2e80abc894afe89ee8b2a3444be1da55/c0255/4_1_7.png 920w","/static/2e80abc894afe89ee8b2a3444be1da55/58fee/4_1_7.png 1051w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/2e80abc894afe89ee8b2a3444be1da55/c0255/4_1_7.png","alt":"4 1 7","title":"4 1 7","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":"On the left there is now the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Variables"}]},{"type":"text","value":" view, which contains the current values of all variables active in the code. You can proceed with the execution line by line by clicking on the down arrow, which is labelled "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Step into"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the image below the loop in the code has already been repeated a few times:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/d7c8044e94fc076c65e443ef49b465e8/e996b/4_1_8.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: 46.52173913043478%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAABk0lEQVQoz12SWW6kMBBAOcc0i1cwYMBmp9Nq5f6XeiMzSqLMx5NLLrtUr+xs23bWdWPbNkKIDH5Aa0Pbdox+YBgH4jwzhUiYJlzjqG2NlPIbpRSdUzS1JVuWmaEf6BtPo5u7mFKaqhK0fUTXDikEVVFS5CV54lH84vGnoCgElZBk5/NNV3tiH2iaBmsbaq0xUrCuE+d7J24tPhhc/6Dt81+4tPocY3OKoiB7fnwyDZGxH2+VhDEGJQWuM8Q94kOH6zWmThcfmPoH2+Q3SqcuS7JtW/9ppq6M/VZOc1FSUduOth1pbIMoBUJYhNAIYSgrdevmjzSKgqIsUsGddVnxfsDaGt97nHNIKdAmFZW0tcUogdEaec83PYS5z6d8VVWINOeyIpvnmWO/bpZ553m9iNOMDAPmirgtMLx2/LgSlzfDdODHkyl8MC8vQjiZ4kWcX4zhIgsh3LpJ80dZEePKdV6cx8G2rhzHwXVdNyne951lWVi/c+f99bK0kXT73t8KX4zjiNKKsixvpUSKE+k1v+L/+QuElghBxo9GuQAAAABJRU5ErkJggg=='); 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":["/static/d7c8044e94fc076c65e443ef49b465e8/a0b58/4_1_8.webp 230w","/static/d7c8044e94fc076c65e443ef49b465e8/bc10c/4_1_8.webp 460w","/static/d7c8044e94fc076c65e443ef49b465e8/966d8/4_1_8.webp 920w","/static/d7c8044e94fc076c65e443ef49b465e8/c139f/4_1_8.webp 1050w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/d7c8044e94fc076c65e443ef49b465e8/81c8e/4_1_8.png 230w","/static/d7c8044e94fc076c65e443ef49b465e8/08a84/4_1_8.png 460w","/static/d7c8044e94fc076c65e443ef49b465e8/c0255/4_1_8.png 920w","/static/d7c8044e94fc076c65e443ef49b465e8/e996b/4_1_8.png 1050w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/d7c8044e94fc076c65e443ef49b465e8/c0255/4_1_8.png","alt":"4 1 8","title":"4 1 8","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 debugger has a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Debug console"}]},{"type":"text","value":" tab, which lets you evaluate expressions with the current values stored in the variables. For example, you can check the value of the Boolean expression in the condition of the loop:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/eeca0983be1396e4128646efa2b0a71d/58fee/4_1_9.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: 41.73913043478261%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAAAsTAAALEwEAmpwYAAABWklEQVQoz22R7W6jMBREeY5NAONvMDaGxLRpo2qlff9nOpVpWrW7++NoLF3Jd2ZuU8rO9Voo+07OmRgTShv8OJHmxBwj67ax5JW8LHjnMcYyDMM3JJOXeGdoLpeNGCKzm3HKoZRGKUXfC6Z5xTiPlIKh6+i6jrattD84n8+0naAXA83t9Y3gImvIOOew1mG0QkvB/rRw/1MoryN5V8T1F3E9/UPaTrjxRHtuaV7uv1niyhIS1lic/XAphDg0xA03BqQyD4fnH+66h/NPmlKuKK2RSh3d1U+kVEgpUXLA2ZEQEt5aRN8hZK1j+KLG/aQuaI6jXArzHPF+JMV0aC1b1+hKYZTEG02YJoz1pJRZ8kZKC957xnHE1oVC0Gzbxl5uPJUbe3nm/vJGzhujD4QpHE67vj/UGoPoe4zRx9vojwNW6rzve5p1XdGPqPp75GHAKEX39zUf+j9q5HfXoOa1sn/qYQAAAABJRU5ErkJggg=='); 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":["/static/eeca0983be1396e4128646efa2b0a71d/a0b58/4_1_9.webp 230w","/static/eeca0983be1396e4128646efa2b0a71d/bc10c/4_1_9.webp 460w","/static/eeca0983be1396e4128646efa2b0a71d/966d8/4_1_9.webp 920w","/static/eeca0983be1396e4128646efa2b0a71d/42749/4_1_9.webp 1051w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/eeca0983be1396e4128646efa2b0a71d/81c8e/4_1_9.png 230w","/static/eeca0983be1396e4128646efa2b0a71d/08a84/4_1_9.png 460w","/static/eeca0983be1396e4128646efa2b0a71d/c0255/4_1_9.png 920w","/static/eeca0983be1396e4128646efa2b0a71d/58fee/4_1_9.png 1051w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/eeca0983be1396e4128646efa2b0a71d/c0255/4_1_9.png","alt":"4 1 9","title":"4 1 9","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":"You can include several breakpoints in your program code. When execution has halted, you can start it again by clicking on the blue triangle. Execution proceeds until it reaches the next breakpoint."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The built-in visual debugger is a good alternative to debugging print statements. It is up to you which you choose to employ more in the future. Each programmer has their own preferences, but it's always a good idea to try different options before settling on any single solution."}]}]},"html":"

    After this section

      \n
    • You will be equipped to use the Visual Studio Code editor to complete exercises on this course
    • \n
    • You will be familiar with the interactive Python interpreter, and will be able to use it to run code
    • \n

    Thus far all the exercises on this course have been completed directly on the course pages in embedded editor windows. Programming in the browser is very suitable for the very first steps in programming, but now it is time to start using a separate editor especially made for programming.

    There are dozens of different editors that are suited to programming. On this course we will use the Visual Studio Code editor, which has been gaining traction in recent years.

    Please install the Visual Studio Code editor on your own computer now. You may also need to install Python and the Visual Studio Code plugin for Python. You will also need the TMC plugin, which will take care of running the tests that go with the exercises. In the TMC plugin, select MOOC as the organization and Python Programming 2024 as the course.

    Here is a guide to installing and running all of these. Read the instructions on working on and submitting exercises, and then complete the task below:

    Please write a program which asks the user which editor they are using. The program should keep on asking until the user types in Visual Studio Code.

    Have a look at the example of expected behaviour below:

    Editor: Emacs\nnot good\nEditor: Vim\nnot good\nEditor: Word\nawful\nEditor: Atom\nnot good\nEditor: Visual Studio Code\nan excellent choice!

    If the user types in Word or Notepad, the program counters with awful. Other unacceptable editor choices receive the reply not good.

    The program should be case-insensitive in its reactions. That is, the same user input in lowercase, uppercase or mixed case should trigger the same reaction:

    Editor: NOTEPAD\nawful\nEditor: viSUal STudiO cODe\nan excellent choice!

    Hint: The simplest way to achieve this is converting all characters to the same case. The Python string method lower converts a string to lowercase entirely. An example of its use:

    mystring = \"Visual Studio CODE\"\nif \"visual studio code\" == mystring.lower():\n    print(\"this was the string I was looking for!\")

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block. The same applies to any upcoming exercises that don't explicitly ask for functions.

    Executing code

    In Visual Studio Code, the easiest way to execute your code is clicking on the triangle in the upper right hand corner. Sometimes it happens that some program is left running in the background, maybe waiting for input from the user or stuck in an infinite loop, without you realising it. You might only notice this when trying to execute the next program, which simply will not run because the previous execution is still hogging up resources. A quick fix is pressing on the keys Control+C at the same time, which stops the execution of any ongoing process. The next program should now run fine.

    The interactive Python interpreter

    One of the most important tools for any Python programmer is the interactive Python interpreter.

    Starting up the interpreter may depend on your platform. In a Linux or Mac system, you can type in python3 in the terminal. In Windows, the command on the command line might be python. Firing up the interpreter on a Mac looks like this:

    \n \n \n \n \n \n \"4\n \n \n

    You can also try a browser-based Python interpreter, such as https://www.python.org/shell/.

    The interpreter is a way of executing Python code line by line as soon as you write it. When you write a line of code and press Enter, the interpreter executes the code right away and shows you the results:

    \n \n \n \n \n \n \"4\n \n \n

    Any Python code that can be written in a file can also be written in the interpreter. You can even assign variables and define methods:

    >>> t = [1,2,3,4,5]\n>>> for number in t:\n...   print(number)\n...\n1\n2\n3\n4\n5\n>>> def absolute_value(number):\n...   if number<0:\n...      number = -number\n...   return number\n...\n>>> x = 10\n>>> y = -7\n>>> absolute_value(number)\nTraceback (most recent call last):\n  File \"<stdin>\", line 1, in <module>\nNameError: name 'number' is not defined\n>>> absolute_value(x)\n10\n>>> absolute_value(y)\n7\n>>>

    The interpreter is best suited to making small checks. For instance, you can try out functions and methods, or check if they exist at all:

    >>> \"TextYteXt\".toupper()\nTraceback (most recent call last):\n  File \"<stdin>\", line 1, in <module>\nAttributeError: 'str' object has no attribute 'toupper'\n>>> \"TextYteXt\".upper()\n'TEXTYTEXT'\n>>>

    If there is a method you need, and you can almost remember its name, it's sometimes faster to skip Google and use the dir function in the interpreter. It tells you which methods are available to use on a given object:

    >>> dir(\"this is a string\")\n['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__',\n'__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__',\n'__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__',\n'__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',\n'__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',\n'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find',\n'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit',\n'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join',\n'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust','rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase',\n'title', 'translate', 'upper', 'zfill']

    Python strings have a lot of methods available, as you can see above. At this point it might be best to ignore any methods that have underscores in their names, but the rest may well prove useful. Some of them you can figure out by trying them out, and the rest you can search for online.

    Python lists don't have quite so many methods:

    >>> dir([])\n['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__',\n'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__',\n'__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__',\n'__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',\n'__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__',\n'__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop',\n'remove', 'reverse', 'sort']\n>>>

    Let's try a few of them, reverse and clear seem promising:

    >>> numbers = [1,2,3,4,5]\n>>> numbers.reverse()\n>>> numbers\n[5, 4, 3, 2, 1]\n>>> numbers.clear()\n>>> numbers\n[]

    As you can see, these methods do pretty much what you would expect based on their names.

    Notice how the interpreter doesn't print out anything when you run the command numbers.reverse(). This is because the interpreter prints something out only if the line of code has a value. The reverse() method here doesn't return any value.

    In the above example we printed out the value of the list numbers by typing in just the name of the variable. In fact, it is rarely necessary to explicitly type in print commands in the interpreter. You can include them if you want to, however.

    Remember to close the interpreter when you are finished. The commands quit() or exit() will close it, as will the key combo Control+D (Linux/Mac) or Control+Z (Windows). Especially in Visual Studio Code this is important, as trying to execute another Python program while the interpreter is still running results in a rather cryptic error message:

    \n \n \n \n \n \n \"4\n \n \n

    The built-in debugger

    We have already spent a fair amount of time and effort on developing debugging skills, mainly through print statement debugging. The Visual Studio Code editor has another tool at your disposal: a built-in visual debugger.

    To start debugging, first you have to define a breakpoint in your code. A breakpoint is a point in your program where the debugger halts execution. You can set a breakpoint by clicking on the left edge of any line in your program.

    The following example is a slightly faulty attempt at solving the exercise The sum of consecutive numbers in the previous part. There is a breakpoint on line 5:

    \n \n \n \n \n \n \"4\n \n \n

    After the breakpoint has been set, choose Start debugging from the Run menu. This opens a list of options, where you should select Python File:

    \n \n \n \n \n \n \"4\n \n \n

    On the left there is now the Variables view, which contains the current values of all variables active in the code. You can proceed with the execution line by line by clicking on the down arrow, which is labelled Step into.

    In the image below the loop in the code has already been repeated a few times:

    \n \n \n \n \n \n \"4\n \n \n

    The debugger has a Debug console tab, which lets you evaluate expressions with the current values stored in the variables. For example, you can check the value of the Boolean expression in the condition of the loop:

    \n \n \n \n \n \n \"4\n \n \n

    You can include several breakpoints in your program code. When execution has halted, you can start it again by clicking on the blue triangle. Execution proceeds until it reaches the next breakpoint.

    The built-in visual debugger is a good alternative to debugging print statements. It is up to you which you choose to employ more in the future. Each programmer has their own preferences, but it's always a good idea to try different options before settling on any single solution.

    ","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-4/1-vscode.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 equipped to use the Visual Studio Code editor to complete exercises on this course"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be familiar with the interactive Python interpreter, and will be able to use it to run code"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Thus far all the exercises on this course have been completed directly on the course pages in embedded editor windows. Programming in the browser is very suitable for the very first steps in programming, but now it is time to start using a separate editor especially made for programming."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are dozens of different editors that are suited to programming. On this course we will use the "},{"type":"element","tagName":"a","properties":{"href":"https://code.visualstudio.com/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Visual Studio Code"}]},{"type":"text","value":" editor, which has been gaining traction in recent years."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please install the Visual Studio Code editor on your own computer now. You may also need to install Python and the Visual Studio Code plugin for Python. You will also need the TMC plugin, which will take care of running the tests that go with the exercises. In the TMC plugin, select "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"MOOC"}]},{"type":"text","value":" as the organization and "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Python Programming 2024"}]},{"type":"text","value":" as the course."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://www.mooc.fi/en/installation/vscode","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Here is a guide"}]},{"type":"text","value":" to installing and running all of these. Read the instructions on working on and submitting exercises, and then complete the task below:"}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Hello Visual Studio Code","tmcname":"part04-01_hello_visual_studio_code"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user which editor they are using. The program should keep on asking until the user types in "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Visual Studio Code"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Have a look at the example of expected behaviour below:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Editor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Emacs"}]},{"type":"text","value":"\nnot good\nEditor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Vim"}]},{"type":"text","value":"\nnot good\nEditor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Word"}]},{"type":"text","value":"\nawful\nEditor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Atom"}]},{"type":"text","value":"\nnot good\nEditor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Visual Studio Code"}]},{"type":"text","value":"\nan excellent choice!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the user types in Word or Notepad, the program counters with "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"awful"}]},{"type":"text","value":". Other unacceptable editor choices receive the reply "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not good"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should be case-insensitive in its reactions. That is, the same user input in lowercase, uppercase or mixed case should trigger the same reaction:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Editor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NOTEPAD"}]},{"type":"text","value":"\nawful\nEditor: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"viSUal STudiO cODe"}]},{"type":"text","value":"\nan excellent choice!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hint: The simplest way to achieve this is converting all characters to the same case. The Python string method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lower"}]},{"type":"text","value":" converts a string to lowercase entirely. An example of its use:"}]},{"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":"mystring "},{"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":"\"Visual Studio CODE\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"visual studio code\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" mystring"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lower"},{"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 "},{"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":"\"this was the string I was looking for!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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 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. The same applies to any upcoming exercises that don't explicitly ask for functions."}]}]},{"type":"element","tagName":"h2","properties":{"id":"executing-code","style":"position:relative;"},"children":[{"type":"text","value":"Executing code"},{"type":"element","tagName":"a","properties":{"href":"#executing-code","ariaLabel":"executing code 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 Visual Studio Code, the easiest way to execute your code is clicking on the triangle in the upper right hand corner. Sometimes it happens that some program is left running in the background, maybe waiting for input from the user or stuck in an infinite loop, without you realising it. You might only notice this when trying to execute the next program, which simply will not run because the previous execution is still hogging up resources. A quick fix is pressing on the keys "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Control"}]},{"type":"text","value":"+"},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"C"}]},{"type":"text","value":" at the same time, which stops the execution of any ongoing process. The next program should now run fine."}]},{"type":"element","tagName":"h2","properties":{"id":"the-interactive-python-interpreter","style":"position:relative;"},"children":[{"type":"text","value":"The interactive Python interpreter"},{"type":"element","tagName":"a","properties":{"href":"#the-interactive-python-interpreter","ariaLabel":"the interactive python interpreter 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":"One of the most important tools for any Python programmer is the interactive Python interpreter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Starting up the interpreter may depend on your platform. In a Linux or Mac system, you can type in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"python3"}]},{"type":"text","value":" in the terminal. In Windows, the command on the command line might be "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"python"}]},{"type":"text","value":". Firing up the interpreter on a Mac looks like this:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/0f246/4_1_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: 17.82608695652174%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAABYlAAAWJQFJUiTwAAAArUlEQVQI13XLywqDMBCFYd//tbroMhiYZFppBG/YJAQTN+o4sVS77bc6HPiL2/Pe1E3ftc8HlmWJiFprKWVVVQAgpUT8/gCglBJCGGPatm1ORV2Zruve49s5F2O01nrvnXMppb7vQwjHfwU+cBxHRBRCAIB5GaUUIg7DoLUGgJRSjDGEME2T995aO8/zLyainDMzExEzH8ex7zsz55yvvW0bEW2ndV2XZSGiK/4AItbceE5Q2z8AAAAASUVORK5CYII='); 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":["/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/a0b58/4_1_1.webp 230w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/bc10c/4_1_1.webp 460w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/966d8/4_1_1.webp 920w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/34186/4_1_1.webp 1118w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/81c8e/4_1_1.png 230w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/08a84/4_1_1.png 460w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/c0255/4_1_1.png 920w","/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/0f246/4_1_1.png 1118w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/7267d91f6d4cbf0f25e0a55ca8dea0ce/c0255/4_1_1.png","alt":"4 1 1","title":"4 1 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":"p","properties":{},"children":[{"type":"text","value":"It is also possible to start the interpreter inside Visual Studio Code. First you should execute a program by clicking on the triangle. This should open a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Terminal"}]},{"type":"text","value":" section on your screen, where you can now type in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"python3"}]},{"type":"text","value":" (or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"python"}]},{"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: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/895b071970fc467ecad71d69e6d70d5a/e431d/4_1_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: 49.130434782608695%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAIAAAA7N+mxAAAACXBIWXMAABYlAAAWJQFJUiTwAAABF0lEQVQoz52RWW7DIBCGuUcLw2I2Ox42O7hZrtCXqve/SgVOokh5aNVPCM32s8wQjDHFlHIOIRhjnfP78r7ttgVuEWedMuZo58mNNak4e+JHnxYsBXPBcfbaaGOsbSKntZZqMM4111qplGfySyYL4h2AASeUUiG4EJxzAAD6BOs8uwr4G6MUGPQU6Wl2K/wNym4y9hD/GwIAfyyFO7vdxLajtUbEaZqsMcZZLsUDKSXnfBzHdV0Ph8M8z62kt5PUWtd1RcRSSs4ZQ9hy+UjLmkuKMYSwx2OMy7KkNtOcOqUUcr1eL5dLrXXbtvPpVOrxO54/h4jSDEoJIeD+yNdfkG3bEHE/MvarQgiD1koPzjml1GNmrw37AVG5Vxy3ozSFAAAAAElFTkSuQmCC'); 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":["/static/895b071970fc467ecad71d69e6d70d5a/a0b58/4_1_2.webp 230w","/static/895b071970fc467ecad71d69e6d70d5a/bc10c/4_1_2.webp 460w","/static/895b071970fc467ecad71d69e6d70d5a/966d8/4_1_2.webp 920w","/static/895b071970fc467ecad71d69e6d70d5a/445df/4_1_2.webp 1380w","/static/895b071970fc467ecad71d69e6d70d5a/78de1/4_1_2.webp 1840w","/static/895b071970fc467ecad71d69e6d70d5a/609ff/4_1_2.webp 1882w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/895b071970fc467ecad71d69e6d70d5a/81c8e/4_1_2.png 230w","/static/895b071970fc467ecad71d69e6d70d5a/08a84/4_1_2.png 460w","/static/895b071970fc467ecad71d69e6d70d5a/c0255/4_1_2.png 920w","/static/895b071970fc467ecad71d69e6d70d5a/b1001/4_1_2.png 1380w","/static/895b071970fc467ecad71d69e6d70d5a/161ec/4_1_2.png 1840w","/static/895b071970fc467ecad71d69e6d70d5a/e431d/4_1_2.png 1882w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/895b071970fc467ecad71d69e6d70d5a/c0255/4_1_2.png","alt":"4 1 2","title":"4 1 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":"You can also try a browser-based Python interpreter, such as "},{"type":"element","tagName":"a","properties":{"href":"https://www.python.org/shell/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://www.python.org/shell/"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The interpreter is a way of executing Python code line by line as soon as you write it. When you write a line of code and press "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Enter"}]},{"type":"text","value":", the interpreter executes the code right away and shows you the results:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/7977922a8e37fd253a7fd34e364e6d58/0f246/4_1_3.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: 19.565217391304348%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAkElEQVQI143LwQqDMAwAUP//53qQRq0zKmJALe0wB3HRZtDtOti7v6Kua0QchmEcRwCoqso5BwDOub7vjTFEpKoi8srO8xQRzYppmh4ZESHiuq7HcXjvY4zMHELw3jPzM4sxLsvCzN88z3PTNGVZtm2773tKSUTu+9Y/FETUdZ21FgAQcdu2kF3Xparph09+Az024L8JLoKAAAAAAElFTkSuQmCC'); 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":["/static/7977922a8e37fd253a7fd34e364e6d58/a0b58/4_1_3.webp 230w","/static/7977922a8e37fd253a7fd34e364e6d58/bc10c/4_1_3.webp 460w","/static/7977922a8e37fd253a7fd34e364e6d58/966d8/4_1_3.webp 920w","/static/7977922a8e37fd253a7fd34e364e6d58/34186/4_1_3.webp 1118w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7977922a8e37fd253a7fd34e364e6d58/81c8e/4_1_3.png 230w","/static/7977922a8e37fd253a7fd34e364e6d58/08a84/4_1_3.png 460w","/static/7977922a8e37fd253a7fd34e364e6d58/c0255/4_1_3.png 920w","/static/7977922a8e37fd253a7fd34e364e6d58/0f246/4_1_3.png 1118w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/7977922a8e37fd253a7fd34e364e6d58/c0255/4_1_3.png","alt":"4 1 3","title":"4 1 3","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":"Any Python code that can be written in a file can also be written in the interpreter. You can even assign variables and define methods:"}]},{"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","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" t "},{"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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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","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":" t"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n"},{"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":" "},{"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"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"absolute_value"}]},{"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","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" number"},{"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","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":" number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"number\n"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" number\n"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" x "},{"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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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":"7"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" absolute_value"},{"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":"\nTraceback "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"most recent call 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":"text","value":"\n File "},{"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":" line "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"module"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\nNameError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'number'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" defined\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" absolute_value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" absolute_value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The interpreter is best suited to making small checks. For instance, you can try out functions and methods, or check if they exist at all:"}]},{"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","operator"]},"children":[{"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":"\"TextYteXt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"toupper"},{"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":"\nTraceback "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"most recent call 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":"text","value":"\n File "},{"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":" line "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"module"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":"\nAttributeError"},{"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":"'str'"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"object"}]},{"type":"text","value":" has no attribute "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'toupper'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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":"\"TextYteXt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"upper"},{"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","string"]},"children":[{"type":"text","value":"'TEXTYTEXT'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If there is a method you need, and you can "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"almost"}]},{"type":"text","value":" remember its name, it's sometimes faster to skip Google and use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dir"}]},{"type":"text","value":" function in the interpreter. It tells you which methods are available to use on a given object:"}]},{"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","operator"]},"children":[{"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":"dir"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"this is 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","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__add__'"}]},{"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":"'__class__'"}]},{"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":"'__contains__'"}]},{"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":"'__delattr__'"}]},{"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":"'__dir__'"}]},{"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":"'__doc__'"}]},{"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":"'__eq__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__format__'"}]},{"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":"'__ge__'"}]},{"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":"'__getattribute__'"}]},{"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":"'__getitem__'"}]},{"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":"'__getnewargs__'"}]},{"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":"'__gt__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__hash__'"}]},{"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":"'__init__'"}]},{"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":"'__init_subclass__'"}]},{"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":"'__iter__'"}]},{"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":"'__le__'"}]},{"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":"'__len__'"}]},{"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":"'__lt__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__mod__'"}]},{"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":"'__mul__'"}]},{"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":"'__ne__'"}]},{"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":"'__new__'"}]},{"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":"'__reduce__'"}]},{"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":"'__reduce_ex__'"}]},{"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":"'__repr__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__rmod__'"}]},{"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":"'__rmul__'"}]},{"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":"'__setattr__'"}]},{"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":"'__sizeof__'"}]},{"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":"'__str__'"}]},{"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":"'__subclasshook__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'capitalize'"}]},{"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":"'casefold'"}]},{"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":"'center'"}]},{"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":"'count'"}]},{"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":"'encode'"}]},{"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":"'endswith'"}]},{"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":"'expandtabs'"}]},{"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":"'find'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'format'"}]},{"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":"'format_map'"}]},{"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":"'index'"}]},{"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":"'isalnum'"}]},{"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":"'isalpha'"}]},{"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":"'isascii'"}]},{"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":"'isdecimal'"}]},{"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":"'isdigit'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'isidentifier'"}]},{"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":"'islower'"}]},{"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":"'isnumeric'"}]},{"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":"'isprintable'"}]},{"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":"'isspace'"}]},{"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":"'istitle'"}]},{"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":"'isupper'"}]},{"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":"'join'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'ljust'"}]},{"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":"'lstrip'"}]},{"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":"'maketrans'"}]},{"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":"'partition'"}]},{"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":"'replace'"}]},{"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":"'rfind'"}]},{"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":"'rindex'"}]},{"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":"'rjust'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'rpartition'"}]},{"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":"'rsplit'"}]},{"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":"'rstrip'"}]},{"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":"'split'"}]},{"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":"'splitlines'"}]},{"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":"'startswith'"}]},{"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":"'strip'"}]},{"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":"'swapcase'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'title'"}]},{"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":"'translate'"}]},{"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":"'zfill'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python strings have a lot of methods available, as you can see above. At this point it might be best to ignore any methods that have underscores in their names, but the rest may well prove useful. Some of them you can figure out by trying them out, and the rest you can search for online."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Python lists don't have quite so many methods:"}]},{"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","operator"]},"children":[{"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":"dir"}]},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__add__'"}]},{"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":"'__class__'"}]},{"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":"'__contains__'"}]},{"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":"'__delattr__'"}]},{"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":"'__delitem__'"}]},{"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":"'__dir__'"}]},{"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":"'__doc__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__eq__'"}]},{"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":"'__format__'"}]},{"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":"'__ge__'"}]},{"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":"'__getattribute__'"}]},{"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":"'__getitem__'"}]},{"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":"'__gt__'"}]},{"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":"'__hash__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__iadd__'"}]},{"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":"'__imul__'"}]},{"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":"'__init__'"}]},{"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":"'__init_subclass__'"}]},{"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":"'__iter__'"}]},{"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":"'__le__'"}]},{"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":"'__len__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__lt__'"}]},{"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":"'__mul__'"}]},{"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":"'__ne__'"}]},{"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":"'__new__'"}]},{"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":"'__reduce__'"}]},{"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":"'__reduce_ex__'"}]},{"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":"'__repr__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__reversed__'"}]},{"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":"'__rmul__'"}]},{"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":"'__setattr__'"}]},{"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":"'__setitem__'"}]},{"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":"'__sizeof__'"}]},{"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":"'__str__'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'__subclasshook__'"}]},{"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":"'append'"}]},{"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":"'clear'"}]},{"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":"'copy'"}]},{"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":"'count'"}]},{"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":"'extend'"}]},{"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":"'index'"}]},{"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":"'insert'"}]},{"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":"'pop'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'remove'"}]},{"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":"'reverse'"}]},{"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":"'sort'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's try a few of them, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reverse"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"clear"}]},{"type":"text","value":" seem promising:"}]},{"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","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" 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","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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"reverse"},{"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","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" numbers\n"},{"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":" "},{"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":"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":" "},{"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":" "},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"clear"},{"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","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" numbers\n"},{"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":"As you can see, these methods do pretty much what you would expect based on their names."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice how the interpreter doesn't print out anything when you run the command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"numbers.reverse()"}]},{"type":"text","value":". This is because the interpreter prints something out only if the line of code has a value. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reverse()"}]},{"type":"text","value":" method here doesn't return any value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the above example we printed out the value of the list "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"numbers"}]},{"type":"text","value":" by typing in just the name of the variable. In fact, it is rarely necessary to explicitly type in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" commands in the interpreter. You can include them if you want to, however."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Remember to close the interpreter when you are finished. The commands "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"quit()"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exit()"}]},{"type":"text","value":" will close it, as will the key combo "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Control"}]},{"type":"text","value":"+"},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"D"}]},{"type":"text","value":" (Linux/Mac) or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Control"}]},{"type":"text","value":"+"},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Z"}]},{"type":"text","value":" (Windows). Especially in Visual Studio Code this is important, as trying to execute another Python program while the interpreter is still running results in a rather cryptic error message:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/030410f1be5c4a9d02209afb96fe30e4/966ce/4_1_4.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: 28.695652173913043%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA7klEQVQY023G0W6EIBCFYd9jBQZQgWEERdx1FTdbTd//lZqYthdNT76T/FXOkyMa0phzTikhekRy1nXGInrvyVqrlP4DVBNJVwbtOA8u9tA2QggAkFJKgO+4kv+MMc6uc8YAZKV0E4YQxwRaMyFqzm91fWOXK2rGfoGSoJSQwEEwwatnTFtM52MtcVrD+KA4O8rWz67PjiaDFz9Znwy+0v2dl1ea15BWjNVxnp/HeX4ceynPZdme615KWbfXvpdtC9QTInnfe+o92c5YYwjRIS4uVHcfHxgWGobGRt1FbaI2wyXqLsj2H6oj1S7KfgH1/DpLnHkC8gAAAABJRU5ErkJggg=='); 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":["/static/030410f1be5c4a9d02209afb96fe30e4/a0b58/4_1_4.webp 230w","/static/030410f1be5c4a9d02209afb96fe30e4/bc10c/4_1_4.webp 460w","/static/030410f1be5c4a9d02209afb96fe30e4/966d8/4_1_4.webp 920w","/static/030410f1be5c4a9d02209afb96fe30e4/445df/4_1_4.webp 1380w","/static/030410f1be5c4a9d02209afb96fe30e4/78de1/4_1_4.webp 1840w","/static/030410f1be5c4a9d02209afb96fe30e4/df82e/4_1_4.webp 2050w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/030410f1be5c4a9d02209afb96fe30e4/81c8e/4_1_4.png 230w","/static/030410f1be5c4a9d02209afb96fe30e4/08a84/4_1_4.png 460w","/static/030410f1be5c4a9d02209afb96fe30e4/c0255/4_1_4.png 920w","/static/030410f1be5c4a9d02209afb96fe30e4/b1001/4_1_4.png 1380w","/static/030410f1be5c4a9d02209afb96fe30e4/161ec/4_1_4.png 1840w","/static/030410f1be5c4a9d02209afb96fe30e4/966ce/4_1_4.png 2050w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/030410f1be5c4a9d02209afb96fe30e4/c0255/4_1_4.png","alt":"4 1 4","title":"4 1 4","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":"h2","properties":{"id":"the-built-in-debugger","style":"position:relative;"},"children":[{"type":"text","value":"The built-in debugger"},{"type":"element","tagName":"a","properties":{"href":"#the-built-in-debugger","ariaLabel":"the built in debugger 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 have already spent a fair amount of time and effort on developing debugging skills, mainly through print statement debugging. The Visual Studio Code editor has another tool at your disposal: a built-in "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"visual debugger"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To start debugging, first you have to define a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"breakpoint"}]},{"type":"text","value":" in your code. A breakpoint is a point in your program where the debugger halts execution. You can set a breakpoint by clicking on the left edge of any line in your program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following example is a slightly faulty attempt at solving the exercise "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"The sum of consecutive numbers"}]},{"type":"text","value":" in the "},{"type":"element","tagName":"a","properties":{"href":"/part-3/1-loops-with-conditions"},"children":[{"type":"text","value":"previous part"}]},{"type":"text","value":". There is a breakpoint on line 5:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 635px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/48f977029694dbc4538579f601adfc5e/1ddef/4_1_5.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: 49.565217391304344%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAAsTAAALEwEAmpwYAAABb0lEQVQoz4WSSY7bMBBFdQ9L4lAUZ1G23EaQ9CaHyP2v8gLRcQ9oN7L4YKFQ/Hg1DN57xCcu553r+cLWGue2kWNCxOHcglsCy7KwLP5NPf9EQ06lF5ScqbXR1o1cK62t1JLJMZLLyloKOWVCCMQQsdYyz/MXDQeF0RqtNKmeWdsF4x0xB1LyBCcs1mCNRc2KeZ76x2manmo4MNVhqDW57tTtRkwFoxXjaeR0OjGOYy9+RvSFMIgja411juYbL+dX9h+/2W+/KLWxXX9irfRipdR/1Qm1Un0m0zSS6oWyvVDWS89pfde9pe9b/dTy0a6IMKuZ5CttuxJi6VsLsaL1++y+02Mkg1iDMaafxThOqEmR6k5uN3LbSWXDGt0Nx/H0psPgY/wwHSTuGCOdMAQhpoC3Bm8VSSxRNEEsXiwuFHyqhNQQWVh8IeTW7/WdcP+D0q4T2H+0xwkdVGLur9W6b10bwdhDrtcYc4+PZTwM/wL3pRm/0skzmwAAAABJRU5ErkJggg=='); 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":["/static/48f977029694dbc4538579f601adfc5e/a0b58/4_1_5.webp 230w","/static/48f977029694dbc4538579f601adfc5e/bc10c/4_1_5.webp 460w","/static/48f977029694dbc4538579f601adfc5e/a4521/4_1_5.webp 635w"],"sizes":"(max-width: 635px) 100vw, 635px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/48f977029694dbc4538579f601adfc5e/81c8e/4_1_5.png 230w","/static/48f977029694dbc4538579f601adfc5e/08a84/4_1_5.png 460w","/static/48f977029694dbc4538579f601adfc5e/1ddef/4_1_5.png 635w"],"sizes":"(max-width: 635px) 100vw, 635px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/48f977029694dbc4538579f601adfc5e/1ddef/4_1_5.png","alt":"4 1 5","title":"4 1 5","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":"After the breakpoint has been set, choose "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Start debugging"}]},{"type":"text","value":" from the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Run"}]},{"type":"text","value":" menu. This opens a list of options, where you should select "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Python File"}]},{"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: 689px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/c4e9f7d2b99f512e3de82b450788ac99/0f79a/4_1_6.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: 50.43478260869565%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAAsTAAALEwEAmpwYAAABr0lEQVQoz22RWW7bMBBAdY9YC0mRlKzdkmwtCdDWiQP0JwtatEV7/2u8gpRRIEk/HmZGnHkYUkGW5czTxLIsG/Psa8f9+czl4cHzeLn4uC7L1j9P//rc3D7fo5QiaNqR+fyLu8c/rJffTF9+0t2+crh9pVtfPPX0RDU9+dgsL+94pl2/IdKSMNwRFGXL9Ok78+cf9HevtMsz3fpMf5W6mPVfkdX9RnmPeMMZWT0QyZwwvCGwxnIaetb5xHwcGA4Nh6aiq0u6pqKtC6xWaJWglbiSvEOQxBFRFBFUVc1xPDGOR7r2QN+PdF3v89bVh4E8L0hTg9aWVJsPuO9JkmxCrQ3LvHI6nrxk6AcvGYfxmvdkNiNNNdoP/584TgjDkMD95bpq6NqOsiipq5qyrCj2BXXdevyG2qBUilQKKSVSqje47bzQ2ozM5hhjfaPWGqVcTLHaYI0hMxbtRElMkgiEdGKNEIokkddcEEUhQdO0fjN3JSEkcRyj3MObFCUEWiRYJTAOKVBSkdo9qc5JU4tSGm0LL/dCdw2HewcndGs7drvdFt+xnd34861n5+ttLuIv6IsuDPMuDa0AAAAASUVORK5CYII='); 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":["/static/c4e9f7d2b99f512e3de82b450788ac99/a0b58/4_1_6.webp 230w","/static/c4e9f7d2b99f512e3de82b450788ac99/bc10c/4_1_6.webp 460w","/static/c4e9f7d2b99f512e3de82b450788ac99/2e76f/4_1_6.webp 689w"],"sizes":"(max-width: 689px) 100vw, 689px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/c4e9f7d2b99f512e3de82b450788ac99/81c8e/4_1_6.png 230w","/static/c4e9f7d2b99f512e3de82b450788ac99/08a84/4_1_6.png 460w","/static/c4e9f7d2b99f512e3de82b450788ac99/0f79a/4_1_6.png 689w"],"sizes":"(max-width: 689px) 100vw, 689px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/c4e9f7d2b99f512e3de82b450788ac99/0f79a/4_1_6.png","alt":"4 1 6","title":"4 1 6","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":"This starts the debugger, which executes your code as usual until the execution reaches a breakpoint and halts. If your code asks for input, remember to type that in in the terminal:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/2e80abc894afe89ee8b2a3444be1da55/58fee/4_1_7.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: 46.95652173913043%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAABjUlEQVQoz2WS227jIBRF/R2T2ObmgG0w4PgSt1E10vz/N60RbqfKqA9LcAQ6nM3e1bpuLMvKum2klAhhQpsO1w9MfsKHQJ5nYsqkGHHW0XU3pJQvKAancLajut9nwhjw1mONxZgOrTVtKxh8prMOpQSyaWiahrou1P9xvV6pG0ErJNXx/sFgPXlM3G72fL0zGqME2x55/llZ33rSpgn5FyFffjDNF2x/ob7WVG/P38SQiWM8pysNtTYIIc51DDO29yjd0TRlousL9Tn1K9W6LhhjUFp/yTUopVFKoVX5l4FhCPTWooRAKoOUGqk0QqhPuS/yq9OU+4IfA9a60xTnHFIKtFEoKXG3DlPq0kRpmlaeTYuact62LaIVtE1LlXNxLxOnxDh6cprp+wEVA+aYcVvGPzdCXMn3D0LcCfEgpnfm+5OUD2I+yPOTKR1UKeVvma+SU75zPA4e+866ruz7znEcJ2W/7xslIcuynPXj8TjvVSWD3gf86D8d/mKapjM+5aOLpELzHZ2fZvwz5S9qfAfjXY2XHQAAAABJRU5ErkJggg=='); 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":["/static/2e80abc894afe89ee8b2a3444be1da55/a0b58/4_1_7.webp 230w","/static/2e80abc894afe89ee8b2a3444be1da55/bc10c/4_1_7.webp 460w","/static/2e80abc894afe89ee8b2a3444be1da55/966d8/4_1_7.webp 920w","/static/2e80abc894afe89ee8b2a3444be1da55/42749/4_1_7.webp 1051w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/2e80abc894afe89ee8b2a3444be1da55/81c8e/4_1_7.png 230w","/static/2e80abc894afe89ee8b2a3444be1da55/08a84/4_1_7.png 460w","/static/2e80abc894afe89ee8b2a3444be1da55/c0255/4_1_7.png 920w","/static/2e80abc894afe89ee8b2a3444be1da55/58fee/4_1_7.png 1051w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/2e80abc894afe89ee8b2a3444be1da55/c0255/4_1_7.png","alt":"4 1 7","title":"4 1 7","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":"On the left there is now the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Variables"}]},{"type":"text","value":" view, which contains the current values of all variables active in the code. You can proceed with the execution line by line by clicking on the down arrow, which is labelled "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Step into"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the image below the loop in the code has already been repeated a few times:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/d7c8044e94fc076c65e443ef49b465e8/e996b/4_1_8.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: 46.52173913043478%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAABk0lEQVQoz12SWW6kMBBAOcc0i1cwYMBmp9Nq5f6XeiMzSqLMx5NLLrtUr+xs23bWdWPbNkKIDH5Aa0Pbdox+YBgH4jwzhUiYJlzjqG2NlPIbpRSdUzS1JVuWmaEf6BtPo5u7mFKaqhK0fUTXDikEVVFS5CV54lH84vGnoCgElZBk5/NNV3tiH2iaBmsbaq0xUrCuE+d7J24tPhhc/6Dt81+4tPocY3OKoiB7fnwyDZGxH2+VhDEGJQWuM8Q94kOH6zWmThcfmPoH2+Q3SqcuS7JtW/9ppq6M/VZOc1FSUduOth1pbIMoBUJYhNAIYSgrdevmjzSKgqIsUsGddVnxfsDaGt97nHNIKdAmFZW0tcUogdEaec83PYS5z6d8VVWINOeyIpvnmWO/bpZ553m9iNOMDAPmirgtMLx2/LgSlzfDdODHkyl8MC8vQjiZ4kWcX4zhIgsh3LpJ80dZEePKdV6cx8G2rhzHwXVdNyne951lWVi/c+f99bK0kXT73t8KX4zjiNKKsixvpUSKE+k1v+L/+QuElghBxo9GuQAAAABJRU5ErkJggg=='); 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":["/static/d7c8044e94fc076c65e443ef49b465e8/a0b58/4_1_8.webp 230w","/static/d7c8044e94fc076c65e443ef49b465e8/bc10c/4_1_8.webp 460w","/static/d7c8044e94fc076c65e443ef49b465e8/966d8/4_1_8.webp 920w","/static/d7c8044e94fc076c65e443ef49b465e8/c139f/4_1_8.webp 1050w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/d7c8044e94fc076c65e443ef49b465e8/81c8e/4_1_8.png 230w","/static/d7c8044e94fc076c65e443ef49b465e8/08a84/4_1_8.png 460w","/static/d7c8044e94fc076c65e443ef49b465e8/c0255/4_1_8.png 920w","/static/d7c8044e94fc076c65e443ef49b465e8/e996b/4_1_8.png 1050w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/d7c8044e94fc076c65e443ef49b465e8/c0255/4_1_8.png","alt":"4 1 8","title":"4 1 8","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 debugger has a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Debug console"}]},{"type":"text","value":" tab, which lets you evaluate expressions with the current values stored in the variables. For example, you can check the value of the Boolean expression in the condition of the loop:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/eeca0983be1396e4128646efa2b0a71d/58fee/4_1_9.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: 41.73913043478261%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAAAsTAAALEwEAmpwYAAABWklEQVQoz22R7W6jMBREeY5NAONvMDaGxLRpo2qlff9nOpVpWrW7++NoLF3Jd2ZuU8rO9Voo+07OmRgTShv8OJHmxBwj67ax5JW8LHjnMcYyDMM3JJOXeGdoLpeNGCKzm3HKoZRGKUXfC6Z5xTiPlIKh6+i6jrattD84n8+0naAXA83t9Y3gImvIOOew1mG0QkvB/rRw/1MoryN5V8T1F3E9/UPaTrjxRHtuaV7uv1niyhIS1lic/XAphDg0xA03BqQyD4fnH+66h/NPmlKuKK2RSh3d1U+kVEgpUXLA2ZEQEt5aRN8hZK1j+KLG/aQuaI6jXArzHPF+JMV0aC1b1+hKYZTEG02YJoz1pJRZ8kZKC957xnHE1oVC0Gzbxl5uPJUbe3nm/vJGzhujD4QpHE67vj/UGoPoe4zRx9vojwNW6rzve5p1XdGPqPp75GHAKEX39zUf+j9q5HfXoOa1sn/qYQAAAABJRU5ErkJggg=='); 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":["/static/eeca0983be1396e4128646efa2b0a71d/a0b58/4_1_9.webp 230w","/static/eeca0983be1396e4128646efa2b0a71d/bc10c/4_1_9.webp 460w","/static/eeca0983be1396e4128646efa2b0a71d/966d8/4_1_9.webp 920w","/static/eeca0983be1396e4128646efa2b0a71d/42749/4_1_9.webp 1051w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/eeca0983be1396e4128646efa2b0a71d/81c8e/4_1_9.png 230w","/static/eeca0983be1396e4128646efa2b0a71d/08a84/4_1_9.png 460w","/static/eeca0983be1396e4128646efa2b0a71d/c0255/4_1_9.png 920w","/static/eeca0983be1396e4128646efa2b0a71d/58fee/4_1_9.png 1051w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/eeca0983be1396e4128646efa2b0a71d/c0255/4_1_9.png","alt":"4 1 9","title":"4 1 9","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":"You can include several breakpoints in your program code. When execution has halted, you can start it again by clicking on the blue triangle. Execution proceeds until it reaches the next breakpoint."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The built-in visual debugger is a good alternative to debugging print statements. It is up to you which you choose to employ more in the future. Each programmer has their own preferences, but it's always a good idea to try different options before settling on any single solution."}]}]},"html":"

    After this section

      \n
    • You will be equipped to use the Visual Studio Code editor to complete exercises on this course
    • \n
    • You will be familiar with the interactive Python interpreter, and will be able to use it to run code
    • \n

    Thus far all the exercises on this course have been completed directly on the course pages in embedded editor windows. Programming in the browser is very suitable for the very first steps in programming, but now it is time to start using a separate editor especially made for programming.

    There are dozens of different editors that are suited to programming. On this course we will use the Visual Studio Code editor, which has been gaining traction in recent years.

    Please install the Visual Studio Code editor on your own computer now. You may also need to install Python and the Visual Studio Code plugin for Python. You will also need the TMC plugin, which will take care of running the tests that go with the exercises. In the TMC plugin, select MOOC as the organization and Python Programming 2024 as the course.

    Here is a guide to installing and running all of these. Read the instructions on working on and submitting exercises, and then complete the task below:

    Please write a program which asks the user which editor they are using. The program should keep on asking until the user types in Visual Studio Code.

    Have a look at the example of expected behaviour below:

    Editor: Emacs\nnot good\nEditor: Vim\nnot good\nEditor: Word\nawful\nEditor: Atom\nnot good\nEditor: Visual Studio Code\nan excellent choice!

    If the user types in Word or Notepad, the program counters with awful. Other unacceptable editor choices receive the reply not good.

    The program should be case-insensitive in its reactions. That is, the same user input in lowercase, uppercase or mixed case should trigger the same reaction:

    Editor: NOTEPAD\nawful\nEditor: viSUal STudiO cODe\nan excellent choice!

    Hint: The simplest way to achieve this is converting all characters to the same case. The Python string method lower converts a string to lowercase entirely. An example of its use:

    mystring = \"Visual Studio CODE\"\nif \"visual studio code\" == mystring.lower():\n    print(\"this was the string I was looking for!\")

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block. The same applies to any upcoming exercises that don't explicitly ask for functions.

    Executing code

    In Visual Studio Code, the easiest way to execute your code is clicking on the triangle in the upper right hand corner. Sometimes it happens that some program is left running in the background, maybe waiting for input from the user or stuck in an infinite loop, without you realising it. You might only notice this when trying to execute the next program, which simply will not run because the previous execution is still hogging up resources. A quick fix is pressing on the keys Control+C at the same time, which stops the execution of any ongoing process. The next program should now run fine.

    The interactive Python interpreter

    One of the most important tools for any Python programmer is the interactive Python interpreter.

    Starting up the interpreter may depend on your platform. In a Linux or Mac system, you can type in python3 in the terminal. In Windows, the command on the command line might be python. Firing up the interpreter on a Mac looks like this:

    \n \n \n \n \n \n \"4\n \n \n

    You can also try a browser-based Python interpreter, such as https://www.python.org/shell/.

    The interpreter is a way of executing Python code line by line as soon as you write it. When you write a line of code and press Enter, the interpreter executes the code right away and shows you the results:

    \n \n \n \n \n \n \"4\n \n \n

    Any Python code that can be written in a file can also be written in the interpreter. You can even assign variables and define methods:

    >>> t = [1,2,3,4,5]\n>>> for number in t:\n...   print(number)\n...\n1\n2\n3\n4\n5\n>>> def absolute_value(number):\n...   if number<0:\n...      number = -number\n...   return number\n...\n>>> x = 10\n>>> y = -7\n>>> absolute_value(number)\nTraceback (most recent call last):\n  File \"<stdin>\", line 1, in <module>\nNameError: name 'number' is not defined\n>>> absolute_value(x)\n10\n>>> absolute_value(y)\n7\n>>>

    The interpreter is best suited to making small checks. For instance, you can try out functions and methods, or check if they exist at all:

    >>> \"TextYteXt\".toupper()\nTraceback (most recent call last):\n  File \"<stdin>\", line 1, in <module>\nAttributeError: 'str' object has no attribute 'toupper'\n>>> \"TextYteXt\".upper()\n'TEXTYTEXT'\n>>>

    If there is a method you need, and you can almost remember its name, it's sometimes faster to skip Google and use the dir function in the interpreter. It tells you which methods are available to use on a given object:

    >>> dir(\"this is a string\")\n['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__',\n'__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__',\n'__hash__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__', '__lt__',\n'__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',\n'__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__',\n'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find',\n'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isascii', 'isdecimal', 'isdigit',\n'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join',\n'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust','rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase',\n'title', 'translate', 'upper', 'zfill']

    Python strings have a lot of methods available, as you can see above. At this point it might be best to ignore any methods that have underscores in their names, but the rest may well prove useful. Some of them you can figure out by trying them out, and the rest you can search for online.

    Python lists don't have quite so many methods:

    >>> dir([])\n['__add__', '__class__', '__contains__', '__delattr__', '__delitem__', '__dir__', '__doc__',\n'__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__gt__', '__hash__',\n'__iadd__', '__imul__', '__init__', '__init_subclass__', '__iter__', '__le__', '__len__',\n'__lt__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',\n'__reversed__', '__rmul__', '__setattr__', '__setitem__', '__sizeof__', '__str__',\n'__subclasshook__', 'append', 'clear', 'copy', 'count', 'extend', 'index', 'insert', 'pop',\n'remove', 'reverse', 'sort']\n>>>

    Let's try a few of them, reverse and clear seem promising:

    >>> numbers = [1,2,3,4,5]\n>>> numbers.reverse()\n>>> numbers\n[5, 4, 3, 2, 1]\n>>> numbers.clear()\n>>> numbers\n[]

    As you can see, these methods do pretty much what you would expect based on their names.

    Notice how the interpreter doesn't print out anything when you run the command numbers.reverse(). This is because the interpreter prints something out only if the line of code has a value. The reverse() method here doesn't return any value.

    In the above example we printed out the value of the list numbers by typing in just the name of the variable. In fact, it is rarely necessary to explicitly type in print commands in the interpreter. You can include them if you want to, however.

    Remember to close the interpreter when you are finished. The commands quit() or exit() will close it, as will the key combo Control+D (Linux/Mac) or Control+Z (Windows). Especially in Visual Studio Code this is important, as trying to execute another Python program while the interpreter is still running results in a rather cryptic error message:

    \n \n \n \n \n \n \"4\n \n \n

    The built-in debugger

    We have already spent a fair amount of time and effort on developing debugging skills, mainly through print statement debugging. The Visual Studio Code editor has another tool at your disposal: a built-in visual debugger.

    To start debugging, first you have to define a breakpoint in your code. A breakpoint is a point in your program where the debugger halts execution. You can set a breakpoint by clicking on the left edge of any line in your program.

    The following example is a slightly faulty attempt at solving the exercise The sum of consecutive numbers in the previous part. There is a breakpoint on line 5:

    \n \n \n \n \n \n \"4\n \n \n

    After the breakpoint has been set, choose Start debugging from the Run menu. This opens a list of options, where you should select Python File:

    \n \n \n \n \n \n \"4\n \n \n

    On the left there is now the Variables view, which contains the current values of all variables active in the code. You can proceed with the execution line by line by clicking on the down arrow, which is labelled Step into.

    In the image below the loop in the code has already been repeated a few times:

    \n \n \n \n \n \n \"4\n \n \n

    The debugger has a Debug console tab, which lets you evaluate expressions with the current values stored in the variables. For example, you can check the value of the Boolean expression in the condition of the loop:

    \n \n \n \n \n \n \"4\n \n \n

    You can include several breakpoints in your program code. When execution has halted, you can start it again by clicking on the blue triangle. Execution proceeds until it reaches the next breakpoint.

    The built-in visual debugger is a good alternative to debugging print statements. It is up to you which you choose to employ more in the future. Each programmer has their own preferences, but it's always a good idea to try different options before settling on any single solution.

    ","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-4/1-vscode.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-4/2-more-functions/page-data.json b/page-data/part-4/2-more-functions/page-data.json index acd617a98..69e716a06 100644 --- a/page-data/part-4/2-more-functions/page-data.json +++ b/page-data/part-4/2-more-functions/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-4/2-more-functions", - "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 know more about the arguments and parameters of functions"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to return values from functions, and how to use the values in your code"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to add type hints for parameters and return values"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's quickly recap Python functions. Functions are defined with the keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":":"}]},{"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":"message"}]},{"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 "},{"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":"\"This message was brought to you by a function\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This function can be called in your program code 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":"text","value":"message"},{"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":"Your program would then print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This message was brought to you by a function"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-parameters-and-arguments-of-a-function","style":"position:relative;"},"children":[{"type":"text","value":"The parameters and arguments of a function"},{"type":"element","tagName":"a","properties":{"href":"#the-parameters-and-arguments-of-a-function","ariaLabel":"the parameters and arguments of a function 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":"A function can take one or more arguments. When the function is called, the arguments are assigned to variables, which are defined in the function definition. These variables are called parameters, and they are listed inside the parentheses after the function name."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following code the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"greet"}]},{"type":"text","value":" has one parameter defined, while the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" has two."}]},{"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":"greet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"\"Hello there,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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 sum of the arguments is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"greet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"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":"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":" "},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hello there, Emily\nThe sum of the arguments is 5"}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Formal vs actual, parameter vs argument"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The terminology around data passed to functions can feel confusing. To make matters worse, some sources refer to what we have called parameters and arguments as "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"formal"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"actual parameters"}]},{"type":"text","value":". Other sources call them "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"formal"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"actual arguments"}]},{"type":"text","value":". "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/glossary.html#term-argument","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"The Python documentation"}]},{"type":"text","value":" specifies only the terms "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"argument"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"parameter"}]},{"type":"text","value":", so that is what we will use as well."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What actually happens when the function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"greet(\"Emily\")"}]},{"type":"text","value":" is executed?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the function definition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"greet(name)"}]},{"type":"text","value":" the parameter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" behaves for all intents and purposes just like a normal variable. We can use it within the function just like we have used variables in the many main functions in our programs thus far."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"greet(\"Emily\")"}]},{"type":"text","value":" the argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Emily\""}]},{"type":"text","value":" is just like any other string we have come across before. For example, we can assign it to a variable."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, when the function call is executed, the value of the argument, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Emily\""}]},{"type":"text","value":", is assigned to the parameter variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":". For the duration of "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"this execution"}]},{"type":"text","value":" of the function, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name = \"Emily\""}]},{"type":"text","value":". When the function is called with a different argument, the value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" will be different."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This terminology may all seem a bit superfluous, but computer science as a discipline does aim to be as exact a science as possible. Using well defined terminology helps."}]}]},{"type":"element","tagName":"h2","properties":{"id":"error-messages-when-running-tests","style":"position:relative;"},"children":[{"type":"text","value":"Error messages when running tests"},{"type":"element","tagName":"a","properties":{"href":"#error-messages-when-running-tests","ariaLabel":"error messages when running tests 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":"Most exercises on this course have automatic tests attached to them. If your program doesn't work as specified in the task, the tests will show an error message, which may or may not be helpful. It is usually worth the trouble to read the error message carefully."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In some situations the error message might not tell you very much at all. In the next exercise below you may come across this error message:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 728px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/b192944b4585f67cefef981a78a1333b/cecac/4_2_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: 49.565217391304344%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB20lEQVQoz42S227TQBRF8wf0BrT2XGNPHF8SO86tSW8JBJAIFEIFbVQJ8QgCxBcjIfWBz1hgl0qFF/qwNPscbR3NzD6NLE3pdrrE7RgXOqSQKGUQQiFlhSQIwjvT8H0fbTSLJ3Pmj46Zz485OprW9Wx2wmKxQGtdD1ZK/ZdGZTTa8nJ5zunygqeLFc8WK96efeDV6Yrl8nltrHx3oSHkLk4fsOp/593kivPpFRcHP3kz+IHxDniwu4kxFm0MRmuMNv+g/76hkIKw2eKwfMFs9Jp+/JjUHpIFR7R0hNNNAmkJlEUGFhFcnzdahU2stVRfJ4SgUYVgQos7jGmfZNiJw4xD7Nihhi30KEKNIuwgYt9ETHXE1ERM9DX7OqIVOpxzBEFAQ1WptjT31xnb65SddcLDyw7mfb/WO+uYrcsE7yzm43bC13sJnzdivmwkfNvK+LSV0InajCcTijyn4SlF6fnMNncZSccwLRm0c3qtjHHar3XFMO2RDEuiQUE87NEeFPjtkG3poYVgb8+rn10PHHoeIxeQFzl5L6foFWSdjKIs6l5Vl/2SPM0ouzm9Tpckigi0wUpJUyqUvglFKVzVFALp+SjP+8Nt7SFv6Qrt+xgh6oG6Wpkq5d9r8wujHCixT9lVsAAAAABJRU5ErkJggg=='); 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":["/static/b192944b4585f67cefef981a78a1333b/a0b58/4_2_1.webp 230w","/static/b192944b4585f67cefef981a78a1333b/bc10c/4_2_1.webp 460w","/static/b192944b4585f67cefef981a78a1333b/8cb3e/4_2_1.webp 728w"],"sizes":"(max-width: 728px) 100vw, 728px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/b192944b4585f67cefef981a78a1333b/81c8e/4_2_1.png 230w","/static/b192944b4585f67cefef981a78a1333b/08a84/4_2_1.png 460w","/static/b192944b4585f67cefef981a78a1333b/cecac/4_2_1.png 728w"],"sizes":"(max-width: 728px) 100vw, 728px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/b192944b4585f67cefef981a78a1333b/cecac/4_2_1.png","alt":"4 2 1","title":"4 2 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":"p","properties":{},"children":[{"type":"text","value":"The message just states that you should be able to call the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" with the specified arguments:"}]},{"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":"line"},{"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":" "},{"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":"p","properties":{},"children":[{"type":"text","value":"The actual issue becomes clear when we try executing the function call specified in the error message. You can do this by copying the function call into your program and clicking on the triangle:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/3fa13c46cf1e3bdd9fe23a5e8bc7778b/e4374/4_2_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: 43.47826086956522%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAABt0lEQVQoz22Ry27TUBRF/QudJLF9X74Pv+LrOE5cQYUAFYakqaCllA75AH6BP19VTEAIGCwt7b2lMznJNF2yG0e225G27WibjsoHbGFx1mOt+y/eOnIfeK80b5xnty2YhpYkBE+oAr6pqJoSEQpSbxBWo51GW405+5SN1xROk0mJE4IfFxdcL5esfInWhkQbgw+BuInkvuB7tuIpS6mVxBqNMhr9F3OnFE+LBZ0QLLTGKIlSiqRQGiPVTFE4rvOcY7rk62pJladkIkf+gRAZQuSYM6kUaCkRQiClJHFTj9l3FLGmeTni+xHbvcB1e8p6pCwHQrmZKauBqt7NLquRUA40VUfwjs1moG3WJMOrK9aXe9bTjmYaibs9cZwIbUcVB8K6/80p/+p8G/FNh2/XmBAYNlv6OJDces/ntuWuaXiMkU91zdE7DraY/aEouHGWg7Xn3nLjHAdn5+1QGN5pTZpn5HlOMvY9b/cTV9str3d7LmNkqEr606O8o3P2bEf0/uyf+bTV1vIxy7BKIU9PuY81X7qKh+B47GoeXeDBVNwrx3El/yVVHFeC21TP+U4ovi0WKCnng8/cdwh54pL27wAAAABJRU5ErkJggg=='); 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":["/static/3fa13c46cf1e3bdd9fe23a5e8bc7778b/a0b58/4_2_2.webp 230w","/static/3fa13c46cf1e3bdd9fe23a5e8bc7778b/bc10c/4_2_2.webp 460w","/static/3fa13c46cf1e3bdd9fe23a5e8bc7778b/966d8/4_2_2.webp 920w","/static/3fa13c46cf1e3bdd9fe23a5e8bc7778b/4c2b6/4_2_2.webp 927w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/3fa13c46cf1e3bdd9fe23a5e8bc7778b/81c8e/4_2_2.png 230w","/static/3fa13c46cf1e3bdd9fe23a5e8bc7778b/08a84/4_2_2.png 460w","/static/3fa13c46cf1e3bdd9fe23a5e8bc7778b/c0255/4_2_2.png 920w","/static/3fa13c46cf1e3bdd9fe23a5e8bc7778b/e4374/4_2_2.png 927w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/3fa13c46cf1e3bdd9fe23a5e8bc7778b/c0255/4_2_2.png","alt":"4 2 2","title":"4 2 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 last lines produced by the execution, highlighted in the above image, tell us that line 4 of our code causes the error "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"IndexError: string index out of range"}]},{"type":"text","value":". In the "},{"type":"element","tagName":"a","properties":{"href":"/part-3/2-working-with-strings"},"children":[{"type":"text","value":"previous part"}]},{"type":"text","value":" there was a similar example, where we tried to use an index that fell outside the scope of the string. This time the error appears because we are trying to access the first character of an empty string, that is, a string of length 0."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Line","tmcname":"part04-02_line"},"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":"line"}]},{"type":"text","value":", which takes two arguments: an integer and a string. The function prints out a line of text, the length of which is specified by the first argument. The character used to draw the line should be the first character in the second argument. If the second argument is an empty string, the line should consist of stars."}]},{"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":"line"},{"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":"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":"\nline"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"\"LOL\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nline"},{"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":" "},{"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":"pre","properties":{},"children":[{"type":"text","value":"%%%%%%%\nLLLLLLLLLL\n***\n"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"function-calls-within-function-calls","style":"position:relative;"},"children":[{"type":"text","value":"Function calls within function calls"},{"type":"element","tagName":"a","properties":{"href":"#function-calls-within-function-calls","ariaLabel":"function calls within function calls 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":"You can call a function from within another function. In fact, we have done so many times before, when we called the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" function within our own functions in the previous part. Our own functions are functionally no different. In the following example the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"greet_many_times"}]},{"type":"text","value":" calls the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"greet"}]},{"type":"text","value":" as many times as specified by the argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"times"}]},{"type":"text","value":":"}]},{"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":"greet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"\"Hello there,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"greet_many_times"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" times"},{"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":"while"}]},{"type":"text","value":" times "},{"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 greet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n times "},{"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\ngreet_many_times"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"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":"3"}]},{"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":"Hello there, Emily\nHello there, Emily\nHello there, Emily"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"A box of hashes","tmcname":"part04-03_box_of_hashes"},"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":"box_of_hashes"}]},{"type":"text","value":", which prints out a rectangle of hash characters. The function takes one argument, which specifies the height of the rectangle. The rectangle should be ten characters wide."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should call the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" from the exercise above for the actual printing out. Copy your solution to that exercise above the code for this exercise. Please don't change anything in your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples 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":"box_of_hashes"},{"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"},{"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":"\nbox_of_hashes"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"##########\n##########\n##########\n##########\n##########\n\n##########\n##########\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"A square of hashes","tmcname":"part04-04_square_of_hashes"},"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":"square_of_hashes"}]},{"type":"text","value":", which draws a square of hash characters. The function takes one argument, which determines the length of the side of the square."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should call the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" from the exercise above for the actual printing out. Copy your solution to that exercise above the code for this exercise. Please don't change anything in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" function."}]},{"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":"text","value":"square_of_hashes"},{"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"},{"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":"\nsquare_of_hashes"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"#####\n#####\n#####\n#####\n#####\n\n###\n###\n###\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"A square","tmcname":"part04-05_square"},"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":"square"}]},{"type":"text","value":", which prints out a square of characters, and takes two arguments. The first parameter specifies the length of the side of the square. The second parameter specifies the character used to draw the square."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should call the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" from the exercise above for the actual printing out. Copy your solution to that exercise above the code for this exercise. Please don't change anything in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" function."}]},{"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":"text","value":"square"},{"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":" "},{"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"},{"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":"\nsquare"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"o\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"*****\n*****\n*****\n*****\n*****\n\nooo\nooo\nooo\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"A triangle","tmcname":"part04-06_triangle"},"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":"triangle"}]},{"type":"text","value":", which draws a triangle of hashes, and takes one argument. The triangle should be as tall and as wide as the value of the argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should call the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" from the exercise above for the actual printing out. Copy your solution to that exercise above the code for this exercise. Please don't change anything in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" function."}]},{"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":"text","value":"triangle"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntriangle"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"#\n##\n###\n####\n#####\n######\n\n#\n##\n###\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"A shape","tmcname":"part04-07_shape"},"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":"shape"}]},{"type":"text","value":", which takes four arguments. The first two parameters specify a triangle, as above, and the character used to draw it. The first parameter also specifies the width of a rectangle, while the third parameter specifies its height. The fourth parameter specifies the filler character of the rectangle. The function prints first the triangle, and then the rectangle below it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should call the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" from the exercise above for the actual printing out. Copy your solution to that exercise above the code for this exercise. Please don't change anything in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" function."}]},{"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":"text","value":"shape"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"X\""}]},{"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":"3"}]},{"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":"\"*\""}]},{"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":"\nshape"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"o\""}]},{"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":"4"}]},{"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":"\"+\""}]},{"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":"\nshape"},{"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":" "},{"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":" "},{"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","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":"pre","properties":{},"children":[{"type":"text","value":"X\nXX\nXXX\nXXXX\nXXXXX\n*****\n*****\n*****\n\no\noo\n++\n++\n++\n++\n\n.\n..\n...\n"}]}]},{"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":"Don't try and solve this exercise \"all at once\". A good first step would be to make sure you can print the triangle reliably. Then you can try adding the rectangle."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is one of the most important skills of a programmer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"concentrate on small, tangible sections of the problem at a time"}]},{"type":"text","value":". Solve and verify partial solutions, and use them to build towards a complete solution."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"A spruce","tmcname":"part04-08_spruce"},"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":"spruce"}]},{"type":"text","value":", which takes one argument. The function prints out the text "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a spruce!"}]},{"type":"text","value":", and the a spruce tree, the size of which is specified by the argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Calling "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"spruce(3)"}]},{"type":"text","value":" should print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"a spruce!\n *\n ***\n*****\n *\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Calling "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"spruce(5)"}]},{"type":"text","value":" should print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"a spruce!\n *\n ***\n *****\n *******\n*********\n *\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB: to the left of the spruce there should be exactly the right amount of whitespace"}]},{"type":"text","value":". If the shape of the spruce looks correct, but the left edge of the tree is not touching the left edge of the text area in the terminal, the tests will not accept the solution."}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-return-value-of-a-function","style":"position:relative;"},"children":[{"type":"text","value":"The return value of a function"},{"type":"element","tagName":"a","properties":{"href":"#the-return-value-of-a-function","ariaLabel":"the return value of a function 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":"Functions can also return values. For instance, the built-in Python function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"returns"}]},{"type":"text","value":" an input string typed in by the user. The value returned by a function can be stored in a 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":"word "},{"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 word: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When you want an integer value from the user, the input from the user has to be converted into an integer. The function we've used for this purpose is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":", and it also returns a value:"}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in an integer: \""}]},{"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":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":" takes the string returned by "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" as its argument, and returns an integer type value, if an integer can be parsed from the string."}]},{"type":"element","tagName":"h2","properties":{"id":"the-return-statement","style":"position:relative;"},"children":[{"type":"text","value":"The return statement"},{"type":"element","tagName":"a","properties":{"href":"#the-return-statement","ariaLabel":"the return statement 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 functions you define yourself can also return values. To do this you need the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" statement. For example, the following function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"my_sum"}]},{"type":"text","value":" returns the sum of its parameters:"}]},{"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":"my_sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_sum"},{"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":" "},{"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":"\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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sum:\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sum: 5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here's another example of a return value. This function asks for the user's name and returns the string the user types in:"}]},{"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":"ask_for_name"}]},{"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 name "},{"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":"\"What is your name? \""}]},{"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":" name\n\nname "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ask_for_name"},{"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":"\"Hello there,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"What is your name? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Anna"}]},{"type":"text","value":"\nHello there, Anna"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" statement ends the execution of the function immediately. The following is a nifty way to create a comparison 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"smallest"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"b"},{"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":"if"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" b"},{"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":" a\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" b\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":"smallest"},{"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":" "},{"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":"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":"smallest"},{"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":" "},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The idea here is that if "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" is smaller than "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":", the function returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" and exits immediately. If not, the execution continues to the next line, where the value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" is returned. A function can never execute two separate "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" statements with a single function call."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3\n2"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can make use of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" statement even if the function doesn't return a value. Its purpose then is to end the execution of the 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"greet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"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":"\"\""}]},{"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":"\"???\""}]},{"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":"\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":"\"Hello there,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ngreet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngreet"},{"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":"\ngreet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mark\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the argument (which gets stored in the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":") is an empty string, the function prints out "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"???"}]},{"type":"text","value":" and exits."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hello there, Emily\n???\nHello there, Mark"}]}]},{"type":"element","tagName":"h2","properties":{"id":"using-return-values-from-functions","style":"position:relative;"},"children":[{"type":"text","value":"Using return values from functions"},{"type":"element","tagName":"a","properties":{"href":"#using-return-values-from-functions","ariaLabel":"using return values from 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 already know that the return values of functions can be stored in 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":"my_sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_sum"},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The sum is\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The sum is 10"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The return value of a function is a value just like any other. It is not necessary to store it in a variable in order to give it as an argument to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command:"}]},{"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":"\"The sum is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" my_sum"},{"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":"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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The return value of a function can become the argument of another 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"my_sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":" a"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"b\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":"difference"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":" a"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"b\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" difference"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_sum"},{"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":" "},{"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":" my_sum"},{"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":" "},{"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","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 result is\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The result is 2"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this case the inner function calls "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"my_sum(5, 2)"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"my_sum(2, 3)"}]},{"type":"text","value":" are executed first. The values they return (7 and 5) are used as the arguments of the outer function call."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The outer function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"difference(7, 5)"}]},{"type":"text","value":" returns the value 2, which is stored in the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"result"}]},{"type":"text","value":" and printed out."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In summary, values returned by functions work exactly like any other value in Python. They can be printed out, stored in variables, used in expressions and used as arguments in other function calls."}]},{"type":"element","tagName":"h2","properties":{"id":"the-difference-between-return-and-print","style":"position:relative;"},"children":[{"type":"text","value":"The difference between return and print"},{"type":"element","tagName":"a","properties":{"href":"#the-difference-between-return-and-print","ariaLabel":"the difference between return and print 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":"Sometimes the difference between a function "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"returning"}]},{"type":"text","value":" a value and a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"print statement"}]},{"type":"text","value":" within a function can be confusing. Let's have a look at two different ways of implementing a function for working out which of two values is greater:"}]},{"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":"max1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":"if"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" b"},{"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":" a\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":" b\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":"max2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":"if"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" b"},{"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":"a"},{"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":"else"}]},{"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":"b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" max1"},{"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":" "},{"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"},{"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":"text","value":"\n\nmax2"},{"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":"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5\n7"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Both versions seem to be working just fine, as the maximum values are printed correctly. There is a fundamental difference between the two, however. The first of the two functions, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"max1"}]},{"type":"text","value":", does not print out anything on its own, but instead it "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"returns"}]},{"type":"text","value":" the value which is greater. If we execute the following line"}]},{"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":"max1"},{"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":" "},{"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":"p","properties":{},"children":[{"type":"text","value":"nothing seems to happen. The return value of the function has to be "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"used"}]},{"type":"text","value":" in some way in the code which called the function. For instance, it can be stored in a variable and printed out:"}]},{"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":"result "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" max1"},{"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":" "},{"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"},{"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":"p","properties":{},"children":[{"type":"text","value":"The second version, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"max2"}]},{"type":"text","value":", uses the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"within the function"}]},{"type":"text","value":" to print out the greater value. If we want to see the value, it is enough to call the 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":"text","value":"max2"},{"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":"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":"p","properties":{},"children":[{"type":"text","value":"and the greater value is printed out. The downside of this handy function is that the value worked out by the function is not available for use in the program which called it. That is why functions which return values are often the better option."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The greatest number","tmcname":"part04-09_greatest_number"},"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":"greatest_number"}]},{"type":"text","value":", which takes three arguments. The function returns the greatest in value of the three."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how the function is used:"}]},{"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":"text","value":"greatest_number"},{"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":" "},{"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":"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 4"}]},{"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":"greatest_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"99"}]},{"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":"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":"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 99"}]},{"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":"greatest_number"},{"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","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","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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 0"}]}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Same characters","tmcname":"part04-10_same_characters"},"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":"same_chars"}]},{"type":"text","value":", which takes one string and two integers as arguments. The integers refer to indexes within the string. The function should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" if the two characters at the indexes specified are the same. Otherwise, and especially if either of the indexes falls outside the scope of the string, the function returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of how the function is used:"}]},{"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":"# same characters m and m"}]},{"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":"same_chars"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"programmer\""}]},{"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":"6"}]},{"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":"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# True"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# different characters p and r"}]},{"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":"same_chars"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"programmer\""}]},{"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":"0"}]},{"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":"4"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# False"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the second index is not within the string"}]},{"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":"same_chars"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"programmer\""}]},{"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":"0"}]},{"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":"12"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# False"}]}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"First, second and last words","tmcname":"part04-11_first_second_last"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write three functions: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"first_word"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"second_word"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"last_word"}]},{"type":"text","value":". Each function takes a string argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As their names imply, the functions return either the first, the second or the last word in the sentence they receive as their string argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In each case you may assume the argument string contains at least two separate words, and all words are separated by exactly one space character. There will be no spaces in the beginning or at the end of the argument 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":"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":"\"it was a dark and stormy python\""}]},{"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":"first_word"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# it"}]},{"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":"second_word"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# was"}]},{"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":"last_word"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# python"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"it\nwas\npython"}]}]},{"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":"\"it was\""}]},{"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":"second_word"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# was"}]},{"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":"last_word"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# was"}]}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-type-of-the-argument","style":"position:relative;"},"children":[{"type":"text","value":"The type of the argument"},{"type":"element","tagName":"a","properties":{"href":"#the-type-of-the-argument","ariaLabel":"the type of the argument 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":"A quick recap of the data types we've come across so far:"}]},{"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"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"left"},"children":[{"type":"text","value":"Type"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Python data type"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Example"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"integer"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"23"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"floating point number"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-0.45"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"string"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Peter Python\""}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"Boolean value"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When you call a function, it will only work correctly if the arguments you give it are of the right type. Let's have a look at an example:"}]},{"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_many_times"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"message"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" times"},{"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":"while"}]},{"type":"text","value":" times "},{"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":"message"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n times "},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function works as expected if we call it 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":"text","value":"print_many_times"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hello there\""}]},{"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":"5"}]},{"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":"Hello there\nHello there\nHello there\nHello there\nHello there"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"However, if we give the function an argument of the wrong type, it will not 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":"print_many_times"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hello 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":"\"Emily\""}]},{"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":"TypeError: '>' not supported between instances of 'str' and 'int'"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The problem here is that the second parameter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"times"}]},{"type":"text","value":" is compared to an integer, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":", on line 2 of the function definition. The argument given was "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Emily\""}]},{"type":"text","value":", which is a string, not an integer. Strings and integers cannot be compared so simply, so an error ensues."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To avoid issues like this you can include "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"type hints"}]},{"type":"text","value":" in your function definitions. The type hint specifies the type of the argument intended for the 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"print_many_times"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"message "},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" times "},{"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":"while"}]},{"type":"text","value":" times "},{"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":"message"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n times "},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This tells anyone using the function that the argument stored in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"message"}]},{"type":"text","value":" is supposed to be a string, and the argument stored in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"times"}]},{"type":"text","value":" is supposed to be an integer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Similarly, the return value of a function can be hinted at in the function definition:"}]},{"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":"ask_for_name"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n name "},{"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":"\"Mikä on nimesi? \""}]},{"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":" name"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This tells the user of the function that the function is supposed to return a string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" Type hinting is literally just hinting about the type of the argument or the return value. It is not a guarantee of type, and definitely not a safeguard against type errors. If a function receives an argument or returns a value of the wrong type, the function is still executed, but it might not work correctly."}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will know more about the arguments and parameters of functions
    • \n
    • You will know how to return values from functions, and how to use the values in your code
    • \n
    • You will be able to add type hints for parameters and return values
    • \n

    Let's quickly recap Python functions. Functions are defined with the keyword def:

    def message():\n    print(\"This message was brought to you by a function\")

    This function can be called in your program code like this:

    message()

    Your program would then print out

    This message was brought to you by a function

    The parameters and arguments of a function

    A function can take one or more arguments. When the function is called, the arguments are assigned to variables, which are defined in the function definition. These variables are called parameters, and they are listed inside the parentheses after the function name.

    In the following code the function greet has one parameter defined, while the function sum has two.

    def greet(name):\n    print(\"Hello there,\", name)\n\ndef sum(a, b):\n    print(\"The sum of the arguments is\", a + b)
    greet(\"Emily\")\nsum(2, 3)

    Hello there, Emily\nThe sum of the arguments is 5

    The terminology around data passed to functions can feel confusing. To make matters worse, some sources refer to what we have called parameters and arguments as formal and actual parameters. Other sources call them formal and actual arguments. The Python documentation specifies only the terms argument and parameter, so that is what we will use as well.

    What actually happens when the function call greet(\"Emily\") is executed?

    In the function definition greet(name) the parameter name behaves for all intents and purposes just like a normal variable. We can use it within the function just like we have used variables in the many main functions in our programs thus far.

    In the function call greet(\"Emily\") the argument \"Emily\" is just like any other string we have come across before. For example, we can assign it to a variable.

    So, when the function call is executed, the value of the argument, \"Emily\", is assigned to the parameter variable name. For the duration of this execution of the function, name = \"Emily\". When the function is called with a different argument, the value of name will be different.

    This terminology may all seem a bit superfluous, but computer science as a discipline does aim to be as exact a science as possible. Using well defined terminology helps.

    Error messages when running tests

    Most exercises on this course have automatic tests attached to them. If your program doesn't work as specified in the task, the tests will show an error message, which may or may not be helpful. It is usually worth the trouble to read the error message carefully.

    In some situations the error message might not tell you very much at all. In the next exercise below you may come across this error message:

    \n \n \n \n \n \n \"4\n \n \n

    The message just states that you should be able to call the function line with the specified arguments:

    line(5, \"\")

    The actual issue becomes clear when we try executing the function call specified in the error message. You can do this by copying the function call into your program and clicking on the triangle:

    \n \n \n \n \n \n \"4\n \n \n

    The last lines produced by the execution, highlighted in the above image, tell us that line 4 of our code causes the error IndexError: string index out of range. In the previous part there was a similar example, where we tried to use an index that fell outside the scope of the string. This time the error appears because we are trying to access the first character of an empty string, that is, a string of length 0.

    Please write a function named line, which takes two arguments: an integer and a string. The function prints out a line of text, the length of which is specified by the first argument. The character used to draw the line should be the first character in the second argument. If the second argument is an empty string, the line should consist of stars.

    An example of expected behaviour:

    line(7, \"%\")\nline(10, \"LOL\")\nline(3, \"\")
    \n%%%%%%%\nLLLLLLLLLL\n***\n

    Function calls within function calls

    You can call a function from within another function. In fact, we have done so many times before, when we called the print function within our own functions in the previous part. Our own functions are functionally no different. In the following example the function greet_many_times calls the function greet as many times as specified by the argument times:

    def greet(name):\n    print(\"Hello there,\", name)\n\ndef greet_many_times(name, times):\n    while times > 0:\n        greet(name)\n        times -= 1\n\ngreet_many_times(\"Emily\", 3)

    Hello there, Emily\nHello there, Emily\nHello there, Emily

    Please write a function named box_of_hashes, which prints out a rectangle of hash characters. The function takes one argument, which specifies the height of the rectangle. The rectangle should be ten characters wide.

    The function should call the function line from the exercise above for the actual printing out. Copy your solution to that exercise above the code for this exercise. Please don't change anything in your line function.

    Some examples of how the function should work:

    box_of_hashes(5)\nprint()\nbox_of_hashes(2)
    \n##########\n##########\n##########\n##########\n##########\n\n##########\n##########\n

    Please write a function named square_of_hashes, which draws a square of hash characters. The function takes one argument, which determines the length of the side of the square.

    The function should call the function line from the exercise above for the actual printing out. Copy your solution to that exercise above the code for this exercise. Please don't change anything in the line function.

    Some examples:

    square_of_hashes(5)\nprint()\nsquare_of_hashes(3)
    \n#####\n#####\n#####\n#####\n#####\n\n###\n###\n###\n

    Please write a function named square, which prints out a square of characters, and takes two arguments. The first parameter specifies the length of the side of the square. The second parameter specifies the character used to draw the square.

    The function should call the function line from the exercise above for the actual printing out. Copy your solution to that exercise above the code for this exercise. Please don't change anything in the line function.

    Some examples:

    square(5, \"*\")\nprint()\nsquare(3, \"o\")
    \n*****\n*****\n*****\n*****\n*****\n\nooo\nooo\nooo\n

    Please write a function named triangle, which draws a triangle of hashes, and takes one argument. The triangle should be as tall and as wide as the value of the argument.

    The function should call the function line from the exercise above for the actual printing out. Copy your solution to that exercise above the code for this exercise. Please don't change anything in the line function.

    Some examples:

    triangle(6)\nprint()\ntriangle(3)
    \n#\n##\n###\n####\n#####\n######\n\n#\n##\n###\n

    Please write a function named shape, which takes four arguments. The first two parameters specify a triangle, as above, and the character used to draw it. The first parameter also specifies the width of a rectangle, while the third parameter specifies its height. The fourth parameter specifies the filler character of the rectangle. The function prints first the triangle, and then the rectangle below it.

    The function should call the function line from the exercise above for the actual printing out. Copy your solution to that exercise above the code for this exercise. Please don't change anything in the line function.

    Some examples:

    shape(5, \"X\", 3, \"*\")\nprint()\nshape(2, \"o\", 4, \"+\")\nprint()\nshape(3, \".\", 0, \",\")
    \nX\nXX\nXXX\nXXXX\nXXXXX\n*****\n*****\n*****\n\no\noo\n++\n++\n++\n++\n\n.\n..\n...\n

    Hint

    Don't try and solve this exercise \"all at once\". A good first step would be to make sure you can print the triangle reliably. Then you can try adding the rectangle.

    This is one of the most important skills of a programmer: concentrate on small, tangible sections of the problem at a time. Solve and verify partial solutions, and use them to build towards a complete solution.

    Please write a function named spruce, which takes one argument. The function prints out the text a spruce!, and the a spruce tree, the size of which is specified by the argument.

    Calling spruce(3) should print out

    \na spruce!\n  *\n ***\n*****\n  *\n

    Calling spruce(5) should print out

    \na spruce!\n    *\n   ***\n  *****\n *******\n*********\n    *\n

    NB: to the left of the spruce there should be exactly the right amount of whitespace. If the shape of the spruce looks correct, but the left edge of the tree is not touching the left edge of the text area in the terminal, the tests will not accept the solution.

    The return value of a function

    Functions can also return values. For instance, the built-in Python function input returns an input string typed in by the user. The value returned by a function can be stored in a variable:

    word = input(\"Please type in a word: \")

    When you want an integer value from the user, the input from the user has to be converted into an integer. The function we've used for this purpose is int, and it also returns a value:

    number = int(input(\"Please type in an integer: \"))

    The function int takes the string returned by input as its argument, and returns an integer type value, if an integer can be parsed from the string.

    The return statement

    The functions you define yourself can also return values. To do this you need the return statement. For example, the following function my_sum returns the sum of its parameters:

    def my_sum(a, b):\n    return a + b\n\nresult = my_sum(2, 3)\n\nprint(\"Sum:\", result)

    Sum: 5

    Here's another example of a return value. This function asks for the user's name and returns the string the user types in:

    def ask_for_name():\n    name = input(\"What is your name? \")\n    return name\n\nname = ask_for_name()\nprint(\"Hello there,\", name)

    What is your name? Anna\nHello there, Anna

    The return statement ends the execution of the function immediately. The following is a nifty way to create a comparison function:

    def smallest(a,b):\n    if a < b:\n        return a\n    return b\n\nprint(smallest(3, 7))\nprint(smallest(5, 2))

    The idea here is that if a is smaller than b, the function returns a and exits immediately. If not, the execution continues to the next line, where the value b is returned. A function can never execute two separate return statements with a single function call.

    3\n2

    You can make use of the return statement even if the function doesn't return a value. Its purpose then is to end the execution of the function:

    def greet(name):\n    if name == \"\":\n        print(\"???\")\n        return\n    print(\"Hello there,\", name)\n\ngreet(\"Emily\")\ngreet(\"\")\ngreet(\"Mark\")

    If the argument (which gets stored in the variable name) is an empty string, the function prints out ??? and exits.

    Hello there, Emily\n???\nHello there, Mark

    Using return values from functions

    We already know that the return values of functions can be stored in variables:

    def my_sum(a, b):\n    return a + b\n\nresult = my_sum(4, 6)\nprint(\"The sum is\", result)

    The sum is 10

    The return value of a function is a value just like any other. It is not necessary to store it in a variable in order to give it as an argument to the print command:

    print(\"The sum is\", my_sum(4, 6))

    The return value of a function can become the argument of another function:

    def my_sum(a, b):\n    return a+b\n\ndef difference(a, b):\n    return a-b\n\nresult = difference(my_sum(5, 2), my_sum(2, 3))\nprint(\"The result is\", result)

    The result is 2

    In this case the inner function calls my_sum(5, 2) and my_sum(2, 3) are executed first. The values they return (7 and 5) are used as the arguments of the outer function call.

    The outer function call difference(7, 5) returns the value 2, which is stored in the variable result and printed out.

    In summary, values returned by functions work exactly like any other value in Python. They can be printed out, stored in variables, used in expressions and used as arguments in other function calls.

    The difference between return and print

    Sometimes the difference between a function returning a value and a print statement within a function can be confusing. Let's have a look at two different ways of implementing a function for working out which of two values is greater:

    def max1(a, b):\n    if a > b:\n        return a\n    else:\n        return b\n\ndef max2(a, b):\n    if a > b:\n        print(a)\n    else:\n        print(b)\n\nresult = max1(3, 5)\nprint(result)\n\nmax2(7, 2)

    5\n7

    Both versions seem to be working just fine, as the maximum values are printed correctly. There is a fundamental difference between the two, however. The first of the two functions, max1, does not print out anything on its own, but instead it returns the value which is greater. If we execute the following line

    max1(3, 5)

    nothing seems to happen. The return value of the function has to be used in some way in the code which called the function. For instance, it can be stored in a variable and printed out:

    result = max1(3, 5)\nprint(result)

    The second version, max2, uses the print command within the function to print out the greater value. If we want to see the value, it is enough to call the function

    max2(7, 5)

    and the greater value is printed out. The downside of this handy function is that the value worked out by the function is not available for use in the program which called it. That is why functions which return values are often the better option.

    Please write a function named greatest_number, which takes three arguments. The function returns the greatest in value of the three.

    An example of how the function is used:

    print(greatest_number(3, 4, 1)) # 4\nprint(greatest_number(99, -4, 7)) # 99\nprint(greatest_number(0, 0, 0)) # 0

    Please write a function named same_chars, which takes one string and two integers as arguments. The integers refer to indexes within the string. The function should return True if the two characters at the indexes specified are the same. Otherwise, and especially if either of the indexes falls outside the scope of the string, the function returns False.

    Some examples of how the function is used:

    # same characters m and m\nprint(same_chars(\"programmer\", 6, 7)) # True\n\n# different characters p and r\nprint(same_chars(\"programmer\", 0, 4)) # False\n\n# the second index is not within the string\nprint(same_chars(\"programmer\", 0, 12)) # False

    Please write three functions: first_word, second_word and last_word. Each function takes a string argument.

    As their names imply, the functions return either the first, the second or the last word in the sentence they receive as their string argument.

    In each case you may assume the argument string contains at least two separate words, and all words are separated by exactly one space character. There will be no spaces in the beginning or at the end of the argument strings.

    sentence = \"it was a dark and stormy python\"\n\nprint(first_word(sentence)) # it\nprint(second_word(sentence)) # was\nprint(last_word(sentence)) # python

    it\nwas\npython

    sentence = \"it was\"\n\nprint(second_word(sentence)) # was\nprint(last_word(sentence)) # was

    The type of the argument

    A quick recap of the data types we've come across so far:

    \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
    TypePython data typeExample
    integerint23
    floating point numberfloat-0.45
    stringstr\"Peter Python\"
    Boolean valueboolTrue

    When you call a function, it will only work correctly if the arguments you give it are of the right type. Let's have a look at an example:

    def print_many_times(message, times):\n    while times > 0:\n        print(message)\n        times -= 1

    The function works as expected if we call it like this:

    print_many_times(\"Hello there\", 5)

    Hello there\nHello there\nHello there\nHello there\nHello there

    However, if we give the function an argument of the wrong type, it will not work:

    print_many_times(\"Hello there\", \"Emily\")

    TypeError: '>' not supported between instances of 'str' and 'int'

    The problem here is that the second parameter times is compared to an integer, 0, on line 2 of the function definition. The argument given was \"Emily\", which is a string, not an integer. Strings and integers cannot be compared so simply, so an error ensues.

    To avoid issues like this you can include type hints in your function definitions. The type hint specifies the type of the argument intended for the function:

    def print_many_times(message : str, times : int):\n    while times > 0:\n        print(message)\n        times -= 1

    This tells anyone using the function that the argument stored in message is supposed to be a string, and the argument stored in times is supposed to be an integer.

    Similarly, the return value of a function can be hinted at in the function definition:

    def ask_for_name() -> str:\n    name = input(\"Mikä on nimesi? \")\n    return name

    This tells the user of the function that the function is supposed to return a string.

    NB: Type hinting is literally just hinting about the type of the argument or the return value. It is not a guarantee of type, and definitely not a safeguard against type errors. If a function receives an argument or returns a value of the wrong type, the function is still executed, but it might not work correctly.

    ","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-4/2-more-functions.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know more about the arguments and parameters of functions"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to return values from functions, and how to use the values in your code"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to add type hints for parameters and return values"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's quickly recap Python functions. Functions are defined with the keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":":"}]},{"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":"message"}]},{"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 "},{"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":"\"This message was brought to you by a function\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This function can be called in your program code 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":"text","value":"message"},{"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":"Your program would then print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This message was brought to you by a function"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-parameters-and-arguments-of-a-function","style":"position:relative;"},"children":[{"type":"text","value":"The parameters and arguments of a function"},{"type":"element","tagName":"a","properties":{"href":"#the-parameters-and-arguments-of-a-function","ariaLabel":"the parameters and arguments of a function 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":"A function can take one or more arguments. When the function is called, the arguments are assigned to variables, which are defined in the function definition. These variables are called parameters, and they are listed inside the parentheses after the function name."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following code the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"greet"}]},{"type":"text","value":" has one parameter defined, while the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" has two."}]},{"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":"greet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"\"Hello there,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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 sum of the arguments is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"greet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"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":"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":" "},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hello there, Emily\nThe sum of the arguments is 5"}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Formal vs actual, parameter vs argument"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The terminology around data passed to functions can feel confusing. To make matters worse, some sources refer to what we have called parameters and arguments as "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"formal"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"actual parameters"}]},{"type":"text","value":". Other sources call them "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"formal"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"actual arguments"}]},{"type":"text","value":". "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/glossary.html#term-argument","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"The Python documentation"}]},{"type":"text","value":" specifies only the terms "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"argument"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"parameter"}]},{"type":"text","value":", so that is what we will use as well."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What actually happens when the function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"greet(\"Emily\")"}]},{"type":"text","value":" is executed?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the function definition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"greet(name)"}]},{"type":"text","value":" the parameter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" behaves for all intents and purposes just like a normal variable. We can use it within the function just like we have used variables in the many main functions in our programs thus far."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"greet(\"Emily\")"}]},{"type":"text","value":" the argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Emily\""}]},{"type":"text","value":" is just like any other string we have come across before. For example, we can assign it to a variable."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, when the function call is executed, the value of the argument, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Emily\""}]},{"type":"text","value":", is assigned to the parameter variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":". For the duration of "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"this execution"}]},{"type":"text","value":" of the function, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name = \"Emily\""}]},{"type":"text","value":". When the function is called with a different argument, the value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" will be different."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This terminology may all seem a bit superfluous, but computer science as a discipline does aim to be as exact a science as possible. Using well defined terminology helps."}]}]},{"type":"element","tagName":"h2","properties":{"id":"error-messages-when-running-tests","style":"position:relative;"},"children":[{"type":"text","value":"Error messages when running tests"},{"type":"element","tagName":"a","properties":{"href":"#error-messages-when-running-tests","ariaLabel":"error messages when running tests 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":"Most exercises on this course have automatic tests attached to them. If your program doesn't work as specified in the task, the tests will show an error message, which may or may not be helpful. It is usually worth the trouble to read the error message carefully."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In some situations the error message might not tell you very much at all. In the next exercise below you may come across this error message:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 728px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/b192944b4585f67cefef981a78a1333b/cecac/4_2_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: 49.565217391304344%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAKCAYAAAC0VX7mAAAACXBIWXMAAAsTAAALEwEAmpwYAAAB20lEQVQoz42S227TQBRF8wf0BrT2XGNPHF8SO86tSW8JBJAIFEIFbVQJ8QgCxBcjIfWBz1hgl0qFF/qwNPscbR3NzD6NLE3pdrrE7RgXOqSQKGUQQiFlhSQIwjvT8H0fbTSLJ3Pmj46Zz485OprW9Wx2wmKxQGtdD1ZK/ZdGZTTa8nJ5zunygqeLFc8WK96efeDV6Yrl8nltrHx3oSHkLk4fsOp/593kivPpFRcHP3kz+IHxDniwu4kxFm0MRmuMNv+g/76hkIKw2eKwfMFs9Jp+/JjUHpIFR7R0hNNNAmkJlEUGFhFcnzdahU2stVRfJ4SgUYVgQos7jGmfZNiJw4xD7Nihhi30KEKNIuwgYt9ETHXE1ERM9DX7OqIVOpxzBEFAQ1WptjT31xnb65SddcLDyw7mfb/WO+uYrcsE7yzm43bC13sJnzdivmwkfNvK+LSV0InajCcTijyn4SlF6fnMNncZSccwLRm0c3qtjHHar3XFMO2RDEuiQUE87NEeFPjtkG3poYVgb8+rn10PHHoeIxeQFzl5L6foFWSdjKIs6l5Vl/2SPM0ouzm9Tpckigi0wUpJUyqUvglFKVzVFALp+SjP+8Nt7SFv6Qrt+xgh6oG6Wpkq5d9r8wujHCixT9lVsAAAAABJRU5ErkJggg=='); 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":["/static/b192944b4585f67cefef981a78a1333b/a0b58/4_2_1.webp 230w","/static/b192944b4585f67cefef981a78a1333b/bc10c/4_2_1.webp 460w","/static/b192944b4585f67cefef981a78a1333b/8cb3e/4_2_1.webp 728w"],"sizes":"(max-width: 728px) 100vw, 728px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/b192944b4585f67cefef981a78a1333b/81c8e/4_2_1.png 230w","/static/b192944b4585f67cefef981a78a1333b/08a84/4_2_1.png 460w","/static/b192944b4585f67cefef981a78a1333b/cecac/4_2_1.png 728w"],"sizes":"(max-width: 728px) 100vw, 728px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/b192944b4585f67cefef981a78a1333b/cecac/4_2_1.png","alt":"4 2 1","title":"4 2 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":"p","properties":{},"children":[{"type":"text","value":"The message just states that you should be able to call the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" with the specified arguments:"}]},{"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":"line"},{"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":" "},{"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":"p","properties":{},"children":[{"type":"text","value":"The actual issue becomes clear when we try executing the function call specified in the error message. You can do this by copying the function call into your program and clicking on the triangle:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/3fa13c46cf1e3bdd9fe23a5e8bc7778b/e4374/4_2_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: 43.47826086956522%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAABt0lEQVQoz22Ry27TUBRF/QudJLF9X74Pv+LrOE5cQYUAFYakqaCllA75AH6BP19VTEAIGCwt7b2lMznJNF2yG0e225G27WibjsoHbGFx1mOt+y/eOnIfeK80b5xnty2YhpYkBE+oAr6pqJoSEQpSbxBWo51GW405+5SN1xROk0mJE4IfFxdcL5esfInWhkQbgw+BuInkvuB7tuIpS6mVxBqNMhr9F3OnFE+LBZ0QLLTGKIlSiqRQGiPVTFE4rvOcY7rk62pJladkIkf+gRAZQuSYM6kUaCkRQiClJHFTj9l3FLGmeTni+xHbvcB1e8p6pCwHQrmZKauBqt7NLquRUA40VUfwjs1moG3WJMOrK9aXe9bTjmYaibs9cZwIbUcVB8K6/80p/+p8G/FNh2/XmBAYNlv6OJDces/ntuWuaXiMkU91zdE7DraY/aEouHGWg7Xn3nLjHAdn5+1QGN5pTZpn5HlOMvY9b/cTV9str3d7LmNkqEr606O8o3P2bEf0/uyf+bTV1vIxy7BKIU9PuY81X7qKh+B47GoeXeDBVNwrx3El/yVVHFeC21TP+U4ovi0WKCnng8/cdwh54pL27wAAAABJRU5ErkJggg=='); 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":["/static/3fa13c46cf1e3bdd9fe23a5e8bc7778b/a0b58/4_2_2.webp 230w","/static/3fa13c46cf1e3bdd9fe23a5e8bc7778b/bc10c/4_2_2.webp 460w","/static/3fa13c46cf1e3bdd9fe23a5e8bc7778b/966d8/4_2_2.webp 920w","/static/3fa13c46cf1e3bdd9fe23a5e8bc7778b/4c2b6/4_2_2.webp 927w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/3fa13c46cf1e3bdd9fe23a5e8bc7778b/81c8e/4_2_2.png 230w","/static/3fa13c46cf1e3bdd9fe23a5e8bc7778b/08a84/4_2_2.png 460w","/static/3fa13c46cf1e3bdd9fe23a5e8bc7778b/c0255/4_2_2.png 920w","/static/3fa13c46cf1e3bdd9fe23a5e8bc7778b/e4374/4_2_2.png 927w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/3fa13c46cf1e3bdd9fe23a5e8bc7778b/c0255/4_2_2.png","alt":"4 2 2","title":"4 2 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 last lines produced by the execution, highlighted in the above image, tell us that line 4 of our code causes the error "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"IndexError: string index out of range"}]},{"type":"text","value":". In the "},{"type":"element","tagName":"a","properties":{"href":"/part-3/2-working-with-strings"},"children":[{"type":"text","value":"previous part"}]},{"type":"text","value":" there was a similar example, where we tried to use an index that fell outside the scope of the string. This time the error appears because we are trying to access the first character of an empty string, that is, a string of length 0."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Line","tmcname":"part04-02_line"},"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":"line"}]},{"type":"text","value":", which takes two arguments: an integer and a string. The function prints out a line of text, the length of which is specified by the first argument. The character used to draw the line should be the first character in the second argument. If the second argument is an empty string, the line should consist of stars."}]},{"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":"line"},{"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":"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":"\nline"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"\"LOL\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nline"},{"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":" "},{"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":"pre","properties":{},"children":[{"type":"text","value":"%%%%%%%\nLLLLLLLLLL\n***\n"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"function-calls-within-function-calls","style":"position:relative;"},"children":[{"type":"text","value":"Function calls within function calls"},{"type":"element","tagName":"a","properties":{"href":"#function-calls-within-function-calls","ariaLabel":"function calls within function calls 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":"You can call a function from within another function. In fact, we have done so many times before, when we called the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" function within our own functions in the previous part. Our own functions are functionally no different. In the following example the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"greet_many_times"}]},{"type":"text","value":" calls the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"greet"}]},{"type":"text","value":" as many times as specified by the argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"times"}]},{"type":"text","value":":"}]},{"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":"greet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"\"Hello there,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"greet_many_times"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" times"},{"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":"while"}]},{"type":"text","value":" times "},{"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 greet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n times "},{"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\ngreet_many_times"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"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":"3"}]},{"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":"Hello there, Emily\nHello there, Emily\nHello there, Emily"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"A box of hashes","tmcname":"part04-03_box_of_hashes"},"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":"box_of_hashes"}]},{"type":"text","value":", which prints out a rectangle of hash characters. The function takes one argument, which specifies the height of the rectangle. The rectangle should be ten characters wide."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should call the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" from the exercise above for the actual printing out. Copy your solution to that exercise above the code for this exercise. Please don't change anything in your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples 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":"box_of_hashes"},{"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"},{"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":"\nbox_of_hashes"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"##########\n##########\n##########\n##########\n##########\n\n##########\n##########\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"A square of hashes","tmcname":"part04-04_square_of_hashes"},"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":"square_of_hashes"}]},{"type":"text","value":", which draws a square of hash characters. The function takes one argument, which determines the length of the side of the square."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should call the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" from the exercise above for the actual printing out. Copy your solution to that exercise above the code for this exercise. Please don't change anything in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" function."}]},{"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":"text","value":"square_of_hashes"},{"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"},{"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":"\nsquare_of_hashes"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"#####\n#####\n#####\n#####\n#####\n\n###\n###\n###\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"A square","tmcname":"part04-05_square"},"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":"square"}]},{"type":"text","value":", which prints out a square of characters, and takes two arguments. The first parameter specifies the length of the side of the square. The second parameter specifies the character used to draw the square."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should call the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" from the exercise above for the actual printing out. Copy your solution to that exercise above the code for this exercise. Please don't change anything in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" function."}]},{"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":"text","value":"square"},{"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":" "},{"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"},{"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":"\nsquare"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"o\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"*****\n*****\n*****\n*****\n*****\n\nooo\nooo\nooo\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"A triangle","tmcname":"part04-06_triangle"},"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":"triangle"}]},{"type":"text","value":", which draws a triangle of hashes, and takes one argument. The triangle should be as tall and as wide as the value of the argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should call the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" from the exercise above for the actual printing out. Copy your solution to that exercise above the code for this exercise. Please don't change anything in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" function."}]},{"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":"text","value":"triangle"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntriangle"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"#\n##\n###\n####\n#####\n######\n\n#\n##\n###\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"A shape","tmcname":"part04-07_shape"},"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":"shape"}]},{"type":"text","value":", which takes four arguments. The first two parameters specify a triangle, as above, and the character used to draw it. The first parameter also specifies the width of a rectangle, while the third parameter specifies its height. The fourth parameter specifies the filler character of the rectangle. The function prints first the triangle, and then the rectangle below it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should call the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" from the exercise above for the actual printing out. Copy your solution to that exercise above the code for this exercise. Please don't change anything in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"line"}]},{"type":"text","value":" function."}]},{"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":"text","value":"shape"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"X\""}]},{"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":"3"}]},{"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":"\"*\""}]},{"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":"\nshape"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"o\""}]},{"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":"4"}]},{"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":"\"+\""}]},{"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":"\nshape"},{"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":" "},{"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":" "},{"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","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":"pre","properties":{},"children":[{"type":"text","value":"X\nXX\nXXX\nXXXX\nXXXXX\n*****\n*****\n*****\n\no\noo\n++\n++\n++\n++\n\n.\n..\n...\n"}]}]},{"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":"Don't try and solve this exercise \"all at once\". A good first step would be to make sure you can print the triangle reliably. Then you can try adding the rectangle."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is one of the most important skills of a programmer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"concentrate on small, tangible sections of the problem at a time"}]},{"type":"text","value":". Solve and verify partial solutions, and use them to build towards a complete solution."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"A spruce","tmcname":"part04-08_spruce"},"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":"spruce"}]},{"type":"text","value":", which takes one argument. The function prints out the text "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a spruce!"}]},{"type":"text","value":", and the a spruce tree, the size of which is specified by the argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Calling "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"spruce(3)"}]},{"type":"text","value":" should print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"a spruce!\n *\n ***\n*****\n *\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Calling "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"spruce(5)"}]},{"type":"text","value":" should print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"a spruce!\n *\n ***\n *****\n *******\n*********\n *\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB: to the left of the spruce there should be exactly the right amount of whitespace"}]},{"type":"text","value":". If the shape of the spruce looks correct, but the left edge of the tree is not touching the left edge of the text area in the terminal, the tests will not accept the solution."}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-return-value-of-a-function","style":"position:relative;"},"children":[{"type":"text","value":"The return value of a function"},{"type":"element","tagName":"a","properties":{"href":"#the-return-value-of-a-function","ariaLabel":"the return value of a function 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":"Functions can also return values. For instance, the built-in Python function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"returns"}]},{"type":"text","value":" an input string typed in by the user. The value returned by a function can be stored in a 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":"word "},{"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 word: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When you want an integer value from the user, the input from the user has to be converted into an integer. The function we've used for this purpose is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":", and it also returns a value:"}]},{"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","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"]},"children":[{"type":"text","value":"\"Please type in an integer: \""}]},{"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":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":" takes the string returned by "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"input"}]},{"type":"text","value":" as its argument, and returns an integer type value, if an integer can be parsed from the string."}]},{"type":"element","tagName":"h2","properties":{"id":"the-return-statement","style":"position:relative;"},"children":[{"type":"text","value":"The return statement"},{"type":"element","tagName":"a","properties":{"href":"#the-return-statement","ariaLabel":"the return statement 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 functions you define yourself can also return values. To do this you need the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" statement. For example, the following function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"my_sum"}]},{"type":"text","value":" returns the sum of its parameters:"}]},{"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":"my_sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_sum"},{"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":" "},{"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":"\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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sum:\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sum: 5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here's another example of a return value. This function asks for the user's name and returns the string the user types in:"}]},{"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":"ask_for_name"}]},{"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 name "},{"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":"\"What is your name? \""}]},{"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":" name\n\nname "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ask_for_name"},{"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":"\"Hello there,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"What is your name? "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Anna"}]},{"type":"text","value":"\nHello there, Anna"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" statement ends the execution of the function immediately. The following is a nifty way to create a comparison 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"smallest"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"b"},{"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":"if"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" b"},{"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":" a\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" b\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":"smallest"},{"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":" "},{"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":"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":"smallest"},{"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":" "},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The idea here is that if "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" is smaller than "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":", the function returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" and exits immediately. If not, the execution continues to the next line, where the value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" is returned. A function can never execute two separate "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" statements with a single function call."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3\n2"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can make use of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" statement even if the function doesn't return a value. Its purpose then is to end the execution of the 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"greet"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"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":"\"\""}]},{"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":"\"???\""}]},{"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":"\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":"\"Hello there,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ngreet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngreet"},{"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":"\ngreet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mark\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the argument (which gets stored in the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":") is an empty string, the function prints out "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"???"}]},{"type":"text","value":" and exits."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hello there, Emily\n???\nHello there, Mark"}]}]},{"type":"element","tagName":"h2","properties":{"id":"using-return-values-from-functions","style":"position:relative;"},"children":[{"type":"text","value":"Using return values from functions"},{"type":"element","tagName":"a","properties":{"href":"#using-return-values-from-functions","ariaLabel":"using return values from 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 already know that the return values of functions can be stored in 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":"my_sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_sum"},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The sum is\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The sum is 10"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The return value of a function is a value just like any other. It is not necessary to store it in a variable in order to give it as an argument to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command:"}]},{"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":"\"The sum is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" my_sum"},{"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":"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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The return value of a function can become the argument of another 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"my_sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":" a"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"b\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":"difference"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":" a"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"b\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" difference"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_sum"},{"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":" "},{"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":" my_sum"},{"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":" "},{"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","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 result is\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The result is 2"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this case the inner function calls "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"my_sum(5, 2)"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"my_sum(2, 3)"}]},{"type":"text","value":" are executed first. The values they return (7 and 5) are used as the arguments of the outer function call."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The outer function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"difference(7, 5)"}]},{"type":"text","value":" returns the value 2, which is stored in the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"result"}]},{"type":"text","value":" and printed out."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In summary, values returned by functions work exactly like any other value in Python. They can be printed out, stored in variables, used in expressions and used as arguments in other function calls."}]},{"type":"element","tagName":"h2","properties":{"id":"the-difference-between-return-and-print","style":"position:relative;"},"children":[{"type":"text","value":"The difference between return and print"},{"type":"element","tagName":"a","properties":{"href":"#the-difference-between-return-and-print","ariaLabel":"the difference between return and print 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":"Sometimes the difference between a function "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"returning"}]},{"type":"text","value":" a value and a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"print statement"}]},{"type":"text","value":" within a function can be confusing. Let's have a look at two different ways of implementing a function for working out which of two values is greater:"}]},{"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":"max1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":"if"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" b"},{"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":" a\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":" b\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":"max2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":"if"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" b"},{"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":"a"},{"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":"else"}]},{"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":"b"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" max1"},{"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":" "},{"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"},{"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":"text","value":"\n\nmax2"},{"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":"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5\n7"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Both versions seem to be working just fine, as the maximum values are printed correctly. There is a fundamental difference between the two, however. The first of the two functions, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"max1"}]},{"type":"text","value":", does not print out anything on its own, but instead it "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"returns"}]},{"type":"text","value":" the value which is greater. If we execute the following line"}]},{"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":"max1"},{"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":" "},{"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":"p","properties":{},"children":[{"type":"text","value":"nothing seems to happen. The return value of the function has to be "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"used"}]},{"type":"text","value":" in some way in the code which called the function. For instance, it can be stored in a variable and printed out:"}]},{"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":"result "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" max1"},{"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":" "},{"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"},{"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":"p","properties":{},"children":[{"type":"text","value":"The second version, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"max2"}]},{"type":"text","value":", uses the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"within the function"}]},{"type":"text","value":" to print out the greater value. If we want to see the value, it is enough to call the 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":"text","value":"max2"},{"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":"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":"p","properties":{},"children":[{"type":"text","value":"and the greater value is printed out. The downside of this handy function is that the value worked out by the function is not available for use in the program which called it. That is why functions which return values are often the better option."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The greatest number","tmcname":"part04-09_greatest_number"},"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":"greatest_number"}]},{"type":"text","value":", which takes three arguments. The function returns the greatest in value of the three."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how the function is used:"}]},{"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":"text","value":"greatest_number"},{"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":" "},{"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":"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 4"}]},{"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":"greatest_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"99"}]},{"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":"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":"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 99"}]},{"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":"greatest_number"},{"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","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","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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 0"}]}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Same characters","tmcname":"part04-10_same_characters"},"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":"same_chars"}]},{"type":"text","value":", which takes one string and two integers as arguments. The integers refer to indexes within the string. The function should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" if the two characters at the indexes specified are the same. Otherwise, and especially if either of the indexes falls outside the scope of the string, the function returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of how the function is used:"}]},{"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":"# same characters m and m"}]},{"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":"same_chars"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"programmer\""}]},{"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":"6"}]},{"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":"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# True"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# different characters p and r"}]},{"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":"same_chars"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"programmer\""}]},{"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":"0"}]},{"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":"4"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# False"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the second index is not within the string"}]},{"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":"same_chars"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"programmer\""}]},{"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":"0"}]},{"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":"12"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# False"}]}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"First, second and last words","tmcname":"part04-11_first_second_last"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write three functions: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"first_word"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"second_word"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"last_word"}]},{"type":"text","value":". Each function takes a string argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As their names imply, the functions return either the first, the second or the last word in the sentence they receive as their string argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In each case you may assume the argument string contains at least two separate words, and all words are separated by exactly one space character. There will be no spaces in the beginning or at the end of the argument 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":"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":"\"it was a dark and stormy python\""}]},{"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":"first_word"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# it"}]},{"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":"second_word"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# was"}]},{"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":"last_word"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# python"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"it\nwas\npython"}]}]},{"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":"\"it was\""}]},{"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":"second_word"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# was"}]},{"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":"last_word"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# was"}]}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-type-of-the-argument","style":"position:relative;"},"children":[{"type":"text","value":"The type of the argument"},{"type":"element","tagName":"a","properties":{"href":"#the-type-of-the-argument","ariaLabel":"the type of the argument 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":"A quick recap of the data types we've come across so far:"}]},{"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"},{"type":"element","tagName":"table","properties":{},"children":[{"type":"element","tagName":"thead","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"th","properties":{"align":"left"},"children":[{"type":"text","value":"Type"}]},{"type":"element","tagName":"th","properties":{"align":"center"},"children":[{"type":"text","value":"Python data type"}]},{"type":"element","tagName":"th","properties":{},"children":[{"type":"text","value":"Example"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"integer"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"23"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"floating point number"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-0.45"}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"string"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Peter Python\""}]}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"Boolean value"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]}]},{"type":"element","tagName":"td","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When you call a function, it will only work correctly if the arguments you give it are of the right type. Let's have a look at an example:"}]},{"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_many_times"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"message"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" times"},{"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":"while"}]},{"type":"text","value":" times "},{"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":"message"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n times "},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function works as expected if we call it 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":"text","value":"print_many_times"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hello there\""}]},{"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":"5"}]},{"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":"Hello there\nHello there\nHello there\nHello there\nHello there"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"However, if we give the function an argument of the wrong type, it will not 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":"print_many_times"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hello 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":"\"Emily\""}]},{"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":"TypeError: '>' not supported between instances of 'str' and 'int'"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The problem here is that the second parameter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"times"}]},{"type":"text","value":" is compared to an integer, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":", on line 2 of the function definition. The argument given was "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Emily\""}]},{"type":"text","value":", which is a string, not an integer. Strings and integers cannot be compared so simply, so an error ensues."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To avoid issues like this you can include "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"type hints"}]},{"type":"text","value":" in your function definitions. The type hint specifies the type of the argument intended for the 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"print_many_times"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"message "},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" times "},{"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":"while"}]},{"type":"text","value":" times "},{"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":"message"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n times "},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This tells anyone using the function that the argument stored in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"message"}]},{"type":"text","value":" is supposed to be a string, and the argument stored in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"times"}]},{"type":"text","value":" is supposed to be an integer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Similarly, the return value of a function can be hinted at in the function definition:"}]},{"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":"ask_for_name"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n name "},{"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":"\"Mikä on nimesi? \""}]},{"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":" name"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This tells the user of the function that the function is supposed to return a string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" Type hinting is literally just hinting about the type of the argument or the return value. It is not a guarantee of type, and definitely not a safeguard against type errors. If a function receives an argument or returns a value of the wrong type, the function is still executed, but it might not work correctly."}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will know more about the arguments and parameters of functions
    • \n
    • You will know how to return values from functions, and how to use the values in your code
    • \n
    • You will be able to add type hints for parameters and return values
    • \n

    Let's quickly recap Python functions. Functions are defined with the keyword def:

    def message():\n    print(\"This message was brought to you by a function\")

    This function can be called in your program code like this:

    message()

    Your program would then print out

    This message was brought to you by a function

    The parameters and arguments of a function

    A function can take one or more arguments. When the function is called, the arguments are assigned to variables, which are defined in the function definition. These variables are called parameters, and they are listed inside the parentheses after the function name.

    In the following code the function greet has one parameter defined, while the function sum has two.

    def greet(name):\n    print(\"Hello there,\", name)\n\ndef sum(a, b):\n    print(\"The sum of the arguments is\", a + b)
    greet(\"Emily\")\nsum(2, 3)

    Hello there, Emily\nThe sum of the arguments is 5

    The terminology around data passed to functions can feel confusing. To make matters worse, some sources refer to what we have called parameters and arguments as formal and actual parameters. Other sources call them formal and actual arguments. The Python documentation specifies only the terms argument and parameter, so that is what we will use as well.

    What actually happens when the function call greet(\"Emily\") is executed?

    In the function definition greet(name) the parameter name behaves for all intents and purposes just like a normal variable. We can use it within the function just like we have used variables in the many main functions in our programs thus far.

    In the function call greet(\"Emily\") the argument \"Emily\" is just like any other string we have come across before. For example, we can assign it to a variable.

    So, when the function call is executed, the value of the argument, \"Emily\", is assigned to the parameter variable name. For the duration of this execution of the function, name = \"Emily\". When the function is called with a different argument, the value of name will be different.

    This terminology may all seem a bit superfluous, but computer science as a discipline does aim to be as exact a science as possible. Using well defined terminology helps.

    Error messages when running tests

    Most exercises on this course have automatic tests attached to them. If your program doesn't work as specified in the task, the tests will show an error message, which may or may not be helpful. It is usually worth the trouble to read the error message carefully.

    In some situations the error message might not tell you very much at all. In the next exercise below you may come across this error message:

    \n \n \n \n \n \n \"4\n \n \n

    The message just states that you should be able to call the function line with the specified arguments:

    line(5, \"\")

    The actual issue becomes clear when we try executing the function call specified in the error message. You can do this by copying the function call into your program and clicking on the triangle:

    \n \n \n \n \n \n \"4\n \n \n

    The last lines produced by the execution, highlighted in the above image, tell us that line 4 of our code causes the error IndexError: string index out of range. In the previous part there was a similar example, where we tried to use an index that fell outside the scope of the string. This time the error appears because we are trying to access the first character of an empty string, that is, a string of length 0.

    Please write a function named line, which takes two arguments: an integer and a string. The function prints out a line of text, the length of which is specified by the first argument. The character used to draw the line should be the first character in the second argument. If the second argument is an empty string, the line should consist of stars.

    An example of expected behaviour:

    line(7, \"%\")\nline(10, \"LOL\")\nline(3, \"\")
    \n%%%%%%%\nLLLLLLLLLL\n***\n

    Function calls within function calls

    You can call a function from within another function. In fact, we have done so many times before, when we called the print function within our own functions in the previous part. Our own functions are functionally no different. In the following example the function greet_many_times calls the function greet as many times as specified by the argument times:

    def greet(name):\n    print(\"Hello there,\", name)\n\ndef greet_many_times(name, times):\n    while times > 0:\n        greet(name)\n        times -= 1\n\ngreet_many_times(\"Emily\", 3)

    Hello there, Emily\nHello there, Emily\nHello there, Emily

    Please write a function named box_of_hashes, which prints out a rectangle of hash characters. The function takes one argument, which specifies the height of the rectangle. The rectangle should be ten characters wide.

    The function should call the function line from the exercise above for the actual printing out. Copy your solution to that exercise above the code for this exercise. Please don't change anything in your line function.

    Some examples of how the function should work:

    box_of_hashes(5)\nprint()\nbox_of_hashes(2)
    \n##########\n##########\n##########\n##########\n##########\n\n##########\n##########\n

    Please write a function named square_of_hashes, which draws a square of hash characters. The function takes one argument, which determines the length of the side of the square.

    The function should call the function line from the exercise above for the actual printing out. Copy your solution to that exercise above the code for this exercise. Please don't change anything in the line function.

    Some examples:

    square_of_hashes(5)\nprint()\nsquare_of_hashes(3)
    \n#####\n#####\n#####\n#####\n#####\n\n###\n###\n###\n

    Please write a function named square, which prints out a square of characters, and takes two arguments. The first parameter specifies the length of the side of the square. The second parameter specifies the character used to draw the square.

    The function should call the function line from the exercise above for the actual printing out. Copy your solution to that exercise above the code for this exercise. Please don't change anything in the line function.

    Some examples:

    square(5, \"*\")\nprint()\nsquare(3, \"o\")
    \n*****\n*****\n*****\n*****\n*****\n\nooo\nooo\nooo\n

    Please write a function named triangle, which draws a triangle of hashes, and takes one argument. The triangle should be as tall and as wide as the value of the argument.

    The function should call the function line from the exercise above for the actual printing out. Copy your solution to that exercise above the code for this exercise. Please don't change anything in the line function.

    Some examples:

    triangle(6)\nprint()\ntriangle(3)
    \n#\n##\n###\n####\n#####\n######\n\n#\n##\n###\n

    Please write a function named shape, which takes four arguments. The first two parameters specify a triangle, as above, and the character used to draw it. The first parameter also specifies the width of a rectangle, while the third parameter specifies its height. The fourth parameter specifies the filler character of the rectangle. The function prints first the triangle, and then the rectangle below it.

    The function should call the function line from the exercise above for the actual printing out. Copy your solution to that exercise above the code for this exercise. Please don't change anything in the line function.

    Some examples:

    shape(5, \"X\", 3, \"*\")\nprint()\nshape(2, \"o\", 4, \"+\")\nprint()\nshape(3, \".\", 0, \",\")
    \nX\nXX\nXXX\nXXXX\nXXXXX\n*****\n*****\n*****\n\no\noo\n++\n++\n++\n++\n\n.\n..\n...\n

    Hint

    Don't try and solve this exercise \"all at once\". A good first step would be to make sure you can print the triangle reliably. Then you can try adding the rectangle.

    This is one of the most important skills of a programmer: concentrate on small, tangible sections of the problem at a time. Solve and verify partial solutions, and use them to build towards a complete solution.

    Please write a function named spruce, which takes one argument. The function prints out the text a spruce!, and the a spruce tree, the size of which is specified by the argument.

    Calling spruce(3) should print out

    \na spruce!\n  *\n ***\n*****\n  *\n

    Calling spruce(5) should print out

    \na spruce!\n    *\n   ***\n  *****\n *******\n*********\n    *\n

    NB: to the left of the spruce there should be exactly the right amount of whitespace. If the shape of the spruce looks correct, but the left edge of the tree is not touching the left edge of the text area in the terminal, the tests will not accept the solution.

    The return value of a function

    Functions can also return values. For instance, the built-in Python function input returns an input string typed in by the user. The value returned by a function can be stored in a variable:

    word = input(\"Please type in a word: \")

    When you want an integer value from the user, the input from the user has to be converted into an integer. The function we've used for this purpose is int, and it also returns a value:

    number = int(input(\"Please type in an integer: \"))

    The function int takes the string returned by input as its argument, and returns an integer type value, if an integer can be parsed from the string.

    The return statement

    The functions you define yourself can also return values. To do this you need the return statement. For example, the following function my_sum returns the sum of its parameters:

    def my_sum(a, b):\n    return a + b\n\nresult = my_sum(2, 3)\n\nprint(\"Sum:\", result)

    Sum: 5

    Here's another example of a return value. This function asks for the user's name and returns the string the user types in:

    def ask_for_name():\n    name = input(\"What is your name? \")\n    return name\n\nname = ask_for_name()\nprint(\"Hello there,\", name)

    What is your name? Anna\nHello there, Anna

    The return statement ends the execution of the function immediately. The following is a nifty way to create a comparison function:

    def smallest(a,b):\n    if a < b:\n        return a\n    return b\n\nprint(smallest(3, 7))\nprint(smallest(5, 2))

    The idea here is that if a is smaller than b, the function returns a and exits immediately. If not, the execution continues to the next line, where the value b is returned. A function can never execute two separate return statements with a single function call.

    3\n2

    You can make use of the return statement even if the function doesn't return a value. Its purpose then is to end the execution of the function:

    def greet(name):\n    if name == \"\":\n        print(\"???\")\n        return\n    print(\"Hello there,\", name)\n\ngreet(\"Emily\")\ngreet(\"\")\ngreet(\"Mark\")

    If the argument (which gets stored in the variable name) is an empty string, the function prints out ??? and exits.

    Hello there, Emily\n???\nHello there, Mark

    Using return values from functions

    We already know that the return values of functions can be stored in variables:

    def my_sum(a, b):\n    return a + b\n\nresult = my_sum(4, 6)\nprint(\"The sum is\", result)

    The sum is 10

    The return value of a function is a value just like any other. It is not necessary to store it in a variable in order to give it as an argument to the print command:

    print(\"The sum is\", my_sum(4, 6))

    The return value of a function can become the argument of another function:

    def my_sum(a, b):\n    return a+b\n\ndef difference(a, b):\n    return a-b\n\nresult = difference(my_sum(5, 2), my_sum(2, 3))\nprint(\"The result is\", result)

    The result is 2

    In this case the inner function calls my_sum(5, 2) and my_sum(2, 3) are executed first. The values they return (7 and 5) are used as the arguments of the outer function call.

    The outer function call difference(7, 5) returns the value 2, which is stored in the variable result and printed out.

    In summary, values returned by functions work exactly like any other value in Python. They can be printed out, stored in variables, used in expressions and used as arguments in other function calls.

    The difference between return and print

    Sometimes the difference between a function returning a value and a print statement within a function can be confusing. Let's have a look at two different ways of implementing a function for working out which of two values is greater:

    def max1(a, b):\n    if a > b:\n        return a\n    else:\n        return b\n\ndef max2(a, b):\n    if a > b:\n        print(a)\n    else:\n        print(b)\n\nresult = max1(3, 5)\nprint(result)\n\nmax2(7, 2)

    5\n7

    Both versions seem to be working just fine, as the maximum values are printed correctly. There is a fundamental difference between the two, however. The first of the two functions, max1, does not print out anything on its own, but instead it returns the value which is greater. If we execute the following line

    max1(3, 5)

    nothing seems to happen. The return value of the function has to be used in some way in the code which called the function. For instance, it can be stored in a variable and printed out:

    result = max1(3, 5)\nprint(result)

    The second version, max2, uses the print command within the function to print out the greater value. If we want to see the value, it is enough to call the function

    max2(7, 5)

    and the greater value is printed out. The downside of this handy function is that the value worked out by the function is not available for use in the program which called it. That is why functions which return values are often the better option.

    Please write a function named greatest_number, which takes three arguments. The function returns the greatest in value of the three.

    An example of how the function is used:

    print(greatest_number(3, 4, 1)) # 4\nprint(greatest_number(99, -4, 7)) # 99\nprint(greatest_number(0, 0, 0)) # 0

    Please write a function named same_chars, which takes one string and two integers as arguments. The integers refer to indexes within the string. The function should return True if the two characters at the indexes specified are the same. Otherwise, and especially if either of the indexes falls outside the scope of the string, the function returns False.

    Some examples of how the function is used:

    # same characters m and m\nprint(same_chars(\"programmer\", 6, 7)) # True\n\n# different characters p and r\nprint(same_chars(\"programmer\", 0, 4)) # False\n\n# the second index is not within the string\nprint(same_chars(\"programmer\", 0, 12)) # False

    Please write three functions: first_word, second_word and last_word. Each function takes a string argument.

    As their names imply, the functions return either the first, the second or the last word in the sentence they receive as their string argument.

    In each case you may assume the argument string contains at least two separate words, and all words are separated by exactly one space character. There will be no spaces in the beginning or at the end of the argument strings.

    sentence = \"it was a dark and stormy python\"\n\nprint(first_word(sentence)) # it\nprint(second_word(sentence)) # was\nprint(last_word(sentence)) # python

    it\nwas\npython

    sentence = \"it was\"\n\nprint(second_word(sentence)) # was\nprint(last_word(sentence)) # was

    The type of the argument

    A quick recap of the data types we've come across so far:

    \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
    TypePython data typeExample
    integerint23
    floating point numberfloat-0.45
    stringstr\"Peter Python\"
    Boolean valueboolTrue

    When you call a function, it will only work correctly if the arguments you give it are of the right type. Let's have a look at an example:

    def print_many_times(message, times):\n    while times > 0:\n        print(message)\n        times -= 1

    The function works as expected if we call it like this:

    print_many_times(\"Hello there\", 5)

    Hello there\nHello there\nHello there\nHello there\nHello there

    However, if we give the function an argument of the wrong type, it will not work:

    print_many_times(\"Hello there\", \"Emily\")

    TypeError: '>' not supported between instances of 'str' and 'int'

    The problem here is that the second parameter times is compared to an integer, 0, on line 2 of the function definition. The argument given was \"Emily\", which is a string, not an integer. Strings and integers cannot be compared so simply, so an error ensues.

    To avoid issues like this you can include type hints in your function definitions. The type hint specifies the type of the argument intended for the function:

    def print_many_times(message : str, times : int):\n    while times > 0:\n        print(message)\n        times -= 1

    This tells anyone using the function that the argument stored in message is supposed to be a string, and the argument stored in times is supposed to be an integer.

    Similarly, the return value of a function can be hinted at in the function definition:

    def ask_for_name() -> str:\n    name = input(\"Mikä on nimesi? \")\n    return name

    This tells the user of the function that the function is supposed to return a string.

    NB: Type hinting is literally just hinting about the type of the argument or the return value. It is not a guarantee of type, and definitely not a safeguard against type errors. If a function receives an argument or returns a value of the wrong type, the function is still executed, but it might not work correctly.

    ","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-4/2-more-functions.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-4/3-lists/page-data.json b/page-data/part-4/3-lists/page-data.json index 617f8331b..100131f0b 100644 --- a/page-data/part-4/3-lists/page-data.json +++ b/page-data/part-4/3-lists/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-4/3-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 know what lists are in Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able access a specified item within a list"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to add items to a list, and how to remove them"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be familiar with built-in list functions and methods"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Thus far in our programs we have stored data with variables, each bit of data usually having its own named variable. This obviously has some limitations, as it can get cumbersome to define separate variables for everything when there is a lot of data to handle."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A Python "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"list"}]},{"type":"text","value":" is a collection of values which is accessed via a single variable name. The contents of the list are written within square brackets. The values contained in the list are called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"items"}]},{"type":"text","value":", or sometimes "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"elements"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following command creates a new, empty 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":"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","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"whereas this command creates a list with five items in it:"}]},{"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":"7"}]},{"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":"2"}]},{"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":"2"}]},{"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":"5"}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"accessing-items-in-a-list","style":"position:relative;"},"children":[{"type":"text","value":"Accessing items in a list"},{"type":"element","tagName":"a","properties":{"href":"#accessing-items-in-a-list","ariaLabel":"accessing items in a list 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 items in a list are indexed in exactly the same way as characters in a string. Indexing starts from zero, and the last index is the length of the list minus 1:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 576px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/b1949d8f0245cf65553e233df717001c/533c1/4_3_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: 26.08695652173913%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAq0lEQVQY032P2QqFQAxD5/9/0Q1FQURFcV9zOYURfbkDIWnTZer2fdc0TYKP4zCgt20z+BwgPs9T/557BzTM82z6vm9d12XwmsdAn2cJHkyM9xnYdZ2yLDPNL5dlsWIK0TTD67o+Ph6f8Bc4zu37XuM4PmiaRsMwmPbsNWAAfeDdxyJXlqXyPFfbtqrrWlVVKU1TJUmiMAwVRZHiOFYQBKbJcYWvwSMHF0WhHw9KgiwilxpXAAAAAElFTkSuQmCC'); 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":["/static/b1949d8f0245cf65553e233df717001c/a0b58/4_3_1.webp 230w","/static/b1949d8f0245cf65553e233df717001c/bc10c/4_3_1.webp 460w","/static/b1949d8f0245cf65553e233df717001c/6264f/4_3_1.webp 576w"],"sizes":"(max-width: 576px) 100vw, 576px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/b1949d8f0245cf65553e233df717001c/81c8e/4_3_1.png 230w","/static/b1949d8f0245cf65553e233df717001c/08a84/4_3_1.png 460w","/static/b1949d8f0245cf65553e233df717001c/533c1/4_3_1.png 576w"],"sizes":"(max-width: 576px) 100vw, 576px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/b1949d8f0245cf65553e233df717001c/533c1/4_3_1.png","alt":"Lists are indexed starting from 0","title":"Lists are indexed starting from 0","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":"A single list item can be accessed just like a single character in a string is accessed, with square brackets:"}]},{"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":"7"}]},{"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":"2"}]},{"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":"2"}]},{"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":"5"}]},{"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":"2"}]},{"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":"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":"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":"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":"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":"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","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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The sum of the first two items:\""}]},{"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":"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":" 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":"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":"7\n2\n5\nThe sum of the first two items: 9"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The entire contents of the list can also be printed out:"}]},{"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":"7"}]},{"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":"2"}]},{"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":"2"}]},{"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":"5"}]},{"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":"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[7, 2, 2, 5, 2]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Unlike strings, lists are mutable, which means their contents can change. You can assign a new value to an item within a list, just like you can assign a new value to a 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_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":"7"}]},{"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":"2"}]},{"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":"2"}]},{"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":"5"}]},{"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":"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":"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":"1"}]},{"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":"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":"text","value":"my_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":"[7, 2, 2, 5, 2]\n[7, 3, 2, 5, 2]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" gives you the number of items in 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":"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":"7"}]},{"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":"2"}]},{"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":"2"}]},{"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":"5"}]},{"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":"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":"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":"my_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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Change the value of an item","tmcname":"part04-12_change_value_of_item"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which initialises a list with the values "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[1, 2, 3, 4, 5]"}]},{"type":"text","value":". Then the program should ask the user for an index and a new value, replace the value at the given index, and print the list again. This should be looped over until the user gives -1 for the index. You can assume all given index values will fall within your list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example execution of the program:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Index: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nNew value: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n[10, 2, 3, 4, 5]\nIndex: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nNew value: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"250"}]},{"type":"text","value":"\n[10, 2, 250, 4, 5]\nIndex: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nNew value: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-45"}]},{"type":"text","value":"\n[10, 2, 250, 4, -45]\nIndex: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]}]}]},{"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 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."}]}]},{"type":"element","tagName":"h2","properties":{"id":"adding-items-to-a-list","style":"position:relative;"},"children":[{"type":"text","value":"Adding items to a list"},{"type":"element","tagName":"a","properties":{"href":"#adding-items-to-a-list","ariaLabel":"adding items to a list 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 "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"append"}]},{"type":"text","value":" method adds items to the end of a list. It works 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":"text","value":"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":"\nnumbers"},{"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":"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":"\nnumbers"},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnumbers"},{"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":"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":"\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":"numbers"},{"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, 10, 3]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following example makes use of two separate 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":"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":"\nshoe_sizes "},{"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\nnumbers"},{"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":"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":"\nnumbers"},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnumbers"},{"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":"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":"\n\nshoe_sizes"},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"37"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nshoe_sizes"},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"44"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nshoe_sizes"},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nshoe_sizes"},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"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":"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"},{"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":"numbers"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Shoe sizes:\""}]},{"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":"shoe_sizes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The item is appended to the list on which the method is called:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Numbers:\n[5, 10, 3]\nShoe sizes:\n[37, 44, 40, 28]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Add items to a list","tmcname":"part04-13_add_items_to_list"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which first asks the user for the number of items to be added. Then the program should ask for the given number of values, one by one, and add them to a list in the order they were typed in. Finally, the list is printed out."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many items: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nItem 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nItem 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"250"}]},{"type":"text","value":"\nItem 3: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"34"}]},{"type":"text","value":"\n[10, 250, 34]"}]}]},{"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 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."}]}]},{"type":"element","tagName":"h2","properties":{"id":"adding-to-a-specific-location","style":"position:relative;"},"children":[{"type":"text","value":"Adding to a specific location"},{"type":"element","tagName":"a","properties":{"href":"#adding-to-a-specific-location","ariaLabel":"adding to a specific location 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":"If you want to specify a location in the list where an item should be added, you can use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"insert"}]},{"type":"text","value":" method. The method adds an item at the specified index. All the items already in the list with an index equal to or higher than the specified index are moved one index further, \"to the right\":"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 627px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/1081c0cb19d0470940fb29238a66544c/e9c9b/4_3_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: 65.65217391304347%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAABVklEQVQ4y62TXW6DMBCEuULP3kP0DD1In/qQSEGJEgTYmBD+J/ocLU1pUV9qaeT14l3P7g5JCEGg6zrVda3b7RbRNI3atlXf99HGN46jWPM8/4D5E/3TsqSbCa/Xq4ZhiKxgis0OU7MNVLcwpCRAIMAmWVVVEbSjLMvF9t4vu90BljRJ01Sn00nn81mXy0W73U7H41HOOWVZFgPzPI9nAi0hwGd+mG+WDFNetEEVRRFZGjg/+7CXhEzQpkiw9WSapniBftEC7thONaYEfIBWxYQEGJ57SUKb3HrRCkpef/9TNmud2eIxyoTZeiW/ifRLqIDSBzVNvUyWUhnc4XCISW0wsN5kaH8GrWxbdNkrhIdETFYksDOAdfIsUMDrD505eY/9Ke9fFcKHQuiin+Fxh91sQwJtdIgGafZ+v4/nEJyKolNVvSvPX1SWb3KO3mXfmJGQOBP4HafX8sdzTtjeAAAAAElFTkSuQmCC'); 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":["/static/1081c0cb19d0470940fb29238a66544c/a0b58/4_3_2.webp 230w","/static/1081c0cb19d0470940fb29238a66544c/bc10c/4_3_2.webp 460w","/static/1081c0cb19d0470940fb29238a66544c/c4111/4_3_2.webp 627w"],"sizes":"(max-width: 627px) 100vw, 627px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/1081c0cb19d0470940fb29238a66544c/81c8e/4_3_2.png 230w","/static/1081c0cb19d0470940fb29238a66544c/08a84/4_3_2.png 460w","/static/1081c0cb19d0470940fb29238a66544c/e9c9b/4_3_2.png 627w"],"sizes":"(max-width: 627px) 100vw, 627px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/1081c0cb19d0470940fb29238a66544c/e9c9b/4_3_2.png","alt":"Inserting an item to a list","title":"Inserting an item to a list","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":"So, for instance this program"}]},{"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":"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"2"}]},{"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":"3"}]},{"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":"4"}]},{"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":"5"}]},{"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":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nnumbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"insert"},{"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","number"]},"children":[{"type":"text","value":"10"}]},{"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":"numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnumbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"insert"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"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":"numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"prints out this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[10, 1, 2, 3, 4, 5, 6]\n[10, 1, 20, 2, 3, 4, 5, 6]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"removing-items-from-a-list","style":"position:relative;"},"children":[{"type":"text","value":"Removing items from a list"},{"type":"element","tagName":"a","properties":{"href":"#removing-items-from-a-list","ariaLabel":"removing items from a list 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":"There are two different approaches to removing an item from a list:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"index"}]},{"type":"text","value":" of the item is known, you can use the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"contents"}]},{"type":"text","value":" of the item are known, you can use the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"remove"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":" takes the index of the item you want to remove as its argument. The following program removes items at indexes 2 and 3 from the list. Notice how the indexes of the remaining items change when one is removed."}]},{"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":"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":" "},{"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":" "},{"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":"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":" "},{"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\nmy_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"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":"text","value":"\nmy_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"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":"\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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 4, 5, 6]\n[1, 2, 4, 6]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It's useful to remember that the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":" also "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"returns"}]},{"type":"text","value":" the removed item:"}]},{"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":"4"}]},{"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":"2"}]},{"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":"7"}]},{"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":"2"}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nnumber "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"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":"number"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"7\n[4, 2, 2, 5]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"remove"}]},{"type":"text","value":", on the other hand, takes the value of the item to be removed as its argument. For example, this program"}]},{"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":"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":" "},{"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":" "},{"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":"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":" "},{"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\nmy_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove"},{"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":"text","value":"\nmy_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove"},{"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"},{"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":"p","properties":{},"children":[{"type":"text","value":"prints out this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 3, 4, 5, 6]\n[1, 3, 4, 6]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method removes the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"first"}]},{"type":"text","value":" occurrence of the value in the list, much like the string function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find"}]},{"type":"text","value":" returns the first occurrence of a 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":"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":"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":" "},{"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":" "},{"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\nmy_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove"},{"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":"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":"\nmy_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove"},{"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":"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[2, 1, 2]\n[2, 2]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Addition and removal","tmcname":"part04-14_addition_and_removal"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to choose between addition and removal. Depending on the choice, the program "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"adds an item to"}]},{"type":"text","value":" or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"removes an item from"}]},{"type":"text","value":" the end of a list. The item that is added must always be one greater than the last item in the list. The first item to be added must be 1."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The list is printed out in the beginning and after each operation. Have a look at the example execution below:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The list is now []\na(d)d, (r)emove or e(x)it: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"d"}]},{"type":"text","value":"\nThe list is now [1]\na(d)d, (r)emove or e(x)it: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"d"}]},{"type":"text","value":"\nThe list is now [1, 2]\na(d)d, (r)emove or e(x)it: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"d"}]},{"type":"text","value":"\nThe list is now [1, 2, 3]\na(d)d, (r)emove or e(x)it: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"r"}]},{"type":"text","value":"\nThe list is now [1, 2]\na(d)d, (r)emove or e(x)it: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"d"}]},{"type":"text","value":"\nThe list is now [1, 2, 3]\na(d)d, (r)emove or e(x)it: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"x"}]},{"type":"text","value":"\nBye!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume that, if the list is empty, there will not be an attempt to remove items."}]},{"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 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."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the specified item is not in the list, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"remove"}]},{"type":"text","value":" function causes an error. Just like with strings, we can check for the presence of an item with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" operator:"}]},{"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":"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":" "},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"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","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The list contains item 1\""}]},{"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":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The list contains item 2\""}]},{"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":"The list contains item 1"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Same word twice","tmcname":"part04-15_same_word_twice"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for words. If the user types in a word for the second time, the program should print out the number of different words typed in, and exit."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"once"}]},{"type":"text","value":"\nWord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"upon"}]},{"type":"text","value":"\nWord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"\nWord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"time"}]},{"type":"text","value":"\nWord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"upon"}]},{"type":"text","value":"\nYou typed in 4 different words"}]}]},{"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 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."}]}]},{"type":"element","tagName":"h2","properties":{"id":"sorting-lists","style":"position:relative;"},"children":[{"type":"text","value":"Sorting lists"},{"type":"element","tagName":"a","properties":{"href":"#sorting-lists","ariaLabel":"sorting lists 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 items in a list can be "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" from smallest to greatest with the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":"."}]},{"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":"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":"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":"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":"text","value":"\nmy_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"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":"my_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":"[1, 2, 2, 4, 5]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice how the method modifies the list itself. Sometimes we don't want to change the original list, so we use the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" instead. It "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"returns"}]},{"type":"text","value":" a sorted 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":"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":"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":"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":"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":"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","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"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","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":"[1, 2, 2, 4, 5]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Remember the difference between the two: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":" changes the order of the original list in place, whereas "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" creates a new, ordered copy of the list. With "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" we can preserve the original order of the 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":"original "},{"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":"2"}]},{"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":"5"}]},{"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":"1"}]},{"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":"2"}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nin_order "},{"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":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"original"},{"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":"original"},{"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":"in_order"},{"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":"[2, 5, 1, 2, 4]\n[1, 2, 2, 4, 5]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"List twice","tmcname":"part04-16_list_twice"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in values and adds them to a list. After each addition, the list is printed out in two different ways:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"in the order the items were added"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"ordered from smallest to greatest"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program exits when the user types in 0."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"New item: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nThe list now: [3]\nThe list in order: [3]\nNew item: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nThe list now: [3, 1]\nThe list in order: [1, 3]\nNew item: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9"}]},{"type":"text","value":"\nThe list now: [3, 1, 9]\nThe list in order: [1, 3, 9]\nNew item: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nThe list now: [3, 1, 9, 5]\nThe list in order: [1, 3, 5, 9]\nNew item: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nBye!"}]}]},{"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 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."}]}]},{"type":"element","tagName":"h2","properties":{"id":"maximum-minimum-and-sum","style":"position:relative;"},"children":[{"type":"text","value":"Maximum, minimum and sum"},{"type":"element","tagName":"a","properties":{"href":"#maximum-minimum-and-sum","ariaLabel":"maximum minimum and sum 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 functions "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"max"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"min"}]},{"type":"text","value":", short for maximum and minimum, return the greatest and smallest item in a list, respectively. The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" returns the sum of all items in 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":"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":"5"}]},{"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":"2"}]},{"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":"3"}]},{"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":"1"}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\ngreatest "},{"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":"max"}]},{"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":"\nsmallest "},{"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":"min"}]},{"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":"\nlist_sum "},{"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":"my_list"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Smallest:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" smallest"},{"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":"\"Greatest:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" greatest"},{"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":"\"Sum:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" list_sum"},{"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":"Smallest: 1\nGreatest: 5\nSum: 15"}]}]},{"type":"element","tagName":"h2","properties":{"id":"methods-vs-functions","style":"position:relative;"},"children":[{"type":"text","value":"Methods vs functions"},{"type":"element","tagName":"a","properties":{"href":"#methods-vs-functions","ariaLabel":"methods vs 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":"There are two different ways of processing lists in Python, which can get confusing. For the most part you will use list "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"methods"}]},{"type":"text","value":", such as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"append"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":". They are used with the dot "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" operator on the list 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_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","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# method calls"}]},{"type":"text","value":"\nmy_list"},{"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":"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":"text","value":"append"},{"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":"text","value":"\nmy_list"},{"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":"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":"text","value":"\nmy_list"},{"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":"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# another method call"}]},{"type":"text","value":"\nmy_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"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":"Some "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"functions"}]},{"type":"text","value":" are happy to take a list as an argument. Above we saw the functions "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"max"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"min"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" do just that:"}]},{"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":"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":" "},{"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":"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# function calls take the list as an argument"}]},{"type":"text","value":"\ngreatest "},{"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":"max"}]},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsmallest "},{"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":"min"}]},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlength "},{"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":"my_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\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":"\"Smallest:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" smallest"},{"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":"\"Greatest:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" greatest"},{"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":"\"Length of the list:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" length"},{"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":"# another function call "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the list itself is an argument, the function returns a sorted copy"}]},{"type":"text","value":"\nin_order "},{"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":"sorted"}]},{"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","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":"in_order"},{"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":"Smallest: 1\nGreatest: 7\nLength of the list: 4\n[1, 2, 3, 7]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"a-list-as-an-argument-or-a-return-value","style":"position:relative;"},"children":[{"type":"text","value":"A list as an argument or a return value"},{"type":"element","tagName":"a","properties":{"href":"#a-list-as-an-argument-or-a-return-value","ariaLabel":"a list as an argument or a return value 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":"Just like the built-in functions above, our own functions can also take a list as an argument and produce a list as a return value. The following function works out the central value in an ordered list, also called the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"median"}]},{"type":"text","value":" value:"}]},{"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":"median"}]},{"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":" "},{"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 ordered "},{"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":"sorted"}]},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n list_centre "},{"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":"ordered"},{"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":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" ordered"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"list_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function creates an ordered version of the list given as an argument and returns the item in the very middle. Notice the integer division operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"//"}]},{"type":"text","value":" used here. The index of a list should always be an integer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function works 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":"text","value":"shoe_sizes "},{"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":"45"}]},{"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":"44"}]},{"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":"36"}]},{"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":"39"}]},{"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":"40"}]},{"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 median of the shoe sizes is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" median"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"shoe_sizes"},{"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\nages "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"56"}]},{"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":"34"}]},{"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":"22"}]},{"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":"5"}]},{"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":"77"}]},{"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":"5"}]},{"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 median of the ages is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" median"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ages"},{"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":"The median of the shoe sizes is 40\nThe median of the ages is 22"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A function can also return a list. The following function asks the user to type in integers and returns the input as 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":"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_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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n user_input "},{"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 an integer, leave empty to exit: \""}]},{"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":"if"}]},{"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":"user_input"},{"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":"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":"break"}]},{"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":"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":"text","value":"user_input"},{"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":" numbers"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function makes use of a helper variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"numbers"}]},{"type":"text","value":", which is a list. All the numbers typed in by the user are added to the list. When the loop is exited from, the function returns the list with the statement "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return numbers"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Calling the function 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":"text","value":"numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" input_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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The greatest number is\""}]},{"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":"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":"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 median of the numbers is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" median"},{"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":"p","properties":{},"children":[{"type":"text","value":"could print this, for example:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in an integer, leave empty to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nPlease type in an integer, leave empty to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-22"}]},{"type":"text","value":"\nPlease type in an integer, leave empty to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nPlease type in an integer, leave empty to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"35"}]},{"type":"text","value":"\nPlease type in an integer, leave empty to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nPlease type in an integer, leave empty to exit:\nThe greatest number is 35\nThe median of the numbers is 4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This small example demonstrates one of the most important uses of functions: they can help you divide your code into smaller, easily understandable and logical wholes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Of course the same functionality could be achieved without writing any of our own 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":"text","value":"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"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n user_input "},{"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 an integer, leave empty to exit: \""}]},{"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":"if"}]},{"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":"user_input"},{"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":"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":"break"}]},{"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":"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":"text","value":"user_input"},{"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\nordered "},{"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":"sorted"}]},{"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":"\nlist_centre "},{"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":"ordered"},{"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":"2"}]},{"type":"text","value":"\nmedian "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ordered"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"list_centre"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The greatest number is\""}]},{"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":"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":"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 median of the numbers is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" median"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this version, following the programming logic is more difficult, as it is no longer clear which commands are a part of which functionality. The code fulfils the same purposes - reading in input, calculating the median value, and so on - but the structure is less clear."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Organising your code into separate functions will improve you program's readability, but also make it easier to handle logical wholes. This in turn helps you verify that the program works as intended, as each function can be tested separately."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Another important use for functions is making code "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reusable"}]},{"type":"text","value":". If you need to achieve some functionality multiple times in your program, it is a good idea to create your own function and name it appropriately:"}]},{"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":"\"Shoe sizes:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nshoe_sizes "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" input_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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Weights:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nweights "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" input_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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Heights:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nheights "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" input_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":"programming-exercise","properties":{"name":"The length of a list","tmcname":"part04-17_length_of_list"},"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":"length"}]},{"type":"text","value":" which takes a list as its argument and returns the length of the 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":"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":"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":" "},{"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":" "},{"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":"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":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" length"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The length is\""}]},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the list given as an argument doesn't need to be stored in any variable"}]},{"type":"text","value":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" length"},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"1"}]},{"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":"1"}]},{"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":"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":"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 length is\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The length is 5\nThe length is 4"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Arithmetic mean","tmcname":"part04-18_mean"},"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":"mean"}]},{"type":"text","value":", which takes a list of integers as an argument. The function returns the arithmetic mean of the values in the 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":"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":"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":" "},{"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":" "},{"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":"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":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" mean"},{"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","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":"\"mean value is\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"mean value is 3.0"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The range of a list","tmcname":"part04-19_range_of_list"},"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":"range_of_list"}]},{"type":"text","value":", which takes a list of integers as an argument. The function returns the difference between the smallest and the largest value in the 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":"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":"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":" "},{"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":" "},{"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":"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":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" range_of_list"},{"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","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 range of the list is\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The range of the list is 4"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are many more ways to use lists in Python. The Python "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/tutorial/datastructures.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"documentation"}]},{"type":"text","value":" is a good place to start if you want to know more."}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will know what lists are in Python
    • \n
    • You will be able access a specified item within a list
    • \n
    • You will know how to add items to a list, and how to remove them
    • \n
    • You will be familiar with built-in list functions and methods
    • \n

    Thus far in our programs we have stored data with variables, each bit of data usually having its own named variable. This obviously has some limitations, as it can get cumbersome to define separate variables for everything when there is a lot of data to handle.

    A Python list is a collection of values which is accessed via a single variable name. The contents of the list are written within square brackets. The values contained in the list are called items, or sometimes elements.

    The following command creates a new, empty list

    my_list = []

    whereas this command creates a list with five items in it:

    my_list = [7, 2, 2, 5, 2]

    Accessing items in a list

    The items in a list are indexed in exactly the same way as characters in a string. Indexing starts from zero, and the last index is the length of the list minus 1:

    \n \n \n \n \n \n \"Lists\n \n \n

    A single list item can be accessed just like a single character in a string is accessed, with square brackets:

    my_list = [7, 2, 2, 5, 2]\n\nprint(my_list[0])\nprint(my_list[1])\nprint(my_list[3])\n\nprint(\"The sum of the first two items:\", my_list[0] + my_list[1])

    7\n2\n5\nThe sum of the first two items: 9

    The entire contents of the list can also be printed out:

    my_list = [7, 2, 2, 5, 2]\nprint(my_list)

    [7, 2, 2, 5, 2]

    Unlike strings, lists are mutable, which means their contents can change. You can assign a new value to an item within a list, just like you can assign a new value to a variable:

    my_list = [7, 2, 2, 5, 2]\nprint(my_list)\nmy_list[1] = 3\nprint(my_list)

    [7, 2, 2, 5, 2]\n[7, 3, 2, 5, 2]

    The function len gives you the number of items in a list:

    my_list = [7, 2, 2, 5, 2]\nprint(len(my_list))

    5

    Please write a program which initialises a list with the values [1, 2, 3, 4, 5]. Then the program should ask the user for an index and a new value, replace the value at the given index, and print the list again. This should be looped over until the user gives -1 for the index. You can assume all given index values will fall within your list.

    An example execution of the program:

    Index: 0\nNew value: 10\n[10, 2, 3, 4, 5]\nIndex: 2\nNew value: 250\n[10, 2, 250, 4, 5]\nIndex: 4\nNew value: -45\n[10, 2, 250, 4, -45]\nIndex: -1

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    Adding items to a list

    The append method adds items to the end of a list. It works like this:

    numbers = []\nnumbers.append(5)\nnumbers.append(10)\nnumbers.append(3)\nprint(numbers)

    [5, 10, 3]

    The following example makes use of two separate lists:

    numbers = []\nshoe_sizes = []\n\nnumbers.append(5)\nnumbers.append(10)\nnumbers.append(3)\n\nshoe_sizes.append(37)\nshoe_sizes.append(44)\nshoe_sizes.append(40)\nshoe_sizes.append(28)\n\nprint(\"Numbers:\")\nprint(numbers)\n\nprint(\"Shoe sizes:\")\nprint(shoe_sizes)

    The item is appended to the list on which the method is called:

    Numbers:\n[5, 10, 3]\nShoe sizes:\n[37, 44, 40, 28]

    Please write a program which first asks the user for the number of items to be added. Then the program should ask for the given number of values, one by one, and add them to a list in the order they were typed in. Finally, the list is printed out.

    An example of expected behaviour:

    How many items: 3\nItem 1: 10\nItem 2: 250\nItem 3: 34\n[10, 250, 34]

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    Adding to a specific location

    If you want to specify a location in the list where an item should be added, you can use the insert method. The method adds an item at the specified index. All the items already in the list with an index equal to or higher than the specified index are moved one index further, \"to the right\":

    \n \n \n \n \n \n \"Inserting\n \n \n

    So, for instance this program

    numbers = [1, 2, 3, 4, 5, 6]\nnumbers.insert(0, 10)\nprint(numbers)\nnumbers.insert(2, 20)\nprint(numbers)

    prints out this:

    [10, 1, 2, 3, 4, 5, 6]\n[10, 1, 20, 2, 3, 4, 5, 6]

    Removing items from a list

    There are two different approaches to removing an item from a list:

      \n
    • If the index of the item is known, you can use the method pop.
    • \n
    • If the contents of the item are known, you can use the method remove.
    • \n

    So, the method pop takes the index of the item you want to remove as its argument. The following program removes items at indexes 2 and 3 from the list. Notice how the indexes of the remaining items change when one is removed.

    my_list = [1, 2, 3, 4, 5, 6]\n\nmy_list.pop(2)\nprint(my_list)\nmy_list.pop(3)\nprint(my_list)

    [1, 2, 4, 5, 6]\n[1, 2, 4, 6]

    It's useful to remember that the method pop also returns the removed item:

    my_list = [4, 2, 7, 2, 5]\n\nnumber = my_list.pop(2)\nprint(number)\nprint(my_list)

    7\n[4, 2, 2, 5]

    The method remove, on the other hand, takes the value of the item to be removed as its argument. For example, this program

    my_list = [1, 2, 3, 4, 5, 6]\n\nmy_list.remove(2)\nprint(my_list)\nmy_list.remove(5)\nprint(my_list)

    prints out this:

    [1, 3, 4, 5, 6]\n[1, 3, 4, 6]

    The method removes the first occurrence of the value in the list, much like the string function find returns the first occurrence of a substring:

    my_list = [1, 2, 1, 2]\n\nmy_list.remove(1)\nprint(my_list)\nmy_list.remove(1)\nprint(my_list)

    [2, 1, 2]\n[2, 2]

    Please write a program which asks the user to choose between addition and removal. Depending on the choice, the program adds an item to or removes an item from the end of a list. The item that is added must always be one greater than the last item in the list. The first item to be added must be 1.

    The list is printed out in the beginning and after each operation. Have a look at the example execution below:

    The list is now []\na(d)d, (r)emove or e(x)it: d\nThe list is now [1]\na(d)d, (r)emove or e(x)it: d\nThe list is now [1, 2]\na(d)d, (r)emove or e(x)it: d\nThe list is now [1, 2, 3]\na(d)d, (r)emove or e(x)it: r\nThe list is now [1, 2]\na(d)d, (r)emove or e(x)it: d\nThe list is now [1, 2, 3]\na(d)d, (r)emove or e(x)it: x\nBye!

    You may assume that, if the list is empty, there will not be an attempt to remove items.

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    If the specified item is not in the list, the remove function causes an error. Just like with strings, we can check for the presence of an item with the in operator:

    my_list = [1, 3, 4]\n\nif 1 in my_list:\n    print(\"The list contains item 1\")\n\nif 2 in my_list:\n    print(\"The list contains item 2\")

    The list contains item 1

    Please write a program which asks the user for words. If the user types in a word for the second time, the program should print out the number of different words typed in, and exit.

    Word: once\nWord: upon\nWord: a\nWord: time\nWord: upon\nYou typed in 4 different words

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    Sorting lists

    The items in a list can be sorted from smallest to greatest with the method sort.

    my_list = [2,5,1,2,4]\nmy_list.sort()\nprint(my_list)

    [1, 2, 2, 4, 5]

    Notice how the method modifies the list itself. Sometimes we don't want to change the original list, so we use the function sorted instead. It returns a sorted list:

    my_list = [2,5,1,2,4]\nprint(sorted(my_list)))

    [1, 2, 2, 4, 5]

    Remember the difference between the two: sort changes the order of the original list in place, whereas sorted creates a new, ordered copy of the list. With sorted we can preserve the original order of the list:

    original = [2, 5, 1, 2, 4]\nin_order = sorted(original)\nprint(original)\nprint(in_order)

    [2, 5, 1, 2, 4]\n[1, 2, 2, 4, 5]

    Please write a program which asks the user to type in values and adds them to a list. After each addition, the list is printed out in two different ways:

      \n
    • in the order the items were added
    • \n
    • ordered from smallest to greatest
    • \n

    The program exits when the user types in 0.

    An example of expected behaviour:

    New item: 3\nThe list now: [3]\nThe list in order: [3]\nNew item: 1\nThe list now: [3, 1]\nThe list in order: [1, 3]\nNew item: 9\nThe list now: [3, 1, 9]\nThe list in order: [1, 3, 9]\nNew item: 5\nThe list now: [3, 1, 9, 5]\nThe list in order: [1, 3, 5, 9]\nNew item: 0\nBye!

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    Maximum, minimum and sum

    The functions max and min, short for maximum and minimum, return the greatest and smallest item in a list, respectively. The function sum returns the sum of all items in a list.

    my_list = [5, 2, 3, 1, 4]\n\ngreatest = max(my_list)\nsmallest = min(my_list)\nlist_sum = sum(my_list)\n\nprint(\"Smallest:\", smallest)\nprint(\"Greatest:\", greatest)\nprint(\"Sum:\", list_sum)

    Smallest: 1\nGreatest: 5\nSum: 15

    Methods vs functions

    There are two different ways of processing lists in Python, which can get confusing. For the most part you will use list methods, such as append and sort. They are used with the dot . operator on the list variable:

    my_list = []\n\n# method calls\nmy_list.append(3)\nmy_list.append(1)\nmy_list.append(7)\nmy_list.append(2)\n\n# another method call\nmy_list.sort()

    Some functions are happy to take a list as an argument. Above we saw the functions max, min, len and sorted do just that:

    my_list = [3, 2, 7, 1]\n\n# function calls take the list as an argument\ngreatest = max(my_list))\nsmallest = min(my_list))\nlength = len(my_list))\n\nprint(\"Smallest:\", smallest)\nprint(\"Greatest:\", greatest)\nprint(\"Length of the list:\", length)\n\n# another function call \n# the list itself is an argument, the function returns a sorted copy\nin_order = sorted(my_list))\nprint(in_order)

    Smallest: 1\nGreatest: 7\nLength of the list: 4\n[1, 2, 3, 7]

    A list as an argument or a return value

    Just like the built-in functions above, our own functions can also take a list as an argument and produce a list as a return value. The following function works out the central value in an ordered list, also called the median value:

    def median(my_list: list):\n    ordered = sorted(my_list))\n    list_centre = len(ordered) // 2\n    return ordered[list_centre]

    The function creates an ordered version of the list given as an argument and returns the item in the very middle. Notice the integer division operator // used here. The index of a list should always be an integer.

    The function works like this:

    shoe_sizes = [45, 44, 36, 39, 40]\nprint(\"The median of the shoe sizes is\", median(shoe_sizes))\n\nages = [1, 56, 34, 22, 5, 77, 5]\nprint(\"The median of the ages is\", median(ages))

    The median of the shoe sizes is 40\nThe median of the ages is 22

    A function can also return a list. The following function asks the user to type in integers and returns the input as a list:

    def input_numbers():\n    numbers = []\n    while True:\n        user_input = input(\"Please type in an integer, leave empty to exit: \")\n        if len(user_input) == 0:\n            break\n        numbers.append(int(user_input))\n    return numbers

    The function makes use of a helper variable numbers, which is a list. All the numbers typed in by the user are added to the list. When the loop is exited from, the function returns the list with the statement return numbers.

    Calling the function like this

    numbers = input_numbers()\n\nprint(\"The greatest number is\", max(numbers))\nprint(\"The median of the numbers is\", median(numbers))

    could print this, for example:

    Please type in an integer, leave empty to exit: 5\nPlease type in an integer, leave empty to exit: -22\nPlease type in an integer, leave empty to exit: 4\nPlease type in an integer, leave empty to exit: 35\nPlease type in an integer, leave empty to exit: 1\nPlease type in an integer, leave empty to exit:\nThe greatest number is 35\nThe median of the numbers is 4

    This small example demonstrates one of the most important uses of functions: they can help you divide your code into smaller, easily understandable and logical wholes.

    Of course the same functionality could be achieved without writing any of our own functions:

    numbers = []\nwhile True:\n    user_input = input(\"Please type in an integer, leave empty to exit: \")\n    if len(user_input) == 0:\n        break\n    numbers.append(int(user_input))\n\nordered = sorted(numbers)\nlist_centre = len(ordered) // 2\nmedian = ordered[list_centre]\n\nprint(\"The greatest number is\", max(numbers))\nprint(\"The median of the numbers is\", median)

    In this version, following the programming logic is more difficult, as it is no longer clear which commands are a part of which functionality. The code fulfils the same purposes - reading in input, calculating the median value, and so on - but the structure is less clear.

    Organising your code into separate functions will improve you program's readability, but also make it easier to handle logical wholes. This in turn helps you verify that the program works as intended, as each function can be tested separately.

    Another important use for functions is making code reusable. If you need to achieve some functionality multiple times in your program, it is a good idea to create your own function and name it appropriately:

    print(\"Shoe sizes:\")\nshoe_sizes = input_numbers()\n\nprint(\"Weights:\")\nweights = input_numbers()\n\nprint(\"Heights:\")\nheights = input_numbers()

    Please write a function named length which takes a list as its argument and returns the length of the list.

    my_list = [1, 2, 3, 4, 5]\nresult = length(my_list)\nprint(\"The length is\", result)\n\n# the list given as an argument doesn't need to be stored in any variable\nresult = length([1, 1, 1, 1])\nprint(\"The length is\", result)

    The length is 5\nThe length is 4

    Please write a function named mean, which takes a list of integers as an argument. The function returns the arithmetic mean of the values in the list.

    my_list = [1, 2, 3, 4, 5]\nresult = mean(my_list))\nprint(\"mean value is\", result)

    mean value is 3.0

    Please write a function named range_of_list, which takes a list of integers as an argument. The function returns the difference between the smallest and the largest value in the list.

    my_list = [1, 2, 3, 4, 5]\nresult = range_of_list(my_list))\nprint(\"The range of the list is\", result)

    The range of the list is 4

    There are many more ways to use lists in Python. The Python documentation is a good place to start if you want to know more.

    ","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-4/3-lists.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know what lists are in Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able access a specified item within a list"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to add items to a list, and how to remove them"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be familiar with built-in list functions and methods"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Thus far in our programs we have stored data with variables, each bit of data usually having its own named variable. This obviously has some limitations, as it can get cumbersome to define separate variables for everything when there is a lot of data to handle."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A Python "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"list"}]},{"type":"text","value":" is a collection of values which is accessed via a single variable name. The contents of the list are written within square brackets. The values contained in the list are called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"items"}]},{"type":"text","value":", or sometimes "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"elements"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following command creates a new, empty 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":"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","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"whereas this command creates a list with five items in it:"}]},{"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":"7"}]},{"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":"2"}]},{"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":"2"}]},{"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":"5"}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"accessing-items-in-a-list","style":"position:relative;"},"children":[{"type":"text","value":"Accessing items in a list"},{"type":"element","tagName":"a","properties":{"href":"#accessing-items-in-a-list","ariaLabel":"accessing items in a list 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 items in a list are indexed in exactly the same way as characters in a string. Indexing starts from zero, and the last index is the length of the list minus 1:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 576px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/b1949d8f0245cf65553e233df717001c/533c1/4_3_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: 26.08695652173913%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAAAsTAAALEwEAmpwYAAAAq0lEQVQY032P2QqFQAxD5/9/0Q1FQURFcV9zOYURfbkDIWnTZer2fdc0TYKP4zCgt20z+BwgPs9T/557BzTM82z6vm9d12XwmsdAn2cJHkyM9xnYdZ2yLDPNL5dlsWIK0TTD67o+Ph6f8Bc4zu37XuM4PmiaRsMwmPbsNWAAfeDdxyJXlqXyPFfbtqrrWlVVKU1TJUmiMAwVRZHiOFYQBKbJcYWvwSMHF0WhHw9KgiwilxpXAAAAAElFTkSuQmCC'); 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":["/static/b1949d8f0245cf65553e233df717001c/a0b58/4_3_1.webp 230w","/static/b1949d8f0245cf65553e233df717001c/bc10c/4_3_1.webp 460w","/static/b1949d8f0245cf65553e233df717001c/6264f/4_3_1.webp 576w"],"sizes":"(max-width: 576px) 100vw, 576px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/b1949d8f0245cf65553e233df717001c/81c8e/4_3_1.png 230w","/static/b1949d8f0245cf65553e233df717001c/08a84/4_3_1.png 460w","/static/b1949d8f0245cf65553e233df717001c/533c1/4_3_1.png 576w"],"sizes":"(max-width: 576px) 100vw, 576px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/b1949d8f0245cf65553e233df717001c/533c1/4_3_1.png","alt":"Lists are indexed starting from 0","title":"Lists are indexed starting from 0","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":"A single list item can be accessed just like a single character in a string is accessed, with square brackets:"}]},{"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":"7"}]},{"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":"2"}]},{"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":"2"}]},{"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":"5"}]},{"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":"2"}]},{"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":"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":"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":"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":"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":"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","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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The sum of the first two items:\""}]},{"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":"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":" 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":"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":"7\n2\n5\nThe sum of the first two items: 9"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The entire contents of the list can also be printed out:"}]},{"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":"7"}]},{"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":"2"}]},{"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":"2"}]},{"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":"5"}]},{"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":"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[7, 2, 2, 5, 2]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Unlike strings, lists are mutable, which means their contents can change. You can assign a new value to an item within a list, just like you can assign a new value to a 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_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":"7"}]},{"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":"2"}]},{"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":"2"}]},{"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":"5"}]},{"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":"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":"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":"1"}]},{"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":"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":"text","value":"my_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":"[7, 2, 2, 5, 2]\n[7, 3, 2, 5, 2]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" gives you the number of items in 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":"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":"7"}]},{"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":"2"}]},{"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":"2"}]},{"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":"5"}]},{"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":"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":"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":"my_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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Change the value of an item","tmcname":"part04-12_change_value_of_item"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which initialises a list with the values "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[1, 2, 3, 4, 5]"}]},{"type":"text","value":". Then the program should ask the user for an index and a new value, replace the value at the given index, and print the list again. This should be looped over until the user gives -1 for the index. You can assume all given index values will fall within your list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example execution of the program:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Index: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nNew value: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\n[10, 2, 3, 4, 5]\nIndex: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nNew value: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"250"}]},{"type":"text","value":"\n[10, 2, 250, 4, 5]\nIndex: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nNew value: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-45"}]},{"type":"text","value":"\n[10, 2, 250, 4, -45]\nIndex: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]}]}]},{"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 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."}]}]},{"type":"element","tagName":"h2","properties":{"id":"adding-items-to-a-list","style":"position:relative;"},"children":[{"type":"text","value":"Adding items to a list"},{"type":"element","tagName":"a","properties":{"href":"#adding-items-to-a-list","ariaLabel":"adding items to a list 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 "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"append"}]},{"type":"text","value":" method adds items to the end of a list. It works 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":"text","value":"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":"\nnumbers"},{"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":"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":"\nnumbers"},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnumbers"},{"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":"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":"\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":"numbers"},{"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, 10, 3]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following example makes use of two separate 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":"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":"\nshoe_sizes "},{"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\nnumbers"},{"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":"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":"\nnumbers"},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnumbers"},{"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":"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":"\n\nshoe_sizes"},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"37"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nshoe_sizes"},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"44"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nshoe_sizes"},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nshoe_sizes"},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"28"}]},{"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":"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"},{"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":"numbers"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Shoe sizes:\""}]},{"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":"shoe_sizes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The item is appended to the list on which the method is called:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Numbers:\n[5, 10, 3]\nShoe sizes:\n[37, 44, 40, 28]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Add items to a list","tmcname":"part04-13_add_items_to_list"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which first asks the user for the number of items to be added. Then the program should ask for the given number of values, one by one, and add them to a list in the order they were typed in. Finally, the list is printed out."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"How many items: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nItem 1: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nItem 2: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"250"}]},{"type":"text","value":"\nItem 3: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"34"}]},{"type":"text","value":"\n[10, 250, 34]"}]}]},{"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 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."}]}]},{"type":"element","tagName":"h2","properties":{"id":"adding-to-a-specific-location","style":"position:relative;"},"children":[{"type":"text","value":"Adding to a specific location"},{"type":"element","tagName":"a","properties":{"href":"#adding-to-a-specific-location","ariaLabel":"adding to a specific location 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":"If you want to specify a location in the list where an item should be added, you can use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"insert"}]},{"type":"text","value":" method. The method adds an item at the specified index. All the items already in the list with an index equal to or higher than the specified index are moved one index further, \"to the right\":"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 627px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/1081c0cb19d0470940fb29238a66544c/e9c9b/4_3_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: 65.65217391304347%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAANCAYAAACpUE5eAAAACXBIWXMAAAsTAAALEwEAmpwYAAABVklEQVQ4y62TXW6DMBCEuULP3kP0DD1In/qQSEGJEgTYmBD+J/ocLU1pUV9qaeT14l3P7g5JCEGg6zrVda3b7RbRNI3atlXf99HGN46jWPM8/4D5E/3TsqSbCa/Xq4ZhiKxgis0OU7MNVLcwpCRAIMAmWVVVEbSjLMvF9t4vu90BljRJ01Sn00nn81mXy0W73U7H41HOOWVZFgPzPI9nAi0hwGd+mG+WDFNetEEVRRFZGjg/+7CXhEzQpkiw9WSapniBftEC7thONaYEfIBWxYQEGJ57SUKb3HrRCkpef/9TNmud2eIxyoTZeiW/ifRLqIDSBzVNvUyWUhnc4XCISW0wsN5kaH8GrWxbdNkrhIdETFYksDOAdfIsUMDrD505eY/9Ke9fFcKHQuiin+Fxh91sQwJtdIgGafZ+v4/nEJyKolNVvSvPX1SWb3KO3mXfmJGQOBP4HafX8sdzTtjeAAAAAElFTkSuQmCC'); 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":["/static/1081c0cb19d0470940fb29238a66544c/a0b58/4_3_2.webp 230w","/static/1081c0cb19d0470940fb29238a66544c/bc10c/4_3_2.webp 460w","/static/1081c0cb19d0470940fb29238a66544c/c4111/4_3_2.webp 627w"],"sizes":"(max-width: 627px) 100vw, 627px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/1081c0cb19d0470940fb29238a66544c/81c8e/4_3_2.png 230w","/static/1081c0cb19d0470940fb29238a66544c/08a84/4_3_2.png 460w","/static/1081c0cb19d0470940fb29238a66544c/e9c9b/4_3_2.png 627w"],"sizes":"(max-width: 627px) 100vw, 627px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/1081c0cb19d0470940fb29238a66544c/e9c9b/4_3_2.png","alt":"Inserting an item to a list","title":"Inserting an item to a list","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":"So, for instance this program"}]},{"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":"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"2"}]},{"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":"3"}]},{"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":"4"}]},{"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":"5"}]},{"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":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nnumbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"insert"},{"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","number"]},"children":[{"type":"text","value":"10"}]},{"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":"numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnumbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"insert"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"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":"numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"prints out this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[10, 1, 2, 3, 4, 5, 6]\n[10, 1, 20, 2, 3, 4, 5, 6]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"removing-items-from-a-list","style":"position:relative;"},"children":[{"type":"text","value":"Removing items from a list"},{"type":"element","tagName":"a","properties":{"href":"#removing-items-from-a-list","ariaLabel":"removing items from a list 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":"There are two different approaches to removing an item from a list:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"index"}]},{"type":"text","value":" of the item is known, you can use the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"contents"}]},{"type":"text","value":" of the item are known, you can use the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"remove"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":" takes the index of the item you want to remove as its argument. The following program removes items at indexes 2 and 3 from the list. Notice how the indexes of the remaining items change when one is removed."}]},{"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":"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":" "},{"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":" "},{"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":"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":" "},{"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\nmy_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"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":"text","value":"\nmy_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"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":"\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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 4, 5, 6]\n[1, 2, 4, 6]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It's useful to remember that the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":" also "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"returns"}]},{"type":"text","value":" the removed item:"}]},{"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":"4"}]},{"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":"2"}]},{"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":"7"}]},{"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":"2"}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nnumber "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"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":"number"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"7\n[4, 2, 2, 5]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"remove"}]},{"type":"text","value":", on the other hand, takes the value of the item to be removed as its argument. For example, this program"}]},{"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":"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":" "},{"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":" "},{"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":"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":" "},{"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\nmy_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove"},{"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":"text","value":"\nmy_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove"},{"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"},{"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":"p","properties":{},"children":[{"type":"text","value":"prints out this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 3, 4, 5, 6]\n[1, 3, 4, 6]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method removes the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"first"}]},{"type":"text","value":" occurrence of the value in the list, much like the string function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"find"}]},{"type":"text","value":" returns the first occurrence of a 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":"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":"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":" "},{"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":" "},{"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\nmy_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove"},{"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":"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":"\nmy_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove"},{"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":"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[2, 1, 2]\n[2, 2]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Addition and removal","tmcname":"part04-14_addition_and_removal"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to choose between addition and removal. Depending on the choice, the program "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"adds an item to"}]},{"type":"text","value":" or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"removes an item from"}]},{"type":"text","value":" the end of a list. The item that is added must always be one greater than the last item in the list. The first item to be added must be 1."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The list is printed out in the beginning and after each operation. Have a look at the example execution below:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The list is now []\na(d)d, (r)emove or e(x)it: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"d"}]},{"type":"text","value":"\nThe list is now [1]\na(d)d, (r)emove or e(x)it: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"d"}]},{"type":"text","value":"\nThe list is now [1, 2]\na(d)d, (r)emove or e(x)it: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"d"}]},{"type":"text","value":"\nThe list is now [1, 2, 3]\na(d)d, (r)emove or e(x)it: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"r"}]},{"type":"text","value":"\nThe list is now [1, 2]\na(d)d, (r)emove or e(x)it: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"d"}]},{"type":"text","value":"\nThe list is now [1, 2, 3]\na(d)d, (r)emove or e(x)it: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"x"}]},{"type":"text","value":"\nBye!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume that, if the list is empty, there will not be an attempt to remove items."}]},{"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 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."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the specified item is not in the list, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"remove"}]},{"type":"text","value":" function causes an error. Just like with strings, we can check for the presence of an item with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" operator:"}]},{"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":"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":" "},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"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","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The list contains item 1\""}]},{"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":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The list contains item 2\""}]},{"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":"The list contains item 1"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Same word twice","tmcname":"part04-15_same_word_twice"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for words. If the user types in a word for the second time, the program should print out the number of different words typed in, and exit."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"once"}]},{"type":"text","value":"\nWord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"upon"}]},{"type":"text","value":"\nWord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"\nWord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"time"}]},{"type":"text","value":"\nWord: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"upon"}]},{"type":"text","value":"\nYou typed in 4 different words"}]}]},{"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 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."}]}]},{"type":"element","tagName":"h2","properties":{"id":"sorting-lists","style":"position:relative;"},"children":[{"type":"text","value":"Sorting lists"},{"type":"element","tagName":"a","properties":{"href":"#sorting-lists","ariaLabel":"sorting lists 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 items in a list can be "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" from smallest to greatest with the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":"."}]},{"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":"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":"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":"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":"text","value":"\nmy_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"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":"my_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":"[1, 2, 2, 4, 5]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice how the method modifies the list itself. Sometimes we don't want to change the original list, so we use the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" instead. It "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"returns"}]},{"type":"text","value":" a sorted 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":"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":"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":"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":"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":"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","builtin"]},"children":[{"type":"text","value":"sorted"}]},{"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","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":"[1, 2, 2, 4, 5]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Remember the difference between the two: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":" changes the order of the original list in place, whereas "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" creates a new, ordered copy of the list. With "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" we can preserve the original order of the 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":"original "},{"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":"2"}]},{"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":"5"}]},{"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":"1"}]},{"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":"2"}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nin_order "},{"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":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"original"},{"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":"original"},{"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":"in_order"},{"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":"[2, 5, 1, 2, 4]\n[1, 2, 2, 4, 5]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"List twice","tmcname":"part04-16_list_twice"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in values and adds them to a list. After each addition, the list is printed out in two different ways:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"in the order the items were added"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"ordered from smallest to greatest"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program exits when the user types in 0."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"New item: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nThe list now: [3]\nThe list in order: [3]\nNew item: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nThe list now: [3, 1]\nThe list in order: [1, 3]\nNew item: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9"}]},{"type":"text","value":"\nThe list now: [3, 1, 9]\nThe list in order: [1, 3, 9]\nNew item: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nThe list now: [3, 1, 9, 5]\nThe list in order: [1, 3, 5, 9]\nNew item: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nBye!"}]}]},{"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 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."}]}]},{"type":"element","tagName":"h2","properties":{"id":"maximum-minimum-and-sum","style":"position:relative;"},"children":[{"type":"text","value":"Maximum, minimum and sum"},{"type":"element","tagName":"a","properties":{"href":"#maximum-minimum-and-sum","ariaLabel":"maximum minimum and sum 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 functions "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"max"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"min"}]},{"type":"text","value":", short for maximum and minimum, return the greatest and smallest item in a list, respectively. The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" returns the sum of all items in 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":"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":"5"}]},{"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":"2"}]},{"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":"3"}]},{"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":"1"}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\ngreatest "},{"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":"max"}]},{"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":"\nsmallest "},{"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":"min"}]},{"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":"\nlist_sum "},{"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":"my_list"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Smallest:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" smallest"},{"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":"\"Greatest:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" greatest"},{"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":"\"Sum:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" list_sum"},{"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":"Smallest: 1\nGreatest: 5\nSum: 15"}]}]},{"type":"element","tagName":"h2","properties":{"id":"methods-vs-functions","style":"position:relative;"},"children":[{"type":"text","value":"Methods vs functions"},{"type":"element","tagName":"a","properties":{"href":"#methods-vs-functions","ariaLabel":"methods vs 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":"There are two different ways of processing lists in Python, which can get confusing. For the most part you will use list "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"methods"}]},{"type":"text","value":", such as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"append"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sort"}]},{"type":"text","value":". They are used with the dot "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" operator on the list 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_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","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# method calls"}]},{"type":"text","value":"\nmy_list"},{"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":"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":"text","value":"append"},{"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":"text","value":"\nmy_list"},{"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":"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":"text","value":"\nmy_list"},{"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":"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# another method call"}]},{"type":"text","value":"\nmy_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"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":"Some "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"functions"}]},{"type":"text","value":" are happy to take a list as an argument. Above we saw the functions "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"max"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"min"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" do just that:"}]},{"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":"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":" "},{"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":"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# function calls take the list as an argument"}]},{"type":"text","value":"\ngreatest "},{"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":"max"}]},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsmallest "},{"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":"min"}]},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlength "},{"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":"my_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\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":"\"Smallest:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" smallest"},{"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":"\"Greatest:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" greatest"},{"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":"\"Length of the list:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" length"},{"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":"# another function call "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the list itself is an argument, the function returns a sorted copy"}]},{"type":"text","value":"\nin_order "},{"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":"sorted"}]},{"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","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":"in_order"},{"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":"Smallest: 1\nGreatest: 7\nLength of the list: 4\n[1, 2, 3, 7]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"a-list-as-an-argument-or-a-return-value","style":"position:relative;"},"children":[{"type":"text","value":"A list as an argument or a return value"},{"type":"element","tagName":"a","properties":{"href":"#a-list-as-an-argument-or-a-return-value","ariaLabel":"a list as an argument or a return value 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":"Just like the built-in functions above, our own functions can also take a list as an argument and produce a list as a return value. The following function works out the central value in an ordered list, also called the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"median"}]},{"type":"text","value":" value:"}]},{"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":"median"}]},{"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":" "},{"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 ordered "},{"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":"sorted"}]},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n list_centre "},{"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":"ordered"},{"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":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" ordered"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"list_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function creates an ordered version of the list given as an argument and returns the item in the very middle. Notice the integer division operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"//"}]},{"type":"text","value":" used here. The index of a list should always be an integer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function works 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":"text","value":"shoe_sizes "},{"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":"45"}]},{"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":"44"}]},{"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":"36"}]},{"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":"39"}]},{"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":"40"}]},{"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 median of the shoe sizes is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" median"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"shoe_sizes"},{"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\nages "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"56"}]},{"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":"34"}]},{"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":"22"}]},{"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":"5"}]},{"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":"77"}]},{"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":"5"}]},{"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 median of the ages is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" median"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"ages"},{"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":"The median of the shoe sizes is 40\nThe median of the ages is 22"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A function can also return a list. The following function asks the user to type in integers and returns the input as 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":"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_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","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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n user_input "},{"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 an integer, leave empty to exit: \""}]},{"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":"if"}]},{"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":"user_input"},{"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":"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":"break"}]},{"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":"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":"text","value":"user_input"},{"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":" numbers"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function makes use of a helper variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"numbers"}]},{"type":"text","value":", which is a list. All the numbers typed in by the user are added to the list. When the loop is exited from, the function returns the list with the statement "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return numbers"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Calling the function 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":"text","value":"numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" input_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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The greatest number is\""}]},{"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":"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":"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 median of the numbers is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" median"},{"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":"p","properties":{},"children":[{"type":"text","value":"could print this, for example:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in an integer, leave empty to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nPlease type in an integer, leave empty to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-22"}]},{"type":"text","value":"\nPlease type in an integer, leave empty to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\nPlease type in an integer, leave empty to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"35"}]},{"type":"text","value":"\nPlease type in an integer, leave empty to exit: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nPlease type in an integer, leave empty to exit:\nThe greatest number is 35\nThe median of the numbers is 4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This small example demonstrates one of the most important uses of functions: they can help you divide your code into smaller, easily understandable and logical wholes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Of course the same functionality could be achieved without writing any of our own 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":"text","value":"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"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n user_input "},{"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 an integer, leave empty to exit: \""}]},{"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":"if"}]},{"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":"user_input"},{"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":"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":"break"}]},{"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":"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":"text","value":"user_input"},{"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\nordered "},{"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":"sorted"}]},{"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":"\nlist_centre "},{"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":"ordered"},{"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":"2"}]},{"type":"text","value":"\nmedian "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ordered"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"list_centre"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The greatest number is\""}]},{"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":"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":"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 median of the numbers is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" median"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this version, following the programming logic is more difficult, as it is no longer clear which commands are a part of which functionality. The code fulfils the same purposes - reading in input, calculating the median value, and so on - but the structure is less clear."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Organising your code into separate functions will improve you program's readability, but also make it easier to handle logical wholes. This in turn helps you verify that the program works as intended, as each function can be tested separately."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Another important use for functions is making code "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reusable"}]},{"type":"text","value":". If you need to achieve some functionality multiple times in your program, it is a good idea to create your own function and name it appropriately:"}]},{"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":"\"Shoe sizes:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nshoe_sizes "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" input_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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Weights:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nweights "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" input_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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Heights:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nheights "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" input_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":"programming-exercise","properties":{"name":"The length of a list","tmcname":"part04-17_length_of_list"},"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":"length"}]},{"type":"text","value":" which takes a list as its argument and returns the length of the 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":"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":"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":" "},{"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":" "},{"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":"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":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" length"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The length is\""}]},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the list given as an argument doesn't need to be stored in any variable"}]},{"type":"text","value":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" length"},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"1"}]},{"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":"1"}]},{"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":"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":"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 length is\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The length is 5\nThe length is 4"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Arithmetic mean","tmcname":"part04-18_mean"},"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":"mean"}]},{"type":"text","value":", which takes a list of integers as an argument. The function returns the arithmetic mean of the values in the 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":"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":"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":" "},{"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":" "},{"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":"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":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" mean"},{"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","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":"\"mean value is\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"mean value is 3.0"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The range of a list","tmcname":"part04-19_range_of_list"},"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":"range_of_list"}]},{"type":"text","value":", which takes a list of integers as an argument. The function returns the difference between the smallest and the largest value in the 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":"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":"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":" "},{"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":" "},{"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":"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":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" range_of_list"},{"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","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 range of the list is\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The range of the list is 4"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are many more ways to use lists in Python. The Python "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/tutorial/datastructures.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"documentation"}]},{"type":"text","value":" is a good place to start if you want to know more."}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will know what lists are in Python
    • \n
    • You will be able access a specified item within a list
    • \n
    • You will know how to add items to a list, and how to remove them
    • \n
    • You will be familiar with built-in list functions and methods
    • \n

    Thus far in our programs we have stored data with variables, each bit of data usually having its own named variable. This obviously has some limitations, as it can get cumbersome to define separate variables for everything when there is a lot of data to handle.

    A Python list is a collection of values which is accessed via a single variable name. The contents of the list are written within square brackets. The values contained in the list are called items, or sometimes elements.

    The following command creates a new, empty list

    my_list = []

    whereas this command creates a list with five items in it:

    my_list = [7, 2, 2, 5, 2]

    Accessing items in a list

    The items in a list are indexed in exactly the same way as characters in a string. Indexing starts from zero, and the last index is the length of the list minus 1:

    \n \n \n \n \n \n \"Lists\n \n \n

    A single list item can be accessed just like a single character in a string is accessed, with square brackets:

    my_list = [7, 2, 2, 5, 2]\n\nprint(my_list[0])\nprint(my_list[1])\nprint(my_list[3])\n\nprint(\"The sum of the first two items:\", my_list[0] + my_list[1])

    7\n2\n5\nThe sum of the first two items: 9

    The entire contents of the list can also be printed out:

    my_list = [7, 2, 2, 5, 2]\nprint(my_list)

    [7, 2, 2, 5, 2]

    Unlike strings, lists are mutable, which means their contents can change. You can assign a new value to an item within a list, just like you can assign a new value to a variable:

    my_list = [7, 2, 2, 5, 2]\nprint(my_list)\nmy_list[1] = 3\nprint(my_list)

    [7, 2, 2, 5, 2]\n[7, 3, 2, 5, 2]

    The function len gives you the number of items in a list:

    my_list = [7, 2, 2, 5, 2]\nprint(len(my_list))

    5

    Please write a program which initialises a list with the values [1, 2, 3, 4, 5]. Then the program should ask the user for an index and a new value, replace the value at the given index, and print the list again. This should be looped over until the user gives -1 for the index. You can assume all given index values will fall within your list.

    An example execution of the program:

    Index: 0\nNew value: 10\n[10, 2, 3, 4, 5]\nIndex: 2\nNew value: 250\n[10, 2, 250, 4, 5]\nIndex: 4\nNew value: -45\n[10, 2, 250, 4, -45]\nIndex: -1

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    Adding items to a list

    The append method adds items to the end of a list. It works like this:

    numbers = []\nnumbers.append(5)\nnumbers.append(10)\nnumbers.append(3)\nprint(numbers)

    [5, 10, 3]

    The following example makes use of two separate lists:

    numbers = []\nshoe_sizes = []\n\nnumbers.append(5)\nnumbers.append(10)\nnumbers.append(3)\n\nshoe_sizes.append(37)\nshoe_sizes.append(44)\nshoe_sizes.append(40)\nshoe_sizes.append(28)\n\nprint(\"Numbers:\")\nprint(numbers)\n\nprint(\"Shoe sizes:\")\nprint(shoe_sizes)

    The item is appended to the list on which the method is called:

    Numbers:\n[5, 10, 3]\nShoe sizes:\n[37, 44, 40, 28]

    Please write a program which first asks the user for the number of items to be added. Then the program should ask for the given number of values, one by one, and add them to a list in the order they were typed in. Finally, the list is printed out.

    An example of expected behaviour:

    How many items: 3\nItem 1: 10\nItem 2: 250\nItem 3: 34\n[10, 250, 34]

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    Adding to a specific location

    If you want to specify a location in the list where an item should be added, you can use the insert method. The method adds an item at the specified index. All the items already in the list with an index equal to or higher than the specified index are moved one index further, \"to the right\":

    \n \n \n \n \n \n \"Inserting\n \n \n

    So, for instance this program

    numbers = [1, 2, 3, 4, 5, 6]\nnumbers.insert(0, 10)\nprint(numbers)\nnumbers.insert(2, 20)\nprint(numbers)

    prints out this:

    [10, 1, 2, 3, 4, 5, 6]\n[10, 1, 20, 2, 3, 4, 5, 6]

    Removing items from a list

    There are two different approaches to removing an item from a list:

      \n
    • If the index of the item is known, you can use the method pop.
    • \n
    • If the contents of the item are known, you can use the method remove.
    • \n

    So, the method pop takes the index of the item you want to remove as its argument. The following program removes items at indexes 2 and 3 from the list. Notice how the indexes of the remaining items change when one is removed.

    my_list = [1, 2, 3, 4, 5, 6]\n\nmy_list.pop(2)\nprint(my_list)\nmy_list.pop(3)\nprint(my_list)

    [1, 2, 4, 5, 6]\n[1, 2, 4, 6]

    It's useful to remember that the method pop also returns the removed item:

    my_list = [4, 2, 7, 2, 5]\n\nnumber = my_list.pop(2)\nprint(number)\nprint(my_list)

    7\n[4, 2, 2, 5]

    The method remove, on the other hand, takes the value of the item to be removed as its argument. For example, this program

    my_list = [1, 2, 3, 4, 5, 6]\n\nmy_list.remove(2)\nprint(my_list)\nmy_list.remove(5)\nprint(my_list)

    prints out this:

    [1, 3, 4, 5, 6]\n[1, 3, 4, 6]

    The method removes the first occurrence of the value in the list, much like the string function find returns the first occurrence of a substring:

    my_list = [1, 2, 1, 2]\n\nmy_list.remove(1)\nprint(my_list)\nmy_list.remove(1)\nprint(my_list)

    [2, 1, 2]\n[2, 2]

    Please write a program which asks the user to choose between addition and removal. Depending on the choice, the program adds an item to or removes an item from the end of a list. The item that is added must always be one greater than the last item in the list. The first item to be added must be 1.

    The list is printed out in the beginning and after each operation. Have a look at the example execution below:

    The list is now []\na(d)d, (r)emove or e(x)it: d\nThe list is now [1]\na(d)d, (r)emove or e(x)it: d\nThe list is now [1, 2]\na(d)d, (r)emove or e(x)it: d\nThe list is now [1, 2, 3]\na(d)d, (r)emove or e(x)it: r\nThe list is now [1, 2]\na(d)d, (r)emove or e(x)it: d\nThe list is now [1, 2, 3]\na(d)d, (r)emove or e(x)it: x\nBye!

    You may assume that, if the list is empty, there will not be an attempt to remove items.

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    If the specified item is not in the list, the remove function causes an error. Just like with strings, we can check for the presence of an item with the in operator:

    my_list = [1, 3, 4]\n\nif 1 in my_list:\n    print(\"The list contains item 1\")\n\nif 2 in my_list:\n    print(\"The list contains item 2\")

    The list contains item 1

    Please write a program which asks the user for words. If the user types in a word for the second time, the program should print out the number of different words typed in, and exit.

    Word: once\nWord: upon\nWord: a\nWord: time\nWord: upon\nYou typed in 4 different words

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    Sorting lists

    The items in a list can be sorted from smallest to greatest with the method sort.

    my_list = [2,5,1,2,4]\nmy_list.sort()\nprint(my_list)

    [1, 2, 2, 4, 5]

    Notice how the method modifies the list itself. Sometimes we don't want to change the original list, so we use the function sorted instead. It returns a sorted list:

    my_list = [2,5,1,2,4]\nprint(sorted(my_list)))

    [1, 2, 2, 4, 5]

    Remember the difference between the two: sort changes the order of the original list in place, whereas sorted creates a new, ordered copy of the list. With sorted we can preserve the original order of the list:

    original = [2, 5, 1, 2, 4]\nin_order = sorted(original)\nprint(original)\nprint(in_order)

    [2, 5, 1, 2, 4]\n[1, 2, 2, 4, 5]

    Please write a program which asks the user to type in values and adds them to a list. After each addition, the list is printed out in two different ways:

      \n
    • in the order the items were added
    • \n
    • ordered from smallest to greatest
    • \n

    The program exits when the user types in 0.

    An example of expected behaviour:

    New item: 3\nThe list now: [3]\nThe list in order: [3]\nNew item: 1\nThe list now: [3, 1]\nThe list in order: [1, 3]\nNew item: 9\nThe list now: [3, 1, 9]\nThe list in order: [1, 3, 9]\nNew item: 5\nThe list now: [3, 1, 9, 5]\nThe list in order: [1, 3, 5, 9]\nNew item: 0\nBye!

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    Maximum, minimum and sum

    The functions max and min, short for maximum and minimum, return the greatest and smallest item in a list, respectively. The function sum returns the sum of all items in a list.

    my_list = [5, 2, 3, 1, 4]\n\ngreatest = max(my_list)\nsmallest = min(my_list)\nlist_sum = sum(my_list)\n\nprint(\"Smallest:\", smallest)\nprint(\"Greatest:\", greatest)\nprint(\"Sum:\", list_sum)

    Smallest: 1\nGreatest: 5\nSum: 15

    Methods vs functions

    There are two different ways of processing lists in Python, which can get confusing. For the most part you will use list methods, such as append and sort. They are used with the dot . operator on the list variable:

    my_list = []\n\n# method calls\nmy_list.append(3)\nmy_list.append(1)\nmy_list.append(7)\nmy_list.append(2)\n\n# another method call\nmy_list.sort()

    Some functions are happy to take a list as an argument. Above we saw the functions max, min, len and sorted do just that:

    my_list = [3, 2, 7, 1]\n\n# function calls take the list as an argument\ngreatest = max(my_list))\nsmallest = min(my_list))\nlength = len(my_list))\n\nprint(\"Smallest:\", smallest)\nprint(\"Greatest:\", greatest)\nprint(\"Length of the list:\", length)\n\n# another function call \n# the list itself is an argument, the function returns a sorted copy\nin_order = sorted(my_list))\nprint(in_order)

    Smallest: 1\nGreatest: 7\nLength of the list: 4\n[1, 2, 3, 7]

    A list as an argument or a return value

    Just like the built-in functions above, our own functions can also take a list as an argument and produce a list as a return value. The following function works out the central value in an ordered list, also called the median value:

    def median(my_list: list):\n    ordered = sorted(my_list))\n    list_centre = len(ordered) // 2\n    return ordered[list_centre]

    The function creates an ordered version of the list given as an argument and returns the item in the very middle. Notice the integer division operator // used here. The index of a list should always be an integer.

    The function works like this:

    shoe_sizes = [45, 44, 36, 39, 40]\nprint(\"The median of the shoe sizes is\", median(shoe_sizes))\n\nages = [1, 56, 34, 22, 5, 77, 5]\nprint(\"The median of the ages is\", median(ages))

    The median of the shoe sizes is 40\nThe median of the ages is 22

    A function can also return a list. The following function asks the user to type in integers and returns the input as a list:

    def input_numbers():\n    numbers = []\n    while True:\n        user_input = input(\"Please type in an integer, leave empty to exit: \")\n        if len(user_input) == 0:\n            break\n        numbers.append(int(user_input))\n    return numbers

    The function makes use of a helper variable numbers, which is a list. All the numbers typed in by the user are added to the list. When the loop is exited from, the function returns the list with the statement return numbers.

    Calling the function like this

    numbers = input_numbers()\n\nprint(\"The greatest number is\", max(numbers))\nprint(\"The median of the numbers is\", median(numbers))

    could print this, for example:

    Please type in an integer, leave empty to exit: 5\nPlease type in an integer, leave empty to exit: -22\nPlease type in an integer, leave empty to exit: 4\nPlease type in an integer, leave empty to exit: 35\nPlease type in an integer, leave empty to exit: 1\nPlease type in an integer, leave empty to exit:\nThe greatest number is 35\nThe median of the numbers is 4

    This small example demonstrates one of the most important uses of functions: they can help you divide your code into smaller, easily understandable and logical wholes.

    Of course the same functionality could be achieved without writing any of our own functions:

    numbers = []\nwhile True:\n    user_input = input(\"Please type in an integer, leave empty to exit: \")\n    if len(user_input) == 0:\n        break\n    numbers.append(int(user_input))\n\nordered = sorted(numbers)\nlist_centre = len(ordered) // 2\nmedian = ordered[list_centre]\n\nprint(\"The greatest number is\", max(numbers))\nprint(\"The median of the numbers is\", median)

    In this version, following the programming logic is more difficult, as it is no longer clear which commands are a part of which functionality. The code fulfils the same purposes - reading in input, calculating the median value, and so on - but the structure is less clear.

    Organising your code into separate functions will improve you program's readability, but also make it easier to handle logical wholes. This in turn helps you verify that the program works as intended, as each function can be tested separately.

    Another important use for functions is making code reusable. If you need to achieve some functionality multiple times in your program, it is a good idea to create your own function and name it appropriately:

    print(\"Shoe sizes:\")\nshoe_sizes = input_numbers()\n\nprint(\"Weights:\")\nweights = input_numbers()\n\nprint(\"Heights:\")\nheights = input_numbers()

    Please write a function named length which takes a list as its argument and returns the length of the list.

    my_list = [1, 2, 3, 4, 5]\nresult = length(my_list)\nprint(\"The length is\", result)\n\n# the list given as an argument doesn't need to be stored in any variable\nresult = length([1, 1, 1, 1])\nprint(\"The length is\", result)

    The length is 5\nThe length is 4

    Please write a function named mean, which takes a list of integers as an argument. The function returns the arithmetic mean of the values in the list.

    my_list = [1, 2, 3, 4, 5]\nresult = mean(my_list))\nprint(\"mean value is\", result)

    mean value is 3.0

    Please write a function named range_of_list, which takes a list of integers as an argument. The function returns the difference between the smallest and the largest value in the list.

    my_list = [1, 2, 3, 4, 5]\nresult = range_of_list(my_list))\nprint(\"The range of the list is\", result)

    The range of the list is 4

    There are many more ways to use lists in Python. The Python documentation is a good place to start if you want to know more.

    ","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-4/3-lists.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-4/4-definite-iteration/page-data.json b/page-data/part-4/4-definite-iteration/page-data.json index 35d1739e1..b76b5bd1d 100644 --- a/page-data/part-4/4-definite-iteration/page-data.json +++ b/page-data/part-4/4-definite-iteration/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-4/4-definite-iteration", - "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 know the difference between definite and indefinite iteration"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how a Python "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop works"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop to iterate through lists and strings"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can use a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop to go through the items in a list, just like we used while loops to go through strings. The following program prints out the items in the list, each on a separate line:"}]},{"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":"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":" "},{"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":"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":" "},{"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\nindex "},{"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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" index "},{"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":"my_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":"text","value":"my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"index"},{"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 index "},{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3\n2\n4\n5\n2"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This obviously works, but it is a rather complicated way of going through a list, as you have to use a helper variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"index"}]},{"type":"text","value":" to remember which item in the list you're at. Fortunately, Python offers a more intuitive way of traversing through lists, strings and other similar structures."}]},{"type":"element","tagName":"h2","properties":{"id":"the-for-loop","style":"position:relative;"},"children":[{"type":"text","value":"The for loop"},{"type":"element","tagName":"a","properties":{"href":"#the-for-loop","ariaLabel":"the for loop 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":"When you want to go through some ready collection of items, the Python "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop will do this for you. For instance, the loop can go through all items in a list from first to last."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When using a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop the program doesn't \"know\" beforehand how many iterations the loop will perform. It will repeat until the condition becomes false, or the loop is otherwise broken out of. That is why it falls under "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"indefinite iteration"}]},{"type":"text","value":". With a for loop the number of iterations is determined when the loop is set up, and so it falls under "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"definite iteration"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The idea is that the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop takes the items in the collection one by one and performs the same actions on each. The programmer does not have to worry about which item is being handled when. The syntax of the for loop is as follows:"}]},{"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":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"variable"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"collection"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"block"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop takes an item in the collection, assigns it to the variable, processes the block of code, and moves on to the next item. When all items in the collection have been processed, execution of the program continues from the line after the loop."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 327px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/4e12b8e1e0df4791611d49d1d158a1b4/00e65/4_4_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: 214.3478260869565%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAArCAYAAAB4pah1AAAACXBIWXMAAAsTAAALEwEAmpwYAAADPUlEQVRIx62X2U4qQRCG5/3Da3hrIt5pjBGXqAiKC264I67gLiJinXx1UpM6Y88MmNNJZZqy++9a/qpuIwmM7+9v/fb7fRWvyxtRFmC73Zbb29v/B3h/fy83NzexbhjQKARmG19eXqTT6fwO0Bbz/fr60vnq6qpsbGzoHJ1fkwrIHweDgUpyYbFYlOnp6aAHIYkB/fj8/NREnJycyPv7uzw9Pcnp6anc3d3FGU8bYKnLh4eHcnR0pJuYr62tKcjBwYGCo9ve3paLiwu5urqSx8dHPci+Dw8PerhaiHJ8fFwmJydlb29Pzs/PZWFhQWNXrVYVHFlfX1cwdKzhoOvraxXmjUZDQaNms6lumsmXl5e6CYu73a5asL+/Lzs7OzGFQqPVasnz87NETKBHMnu4wanHx8e6MC0pJJOBIeBETF5fX+PFtoAEEEMAbXgmJL+pgDZ6vZ5aSOw+Pj4kxNdcQLPMAEiW8RCdEd4fHAS0GEIbknF2dqYUIdO1Wk3nZBYWWDwzLTRA4hUir20iyzSMoQEhM5RZXl6Wer2uFu7u7kq5XFZLV1ZWlMxDA+IWvwHa3NxUAL5wkC9JGgkQC81lCE+FAEqFMKgImu5IMXx7e9NsklUqCLGscwiJSwPUSvGAuEOG0VFBNAXIzRydHeiBMivF+Ga/x8bGZGJiIu7evuZ9GeJR0GXrg/AOPS0LYY67NIkQD/FiZmZG5ubm/jYHcwOwrAEHs3jI/RNhjXVjTjK3WIBAJTsQYvss+9ZvoYusxWMpNLHLCDKXSiWZnZ2Vra2toWijWfYuYQ2AWOu78Ug89D0wrZZtAJjFwxjQlLhPtyEEJMgL/KT0kjz8AZjsKLhMYuyhZILOPMnsh6HT8u7eXAu927gEgXGTmCLM7ZpMrs912WdymITkAsJ2yg3LoBHWkiTm/iWW3Edp/sNDDwiReXnNz88rwSuVioIWCgVZWlqKO48xwJ4t3JRBQKyi9XOF0q25FoxKvuMkyy/4PsyKIa8J3wNDz7wfgJAb64gJDRZhjku+daW+D0McMwuoZeLldXlcDT6J/Rvb3AxxLxcwaaFdob+y0A/bPDU1JYuLiz9eXiMDppF3mH98/gAHC+9HMp5l8gAAAABJRU5ErkJggg=='); 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":["/static/4e12b8e1e0df4791611d49d1d158a1b4/a0b58/4_4_1.webp 230w","/static/4e12b8e1e0df4791611d49d1d158a1b4/e25e9/4_4_1.webp 327w"],"sizes":"(max-width: 327px) 100vw, 327px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/4e12b8e1e0df4791611d49d1d158a1b4/81c8e/4_4_1.png 230w","/static/4e12b8e1e0df4791611d49d1d158a1b4/00e65/4_4_1.png 327w"],"sizes":"(max-width: 327px) 100vw, 327px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/4e12b8e1e0df4791611d49d1d158a1b4/00e65/4_4_1.png","alt":"Iterating through a list","title":"Iterating through a list","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 following program prints out all the items in a list using a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop:"}]},{"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":"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":" "},{"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":"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":" "},{"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"item"},{"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":"3\n2\n4\n5\n2"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Compared to the example at the beginning of this section, the structure is much easier to understand. A "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop makes straightforward traversal through a collection of items very simple."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The same principle applies to characters in 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":"name "},{"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 your name: \""}]},{"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":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" name"},{"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":"character"},{"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 your name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Grace"}]},{"type":"text","value":"\nG\nr\na\nc\ne"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Star-studded","tmcname":"part04-20_star_studded"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in a string. The program then prints each input character on a separate line. After each character there should be a star (*) printed on its own line."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is how it should work:"}]},{"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":"Python"}]},{"type":"text","value":"\nP\n*\ny\n*\nt\n*\nh\n*\no\n*\nn\n*"}]}]},{"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 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."}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-function-range","style":"position:relative;"},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"a","properties":{"href":"#the-function-range","ariaLabel":"the function range 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":"Often you know how many times you want to repeat a certain bit of code. You might, for example, wish to go through all the numbers between 1 and 100. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":" function plugged into a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop will do this for you."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are a few different ways to call the range function. The simplest way is to give the function just one argument, which signifies the end-point of the range. The end-point itself is excluded, just like with string slices. In other words, the function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range(n)"}]},{"type":"text","value":" provides a loop with a range from 0 to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n-1"}]},{"type":"text","value":":"}]},{"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":"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":"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","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":"i"},{"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\n1\n2\n3\n4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With two arguments, the function will return a range between the two numbers. The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range(a,b)"}]},{"type":"text","value":" provides a range starting from "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" and ending at "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b-1"}]},{"type":"text","value":":"}]},{"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":"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":"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":" "},{"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":"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":"i"},{"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":"3\n4\n5\n6"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Finally, with a third argument you can also specify the size of the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"step"}]},{"type":"text","value":" the range takes between each value. The function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range(a, b, c)"}]},{"type":"text","value":" provides a range starting from "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":", ending at "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b-1"}]},{"type":"text","value":", and changing by "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"c"}]},{"type":"text","value":" with every 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":"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"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":"\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":"i"},{"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":"1\n3\n5\n7"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A step can also be negative. Then the range will be in reversed orded. Notice the first two arguments are also flipped here:"}]},{"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":"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":"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":" "},{"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":" "},{"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":"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":"i"},{"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":"6\n5\n4\n3"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"From negative to positive","tmcname":"part04-21_negative_to_positive"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a positive integer N. The program then prints out all numbers between -N and N inclusive, but "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"leaves out the number 0"}]},{"type":"text","value":". Each number should be printed on a separate line."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a positive integer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n-4\n-3\n-2\n-1\n1\n2\n3\n4"}]}]},{"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 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."}]}]},{"type":"element","tagName":"h2","properties":{"id":"from-a-range-to-a-list","style":"position:relative;"},"children":[{"type":"text","value":"From a range to a list"},{"type":"element","tagName":"a","properties":{"href":"#from-a-range-to-a-list","ariaLabel":"from a range to a list 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 function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":" returns a range object, which in many ways behaves like a list, but isn't actually one. If you try printing out the value the function returns, you will only see a description of a range object:"}]},{"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":"numbers "},{"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":"range"}]},{"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":" "},{"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":"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":"numbers"},{"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":"range(2, 7)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list"}]},{"type":"text","value":" will convert a range into a list. The list will contain all the values that are in the range. The Advanced Course in Programming course, which follows this one, will shed more light on this subject."}]},{"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":"numbers "},{"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":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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":"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":" "},{"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":"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":"numbers"},{"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":"[2, 3, 4, 5, 6]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"a-reminder-of-the-demands-of-the-automatic-tests","style":"position:relative;"},"children":[{"type":"text","value":"A reminder of the demands of the automatic tests"},{"type":"element","tagName":"a","properties":{"href":"#a-reminder-of-the-demands-of-the-automatic-tests","ariaLabel":"a reminder of the demands of the automatic tests 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":"So far, when the exercises have asked you to write functions, the templates have looked 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","comment"]},"children":[{"type":"text","value":"# Write your solution here"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# You can test your function by calling it within the following 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 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":"\"it was a dark and stormy python\""}]},{"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":"first_word"},{"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":"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":"second_word"},{"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":"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":"last_word"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"From now on there will be no more reminders in the templates to use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" block. However, the automatic tests will still demand its use, so you will have to add the block yourself when you test your function within the main function of your program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" some exercises, like the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Palindromes"}]},{"type":"text","value":" exercise coming up in this section, expect you to also write code which calls the function you wrote. This code should "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" be placed within an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" block. The automatic tests will not execute any code within that block, so your solution will not be complete if you place your function calls there."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"List of stars","tmcname":"part04-22_list_of_stars"},"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":"list_of_stars"}]},{"type":"text","value":", which takes a list of integers as its argument. The function should print out lines of star characters. The numbers in the list specify how many stars each line should contain."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, with the function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list_of_stars([3, 7, 1, 1, 2])"}]},{"type":"text","value":" the following should be printed out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"***\n*******\n*\n*\n**\n"}]}]},{"type":"comment","value":" **Huomaa** että tällä hetkellä Windowsissa on ongelmia joidenkin tehtävien testien suorittamisessa. Jos törmäät seuraavaan virheilmoitukseen\n\n\"Listan\n\nvoit suorittaa testit lähettämällä ne palvelimelle valitsemalla testien suoritusnapin oikealla puolella olevasta symbolista avautuvasta TMC-valikosta _Submit solutions_.\n\nOngelman saa korjattua menemällä laajennuksen asennusvalikkoon ja muuttamalla \"TMC Data\" -kohdassa tehtävien sijainnin johonkin toiseen sijaintiin, jonka tiedostopolku on lyhempi, allaolevassa kuvassa nappi _change path_. Siirrossa saattaa kestää hetken, joten odotathan operaation päättymistä.\n\n\"Listan\n\nOngelmaan pyritään saamaan parempi ratkaisu lähipäivinä. "}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Anagrams","tmcname":"part04-23_anagrams"},"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":"anagrams"}]},{"type":"text","value":", which takes two strings as arguments. The function returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" if the strings are anagrams of each other. Two words are anagrams if they contain exactly the same characters."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples 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":"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":"anagrams"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tame\""}]},{"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":"\"meta\""}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# True"}]},{"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":"anagrams"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tame\""}]},{"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":"\"mate\""}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# True"}]},{"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":"anagrams"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tame\""}]},{"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":"\"team\""}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# True"}]},{"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":"anagrams"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tabby\""}]},{"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":"\"batty\""}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# False"}]},{"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":"anagrams"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hint: the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" can be used on strings as well."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Palindromes","tmcname":"part04-24_palindromes"},"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":"palindromes"}]},{"type":"text","value":", which takes a string argument and returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" if the string is a palindrome. Palindromes are words which are spelled exactly the same backwards and forwards."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please also write a main program which asks the user to type in words until they type in a palindrome:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a palindrome: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]},{"type":"text","value":"\nthat wasn't a palindrome\nPlease type in a palindrome: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"java"}]},{"type":"text","value":"\nthat wasn't a palindrome\nPlease type in a palindrome: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"oddoreven"}]},{"type":"text","value":"\nthat wasn't a palindrome\nPlease type in a palindrome: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"neveroddoreven"}]},{"type":"text","value":"\nneveroddoreven is a palindrome!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":", the main program "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"should not be"}]},{"type":"text","value":" within an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\":"}]},{"type":"text","value":" block"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The sum of positive numbers","tmcname":"part04-25_sum_of_positives"},"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":"sum_of_positives"}]},{"type":"text","value":", which takes a list of integers as its argument. The function returns the sum of the positive values in the 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":"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":"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":"2"}]},{"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":"3"}]},{"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":"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":"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":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sum_of_positives"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The result is\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The result is 9"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In these exercises we will be using lists as arguments and return values. This was covered in the "},{"type":"element","tagName":"a","properties":{"href":"/part-4/3-lists#a-list-as-an-argument-or-a-return-value"},"children":[{"type":"text","value":"previous section"}]},{"type":"text","value":", if you need a refresher."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Even numbers","tmcname":"part04-26_even_numbers"},"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":"even_numbers"}]},{"type":"text","value":", which takes a list of integers as an argument. The function returns a new list containing the even numbers from the original 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":"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":"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":" "},{"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":" "},{"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":"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":"\nnew_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" even_numbers"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"original\""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"new\""}]},{"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":"original [1, 2, 3, 4, 5]\nnew [2, 4]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The sum of lists","tmcname":"part04-27_sum_of_lists"},"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":"list_sum"}]},{"type":"text","value":" which takes two lists of integers as arguments. The function returns a new list which contains the sums of the items at each index in the two original lists. You may assume both lists have the same number of items."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function at 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":"a "},{"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":"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":" "},{"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":"\nb "},{"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":"7"}]},{"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":"8"}]},{"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":"9"}]},{"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":"list_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# [8, 10, 12]"}]}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Distinct numbers","tmcname":"part04-28_distinct_numbers"},"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":"distinct_numbers"}]},{"type":"text","value":", which takes a list of integers as its argument. The function returns a new list containing the numbers from the original list in order of magnitude, and so that each distinct number is present only once."}]},{"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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"},{"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":"distinct_numbers"},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# [1, 2, 3]"}]}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"finding-the-best-or-the-worst-item-in-a-list","style":"position:relative;"},"children":[{"type":"text","value":"Finding the best or the worst item in a list"},{"type":"element","tagName":"a","properties":{"href":"#finding-the-best-or-the-worst-item-in-a-list","ariaLabel":"finding the best or the worst item in a list 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":"A very common programming task is finding the best or worst item in a list, according to some criteria. A simple solution is using a helper variable to \"remember\" which of the items processed so far was the most suitable. This temporary best choice is then compared to each item in turn, and at the end of the iteration the variable contains the best of the bunch."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A rough draft which doesn't quite compile yet:"}]},{"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":"best "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" initial_value "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# The initial value depends on the situation"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"if"}]},{"type":"text","value":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" better than best"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n best "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" item\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# We now have the best one figured out!"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The details of the final program code depend on the type of the items in the list, and also on the criteria for choosing the best (or worst) item. Sometimes you may need more than one helper variable."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's practice this method a little."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The length of the longest in the list","tmcname":"part04-29_length_of_longest"},"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":"length_of_longest"}]},{"type":"text","value":", which takes a list of strings as its argument. The function returns the length of the longest 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_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":"\"first\""}]},{"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":"\"second\""}]},{"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":"\"fourth\""}]},{"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":"\"eleventh\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" length_of_longest"},{"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":"result"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"\"adele\""}]},{"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":"\"mark\""}]},{"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":"\"dorothy\""}]},{"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":"\"tim\""}]},{"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":"\"hedy\""}]},{"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":"\"richard\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" length_of_longest"},{"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":"result"},{"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":"8\n7"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The shortest in the list","tmcname":"part04-30_shortest_in_list"},"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":"shortest"}]},{"type":"text","value":", which takes a list of strings as its argument. The function returns whichever of the strings is the shortest. If more than one are equally short, the function can return any of the shortest strings (there will be no such situation in the tests). You may assume there will be no empty strings in the 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":"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":"\"first\""}]},{"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":"\"second\""}]},{"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":"\"fourth\""}]},{"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":"\"eleventh\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" shortest"},{"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":"result"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"\"adele\""}]},{"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":"\"mark\""}]},{"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":"\"dorothy\""}]},{"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":"\"tim\""}]},{"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":"\"hedy\""}]},{"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":"\"richard\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" shortest"},{"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":"result"},{"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":"first\ntim"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"All the longest in the list","tmcname":"part04-31_all_longest_in_list"},"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":"all_the_longest"}]},{"type":"text","value":", which takes a list of strings as its argument. The function should return a new list containing the longest string in the original list. If more than one are equally long, the function should return all of the longest strings."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The order of the strings in the returned list should be the same as in the original."}]},{"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":"\"first\""}]},{"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":"\"second\""}]},{"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":"\"fourth\""}]},{"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":"\"eleventh\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" all_the_longest"},{"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":"result"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ['eleventh']"}]}]}]}]},{"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":"\"adele\""}]},{"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":"\"mark\""}]},{"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":"\"dorothy\""}]},{"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":"\"tim\""}]},{"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":"\"hedy\""}]},{"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":"\"richard\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" all_the_longest"},{"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":"result"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ['dorothy', 'richard']"}]}]}]}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will know the difference between definite and indefinite iteration
    • \n
    • You will know how a Python for loop works
    • \n
    • You will be able to use a for loop to iterate through lists and strings
    • \n

    You can use a while loop to go through the items in a list, just like we used while loops to go through strings. The following program prints out the items in the list, each on a separate line:

    my_list = [3, 2, 4, 5, 2]\n\nindex = 0\nwhile index < len(my_list):\n    print(my_list[index])\n    index += 1

    3\n2\n4\n5\n2

    This obviously works, but it is a rather complicated way of going through a list, as you have to use a helper variable index to remember which item in the list you're at. Fortunately, Python offers a more intuitive way of traversing through lists, strings and other similar structures.

    The for loop

    When you want to go through some ready collection of items, the Python for loop will do this for you. For instance, the loop can go through all items in a list from first to last.

    When using a while loop the program doesn't \"know\" beforehand how many iterations the loop will perform. It will repeat until the condition becomes false, or the loop is otherwise broken out of. That is why it falls under indefinite iteration. With a for loop the number of iterations is determined when the loop is set up, and so it falls under definite iteration.

    The idea is that the for loop takes the items in the collection one by one and performs the same actions on each. The programmer does not have to worry about which item is being handled when. The syntax of the for loop is as follows:

    for <variable> in <collection>:\n    <block>

    The for loop takes an item in the collection, assigns it to the variable, processes the block of code, and moves on to the next item. When all items in the collection have been processed, execution of the program continues from the line after the loop.

    \n \n \n \n \n \n \"Iterating\n \n \n

    The following program prints out all the items in a list using a for loop:

    my_list = [3, 2, 4, 5, 2]\n\nfor item in my_list:\n    print(item)

    3\n2\n4\n5\n2

    Compared to the example at the beginning of this section, the structure is much easier to understand. A for loop makes straightforward traversal through a collection of items very simple.

    The same principle applies to characters in a string:

    name = input(\"Please type in your name: \")\n\nfor character in name:\n    print(character)

    Please type in your name: Grace\nG\nr\na\nc\ne

    Please write a program which asks the user to type in a string. The program then prints each input character on a separate line. After each character there should be a star (*) printed on its own line.

    This is how it should work:

    Please type in a string: Python\nP\n*\ny\n*\nt\n*\nh\n*\no\n*\nn\n*

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    The function range

    Often you know how many times you want to repeat a certain bit of code. You might, for example, wish to go through all the numbers between 1 and 100. The range function plugged into a for loop will do this for you.

    There are a few different ways to call the range function. The simplest way is to give the function just one argument, which signifies the end-point of the range. The end-point itself is excluded, just like with string slices. In other words, the function call range(n) provides a loop with a range from 0 to n-1:

    for i in range(5):\n    print(i)

    0\n1\n2\n3\n4

    With two arguments, the function will return a range between the two numbers. The function range(a,b) provides a range starting from a and ending at b-1:

    for i in range(3, 7):\n    print(i)

    3\n4\n5\n6

    Finally, with a third argument you can also specify the size of the step the range takes between each value. The function call range(a, b, c) provides a range starting from a, ending at b-1, and changing by c with every step:

    for i in range(1, 9, 2):\n    print(i)

    1\n3\n5\n7

    A step can also be negative. Then the range will be in reversed orded. Notice the first two arguments are also flipped here:

    for i in range(6, 2, -1):\n    print(i)

    6\n5\n4\n3

    Please write a program which asks the user for a positive integer N. The program then prints out all numbers between -N and N inclusive, but leaves out the number 0. Each number should be printed on a separate line.

    An example of expected behaviour:

    Please type in a positive integer: 4\n-4\n-3\n-2\n-1\n1\n2\n3\n4

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    From a range to a list

    The function range returns a range object, which in many ways behaves like a list, but isn't actually one. If you try printing out the value the function returns, you will only see a description of a range object:

    numbers = range(2, 7)\nprint(numbers)

    range(2, 7)

    The function list will convert a range into a list. The list will contain all the values that are in the range. The Advanced Course in Programming course, which follows this one, will shed more light on this subject.

    numbers = list(range(2, 7))\nprint(numbers)

    [2, 3, 4, 5, 6]

    A reminder of the demands of the automatic tests

    So far, when the exercises have asked you to write functions, the templates have looked like this:

    # Write your solution here\n# You can test your function by calling it within the following block\nif __name__ == \"__main__\":\n    sentence = \"it was a dark and stormy python\"\n    print(first_word(sentence))\n    print(second_word(sentence))\n    print(last_word(sentence))

    From now on there will be no more reminders in the templates to use the if __name__ == \"__main__\" block. However, the automatic tests will still demand its use, so you will have to add the block yourself when you test your function within the main function of your program.

    NB: some exercises, like the Palindromes exercise coming up in this section, expect you to also write code which calls the function you wrote. This code should not be placed within an if __name__ == \"__main__\" block. The automatic tests will not execute any code within that block, so your solution will not be complete if you place your function calls there.

    Please write a function named list_of_stars, which takes a list of integers as its argument. The function should print out lines of star characters. The numbers in the list specify how many stars each line should contain.

    For example, with the function call list_of_stars([3, 7, 1, 1, 2]) the following should be printed out:

    \n***\n*******\n*\n*\n**\n

    Please write a function named anagrams, which takes two strings as arguments. The function returns True if the strings are anagrams of each other. Two words are anagrams if they contain exactly the same characters.

    Some examples of how the function should work:

    print(anagrams(\"tame\", \"meta\")) # True\nprint(anagrams(\"tame\", \"mate\")) # True\nprint(anagrams(\"tame\", \"team\")) # True\nprint(anagrams(\"tabby\", \"batty\")) # False\nprint(anagrams(\"python\", \"java\")) # False

    Hint: the function sorted can be used on strings as well.

    Please write a function named palindromes, which takes a string argument and returns True if the string is a palindrome. Palindromes are words which are spelled exactly the same backwards and forwards.

    Please also write a main program which asks the user to type in words until they type in a palindrome:

    Please type in a palindrome: python\nthat wasn't a palindrome\nPlease type in a palindrome: java\nthat wasn't a palindrome\nPlease type in a palindrome: oddoreven\nthat wasn't a palindrome\nPlease type in a palindrome: neveroddoreven\nneveroddoreven is a palindrome!

    NB:, the main program should not be within an if __name__ == \"__main__\": block

    Please write a function named sum_of_positives, which takes a list of integers as its argument. The function returns the sum of the positive values in the list.

    my_list = [1, -2, 3, -4, 5]\nresult = sum_of_positives(my_list)\nprint(\"The result is\", result)

    The result is 9

    In these exercises we will be using lists as arguments and return values. This was covered in the previous section, if you need a refresher.

    Please write a function named even_numbers, which takes a list of integers as an argument. The function returns a new list containing the even numbers from the original list.

    my_list = [1, 2, 3, 4, 5]\nnew_list = even_numbers(my_list)\nprint(\"original\", my_list)\nprint(\"new\", new_list)

    original [1, 2, 3, 4, 5]\nnew [2, 4]

    Please write a function named list_sum which takes two lists of integers as arguments. The function returns a new list which contains the sums of the items at each index in the two original lists. You may assume both lists have the same number of items.

    An example of the function at work:

    a = [1, 2, 3]\nb = [7, 8, 9]\nprint(list_sum(a, b)) # [8, 10, 12]

    Please write a function named distinct_numbers, which takes a list of integers as its argument. The function returns a new list containing the numbers from the original list in order of magnitude, and so that each distinct number is present only once.

    my_list = [3, 2, 2, 1, 3, 3, 1]\nprint(distinct_numbers(my_list)) # [1, 2, 3]

    Finding the best or the worst item in a list

    A very common programming task is finding the best or worst item in a list, according to some criteria. A simple solution is using a helper variable to \"remember\" which of the items processed so far was the most suitable. This temporary best choice is then compared to each item in turn, and at the end of the iteration the variable contains the best of the bunch.

    A rough draft which doesn't quite compile yet:

    best = initial_value # The initial value depends on the situation\nfor item in my_list:\n    if item is better than best:\n        best = item\n\n# We now have the best one figured out!

    The details of the final program code depend on the type of the items in the list, and also on the criteria for choosing the best (or worst) item. Sometimes you may need more than one helper variable.

    Let's practice this method a little.

    Please write a function named length_of_longest, which takes a list of strings as its argument. The function returns the length of the longest string.

    my_list = [\"first\", \"second\", \"fourth\", \"eleventh\"]\n\nresult = length_of_longest(my_list)\nprint(result)
    my_list = [\"adele\", \"mark\", \"dorothy\", \"tim\", \"hedy\", \"richard\"]\n\nresult = length_of_longest(my_list)\nprint(result)

    8\n7

    Please write a function named shortest, which takes a list of strings as its argument. The function returns whichever of the strings is the shortest. If more than one are equally short, the function can return any of the shortest strings (there will be no such situation in the tests). You may assume there will be no empty strings in the list.

    my_list = [\"first\", \"second\", \"fourth\", \"eleventh\"]\n\nresult = shortest(my_list)\nprint(result)
    my_list = [\"adele\", \"mark\", \"dorothy\", \"tim\", \"hedy\", \"richard\"]\n\nresult = shortest(my_list)\nprint(result)

    first\ntim

    Please write a function named all_the_longest, which takes a list of strings as its argument. The function should return a new list containing the longest string in the original list. If more than one are equally long, the function should return all of the longest strings.

    The order of the strings in the returned list should be the same as in the original.

    my_list = [\"first\", \"second\", \"fourth\", \"eleventh\"]\n\nresult = all_the_longest(my_list)\nprint(result) # ['eleventh']
    my_list = [\"adele\", \"mark\", \"dorothy\", \"tim\", \"hedy\", \"richard\"]\n\nresult = all_the_longest(my_list)\nprint(result) # ['dorothy', 'richard']
    ","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-4/4-definite-iteration.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know the difference between definite and indefinite iteration"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how a Python "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop works"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop to iterate through lists and strings"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can use a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop to go through the items in a list, just like we used while loops to go through strings. The following program prints out the items in the list, each on a separate line:"}]},{"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":"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":" "},{"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":"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":" "},{"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\nindex "},{"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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" index "},{"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":"my_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":"text","value":"my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"index"},{"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 index "},{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3\n2\n4\n5\n2"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This obviously works, but it is a rather complicated way of going through a list, as you have to use a helper variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"index"}]},{"type":"text","value":" to remember which item in the list you're at. Fortunately, Python offers a more intuitive way of traversing through lists, strings and other similar structures."}]},{"type":"element","tagName":"h2","properties":{"id":"the-for-loop","style":"position:relative;"},"children":[{"type":"text","value":"The for loop"},{"type":"element","tagName":"a","properties":{"href":"#the-for-loop","ariaLabel":"the for loop 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":"When you want to go through some ready collection of items, the Python "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop will do this for you. For instance, the loop can go through all items in a list from first to last."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When using a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop the program doesn't \"know\" beforehand how many iterations the loop will perform. It will repeat until the condition becomes false, or the loop is otherwise broken out of. That is why it falls under "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"indefinite iteration"}]},{"type":"text","value":". With a for loop the number of iterations is determined when the loop is set up, and so it falls under "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"definite iteration"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The idea is that the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop takes the items in the collection one by one and performs the same actions on each. The programmer does not have to worry about which item is being handled when. The syntax of the for loop is as follows:"}]},{"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":"for"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"variable"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"collection"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"block"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop takes an item in the collection, assigns it to the variable, processes the block of code, and moves on to the next item. When all items in the collection have been processed, execution of the program continues from the line after the loop."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 327px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/4e12b8e1e0df4791611d49d1d158a1b4/00e65/4_4_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: 214.3478260869565%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAArCAYAAAB4pah1AAAACXBIWXMAAAsTAAALEwEAmpwYAAADPUlEQVRIx62X2U4qQRCG5/3Da3hrIt5pjBGXqAiKC264I67gLiJinXx1UpM6Y88MmNNJZZqy++9a/qpuIwmM7+9v/fb7fRWvyxtRFmC73Zbb29v/B3h/fy83NzexbhjQKARmG19eXqTT6fwO0Bbz/fr60vnq6qpsbGzoHJ1fkwrIHweDgUpyYbFYlOnp6aAHIYkB/fj8/NREnJycyPv7uzw9Pcnp6anc3d3FGU8bYKnLh4eHcnR0pJuYr62tKcjBwYGCo9ve3paLiwu5urqSx8dHPci+Dw8PerhaiHJ8fFwmJydlb29Pzs/PZWFhQWNXrVYVHFlfX1cwdKzhoOvraxXmjUZDQaNms6lumsmXl5e6CYu73a5asL+/Lzs7OzGFQqPVasnz87NETKBHMnu4wanHx8e6MC0pJJOBIeBETF5fX+PFtoAEEEMAbXgmJL+pgDZ6vZ5aSOw+Pj4kxNdcQLPMAEiW8RCdEd4fHAS0GEIbknF2dqYUIdO1Wk3nZBYWWDwzLTRA4hUir20iyzSMoQEhM5RZXl6Wer2uFu7u7kq5XFZLV1ZWlMxDA+IWvwHa3NxUAL5wkC9JGgkQC81lCE+FAEqFMKgImu5IMXx7e9NsklUqCLGscwiJSwPUSvGAuEOG0VFBNAXIzRydHeiBMivF+Ga/x8bGZGJiIu7evuZ9GeJR0GXrg/AOPS0LYY67NIkQD/FiZmZG5ubm/jYHcwOwrAEHs3jI/RNhjXVjTjK3WIBAJTsQYvss+9ZvoYusxWMpNLHLCDKXSiWZnZ2Vra2toWijWfYuYQ2AWOu78Ug89D0wrZZtAJjFwxjQlLhPtyEEJMgL/KT0kjz8AZjsKLhMYuyhZILOPMnsh6HT8u7eXAu927gEgXGTmCLM7ZpMrs912WdymITkAsJ2yg3LoBHWkiTm/iWW3Edp/sNDDwiReXnNz88rwSuVioIWCgVZWlqKO48xwJ4t3JRBQKyi9XOF0q25FoxKvuMkyy/4PsyKIa8J3wNDz7wfgJAb64gJDRZhjku+daW+D0McMwuoZeLldXlcDT6J/Rvb3AxxLxcwaaFdob+y0A/bPDU1JYuLiz9eXiMDppF3mH98/gAHC+9HMp5l8gAAAABJRU5ErkJggg=='); 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":["/static/4e12b8e1e0df4791611d49d1d158a1b4/a0b58/4_4_1.webp 230w","/static/4e12b8e1e0df4791611d49d1d158a1b4/e25e9/4_4_1.webp 327w"],"sizes":"(max-width: 327px) 100vw, 327px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/4e12b8e1e0df4791611d49d1d158a1b4/81c8e/4_4_1.png 230w","/static/4e12b8e1e0df4791611d49d1d158a1b4/00e65/4_4_1.png 327w"],"sizes":"(max-width: 327px) 100vw, 327px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/4e12b8e1e0df4791611d49d1d158a1b4/00e65/4_4_1.png","alt":"Iterating through a list","title":"Iterating through a list","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 following program prints out all the items in a list using a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop:"}]},{"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":"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":" "},{"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":"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":" "},{"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"item"},{"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":"3\n2\n4\n5\n2"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Compared to the example at the beginning of this section, the structure is much easier to understand. A "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop makes straightforward traversal through a collection of items very simple."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The same principle applies to characters in 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":"name "},{"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 your name: \""}]},{"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":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" name"},{"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":"character"},{"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 your name: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Grace"}]},{"type":"text","value":"\nG\nr\na\nc\ne"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Star-studded","tmcname":"part04-20_star_studded"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in a string. The program then prints each input character on a separate line. After each character there should be a star (*) printed on its own line."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is how it should work:"}]},{"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":"Python"}]},{"type":"text","value":"\nP\n*\ny\n*\nt\n*\nh\n*\no\n*\nn\n*"}]}]},{"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 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."}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-function-range","style":"position:relative;"},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"element","tagName":"a","properties":{"href":"#the-function-range","ariaLabel":"the function range 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":"Often you know how many times you want to repeat a certain bit of code. You might, for example, wish to go through all the numbers between 1 and 100. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":" function plugged into a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop will do this for you."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are a few different ways to call the range function. The simplest way is to give the function just one argument, which signifies the end-point of the range. The end-point itself is excluded, just like with string slices. In other words, the function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range(n)"}]},{"type":"text","value":" provides a loop with a range from 0 to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n-1"}]},{"type":"text","value":":"}]},{"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":"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":"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","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":"i"},{"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\n1\n2\n3\n4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With two arguments, the function will return a range between the two numbers. The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range(a,b)"}]},{"type":"text","value":" provides a range starting from "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" and ending at "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b-1"}]},{"type":"text","value":":"}]},{"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":"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":"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":" "},{"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":"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":"i"},{"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":"3\n4\n5\n6"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Finally, with a third argument you can also specify the size of the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"step"}]},{"type":"text","value":" the range takes between each value. The function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range(a, b, c)"}]},{"type":"text","value":" provides a range starting from "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":", ending at "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b-1"}]},{"type":"text","value":", and changing by "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"c"}]},{"type":"text","value":" with every 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":"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"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":"\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":"i"},{"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":"1\n3\n5\n7"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A step can also be negative. Then the range will be in reversed orded. Notice the first two arguments are also flipped here:"}]},{"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":"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":"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":" "},{"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":" "},{"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":"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":"i"},{"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":"6\n5\n4\n3"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"From negative to positive","tmcname":"part04-21_negative_to_positive"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for a positive integer N. The program then prints out all numbers between -N and N inclusive, but "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"leaves out the number 0"}]},{"type":"text","value":". Each number should be printed on a separate line."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected behaviour:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a positive integer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n-4\n-3\n-2\n-1\n1\n2\n3\n4"}]}]},{"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 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."}]}]},{"type":"element","tagName":"h2","properties":{"id":"from-a-range-to-a-list","style":"position:relative;"},"children":[{"type":"text","value":"From a range to a list"},{"type":"element","tagName":"a","properties":{"href":"#from-a-range-to-a-list","ariaLabel":"from a range to a list 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 function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":" returns a range object, which in many ways behaves like a list, but isn't actually one. If you try printing out the value the function returns, you will only see a description of a range object:"}]},{"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":"numbers "},{"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":"range"}]},{"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":" "},{"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":"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":"numbers"},{"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":"range(2, 7)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list"}]},{"type":"text","value":" will convert a range into a list. The list will contain all the values that are in the range. The Advanced Course in Programming course, which follows this one, will shed more light on this subject."}]},{"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":"numbers "},{"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":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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":"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":" "},{"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":"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":"numbers"},{"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":"[2, 3, 4, 5, 6]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"a-reminder-of-the-demands-of-the-automatic-tests","style":"position:relative;"},"children":[{"type":"text","value":"A reminder of the demands of the automatic tests"},{"type":"element","tagName":"a","properties":{"href":"#a-reminder-of-the-demands-of-the-automatic-tests","ariaLabel":"a reminder of the demands of the automatic tests 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":"So far, when the exercises have asked you to write functions, the templates have looked 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","comment"]},"children":[{"type":"text","value":"# Write your solution here"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# You can test your function by calling it within the following 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 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":"\"it was a dark and stormy python\""}]},{"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":"first_word"},{"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":"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":"second_word"},{"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":"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":"last_word"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"From now on there will be no more reminders in the templates to use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" block. However, the automatic tests will still demand its use, so you will have to add the block yourself when you test your function within the main function of your program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" some exercises, like the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Palindromes"}]},{"type":"text","value":" exercise coming up in this section, expect you to also write code which calls the function you wrote. This code should "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" be placed within an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" block. The automatic tests will not execute any code within that block, so your solution will not be complete if you place your function calls there."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"List of stars","tmcname":"part04-22_list_of_stars"},"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":"list_of_stars"}]},{"type":"text","value":", which takes a list of integers as its argument. The function should print out lines of star characters. The numbers in the list specify how many stars each line should contain."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, with the function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list_of_stars([3, 7, 1, 1, 2])"}]},{"type":"text","value":" the following should be printed out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"***\n*******\n*\n*\n**\n"}]}]},{"type":"comment","value":" **Huomaa** että tällä hetkellä Windowsissa on ongelmia joidenkin tehtävien testien suorittamisessa. Jos törmäät seuraavaan virheilmoitukseen\n\n\"Listan\n\nvoit suorittaa testit lähettämällä ne palvelimelle valitsemalla testien suoritusnapin oikealla puolella olevasta symbolista avautuvasta TMC-valikosta _Submit solutions_.\n\nOngelman saa korjattua menemällä laajennuksen asennusvalikkoon ja muuttamalla \"TMC Data\" -kohdassa tehtävien sijainnin johonkin toiseen sijaintiin, jonka tiedostopolku on lyhempi, allaolevassa kuvassa nappi _change path_. Siirrossa saattaa kestää hetken, joten odotathan operaation päättymistä.\n\n\"Listan\n\nOngelmaan pyritään saamaan parempi ratkaisu lähipäivinä. "}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Anagrams","tmcname":"part04-23_anagrams"},"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":"anagrams"}]},{"type":"text","value":", which takes two strings as arguments. The function returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" if the strings are anagrams of each other. Two words are anagrams if they contain exactly the same characters."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples 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":"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":"anagrams"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tame\""}]},{"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":"\"meta\""}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# True"}]},{"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":"anagrams"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tame\""}]},{"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":"\"mate\""}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# True"}]},{"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":"anagrams"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tame\""}]},{"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":"\"team\""}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# True"}]},{"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":"anagrams"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tabby\""}]},{"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":"\"batty\""}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# False"}]},{"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":"anagrams"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hint: the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" can be used on strings as well."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Palindromes","tmcname":"part04-24_palindromes"},"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":"palindromes"}]},{"type":"text","value":", which takes a string argument and returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" if the string is a palindrome. Palindromes are words which are spelled exactly the same backwards and forwards."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please also write a main program which asks the user to type in words until they type in a palindrome:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a palindrome: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"python"}]},{"type":"text","value":"\nthat wasn't a palindrome\nPlease type in a palindrome: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"java"}]},{"type":"text","value":"\nthat wasn't a palindrome\nPlease type in a palindrome: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"oddoreven"}]},{"type":"text","value":"\nthat wasn't a palindrome\nPlease type in a palindrome: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"neveroddoreven"}]},{"type":"text","value":"\nneveroddoreven is a palindrome!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":", the main program "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"should not be"}]},{"type":"text","value":" within an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\":"}]},{"type":"text","value":" block"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The sum of positive numbers","tmcname":"part04-25_sum_of_positives"},"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":"sum_of_positives"}]},{"type":"text","value":", which takes a list of integers as its argument. The function returns the sum of the positive values in the 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":"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":"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":"2"}]},{"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":"3"}]},{"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":"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":"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":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sum_of_positives"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The result is\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The result is 9"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In these exercises we will be using lists as arguments and return values. This was covered in the "},{"type":"element","tagName":"a","properties":{"href":"/part-4/3-lists#a-list-as-an-argument-or-a-return-value"},"children":[{"type":"text","value":"previous section"}]},{"type":"text","value":", if you need a refresher."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Even numbers","tmcname":"part04-26_even_numbers"},"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":"even_numbers"}]},{"type":"text","value":", which takes a list of integers as an argument. The function returns a new list containing the even numbers from the original 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":"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":"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":" "},{"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":" "},{"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":"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":"\nnew_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" even_numbers"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"original\""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"new\""}]},{"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":"original [1, 2, 3, 4, 5]\nnew [2, 4]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The sum of lists","tmcname":"part04-27_sum_of_lists"},"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":"list_sum"}]},{"type":"text","value":" which takes two lists of integers as arguments. The function returns a new list which contains the sums of the items at each index in the two original lists. You may assume both lists have the same number of items."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function at 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":"a "},{"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":"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":" "},{"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":"\nb "},{"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":"7"}]},{"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":"8"}]},{"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":"9"}]},{"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":"list_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# [8, 10, 12]"}]}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Distinct numbers","tmcname":"part04-28_distinct_numbers"},"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":"distinct_numbers"}]},{"type":"text","value":", which takes a list of integers as its argument. The function returns a new list containing the numbers from the original list in order of magnitude, and so that each distinct number is present only once."}]},{"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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"},{"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":"distinct_numbers"},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# [1, 2, 3]"}]}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"finding-the-best-or-the-worst-item-in-a-list","style":"position:relative;"},"children":[{"type":"text","value":"Finding the best or the worst item in a list"},{"type":"element","tagName":"a","properties":{"href":"#finding-the-best-or-the-worst-item-in-a-list","ariaLabel":"finding the best or the worst item in a list 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":"A very common programming task is finding the best or worst item in a list, according to some criteria. A simple solution is using a helper variable to \"remember\" which of the items processed so far was the most suitable. This temporary best choice is then compared to each item in turn, and at the end of the iteration the variable contains the best of the bunch."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A rough draft which doesn't quite compile yet:"}]},{"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":"best "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" initial_value "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# The initial value depends on the situation"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"if"}]},{"type":"text","value":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" better than best"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n best "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" item\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# We now have the best one figured out!"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The details of the final program code depend on the type of the items in the list, and also on the criteria for choosing the best (or worst) item. Sometimes you may need more than one helper variable."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's practice this method a little."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The length of the longest in the list","tmcname":"part04-29_length_of_longest"},"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":"length_of_longest"}]},{"type":"text","value":", which takes a list of strings as its argument. The function returns the length of the longest 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_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":"\"first\""}]},{"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":"\"second\""}]},{"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":"\"fourth\""}]},{"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":"\"eleventh\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" length_of_longest"},{"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":"result"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"\"adele\""}]},{"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":"\"mark\""}]},{"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":"\"dorothy\""}]},{"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":"\"tim\""}]},{"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":"\"hedy\""}]},{"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":"\"richard\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" length_of_longest"},{"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":"result"},{"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":"8\n7"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The shortest in the list","tmcname":"part04-30_shortest_in_list"},"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":"shortest"}]},{"type":"text","value":", which takes a list of strings as its argument. The function returns whichever of the strings is the shortest. If more than one are equally short, the function can return any of the shortest strings (there will be no such situation in the tests). You may assume there will be no empty strings in the 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":"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":"\"first\""}]},{"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":"\"second\""}]},{"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":"\"fourth\""}]},{"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":"\"eleventh\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" shortest"},{"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":"result"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"\"adele\""}]},{"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":"\"mark\""}]},{"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":"\"dorothy\""}]},{"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":"\"tim\""}]},{"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":"\"hedy\""}]},{"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":"\"richard\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" shortest"},{"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":"result"},{"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":"first\ntim"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"All the longest in the list","tmcname":"part04-31_all_longest_in_list"},"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":"all_the_longest"}]},{"type":"text","value":", which takes a list of strings as its argument. The function should return a new list containing the longest string in the original list. If more than one are equally long, the function should return all of the longest strings."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The order of the strings in the returned list should be the same as in the original."}]},{"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":"\"first\""}]},{"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":"\"second\""}]},{"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":"\"fourth\""}]},{"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":"\"eleventh\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" all_the_longest"},{"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":"result"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ['eleventh']"}]}]}]}]},{"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":"\"adele\""}]},{"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":"\"mark\""}]},{"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":"\"dorothy\""}]},{"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":"\"tim\""}]},{"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":"\"hedy\""}]},{"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":"\"richard\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" all_the_longest"},{"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":"result"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ['dorothy', 'richard']"}]}]}]}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will know the difference between definite and indefinite iteration
    • \n
    • You will know how a Python for loop works
    • \n
    • You will be able to use a for loop to iterate through lists and strings
    • \n

    You can use a while loop to go through the items in a list, just like we used while loops to go through strings. The following program prints out the items in the list, each on a separate line:

    my_list = [3, 2, 4, 5, 2]\n\nindex = 0\nwhile index < len(my_list):\n    print(my_list[index])\n    index += 1

    3\n2\n4\n5\n2

    This obviously works, but it is a rather complicated way of going through a list, as you have to use a helper variable index to remember which item in the list you're at. Fortunately, Python offers a more intuitive way of traversing through lists, strings and other similar structures.

    The for loop

    When you want to go through some ready collection of items, the Python for loop will do this for you. For instance, the loop can go through all items in a list from first to last.

    When using a while loop the program doesn't \"know\" beforehand how many iterations the loop will perform. It will repeat until the condition becomes false, or the loop is otherwise broken out of. That is why it falls under indefinite iteration. With a for loop the number of iterations is determined when the loop is set up, and so it falls under definite iteration.

    The idea is that the for loop takes the items in the collection one by one and performs the same actions on each. The programmer does not have to worry about which item is being handled when. The syntax of the for loop is as follows:

    for <variable> in <collection>:\n    <block>

    The for loop takes an item in the collection, assigns it to the variable, processes the block of code, and moves on to the next item. When all items in the collection have been processed, execution of the program continues from the line after the loop.

    \n \n \n \n \n \n \"Iterating\n \n \n

    The following program prints out all the items in a list using a for loop:

    my_list = [3, 2, 4, 5, 2]\n\nfor item in my_list:\n    print(item)

    3\n2\n4\n5\n2

    Compared to the example at the beginning of this section, the structure is much easier to understand. A for loop makes straightforward traversal through a collection of items very simple.

    The same principle applies to characters in a string:

    name = input(\"Please type in your name: \")\n\nfor character in name:\n    print(character)

    Please type in your name: Grace\nG\nr\na\nc\ne

    Please write a program which asks the user to type in a string. The program then prints each input character on a separate line. After each character there should be a star (*) printed on its own line.

    This is how it should work:

    Please type in a string: Python\nP\n*\ny\n*\nt\n*\nh\n*\no\n*\nn\n*

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    The function range

    Often you know how many times you want to repeat a certain bit of code. You might, for example, wish to go through all the numbers between 1 and 100. The range function plugged into a for loop will do this for you.

    There are a few different ways to call the range function. The simplest way is to give the function just one argument, which signifies the end-point of the range. The end-point itself is excluded, just like with string slices. In other words, the function call range(n) provides a loop with a range from 0 to n-1:

    for i in range(5):\n    print(i)

    0\n1\n2\n3\n4

    With two arguments, the function will return a range between the two numbers. The function range(a,b) provides a range starting from a and ending at b-1:

    for i in range(3, 7):\n    print(i)

    3\n4\n5\n6

    Finally, with a third argument you can also specify the size of the step the range takes between each value. The function call range(a, b, c) provides a range starting from a, ending at b-1, and changing by c with every step:

    for i in range(1, 9, 2):\n    print(i)

    1\n3\n5\n7

    A step can also be negative. Then the range will be in reversed orded. Notice the first two arguments are also flipped here:

    for i in range(6, 2, -1):\n    print(i)

    6\n5\n4\n3

    Please write a program which asks the user for a positive integer N. The program then prints out all numbers between -N and N inclusive, but leaves out the number 0. Each number should be printed on a separate line.

    An example of expected behaviour:

    Please type in a positive integer: 4\n-4\n-3\n-2\n-1\n1\n2\n3\n4

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    From a range to a list

    The function range returns a range object, which in many ways behaves like a list, but isn't actually one. If you try printing out the value the function returns, you will only see a description of a range object:

    numbers = range(2, 7)\nprint(numbers)

    range(2, 7)

    The function list will convert a range into a list. The list will contain all the values that are in the range. The Advanced Course in Programming course, which follows this one, will shed more light on this subject.

    numbers = list(range(2, 7))\nprint(numbers)

    [2, 3, 4, 5, 6]

    A reminder of the demands of the automatic tests

    So far, when the exercises have asked you to write functions, the templates have looked like this:

    # Write your solution here\n# You can test your function by calling it within the following block\nif __name__ == \"__main__\":\n    sentence = \"it was a dark and stormy python\"\n    print(first_word(sentence))\n    print(second_word(sentence))\n    print(last_word(sentence))

    From now on there will be no more reminders in the templates to use the if __name__ == \"__main__\" block. However, the automatic tests will still demand its use, so you will have to add the block yourself when you test your function within the main function of your program.

    NB: some exercises, like the Palindromes exercise coming up in this section, expect you to also write code which calls the function you wrote. This code should not be placed within an if __name__ == \"__main__\" block. The automatic tests will not execute any code within that block, so your solution will not be complete if you place your function calls there.

    Please write a function named list_of_stars, which takes a list of integers as its argument. The function should print out lines of star characters. The numbers in the list specify how many stars each line should contain.

    For example, with the function call list_of_stars([3, 7, 1, 1, 2]) the following should be printed out:

    \n***\n*******\n*\n*\n**\n

    Please write a function named anagrams, which takes two strings as arguments. The function returns True if the strings are anagrams of each other. Two words are anagrams if they contain exactly the same characters.

    Some examples of how the function should work:

    print(anagrams(\"tame\", \"meta\")) # True\nprint(anagrams(\"tame\", \"mate\")) # True\nprint(anagrams(\"tame\", \"team\")) # True\nprint(anagrams(\"tabby\", \"batty\")) # False\nprint(anagrams(\"python\", \"java\")) # False

    Hint: the function sorted can be used on strings as well.

    Please write a function named palindromes, which takes a string argument and returns True if the string is a palindrome. Palindromes are words which are spelled exactly the same backwards and forwards.

    Please also write a main program which asks the user to type in words until they type in a palindrome:

    Please type in a palindrome: python\nthat wasn't a palindrome\nPlease type in a palindrome: java\nthat wasn't a palindrome\nPlease type in a palindrome: oddoreven\nthat wasn't a palindrome\nPlease type in a palindrome: neveroddoreven\nneveroddoreven is a palindrome!

    NB:, the main program should not be within an if __name__ == \"__main__\": block

    Please write a function named sum_of_positives, which takes a list of integers as its argument. The function returns the sum of the positive values in the list.

    my_list = [1, -2, 3, -4, 5]\nresult = sum_of_positives(my_list)\nprint(\"The result is\", result)

    The result is 9

    In these exercises we will be using lists as arguments and return values. This was covered in the previous section, if you need a refresher.

    Please write a function named even_numbers, which takes a list of integers as an argument. The function returns a new list containing the even numbers from the original list.

    my_list = [1, 2, 3, 4, 5]\nnew_list = even_numbers(my_list)\nprint(\"original\", my_list)\nprint(\"new\", new_list)

    original [1, 2, 3, 4, 5]\nnew [2, 4]

    Please write a function named list_sum which takes two lists of integers as arguments. The function returns a new list which contains the sums of the items at each index in the two original lists. You may assume both lists have the same number of items.

    An example of the function at work:

    a = [1, 2, 3]\nb = [7, 8, 9]\nprint(list_sum(a, b)) # [8, 10, 12]

    Please write a function named distinct_numbers, which takes a list of integers as its argument. The function returns a new list containing the numbers from the original list in order of magnitude, and so that each distinct number is present only once.

    my_list = [3, 2, 2, 1, 3, 3, 1]\nprint(distinct_numbers(my_list)) # [1, 2, 3]

    Finding the best or the worst item in a list

    A very common programming task is finding the best or worst item in a list, according to some criteria. A simple solution is using a helper variable to \"remember\" which of the items processed so far was the most suitable. This temporary best choice is then compared to each item in turn, and at the end of the iteration the variable contains the best of the bunch.

    A rough draft which doesn't quite compile yet:

    best = initial_value # The initial value depends on the situation\nfor item in my_list:\n    if item is better than best:\n        best = item\n\n# We now have the best one figured out!

    The details of the final program code depend on the type of the items in the list, and also on the criteria for choosing the best (or worst) item. Sometimes you may need more than one helper variable.

    Let's practice this method a little.

    Please write a function named length_of_longest, which takes a list of strings as its argument. The function returns the length of the longest string.

    my_list = [\"first\", \"second\", \"fourth\", \"eleventh\"]\n\nresult = length_of_longest(my_list)\nprint(result)
    my_list = [\"adele\", \"mark\", \"dorothy\", \"tim\", \"hedy\", \"richard\"]\n\nresult = length_of_longest(my_list)\nprint(result)

    8\n7

    Please write a function named shortest, which takes a list of strings as its argument. The function returns whichever of the strings is the shortest. If more than one are equally short, the function can return any of the shortest strings (there will be no such situation in the tests). You may assume there will be no empty strings in the list.

    my_list = [\"first\", \"second\", \"fourth\", \"eleventh\"]\n\nresult = shortest(my_list)\nprint(result)
    my_list = [\"adele\", \"mark\", \"dorothy\", \"tim\", \"hedy\", \"richard\"]\n\nresult = shortest(my_list)\nprint(result)

    first\ntim

    Please write a function named all_the_longest, which takes a list of strings as its argument. The function should return a new list containing the longest string in the original list. If more than one are equally long, the function should return all of the longest strings.

    The order of the strings in the returned list should be the same as in the original.

    my_list = [\"first\", \"second\", \"fourth\", \"eleventh\"]\n\nresult = all_the_longest(my_list)\nprint(result) # ['eleventh']
    my_list = [\"adele\", \"mark\", \"dorothy\", \"tim\", \"hedy\", \"richard\"]\n\nresult = all_the_longest(my_list)\nprint(result) # ['dorothy', 'richard']
    ","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-4/4-definite-iteration.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-4/5-print-statement-formatting/page-data.json b/page-data/part-4/5-print-statement-formatting/page-data.json index ae8949906..4139cc79d 100644 --- a/page-data/part-4/5-print-statement-formatting/page-data.json +++ b/page-data/part-4/5-print-statement-formatting/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-4/5-print-statement-formatting", - "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 know how to use arguments to format the result of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use f-strings to format printouts"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So far we have learnt three methods for formulating the argument given to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The first is the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" operator for strings. It allows simple concatenation of string segments:"}]},{"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":"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":"\"Mark\""}]},{"type":"text","value":"\nage "},{"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":"37"}]},{"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":"\"Hi \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"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":"\" your age is \""}]},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"age"},{"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":"\" years\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This method will not work if any of the segments are not strings. In the example above, the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"age"}]},{"type":"text","value":" has been converted into a string with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":" function, since it is an integer and cannot be concatenated as is."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The second method is considering each segment of the argument as a separate argument, and splitting them up with commas:"}]},{"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":"\"Hi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"\"your age is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" age"},{"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":"\"years\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This code produces the exact same result as the previous version. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command normally adds a space character between each argument.The advantage here is that the segments can be of different types, so there is no need to convert anything into a string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you want to remove the automatically added spaces, you can add a special named argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sep"}]},{"type":"text","value":":"}]},{"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":"\"Hi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"\"your age is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" age"},{"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":"\"years\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sep"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"p","properties":{},"children":[{"type":"text","value":"This prints out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"HiMarkyour age is37years"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sep=\"\""}]},{"type":"text","value":" is a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"keyword argument"}]},{"type":"text","value":", and its name is short for "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"separator"}]},{"type":"text","value":". It specifies that the other arguments should now be separated by an empty string. You can set the separator to any string you like. For example, if you wanted each argument on a separate line, you could set the separator to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"text","value":", which is the newline character:"}]},{"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":"\"Hi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"\"your age is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" age"},{"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":"\"years\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sep"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"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":"Hi\nMark\nyour age is\n37\nyears"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"By default, the print command always ends in a newline character, but you can change this as well. The keyword argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"end"}]},{"type":"text","value":" specifies what is put at the end of a line. Setting "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"end"}]},{"type":"text","value":" to an empty string means that there is no newline character at the end of the printout:"}]},{"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":"\"Hi \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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"},{"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":"\"there!\""}]},{"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":"Hi there!"}]}]},{"type":"element","tagName":"h2","properties":{"id":"f-strings","style":"position:relative;"},"children":[{"type":"text","value":"f-strings"},{"type":"element","tagName":"a","properties":{"href":"#f-strings","ariaLabel":"f 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 third method to prepare strings is f-strings. The previous example with the name and the age would look like this formulated with f-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":"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":"\"Erkki\""}]},{"type":"text","value":"\nage "},{"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":"39"}]},{"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\"Hi "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" your age 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":"age"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"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":"Thus far we have only used very simple f-strings, but they can be very versatile in formatting string type content. One very common use case is setting the number of decimals that are printed out with a floating point number. By default the number is quite high:"}]},{"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":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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\"The number 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":"number"},{"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":"The number is 0.333333333333333"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The specific format we want the number to be displayed in can be set within the curly brackets of the variable expression. Let's add a colon character and a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"format specifier"}]},{"type":"text","value":" after the variable name:"}]},{"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":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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\"The number 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":"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":"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":"The number "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.33"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The format specifier "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":".2f"}]},{"type":"text","value":" states that we want to display 2 decimals. The letter "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"f"}]},{"type":"text","value":" at the end means that we want the variable to be displayed as a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]},{"type":"text","value":", i.e. a floating point number."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here's another example, where we specify the amount of whitespace reserved for the variable in the printout. Both times the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" is included in the resulting string, it has a space of 15 characters reserved. First the names are justified to the left, and then they are justified to the right:"}]},{"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":"names "},{"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":"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":"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":"for"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" names"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"name"},{"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":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" centre "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"name"},{"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":">15"}]},{"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":"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":"Steve centre Steve\nJean centre Jean\nKatherine centre Katherine\nPaul centre Paul"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The uses of f-strings are not restricted to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" commands. They can be assigned to variables and combined with other 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":"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":"\"Larry\""}]},{"type":"text","value":"\nage "},{"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":"48"}]},{"type":"text","value":"\ncity "},{"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":"\"Palo Alto\""}]},{"type":"text","value":"\ngreeting "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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\"Hi "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", you are "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"age"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" years of age\""}]}]},{"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":"greeting "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"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\", and you live 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":"city"},{"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":"Hi Larry, you are 48 years of age, and you live in Palo Alto"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can think of an f-string as a sort of function, which creates a normal string based on the \"arguments\" within the curly brackets."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Integers to strings","tmcname":"part04-32_integers_to_strings"},"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":"formatted"}]},{"type":"text","value":", which takes a list of floating point numbers as its argument. The function returns a new list, which contains each element of the original list in string format, rounded to two decimal points. The order of the items in the list should remain unchanged."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Hint: use f-strings to format the floating point numbers into suitable strings."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected beahviour:"}]},{"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.234"}]},{"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":"0.3333"}]},{"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":"0.11111"}]},{"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":"3.446"}]},{"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":" formatted"},{"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":"['1.23', '0.33', '0.11', '3.45']"}]}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will know how to use arguments to format the result of the print command
    • \n
    • You will be able to use f-strings to format printouts
    • \n

    So far we have learnt three methods for formulating the argument given to the print command.

    The first is the + operator for strings. It allows simple concatenation of string segments:

    name = \"Mark\"\nage = 37\nprint(\"Hi \" + name + \" your age is \" + str(age) + \" years\" )

    This method will not work if any of the segments are not strings. In the example above, the variable age has been converted into a string with the str function, since it is an integer and cannot be concatenated as is.

    The second method is considering each segment of the argument as a separate argument, and splitting them up with commas:

    print(\"Hi\", name, \"your age is\", age, \"years\" )

    This code produces the exact same result as the previous version. The print command normally adds a space character between each argument.The advantage here is that the segments can be of different types, so there is no need to convert anything into a string.

    If you want to remove the automatically added spaces, you can add a special named argument sep:

    print(\"Hi\", name, \"your age is\", age, \"years\", sep=\"\")

    This prints out

    HiMarkyour age is37years

    The argument sep=\"\" is a keyword argument, and its name is short for separator. It specifies that the other arguments should now be separated by an empty string. You can set the separator to any string you like. For example, if you wanted each argument on a separate line, you could set the separator to \"\\n\", which is the newline character:

    print(\"Hi\", name, \"your age is\", age, \"years\", sep=\"\\n\")

    Hi\nMark\nyour age is\n37\nyears

    By default, the print command always ends in a newline character, but you can change this as well. The keyword argument end specifies what is put at the end of a line. Setting end to an empty string means that there is no newline character at the end of the printout:

    print(\"Hi \", end=\"\")\nprint(\"there!\")

    Hi there!

    f-strings

    The third method to prepare strings is f-strings. The previous example with the name and the age would look like this formulated with f-strings:

    name = \"Erkki\"\nage = 39\nprint(f\"Hi {name} your age is {age} years\")

    Thus far we have only used very simple f-strings, but they can be very versatile in formatting string type content. One very common use case is setting the number of decimals that are printed out with a floating point number. By default the number is quite high:

    number = 1/3\nprint(f\"The number is {number}\")

    The number is 0.333333333333333

    The specific format we want the number to be displayed in can be set within the curly brackets of the variable expression. Let's add a colon character and a format specifier after the variable name:

    number = 1/3\nprint(f\"The number is {number:.2f}\")
    The number is 0.33

    The format specifier .2f states that we want to display 2 decimals. The letter f at the end means that we want the variable to be displayed as a float, i.e. a floating point number.

    Here's another example, where we specify the amount of whitespace reserved for the variable in the printout. Both times the variable name is included in the resulting string, it has a space of 15 characters reserved. First the names are justified to the left, and then they are justified to the right:

    names =  [ \"Steve\", \"Jean\", \"Katherine\", \"Paul\" ]\nfor name in names:\n  print(f\"{name:15} centre {name:>15}\")
    Steve           centre           Steve\nJean            centre            Jean\nKatherine       centre       Katherine\nPaul            centre            Paul

    The uses of f-strings are not restricted to print commands. They can be assigned to variables and combined with other strings:

    name = \"Larry\"\nage = 48\ncity = \"Palo Alto\"\ngreeting = f\"Hi {name}, you are {age} years of age\"\nprint(greeting + f\", and you live in {city}\")

    Hi Larry, you are 48 years of age, and you live in Palo Alto

    You can think of an f-string as a sort of function, which creates a normal string based on the \"arguments\" within the curly brackets.

    Please write a function named formatted, which takes a list of floating point numbers as its argument. The function returns a new list, which contains each element of the original list in string format, rounded to two decimal points. The order of the items in the list should remain unchanged.

    Hint: use f-strings to format the floating point numbers into suitable strings.

    An example of expected beahviour:

    my_list = [1.234, 0.3333, 0.11111, 3.446]\nnew_list = formatted(my_list)\nprint(new_list)

    ['1.23', '0.33', '0.11', '3.45']

    ","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-4/5-print-statement-formatting.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know how to use arguments to format the result of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use f-strings to format printouts"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So far we have learnt three methods for formulating the argument given to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The first is the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" operator for strings. It allows simple concatenation of string segments:"}]},{"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":"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":"\"Mark\""}]},{"type":"text","value":"\nage "},{"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":"37"}]},{"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":"\"Hi \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"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":"\" your age is \""}]},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"age"},{"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":"\" years\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This method will not work if any of the segments are not strings. In the example above, the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"age"}]},{"type":"text","value":" has been converted into a string with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":" function, since it is an integer and cannot be concatenated as is."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The second method is considering each segment of the argument as a separate argument, and splitting them up with commas:"}]},{"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":"\"Hi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"\"your age is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" age"},{"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":"\"years\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This code produces the exact same result as the previous version. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command normally adds a space character between each argument.The advantage here is that the segments can be of different types, so there is no need to convert anything into a string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you want to remove the automatically added spaces, you can add a special named argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sep"}]},{"type":"text","value":":"}]},{"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":"\"Hi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"\"your age is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" age"},{"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":"\"years\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sep"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"p","properties":{},"children":[{"type":"text","value":"This prints out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"HiMarkyour age is37years"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sep=\"\""}]},{"type":"text","value":" is a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"keyword argument"}]},{"type":"text","value":", and its name is short for "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"separator"}]},{"type":"text","value":". It specifies that the other arguments should now be separated by an empty string. You can set the separator to any string you like. For example, if you wanted each argument on a separate line, you could set the separator to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"text","value":", which is the newline character:"}]},{"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":"\"Hi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"\"your age is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" age"},{"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":"\"years\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sep"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"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":"Hi\nMark\nyour age is\n37\nyears"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"By default, the print command always ends in a newline character, but you can change this as well. The keyword argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"end"}]},{"type":"text","value":" specifies what is put at the end of a line. Setting "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"end"}]},{"type":"text","value":" to an empty string means that there is no newline character at the end of the printout:"}]},{"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":"\"Hi \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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"},{"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":"\"there!\""}]},{"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":"Hi there!"}]}]},{"type":"element","tagName":"h2","properties":{"id":"f-strings","style":"position:relative;"},"children":[{"type":"text","value":"f-strings"},{"type":"element","tagName":"a","properties":{"href":"#f-strings","ariaLabel":"f 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 third method to prepare strings is f-strings. The previous example with the name and the age would look like this formulated with f-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":"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":"\"Erkki\""}]},{"type":"text","value":"\nage "},{"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":"39"}]},{"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\"Hi "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" your age 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":"age"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"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":"Thus far we have only used very simple f-strings, but they can be very versatile in formatting string type content. One very common use case is setting the number of decimals that are printed out with a floating point number. By default the number is quite high:"}]},{"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":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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\"The number 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":"number"},{"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":"The number is 0.333333333333333"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The specific format we want the number to be displayed in can be set within the curly brackets of the variable expression. Let's add a colon character and a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"format specifier"}]},{"type":"text","value":" after the variable name:"}]},{"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":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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\"The number 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":"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":"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":"The number "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0.33"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The format specifier "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":".2f"}]},{"type":"text","value":" states that we want to display 2 decimals. The letter "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"f"}]},{"type":"text","value":" at the end means that we want the variable to be displayed as a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]},{"type":"text","value":", i.e. a floating point number."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here's another example, where we specify the amount of whitespace reserved for the variable in the printout. Both times the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" is included in the resulting string, it has a space of 15 characters reserved. First the names are justified to the left, and then they are justified to the right:"}]},{"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":"names "},{"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":"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":"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":"for"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" names"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"name"},{"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":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" centre "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"name"},{"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":">15"}]},{"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":"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":"Steve centre Steve\nJean centre Jean\nKatherine centre Katherine\nPaul centre Paul"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The uses of f-strings are not restricted to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" commands. They can be assigned to variables and combined with other 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":"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":"\"Larry\""}]},{"type":"text","value":"\nage "},{"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":"48"}]},{"type":"text","value":"\ncity "},{"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":"\"Palo Alto\""}]},{"type":"text","value":"\ngreeting "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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\"Hi "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", you are "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"age"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" years of age\""}]}]},{"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":"greeting "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"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\", and you live 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":"city"},{"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":"Hi Larry, you are 48 years of age, and you live in Palo Alto"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can think of an f-string as a sort of function, which creates a normal string based on the \"arguments\" within the curly brackets."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Integers to strings","tmcname":"part04-32_integers_to_strings"},"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":"formatted"}]},{"type":"text","value":", which takes a list of floating point numbers as its argument. The function returns a new list, which contains each element of the original list in string format, rounded to two decimal points. The order of the items in the list should remain unchanged."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Hint: use f-strings to format the floating point numbers into suitable strings."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of expected beahviour:"}]},{"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.234"}]},{"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":"0.3333"}]},{"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":"0.11111"}]},{"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":"3.446"}]},{"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":" formatted"},{"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":"['1.23', '0.33', '0.11', '3.45']"}]}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will know how to use arguments to format the result of the print command
    • \n
    • You will be able to use f-strings to format printouts
    • \n

    So far we have learnt three methods for formulating the argument given to the print command.

    The first is the + operator for strings. It allows simple concatenation of string segments:

    name = \"Mark\"\nage = 37\nprint(\"Hi \" + name + \" your age is \" + str(age) + \" years\" )

    This method will not work if any of the segments are not strings. In the example above, the variable age has been converted into a string with the str function, since it is an integer and cannot be concatenated as is.

    The second method is considering each segment of the argument as a separate argument, and splitting them up with commas:

    print(\"Hi\", name, \"your age is\", age, \"years\" )

    This code produces the exact same result as the previous version. The print command normally adds a space character between each argument.The advantage here is that the segments can be of different types, so there is no need to convert anything into a string.

    If you want to remove the automatically added spaces, you can add a special named argument sep:

    print(\"Hi\", name, \"your age is\", age, \"years\", sep=\"\")

    This prints out

    HiMarkyour age is37years

    The argument sep=\"\" is a keyword argument, and its name is short for separator. It specifies that the other arguments should now be separated by an empty string. You can set the separator to any string you like. For example, if you wanted each argument on a separate line, you could set the separator to \"\\n\", which is the newline character:

    print(\"Hi\", name, \"your age is\", age, \"years\", sep=\"\\n\")

    Hi\nMark\nyour age is\n37\nyears

    By default, the print command always ends in a newline character, but you can change this as well. The keyword argument end specifies what is put at the end of a line. Setting end to an empty string means that there is no newline character at the end of the printout:

    print(\"Hi \", end=\"\")\nprint(\"there!\")

    Hi there!

    f-strings

    The third method to prepare strings is f-strings. The previous example with the name and the age would look like this formulated with f-strings:

    name = \"Erkki\"\nage = 39\nprint(f\"Hi {name} your age is {age} years\")

    Thus far we have only used very simple f-strings, but they can be very versatile in formatting string type content. One very common use case is setting the number of decimals that are printed out with a floating point number. By default the number is quite high:

    number = 1/3\nprint(f\"The number is {number}\")

    The number is 0.333333333333333

    The specific format we want the number to be displayed in can be set within the curly brackets of the variable expression. Let's add a colon character and a format specifier after the variable name:

    number = 1/3\nprint(f\"The number is {number:.2f}\")
    The number is 0.33

    The format specifier .2f states that we want to display 2 decimals. The letter f at the end means that we want the variable to be displayed as a float, i.e. a floating point number.

    Here's another example, where we specify the amount of whitespace reserved for the variable in the printout. Both times the variable name is included in the resulting string, it has a space of 15 characters reserved. First the names are justified to the left, and then they are justified to the right:

    names =  [ \"Steve\", \"Jean\", \"Katherine\", \"Paul\" ]\nfor name in names:\n  print(f\"{name:15} centre {name:>15}\")
    Steve           centre           Steve\nJean            centre            Jean\nKatherine       centre       Katherine\nPaul            centre            Paul

    The uses of f-strings are not restricted to print commands. They can be assigned to variables and combined with other strings:

    name = \"Larry\"\nage = 48\ncity = \"Palo Alto\"\ngreeting = f\"Hi {name}, you are {age} years of age\"\nprint(greeting + f\", and you live in {city}\")

    Hi Larry, you are 48 years of age, and you live in Palo Alto

    You can think of an f-string as a sort of function, which creates a normal string based on the \"arguments\" within the curly brackets.

    Please write a function named formatted, which takes a list of floating point numbers as its argument. The function returns a new list, which contains each element of the original list in string format, rounded to two decimal points. The order of the items in the list should remain unchanged.

    Hint: use f-strings to format the floating point numbers into suitable strings.

    An example of expected beahviour:

    my_list = [1.234, 0.3333, 0.11111, 3.446]\nnew_list = formatted(my_list)\nprint(new_list)

    ['1.23', '0.33', '0.11', '3.45']

    ","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-4/5-print-statement-formatting.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-4/6-strings-and-lists/page-data.json b/page-data/part-4/6-strings-and-lists/page-data.json index 5eaadaf8e..f5547063f 100644 --- a/page-data/part-4/6-strings-and-lists/page-data.json +++ b/page-data/part-4/6-strings-and-lists/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-4/6-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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"everything_reversed"}]},{"type":"text","value":", which takes a list of strings as its argument. The function returns a new list with all of the items on the original list reversed. Also the order of items should be reversed on the new list."}]},{"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":"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":"/static/6a61f09e6f9cd09d51315c7fde142ab9/6b1e2/4_6_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":["/static/6a61f09e6f9cd09d51315c7fde142ab9/a0b58/4_6_1.webp 230w","/static/6a61f09e6f9cd09d51315c7fde142ab9/f44a8/4_6_1.webp 281w"],"sizes":"(max-width: 281px) 100vw, 281px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6a61f09e6f9cd09d51315c7fde142ab9/81c8e/4_6_1.png 230w","/static/6a61f09e6f9cd09d51315c7fde142ab9/6b1e2/4_6_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":"/static/6a61f09e6f9cd09d51315c7fde142ab9/6b1e2/4_6_1.png","alt":"4 6 1","title":"4 6 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":"/static/374387f1b515a88857cfbac235dfb942/b1a44/4_6_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":["/static/374387f1b515a88857cfbac235dfb942/a0b58/4_6_2.webp 230w","/static/374387f1b515a88857cfbac235dfb942/f6dfe/4_6_2.webp 296w"],"sizes":"(max-width: 296px) 100vw, 296px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/374387f1b515a88857cfbac235dfb942/81c8e/4_6_2.png 230w","/static/374387f1b515a88857cfbac235dfb942/b1a44/4_6_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":"/static/374387f1b515a88857cfbac235dfb942/b1a44/4_6_2.png","alt":"4 6 2","title":"4 6 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":"Please use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"isupper"}]},{"type":"text","value":" method to write 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 as an argument. The function returns a new list, containing only those items from the original which do not consist of solely uppercase characters."}]},{"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":"programming-exercise","properties":{"name":"Neighbours in a list","tmcname":"part04-37_neighbours_in_list"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Given a list of integers, let's decide that two consecutive items in the list are neighbours if their difference is 1. So, items 1 and 2 would be neighbours, and so would items 56 and 55."}]},{"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":"longest_series_of_neighbours"}]},{"type":"text","value":", which looks for the longest series of neighbours within the list, and returns its length."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, in the list "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[1, 2, 5, 4, 3, 4]"}]},{"type":"text","value":" the longest list of neighbours would be "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[5, 4, 3, 4]"}]},{"type":"text","value":", with a length of 4."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example function call:"}]},{"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":"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":" "},{"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":" "},{"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"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":" "},{"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":"longest_series_of_neighbours"},{"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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4"}]}]}]},{"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":"/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":"comment","value":" **Huomaa** että tällä hetkellä Windowsissa on ongelmia joidenkin tehtävien testien suorittamisessa. Jos törmäät seuraavaan virheilmoitukseen\n\n\"Listan\n\nvoit suorittaa testit lähettämällä ne palvelimelle valitsemalla testien suoritusnapin oikealla puolella olevasta symbolista avautuvasta TMC-valikosta _Submit solutions_.\n\nOngelman saa korjattua menemällä laajennuksen asennusvalikkoon ja muuttamalla \"TMC Data\" -kohdassa tehtävien sijainnin johonkin toiseen sijaintiin, jonka tiedostopolku on lyhempi, allaolevassa kuvassa nappi _change path_. Siirrossa saattaa kestää hetken, joten odotathan operaation päättymistä.\n\n\"Listan\n\nOngelmaan pyritään saamaan parempi ratkaisu lähipäivinä. "}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this week's materials."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"25acd630-8857-5eb3-9271-ff44ceda207f"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will be familiar with more methods for slicing strings and lists
    • \n
    • You will understand what immutability of strings means
    • \n
    • You will be able to use the methods count and replace
    • \n

    You are already familiar with the [] syntax for accessing a part of a string:

    my_string = \"exemplary\"\nprint(my_string[3:7])

    mpla

    The same syntax works with lists. Lists can be sliced just like strings:

    my_list = [3,4,2,4,6,1,2,4,2]\nprint(my_list[3:7])

    [4, 6, 1, 2]

    More slices

    In fact, the [] syntax works very similarly to the range function, which means we can also give it a step:

    my_string = \"exemplary\"\nprint(my_string[0:7:2])\nmy_list = [1,2,3,4,5,6,7,8]\nprint(my_list[6:2:-1])

    eepa\n[7, 6, 5, 4]

    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:

    my_string = input(\"Please type in a string: \")\nprint(my_string[::-1])

    Please type in a string: exemplary\nyralpmexe

    Warning: using global variables within functions

    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 global variables.

    Using global variables from within functions is usually a bad idea. Among other issues, doing so may cause bugs which are difficult to trace.

    Below is an example of a function which uses a global variable \"by mistake\":

    def print_reversed(names: list):\n    # using the global variable instead of the parameter by accident\n    i = len(name_list) - 1\n    while i >= 0:\n        print(name_list[i])\n        i -= 1\n\n# here the global variable is assigned\nname_list = [\"Steve\", \"Jean\", \"Katherine\", \"Paul\"]\nprint_reversed(name_list)\nprint()\nprint_reversed([\"Huey\", \"Dewey\", \"Louie\"])

    Paul\nKatherine\nJean\nSteve

    Paul\nKatherine\nJean\nSteve

    Even though both function calls have the right kind of argument, the function always prints out what is stored in the global variable name_list.

    To make matters even more muddled, remember that all code for testing your functions should be placed within the if __name__ == \"__main__\": block for the automatic tests. The previous example should be modified:

    def print_reversed(names: list):\n    # using the global variable instead of the parameter by accident\n    i = len(name_list) - 1\n    while i>=0:\n        print(name_list[i])\n        i -= 1\n\n# All the code for testing the function should be within this block\nif __name__ == \"__main__\":\n    # here the global variable is assigned\n    name_list = [\"Steve\", \"Jean\", \"Katherine\", \"Paul\"]\n    print_reversed(name_list)\n    print()\n    print_reversed([\"Huey\", \"Dewey\", \"Louie\"])

    Notice the global variable is assigned within the if block now.

    The automatic tests in the TMC system are executed without running any of the code in the if block. So, in this latter example the function wouldn't even theoretically work, since it refers to the variable name_list, which doesn't exist at all when the tests are executed.

    Please write a function named everything_reversed, which takes a list of strings as its argument. The function returns a new list with all of the items on the original list reversed. Also the order of items should be reversed on the new list.

    An example of how the function should work:

    my_list = [\"Hi\", \"there\", \"example\", \"one more\"]\nnew_list = everything_reversed(my_list)\nprint(new_list)

    ['erom eno', 'elpmaxe', 'ereht', 'iH']

    Strings are immutable

    Strings and lists have a lot in common, especially in the way they behave with different operators. The main difference is that strings are immutable. That means they cannot be changed.

    my_string = \"exemplary\"\nmy_string[0] = \"a\"

    Strings cannot be changed, so the execution of this program causes an error:

    TypeError: 'str' object does not support item assignment

    A similar error follows if you try to sort a string with the sort method.

    Strings themselves are immutable, but the variables holding them are not. A string can be replaced by another string.

    The following two examples are thus fundamentally different:

    my_list = [1,2,3]\nmy_list[0] = 10
    \n \n \n \n \n \n \"4\n \n \n

    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.

    We will return to this subject in the next part, where references to lists are explored in more detail.

    More methods for lists and strings

    The method count counts the number of times the specified item or substring occurs in the target. The method works similarly with both strings and lists:

    my_string = \"How much wood would a woodchuck chuck if a woodchuck could chuck wood\"\nprint(my_string.count(\"ch\"))\n\nmy_list = [1,2,3,1,4,5,1,6]\nprint(my_list.count(1))

    5\n3

    The method will not count overlapping occurrences. For example, in the string aaaa the method counts only two occurrences of the substring aa, even though there would actually be three if overlapping occurrences were allowed.

    The method replace creates a new string, where a specified substring is replaced with another string:

    my_string = \"Hi there\"\nnew_string = my_string.replace(\"Hi\", \"Hey\")\nprint(new_string)

    Hey there

    The method will replace all occurrences of the substring:

    sentence = \"sheila sells seashells on the seashore\"\nprint(sentence.replace(\"she\", \"SHE\"))

    SHEila sells seaSHElls on the seashore

    When using the replace method, a typical mistake is forgetting that strings are immutable:

    my_string = \"Python is fun\"\n\n# Replaces the substring but doesn't store the result...\nmy_string.replace(\"Python\", \"Java\")\nprint(my_string)

    Python is fun

    If the old string is no longer needed, the new string can be assigned to the same variable:

    my_string = \"Python is fun\"\n\n# Replaces the substring and stores the result in the same variable\nmy_string = my_string.replace(\"Python\", \"Java\")\nprint(my_string)

    Java is fun

    Please write a function named most_common_character, 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.

    An example of expected behaviour:

    first_string = \"abcdbde\"\nprint(most_common_character(first_string))\n\nsecond_string = \"exemplaryelementary\"\nprint(most_common_character(second_string))

    b\ne

    Please write a function named no_vowels, which takes a string argument. The function returns a new string, which should be the same as the original but with all vowels removed.

    You can assume the string will contain only characters from the lowercase English alphabet a...z.

    An example of expected behaviour:

    my_string = \"this is an example\"\nprint(no_vowels(my_string))

    ths s n xmpl

    The Python string method isupper() returns True if a string consists of only uppercase characters.

    Some examples:

    print(\"XYZ\".isupper())\n\nis_it_upper = \"Abc\".isupper()\nprint(is_it_upper)

    True\nFalse

    Please use the isupper method to write a function named no_shouting, which takes a list of strings as an argument. The function returns a new list, containing only those items from the original which do not consist of solely uppercase characters.

    An example of expected behaviour:

    my_list = [\"ABC\", \"def\", \"UPPER\", \"ANOTHERUPPER\", \"lower\", \"another lower\", \"Capitalized\"]\npruned_list = no_shouting(my_list)\nprint(pruned_list)

    ['def', 'lower', 'another lower', 'Capitalized']

    Given a list of integers, let's decide that two consecutive items in the list are neighbours if their difference is 1. So, items 1 and 2 would be neighbours, and so would items 56 and 55.

    Please write a function named longest_series_of_neighbours, which looks for the longest series of neighbours within the list, and returns its length.

    For example, in the list [1, 2, 5, 4, 3, 4] the longest list of neighbours would be [5, 4, 3, 4], with a length of 4.

    An example function call:

    my_list = [1, 2, 5, 7, 6, 5, 6, 3, 4, 1, 0]\nprint(longest_series_of_neighbours(my_list))

    4

    Developing a larger programming project

    This fourth part culminates in a slightly larger programming project, where you get to apply many of the techniques learnt so far.

    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.

    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:

    def main():\n    points = []\n    # your program code goes here\n\nmain()

    Now the helper functions can be tested without running the main function:

    # helper function for determining the grade based on the amount of points\ndef grade(points):\n    # more code\n\ndef main():\n    all_points = []\n    # your program code goes here\n\n# comment out the main function\n#main()\n\n# test the helper function\nstudent_points = 35\nresult = grade(student_points)\nprint(result)

    Passing data from one function to another

    When a program contains multiple functions, the question arises: how do you pass data from one function to another?

    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:

    def input_from_user(how_many: int):\n    print(f\"Please type in {how_many} numbers:\")\n    numbers = []\n\n    for i in range(how_many):\n        number = int(input(f\"Number {i+1}: \"))\n        numbers.append(number)\n\n    return numbers\n\ndef print_result(numbers: list):\n    print(\"The numbers are: \")\n    for number in numbers:\n        print(number)\n\ndef analyze(numbers: list):\n    mean = sum(numbers) / len(numbers)\n    return f\"There are altogether {len(numbers)} numbers, the mean is {mean}, the smallest is {min(numbers)} and the greatest is {max(numbers)}\"\n\n# the \"main function\" using these functions\ninputs = input_from_user(5)\nprint_result(inputs)\nanalysis_result = analyze(inputs)\nprint(analysis_result)

    When the program is executed, it could go like this:

    Please type in 5 numbers:\nNumber 1: 10\nNumber 2: 34\nNumber 3: -32\nNumber 4: 99\nNumber 5: -53\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

    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 inputs.

    If the input is needed in a function, it is passed as an argument. This happens with the functions print_result and analyze. If the function produces data that is needed elsewhere in the program, the function returns it with the return command, and it is stored in a variable in the main function. This happens with the functions input_from_user and analyze.

    You could use the global variable inputs from the main function directly in the helper functions. We have already covered why that is a bad idea, but here is another explanation. 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.

    Passing data into and out of functions is best handled by arguments and return values.

    You could also separate the implicit main function in the example above into its own function. Then the variable inputs would no longer be a global variable, but instead a local variable within the main function:

    # your main function goes here\ndef main():\n    inputs = input_from_user(5)\n    print_result(inputs)\n    analysis_result = analyze(inputs)\n\n    print(analysis_result)\n\n# run the main function\nmain()

    In this exercise you will write a program for printing out grade statistics for a university course.

    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.

    Exam points are integers between 0 and 20. The number of exercises completed is an integer between 0 and 100.

    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.

    And example of how the data is typed in:

    Exam points and exercises completed: 15 87\nExam points and exercises completed: 10 55\nExam points and exercises completed: 11 40\nExam points and exercises completed: 4 17\nExam points and exercises completed:\nStatistics:

    When the user types in an empty line, the program prints out statistics. They are formulated as follows:

    The exercises completed are converted into exercise points, 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.

    The grade for the course is determined based on the following table:

    \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
    exam points + exercise pointsgrade
    0–140 (i.e. fail)
    15–171
    18–202
    21–233
    24–274
    28–305

    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.

    With the example input from above the program would print out the following statistics:

    \nStatistics:\nPoints average: 14.5\nPass percentage: 75.0\nGrade distribution:\n  5:\n  4:\n  3: *\n  2:\n  1: **\n  0: *\n

    Floating point numbers should be printed out with one decimal precision.

    NB: this exercise doesn't ask you to write any specific functions, so you should not place any code within an if __name__ == \"__main__\" block. If any functionality in your program is e.g. in the main function, you should include the code calling this function normally, and not contain it in an if block like in the exercises which specify certain functions.

    Hint:

    The user input in this program consists of lines with two integer values:

    Exam points and exercises completed: 15 87

    You have to first split the input line in two and then convert the sections into integers with the int function. Splitting the input can be achieved in the same way as in the exercise First, second and last words, but there is a simpler way as well. The string method split will chop the input up nicely. You will find more information by searching for python string split online.

    Please respond to a quick questionnaire on this week's materials.

    ","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-4/6-strings-and-lists.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"everything_reversed"}]},{"type":"text","value":", which takes a list of strings as its argument. The function returns a new list with all of the items on the original list reversed. Also the order of items should be reversed on the new list."}]},{"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":"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":"/static/6a61f09e6f9cd09d51315c7fde142ab9/6b1e2/4_6_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":["/static/6a61f09e6f9cd09d51315c7fde142ab9/a0b58/4_6_1.webp 230w","/static/6a61f09e6f9cd09d51315c7fde142ab9/f44a8/4_6_1.webp 281w"],"sizes":"(max-width: 281px) 100vw, 281px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6a61f09e6f9cd09d51315c7fde142ab9/81c8e/4_6_1.png 230w","/static/6a61f09e6f9cd09d51315c7fde142ab9/6b1e2/4_6_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":"/static/6a61f09e6f9cd09d51315c7fde142ab9/6b1e2/4_6_1.png","alt":"4 6 1","title":"4 6 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":"/static/374387f1b515a88857cfbac235dfb942/b1a44/4_6_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":["/static/374387f1b515a88857cfbac235dfb942/a0b58/4_6_2.webp 230w","/static/374387f1b515a88857cfbac235dfb942/f6dfe/4_6_2.webp 296w"],"sizes":"(max-width: 296px) 100vw, 296px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/374387f1b515a88857cfbac235dfb942/81c8e/4_6_2.png 230w","/static/374387f1b515a88857cfbac235dfb942/b1a44/4_6_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":"/static/374387f1b515a88857cfbac235dfb942/b1a44/4_6_2.png","alt":"4 6 2","title":"4 6 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":"Please use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"isupper"}]},{"type":"text","value":" method to write 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 as an argument. The function returns a new list, containing only those items from the original which do not consist of solely uppercase characters."}]},{"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":"programming-exercise","properties":{"name":"Neighbours in a list","tmcname":"part04-37_neighbours_in_list"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Given a list of integers, let's decide that two consecutive items in the list are neighbours if their difference is 1. So, items 1 and 2 would be neighbours, and so would items 56 and 55."}]},{"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":"longest_series_of_neighbours"}]},{"type":"text","value":", which looks for the longest series of neighbours within the list, and returns its length."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, in the list "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[1, 2, 5, 4, 3, 4]"}]},{"type":"text","value":" the longest list of neighbours would be "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[5, 4, 3, 4]"}]},{"type":"text","value":", with a length of 4."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example function call:"}]},{"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":"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":" "},{"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":" "},{"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"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":" "},{"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":"longest_series_of_neighbours"},{"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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"4"}]}]}]},{"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":"/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":"comment","value":" **Huomaa** että tällä hetkellä Windowsissa on ongelmia joidenkin tehtävien testien suorittamisessa. Jos törmäät seuraavaan virheilmoitukseen\n\n\"Listan\n\nvoit suorittaa testit lähettämällä ne palvelimelle valitsemalla testien suoritusnapin oikealla puolella olevasta symbolista avautuvasta TMC-valikosta _Submit solutions_.\n\nOngelman saa korjattua menemällä laajennuksen asennusvalikkoon ja muuttamalla \"TMC Data\" -kohdassa tehtävien sijainnin johonkin toiseen sijaintiin, jonka tiedostopolku on lyhempi, allaolevassa kuvassa nappi _change path_. Siirrossa saattaa kestää hetken, joten odotathan operaation päättymistä.\n\n\"Listan\n\nOngelmaan pyritään saamaan parempi ratkaisu lähipäivinä. "}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this week's materials."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"25acd630-8857-5eb3-9271-ff44ceda207f"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will be familiar with more methods for slicing strings and lists
    • \n
    • You will understand what immutability of strings means
    • \n
    • You will be able to use the methods count and replace
    • \n

    You are already familiar with the [] syntax for accessing a part of a string:

    my_string = \"exemplary\"\nprint(my_string[3:7])

    mpla

    The same syntax works with lists. Lists can be sliced just like strings:

    my_list = [3,4,2,4,6,1,2,4,2]\nprint(my_list[3:7])

    [4, 6, 1, 2]

    More slices

    In fact, the [] syntax works very similarly to the range function, which means we can also give it a step:

    my_string = \"exemplary\"\nprint(my_string[0:7:2])\nmy_list = [1,2,3,4,5,6,7,8]\nprint(my_list[6:2:-1])

    eepa\n[7, 6, 5, 4]

    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:

    my_string = input(\"Please type in a string: \")\nprint(my_string[::-1])

    Please type in a string: exemplary\nyralpmexe

    Warning: using global variables within functions

    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 global variables.

    Using global variables from within functions is usually a bad idea. Among other issues, doing so may cause bugs which are difficult to trace.

    Below is an example of a function which uses a global variable \"by mistake\":

    def print_reversed(names: list):\n    # using the global variable instead of the parameter by accident\n    i = len(name_list) - 1\n    while i >= 0:\n        print(name_list[i])\n        i -= 1\n\n# here the global variable is assigned\nname_list = [\"Steve\", \"Jean\", \"Katherine\", \"Paul\"]\nprint_reversed(name_list)\nprint()\nprint_reversed([\"Huey\", \"Dewey\", \"Louie\"])

    Paul\nKatherine\nJean\nSteve

    Paul\nKatherine\nJean\nSteve

    Even though both function calls have the right kind of argument, the function always prints out what is stored in the global variable name_list.

    To make matters even more muddled, remember that all code for testing your functions should be placed within the if __name__ == \"__main__\": block for the automatic tests. The previous example should be modified:

    def print_reversed(names: list):\n    # using the global variable instead of the parameter by accident\n    i = len(name_list) - 1\n    while i>=0:\n        print(name_list[i])\n        i -= 1\n\n# All the code for testing the function should be within this block\nif __name__ == \"__main__\":\n    # here the global variable is assigned\n    name_list = [\"Steve\", \"Jean\", \"Katherine\", \"Paul\"]\n    print_reversed(name_list)\n    print()\n    print_reversed([\"Huey\", \"Dewey\", \"Louie\"])

    Notice the global variable is assigned within the if block now.

    The automatic tests in the TMC system are executed without running any of the code in the if block. So, in this latter example the function wouldn't even theoretically work, since it refers to the variable name_list, which doesn't exist at all when the tests are executed.

    Please write a function named everything_reversed, which takes a list of strings as its argument. The function returns a new list with all of the items on the original list reversed. Also the order of items should be reversed on the new list.

    An example of how the function should work:

    my_list = [\"Hi\", \"there\", \"example\", \"one more\"]\nnew_list = everything_reversed(my_list)\nprint(new_list)

    ['erom eno', 'elpmaxe', 'ereht', 'iH']

    Strings are immutable

    Strings and lists have a lot in common, especially in the way they behave with different operators. The main difference is that strings are immutable. That means they cannot be changed.

    my_string = \"exemplary\"\nmy_string[0] = \"a\"

    Strings cannot be changed, so the execution of this program causes an error:

    TypeError: 'str' object does not support item assignment

    A similar error follows if you try to sort a string with the sort method.

    Strings themselves are immutable, but the variables holding them are not. A string can be replaced by another string.

    The following two examples are thus fundamentally different:

    my_list = [1,2,3]\nmy_list[0] = 10
    \n \n \n \n \n \n \"4\n \n \n

    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.

    We will return to this subject in the next part, where references to lists are explored in more detail.

    More methods for lists and strings

    The method count counts the number of times the specified item or substring occurs in the target. The method works similarly with both strings and lists:

    my_string = \"How much wood would a woodchuck chuck if a woodchuck could chuck wood\"\nprint(my_string.count(\"ch\"))\n\nmy_list = [1,2,3,1,4,5,1,6]\nprint(my_list.count(1))

    5\n3

    The method will not count overlapping occurrences. For example, in the string aaaa the method counts only two occurrences of the substring aa, even though there would actually be three if overlapping occurrences were allowed.

    The method replace creates a new string, where a specified substring is replaced with another string:

    my_string = \"Hi there\"\nnew_string = my_string.replace(\"Hi\", \"Hey\")\nprint(new_string)

    Hey there

    The method will replace all occurrences of the substring:

    sentence = \"sheila sells seashells on the seashore\"\nprint(sentence.replace(\"she\", \"SHE\"))

    SHEila sells seaSHElls on the seashore

    When using the replace method, a typical mistake is forgetting that strings are immutable:

    my_string = \"Python is fun\"\n\n# Replaces the substring but doesn't store the result...\nmy_string.replace(\"Python\", \"Java\")\nprint(my_string)

    Python is fun

    If the old string is no longer needed, the new string can be assigned to the same variable:

    my_string = \"Python is fun\"\n\n# Replaces the substring and stores the result in the same variable\nmy_string = my_string.replace(\"Python\", \"Java\")\nprint(my_string)

    Java is fun

    Please write a function named most_common_character, 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.

    An example of expected behaviour:

    first_string = \"abcdbde\"\nprint(most_common_character(first_string))\n\nsecond_string = \"exemplaryelementary\"\nprint(most_common_character(second_string))

    b\ne

    Please write a function named no_vowels, which takes a string argument. The function returns a new string, which should be the same as the original but with all vowels removed.

    You can assume the string will contain only characters from the lowercase English alphabet a...z.

    An example of expected behaviour:

    my_string = \"this is an example\"\nprint(no_vowels(my_string))

    ths s n xmpl

    The Python string method isupper() returns True if a string consists of only uppercase characters.

    Some examples:

    print(\"XYZ\".isupper())\n\nis_it_upper = \"Abc\".isupper()\nprint(is_it_upper)

    True\nFalse

    Please use the isupper method to write a function named no_shouting, which takes a list of strings as an argument. The function returns a new list, containing only those items from the original which do not consist of solely uppercase characters.

    An example of expected behaviour:

    my_list = [\"ABC\", \"def\", \"UPPER\", \"ANOTHERUPPER\", \"lower\", \"another lower\", \"Capitalized\"]\npruned_list = no_shouting(my_list)\nprint(pruned_list)

    ['def', 'lower', 'another lower', 'Capitalized']

    Given a list of integers, let's decide that two consecutive items in the list are neighbours if their difference is 1. So, items 1 and 2 would be neighbours, and so would items 56 and 55.

    Please write a function named longest_series_of_neighbours, which looks for the longest series of neighbours within the list, and returns its length.

    For example, in the list [1, 2, 5, 4, 3, 4] the longest list of neighbours would be [5, 4, 3, 4], with a length of 4.

    An example function call:

    my_list = [1, 2, 5, 7, 6, 5, 6, 3, 4, 1, 0]\nprint(longest_series_of_neighbours(my_list))

    4

    Developing a larger programming project

    This fourth part culminates in a slightly larger programming project, where you get to apply many of the techniques learnt so far.

    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.

    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:

    def main():\n    points = []\n    # your program code goes here\n\nmain()

    Now the helper functions can be tested without running the main function:

    # helper function for determining the grade based on the amount of points\ndef grade(points):\n    # more code\n\ndef main():\n    all_points = []\n    # your program code goes here\n\n# comment out the main function\n#main()\n\n# test the helper function\nstudent_points = 35\nresult = grade(student_points)\nprint(result)

    Passing data from one function to another

    When a program contains multiple functions, the question arises: how do you pass data from one function to another?

    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:

    def input_from_user(how_many: int):\n    print(f\"Please type in {how_many} numbers:\")\n    numbers = []\n\n    for i in range(how_many):\n        number = int(input(f\"Number {i+1}: \"))\n        numbers.append(number)\n\n    return numbers\n\ndef print_result(numbers: list):\n    print(\"The numbers are: \")\n    for number in numbers:\n        print(number)\n\ndef analyze(numbers: list):\n    mean = sum(numbers) / len(numbers)\n    return f\"There are altogether {len(numbers)} numbers, the mean is {mean}, the smallest is {min(numbers)} and the greatest is {max(numbers)}\"\n\n# the \"main function\" using these functions\ninputs = input_from_user(5)\nprint_result(inputs)\nanalysis_result = analyze(inputs)\nprint(analysis_result)

    When the program is executed, it could go like this:

    Please type in 5 numbers:\nNumber 1: 10\nNumber 2: 34\nNumber 3: -32\nNumber 4: 99\nNumber 5: -53\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

    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 inputs.

    If the input is needed in a function, it is passed as an argument. This happens with the functions print_result and analyze. If the function produces data that is needed elsewhere in the program, the function returns it with the return command, and it is stored in a variable in the main function. This happens with the functions input_from_user and analyze.

    You could use the global variable inputs from the main function directly in the helper functions. We have already covered why that is a bad idea, but here is another explanation. 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.

    Passing data into and out of functions is best handled by arguments and return values.

    You could also separate the implicit main function in the example above into its own function. Then the variable inputs would no longer be a global variable, but instead a local variable within the main function:

    # your main function goes here\ndef main():\n    inputs = input_from_user(5)\n    print_result(inputs)\n    analysis_result = analyze(inputs)\n\n    print(analysis_result)\n\n# run the main function\nmain()

    In this exercise you will write a program for printing out grade statistics for a university course.

    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.

    Exam points are integers between 0 and 20. The number of exercises completed is an integer between 0 and 100.

    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.

    And example of how the data is typed in:

    Exam points and exercises completed: 15 87\nExam points and exercises completed: 10 55\nExam points and exercises completed: 11 40\nExam points and exercises completed: 4 17\nExam points and exercises completed:\nStatistics:

    When the user types in an empty line, the program prints out statistics. They are formulated as follows:

    The exercises completed are converted into exercise points, 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.

    The grade for the course is determined based on the following table:

    \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
    exam points + exercise pointsgrade
    0–140 (i.e. fail)
    15–171
    18–202
    21–233
    24–274
    28–305

    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.

    With the example input from above the program would print out the following statistics:

    \nStatistics:\nPoints average: 14.5\nPass percentage: 75.0\nGrade distribution:\n  5:\n  4:\n  3: *\n  2:\n  1: **\n  0: *\n

    Floating point numbers should be printed out with one decimal precision.

    NB: this exercise doesn't ask you to write any specific functions, so you should not place any code within an if __name__ == \"__main__\" block. If any functionality in your program is e.g. in the main function, you should include the code calling this function normally, and not contain it in an if block like in the exercises which specify certain functions.

    Hint:

    The user input in this program consists of lines with two integer values:

    Exam points and exercises completed: 15 87

    You have to first split the input line in two and then convert the sections into integers with the int function. Splitting the input can be achieved in the same way as in the exercise First, second and last words, but there is a simpler way as well. The string method split will chop the input up nicely. You will find more information by searching for python string split online.

    Please respond to a quick questionnaire on this week's materials.

    ","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-4/6-strings-and-lists.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-4/page-data.json b/page-data/part-4/page-data.json index fc1636035..5cd6184b9 100644 --- a/page-data/part-4/page-data.json +++ b/page-data/part-4/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/part-4", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-4","title":"Part 4"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-4/index.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-4","title":"Part 4"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-4/index.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-5/1-more-lists/page-data.json b/page-data/part-5/1-more-lists/page-data.json index 2ccc93866..447be90e4 100644 --- a/page-data/part-5/1-more-lists/page-data.json +++ b/page-data/part-5/1-more-lists/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-5/1-more-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 able to create lists with different types of items"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to use lists to organise data"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to store a matrix as a two-dimensional list"}]},{"type":"text","value":"\n"}]}]},{"type":"comment","value":"the same text is in sections 3-1, 5-1 and 6-1, check them all if you're changing this"},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"About the exercises on this course"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Becoming a proficient programmer requires a lot of practice, sometimes even quite mechanical practice. It also involves developing problem solving skills and applying intuition. This is why there are a lot of exercises of different kinds on this course. Some of them ask you to quite straightforwardly apply what you have learnt in the material, but some of them are intentionally more challenging and open-ended."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some of the exercises might at first seem overwhelming, but this is nothing to worry about. None of the exercises is strictly mandatory, and in fact "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"only 25 % of the points in each part is required to pass the course."}]},{"type":"text","value":" You can find more details about passing the course on the "},{"type":"element","tagName":"a","properties":{"href":"/grading-and-exams"},"children":[{"type":"text","value":"page on grading"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"The exercises are not in any specific order of difficulty."}]},{"type":"text","value":" Each section usually introduces some new programming concepts, and these are then practised with both simpler and more complicated exercises. "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"If you come across an exercise that feels too difficult, move on to the next one."}]},{"type":"text","value":" You can always come back to the more difficult exercises if you have time later."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the going inevitably gets tough, a word of consolation: a task that seems impossibly difficult this week will likely feel rather easy in about four weeks' time."}]}]},{"type":"element","tagName":"h2","properties":{"id":"lists-with-different-types-of-data","style":"position:relative;"},"children":[{"type":"text","value":"Lists with different types of data"},{"type":"element","tagName":"a","properties":{"href":"#lists-with-different-types-of-data","ariaLabel":"lists with different types of data 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 the previous part we mainly handled lists with integer items, but any types of values can be stored in lists. A list of strings 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":"text","value":"names "},{"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":"\"Marlyn\""}]},{"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":"\"Ruth\""}]},{"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"},{"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":"names"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnames"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"David\""}]},{"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":"names"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Number of names on the list:\""}]},{"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":"len"}]},{"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":"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":"\"Names in alphabetical order:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnames"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"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":"for"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" names"},{"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"},{"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":"['Marlyn', 'Ruth', 'Paul']\n['Marlyn', 'Ruth', 'Paul', 'David']\nNumber of names on the list: 4\nNames in alphabetical order:\nDavid\nMarlyn\nPaul\nRuth"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Floating point numbers are also valid list items:"}]},{"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":"measurements "},{"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","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2.5"}]},{"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":"1.1"}]},{"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":"7.5"}]},{"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":"14.6"}]},{"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":"21.0"}]},{"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":"19.2"}]},{"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":" measure "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" measurements"},{"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":"measure"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nmean "},{"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":"measurements"},{"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":"measurements"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The mean is:\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"-2.5\n1.1\n7.5\n14.6\n21.0\n19.2\nThe mean is: 10.15"}]}]},{"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":"reminder-using-global-variables-within-functions","style":"position:relative;"},"children":[{"type":"text","value":"Reminder: using global variables within functions"},{"type":"element","tagName":"a","properties":{"href":"#reminder-using-global-variables-within-functions","ariaLabel":"reminder 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":"h2","properties":{"id":"warning-overwriting-a-parameter-and-returning-too-early","style":"position:relative;"},"children":[{"type":"text","value":"Warning: overwriting a parameter and returning too early"},{"type":"element","tagName":"a","properties":{"href":"#warning-overwriting-a-parameter-and-returning-too-early","ariaLabel":"warning overwriting a parameter and returning too early 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":"There are a couple of novel sources of bugs we should look at before jumping into the exercises in this part. Let's have a look at a function which tells us whether an integer is found within a list. Both are defined as parameters of the 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"number_in_list"}]},{"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":"text","value":" number"},{"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":"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":"if"}]},{"type":"text","value":" number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" number"},{"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","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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","boolean"]},"children":[{"type":"text","value":"False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This function seems to always return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":". The reason is that the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop overwrites the value stored in the parameter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":". Thus the condition in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statement is always true."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Renaming the parameter solves the problem:"}]},{"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":"number_in_list"}]},{"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":"text","value":" searched_number"},{"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":"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":"if"}]},{"type":"text","value":" number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" searched_number"},{"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","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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","boolean"]},"children":[{"type":"text","value":"False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now the condition in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statement looks better. But there is a new problem, since the function still doesn't seem to work correctly. Trying out the following manifests a bug:"}]},{"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":"found "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" number_in_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":"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":" "},{"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":" "},{"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":" "},{"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","punctuation"]},"children":[{"type":"text","value":","}]},{"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":"\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":"found"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# prints out False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The issue here is that the function returns too early, without checking all the numbers in the list. In fact, the function takes only the first item in the list, and returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" depending on its value. We cannot know whether a number is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not present"}]},{"type":"text","value":" in the list until we have checked all the items in the list. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return False"}]},{"type":"text","value":" command should be placed outside the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop:"}]},{"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":"number_in_list"}]},{"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":"text","value":" searched_number"},{"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":"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":"if"}]},{"type":"text","value":" number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" searched_number"},{"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","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at another faulty 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"unique_numbers"}]},{"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","comment"]},"children":[{"type":"text","value":"# a helper variable to store all the numbers we've already checked"}]},{"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 "},{"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","comment"]},"children":[{"type":"text","value":"# have we seen this number already?"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"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":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\nunique "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" unique_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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"2"}]},{"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":"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":"\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":"unique"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# prints out True"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This function is supposed to check whether all numbers in a list are distinct from each other, but it always returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here the function again overwrites the value stored in its parameter by mistake. The function tries to use the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"numbers"}]},{"type":"text","value":" to store all the numbers already checked, but this overwrites the original argument list. Renaming the helper variable is an easy fix:"}]},{"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":"unique_numbers"}]},{"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","comment"]},"children":[{"type":"text","value":"# a helper variable to store all the numbers we've already checked"}]},{"type":"text","value":"\n numbers_checked "},{"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","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","comment"]},"children":[{"type":"text","value":"# have we seen this number already?"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" number "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" numbers_checked"},{"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","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n numbers_checked"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\nunique "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" unique_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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"2"}]},{"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":"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":"\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":"unique"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# prints out False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Problems like this, and many others, can be located and fixed with the help of the debugger or the "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":". Learning to use these efficiently cannot be emphasised enough."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The longest string","tmcname":"part05-01_longest_string"},"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":"longest(strings: list)"}]},{"type":"text","value":", which takes a list of strings as its argument. The function finds and returns the longest string in the list. You may assume there is always a single longest string in the list."}]},{"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":"\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 strings "},{"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":"\"hiya\""}]},{"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":"\"hello\""}]},{"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":"\"howdydoody\""}]},{"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":"\"hi there\""}]},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strings"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"howdydoody"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"lists-within-lists","style":"position:relative;"},"children":[{"type":"text","value":"Lists within lists"},{"type":"element","tagName":"a","properties":{"href":"#lists-within-lists","ariaLabel":"lists within lists 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 items in a list can be lists themselves:"}]},{"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","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":" "},{"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":" "},{"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","punctuation"]},"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":"4"}]},{"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":"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":"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":"2"}]},{"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":"2"}]},{"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":"5"}]},{"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":"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":"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":"\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":"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":"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":"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":"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[[5, 2, 3], [4, 1], [2, 2, 5, 1]]\n[4, 1]\n4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Why would lists within lists be useful?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Remember that lists can contain items of different types. You could store information about a person in a list. For instance, you could include their name as the first item, their age as the second item, and their height in meters as the third item:"}]},{"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","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anne\""}]},{"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":"12"}]},{"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":"1.45"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A database of persons could then be a list, whose items would be lists containing information about a single person:"}]},{"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":"persons "},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betty\""}]},{"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":"10"}]},{"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":"1.37"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"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":"7"}]},{"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":"1.25"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"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":"32"}]},{"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":"1.64"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Alan\""}]},{"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":"39"}]},{"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":"1.78"}]},{"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":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" persons"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" person"},{"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":"\n age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" person"},{"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":"text","value":"\n height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" person"},{"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":"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":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": age "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"age"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" years, height "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" meters\""}]}]},{"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":"Betty: age 10 years, height 1.37 meters\nPeter: age 7 years, height 1.25 meters\nEmily: age 32 years, height 1.64 meters\nAlan: age 39 years, height 1.78 meters"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop goes through the items in the outer list one by one. That is, each list containing information about a single person is, in turn, assigned to the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"person"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lists arent always the best way to present data, such as information about a person. We will soon come across Python "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"dictionaries"}]},{"type":"text","value":", which are often better suited to such situations."}]},{"type":"element","tagName":"h2","properties":{"id":"matrices","style":"position:relative;"},"children":[{"type":"text","value":"Matrices"},{"type":"element","tagName":"a","properties":{"href":"#matrices","ariaLabel":"matrices 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":"A two-dimensional array, or a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"matrix"}]},{"type":"text","value":", is also a natural application of a list within a list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, the following matrix"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 151px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/57e8504bd70c2538405be5b5e1572347/29fe9/5_1_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: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAABw0lEQVQ4y62UyYoCQQyG+3EF38YHUPHoyYsLeFLw4smDO654UVBxAXfN8AVS1LSt4zATCJ1OUn+l8qcqKBQKMhwOpd1uq3Y6Hen3+1Iul6XZbEq323UxbHzEyCHX1pgGgEXJbDaTV/IuFrADst/vpdFouMBgMJDT6aR2r9fTyhB8xJDH4yH1el02m43+3+93CSgT2W63kkwmHeBoNHKAuVxOstmsA/RPlUgktBVPgPzsdjv9Iiw6n89qT6dTmc/nauMzwNvtpjHLo+KA5kbJZDJx4L7gI/ayhzBGk+kLO6MsqFQqyuR4PHYxbHzEyMFnMVqEBtZsyr5cLqpUwcLD4SDX61UVP198xMixfF8Dv6G+UA0AYcFHJS+PbKSs12tJpVIOxCelWq1KsViMZJkJYK0jxeZwtVpJOp2W4/HoAG1s8vm8lEqlSED6+Q3wt0dmVN4eOUwK3z+R8snYmP+jsfn3wTaW7T7TozDLy+VSSQtfPUhYLBZ61CeWuZPxeNyB+CzDfiaTeWKZFyoWi2krIl+bWq3mdvMr5PlqtVpPFQLAjNrzpRX6RzanPV8/3RTL9e1vzxf98wGtWmJGED4f0F9DhV9fOel+Sn/2BgAAAABJRU5ErkJggg=='); 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":["/static/57e8504bd70c2538405be5b5e1572347/8d139/5_1_1.webp 151w"],"sizes":"(max-width: 151px) 100vw, 151px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/57e8504bd70c2538405be5b5e1572347/29fe9/5_1_1.png 151w"],"sizes":"(max-width: 151px) 100vw, 151px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/57e8504bd70c2538405be5b5e1572347/29fe9/5_1_1.png","alt":"5 1 1","title":"5 1 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":"p","properties":{},"children":[{"type":"text","value":"could be presented as a two-dimensional list in Python like so:"}]},{"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_matrix "},{"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":"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":" "},{"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":" "},{"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","punctuation"]},"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":"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":" "},{"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":"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":"4"}]},{"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":"5"}]},{"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":"6"}]},{"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":"Since a matrix is a list containing lists, the individual elements within the matrix can be accessed using consecutive square brackets. The first index refers to the row, and the second to the column. Indexing starts from zero, so for example "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"my_matrix[0][1]"}]},{"type":"text","value":" refers to the second item on the first row."}]},{"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_matrix "},{"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":"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":" "},{"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":" "},{"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","punctuation"]},"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":"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":" "},{"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":"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":"4"}]},{"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":"5"}]},{"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":"6"}]},{"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":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_matrix"},{"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":"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":"text","value":"\nmy_matrix"},{"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":"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":"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_matrix"},{"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":"2\n[[1, 2, 3], [10, 2, 1], [4, 5, 6]]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Like any other list, the rows of the matrix can be traversed wth a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop. The following code prints out each row of the matrix on a separate line:"}]},{"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_matrix "},{"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":"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":" "},{"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":" "},{"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","punctuation"]},"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":"4"}]},{"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":"5"}]},{"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":"6"}]},{"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":" "},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":" row "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_matrix"},{"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":"row"},{"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":"[1, 2, 3]\n[4, 5, 6]\n[7, 8, 9]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Likewise, nested loops can be used to access the individual elements. The following code prints out each element in the matrix on a separate line with the help of two "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loops:"}]},{"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_matrix "},{"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":"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":" "},{"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":" "},{"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","punctuation"]},"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":"4"}]},{"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":"5"}]},{"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":"6"}]},{"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":" "},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":" row "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_matrix"},{"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":"\"a new row\""}]},{"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":" element "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" row"},{"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":"element"},{"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":"a new row\n1\n2\n3\na new row\n4\n5\n6\na new row\n7\n8\n9"}]}]},{"type":"element","tagName":"h2","properties":{"id":"visualising-code-containing-lists-within-lists","style":"position:relative;"},"children":[{"type":"text","value":"Visualising code containing lists within lists"},{"type":"element","tagName":"a","properties":{"href":"#visualising-code-containing-lists-within-lists","ariaLabel":"visualising code containing lists within lists 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":"Programs containing lists within lists can feel hard to grasp at first. The "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":" from Python Tutor is a great help in understanding how they work. The following is a visualisation of the example above:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/10e6583e2d3db7e04fba937644e94e43/87a80/5_1_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: 45.21739130434783%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAABV0lEQVQoz21R2U4DMQzc//82UB+gII6XFtru0e0m2dxxdpCTXiAsWbbla8ZulmXBvQSfMB4l9OzwJ4WUEpxzRf3Feo+cc8mxbbiQiGBmjRACtDIYB4lxEPDO10brSg03i9OEGGOJq6ayTClV8nVgztDzjOk0lQajddm25IxMrARmwgNiCFc0bFk5x2jZb7gpWVuCoeux235haNsSs9yfhBf7QBBiBlGGs7YsuJembD5TyJlgjcc4zJiVL8MYCWuhpR1WTxu8fBxgXMZsOnTiEZPaQOgHhNShMVJCSgkhxBUV0YJMyxVhRbnAesL6fY/12w7WJ4ToMIgWJ/kNF7ZIZNFQrHeIkb97xND3OOz36NoWfd/DGFMQVgYZMRIS5espfMg4jCto91kpW5ugVYB39dCXT/Jj6PyMi/z2b7HSRyjzjERTveGN1v9yq7n3bz0hSkj9ipgEfgBiZr5+y7pYPQAAAABJRU5ErkJggg=='); 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":["/static/10e6583e2d3db7e04fba937644e94e43/a0b58/5_1_2.webp 230w","/static/10e6583e2d3db7e04fba937644e94e43/bc10c/5_1_2.webp 460w","/static/10e6583e2d3db7e04fba937644e94e43/966d8/5_1_2.webp 920w","/static/10e6583e2d3db7e04fba937644e94e43/0ea8a/5_1_2.webp 973w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/10e6583e2d3db7e04fba937644e94e43/81c8e/5_1_2.png 230w","/static/10e6583e2d3db7e04fba937644e94e43/08a84/5_1_2.png 460w","/static/10e6583e2d3db7e04fba937644e94e43/c0255/5_1_2.png 920w","/static/10e6583e2d3db7e04fba937644e94e43/87a80/5_1_2.png 973w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/10e6583e2d3db7e04fba937644e94e43/c0255/5_1_2.png","alt":"5 1 2","title":"5 1 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 image above reveals that a 3 by 3 matrix technically consists of four lists. The first list represents the entire matrix. The three remaining lists are items in the first list, and represent the rows."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As multidimensional lists can be traversed with nested loops, it would be natural to think of the lists themselves as nested, but the image above shows us this isn't actually so. Instead, the list representing the whole matrix \"points\" to each individual list representing a row in the matrix. This is called a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reference"}]},{"type":"text","value":", and in the "},{"type":"element","tagName":"a","properties":{"href":"/part-5/2-references"},"children":[{"type":"text","value":"following section"}]},{"type":"text","value":" the idea will be explored more thoroughly."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the image above the execution has progressed to the second row of the matrix, and this list is what the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"row"}]},{"type":"text","value":" currently refers to. The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"element"}]},{"type":"text","value":" contains the element the execution is currently at. The value stored in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"element"}]},{"type":"text","value":" is the middle item in the list, i.e. 5."}]},{"type":"element","tagName":"h2","properties":{"id":"accessing-items-in-a-matrix","style":"position:relative;"},"children":[{"type":"text","value":"Accessing items in a matrix"},{"type":"element","tagName":"a","properties":{"href":"#accessing-items-in-a-matrix","ariaLabel":"accessing items in a matrix 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":"Accessing a single row within a matrix is simple - just choose the desired row. The following function calculates the sum of the elements on a chosen row:"}]},{"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":"sum_of_row"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_matrix"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" row_no"},{"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","comment"]},"children":[{"type":"text","value":"# choose the desired row from within the matrix"}]},{"type":"text","value":"\n row "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_matrix"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"row_no"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n row_sum "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" row"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n row_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" item\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" row_sum\n\nm "},{"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":"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":"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":" "},{"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":" "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"1"}]},{"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":"12"}]},{"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":"11"}]},{"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":" "},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"5"}]},{"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":" "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"15"}]},{"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":"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":"text","value":"\n\nmy_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sum_of_row"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"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":"1"}]},{"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_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# prints out 33 (which equals 9 + 1 + 12 + 11)"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Working with columns within a matrix is slightly more complicated, as the matrix is stored by rows:"}]},{"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":"sum_of_column"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_matrix"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" column_no"},{"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","comment"]},"children":[{"type":"text","value":"# go through each row and select the item at the chosen position"}]},{"type":"text","value":"\n column_sum "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" row "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_matrix"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n column_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" row"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"column_no"},{"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":" column_sum\n\nm "},{"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":"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":"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":" "},{"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":" "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"1"}]},{"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":"12"}]},{"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":"11"}]},{"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":" "},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"5"}]},{"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":" "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"15"}]},{"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":"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":"text","value":"\n\nmy_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sum_of_column"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"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":"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_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# prints out 39 (which equals 3 + 12 + 9 + 15)"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The column handled here consists of the elements at index 2 on "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"each row"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"The visualisation tool"}]},{"type":"text","value":" is definitely recommended for understanding how these functions work."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Changing the value of a single element within the matrix is simple: choose a row within the matrix, and then a column within the row:"}]},{"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":"change_value"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_matrix"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" row_no"},{"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":"text","value":" column_no"},{"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":"text","value":" new_value"},{"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","comment"]},"children":[{"type":"text","value":"# choose the desired row"}]},{"type":"text","value":"\n row "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_matrix"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"row_no"},{"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":"# select the correct item within the row"}]},{"type":"text","value":"\n row"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"column_no"},{"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":" new_value\n\nm "},{"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":"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":"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":" "},{"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":" "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"1"}]},{"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":"12"}]},{"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":"11"}]},{"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":" "},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"5"}]},{"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":" "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"15"}]},{"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":"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":"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":"m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nchange_value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"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":"2"}]},{"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":"3"}]},{"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":"1000"}]},{"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":"m"},{"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, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 5], [2, 9, 15, 1]]\n[[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 1000], [2, 9, 15, 1]]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice how above we used the indexes of the row and column to access a chosen element. If we want to change the contents of the matrix, we have to access the elements by their indexes. This means that we can't use a simple "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for item in list"}]},{"type":"text","value":" loop to traverse the matrix if we want to change the contents of the matrix."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Instead, we will have to keep track of the indexes of the elements, for example with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop, or a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":" function. The following code increases the value of each element in the matrix by one:"}]},{"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":"m "},{"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":"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","punctuation"]},"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":"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","punctuation"]},"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":"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"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":"m"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# using the number of rows in the matrix"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" j "},{"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":"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":"m"},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# using the number of items on each row "}]},{"type":"text","value":"\n m"},{"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":"j"},{"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\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":"m"},{"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":"[[2, 3, 4], [5, 6, 7], [8, 9, 10]]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The outer loop goes through indexes from zero to the length of the matrix, that is, the number of rows in the matrix. The inner loop goes through indexes from zero to the length of each row within the matrix."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Number of matching elements","tmcname":"part05-02_number_of_elements"},"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":"count_matching_elements(my_matrix: list, element: int)"}]},{"type":"text","value":", which takes a two-dimensional array of integers and a single integer value as its arguments. The function then counts how many elements within the matrix match the argument value."}]},{"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":"m "},{"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":"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":" "},{"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":" "},{"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":"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":"0"}]},{"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":"3"}]},{"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":"4"}]},{"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":" "},{"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":"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","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":"count_matching_elements"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"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":"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":"3"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"a-two-dimensional-array-as-a-data-structure-in-a-game","style":"position:relative;"},"children":[{"type":"text","value":"A two-dimensional array as a data structure in a game"},{"type":"element","tagName":"a","properties":{"href":"#a-two-dimensional-array-as-a-data-structure-in-a-game","ariaLabel":"a two dimensional array as a data structure in a game 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":"A matrix can be a very useful data structure in many different games. For example, the grid of a sudoku game in the image below"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 410px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/5a4d938ae5202e8db72b179ff4d76293/d68e4/5_1_3.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: 100.43478260869566%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAAACXBIWXMAABYlAAAWJQFJUiTwAAADmUlEQVQ4y0WUWXOqPACG/fX9B163vTg67dQuap2OYBfbutQFRSmQoDGQQBAQqsXd5hvLmfO915l5kndJStM0XdcBAJ7nfX5+uq5bKpUIIQghTdMAAISQfr//8vJCCNE0zbZtVVWn0ylCKAV+5bou59w0zcPhUCgUVqtVFEWKomCMV6uVYRjPz8/T6dS27c1mY1kW55wQktJ13XGc4XAYBIEsy9Pp9OLiAmMMITQMAwBgWVar1SqXy8kxx3EGg4Hv+xDClOu6+/3e933OOWOMcy6KIsY4DEP7V7vdbjKZNJvN+Xzued5+v0+OMcZSyd0kSbJtu9PpUEr//PkDIVRVFQCgqup4PK7Vag8PD4wxSZJM05QkiTGmadqRfDgcHMfxPI9SyjkXBIEQ4nnecrn8+vpar9fj8ViSpIR2OBwopaZpHt98f39fLBaz2WyhUMhkMqIonpyc5HK5q6ur29vb6+vrQqFwdnaWTqcFQchms6VSKZvN3t3dvby8pCilm80GITSZTAghEMJ8Pr/ZbGazWRRFQRB8f38DAFqtVuLwer3+322EUBiGSVoAANM0c7lcGIaEEMYYpdTzPFmWX19fk8zm8zmEcLlcHnPGGEdRhBDabreGYaxWq3w+DyG0LAv+KgxDRVFEUXRdV1VVCCFCaL1eY4z/kiGEcRxrmrZYLG5ubizLIoQYv/J9X5blSqUym80Gg0GSfxzHRzJCiFJq2/bhcMAYu66bz+fjOA6CACFECFksFrquV6vVIAgSgzDGPz8/lmWl2u12t9vtdDoY40ajIUnS6enpaDRSFKVSqdTrdcMw3t7eLi8vW61Wu92GEH58fGCMFUVJ1Wq1drstiqIsy+VyuVqtptPpcrn89PTUaDRqtdrj42Mulzs/P6/X66IottttQRAkSep0On/rGQQB59x13SiKyuVyUs/lchnHMWNsOBy+v7/PZjPf9//V03XdlKqqpmkmk5AkiVKayWRs206M0XXdNM1msykIQhAEvV6PECJJ0mQyAQCkHMfZbreO43DOKaWMsWKxuFqtwjCcz+dhGMZxbBhGUhJK6W63M03TdV1K6ZGMMR4Oh57n9Xo927az2WwS8mg0AgBgjBPydDqVZZlS2u/3Hcc5khFClmWFYcg5t22bc14qlXa73WKxiOM4GcZoNOp2u/+GkczGcZzUeDxOps8YSz6GarVqWVbSdoSQaZqKonQ6HcYYAIBSqut60qL/AD7hB6pgN21XAAAAAElFTkSuQmCC'); 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":["/static/5a4d938ae5202e8db72b179ff4d76293/a0b58/5_1_3.webp 230w","/static/5a4d938ae5202e8db72b179ff4d76293/0cb6c/5_1_3.webp 410w"],"sizes":"(max-width: 410px) 100vw, 410px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/5a4d938ae5202e8db72b179ff4d76293/81c8e/5_1_3.png 230w","/static/5a4d938ae5202e8db72b179ff4d76293/d68e4/5_1_3.png 410w"],"sizes":"(max-width: 410px) 100vw, 410px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/5a4d938ae5202e8db72b179ff4d76293/d68e4/5_1_3.png","alt":"5 1 3","title":"5 1 3","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":"can be represented in matrix form like so:"}]},{"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":"sudoku "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"8"}]},{"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":"0"}]},{"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":"3"}]},{"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":"0"}]},{"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":"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","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","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","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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"5"}]},{"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":"0"}]},{"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":"7"}]},{"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":"0"}]},{"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":"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","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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"0"}]},{"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":"3"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"4"}]},{"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","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","number"]},"children":[{"type":"text","value":"9"}]},{"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":"4"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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","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","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","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","number"]},"children":[{"type":"text","value":"7"}]},{"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":"3"}]},{"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":"0"}]},{"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":"5"}]},{"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":"6"}]},{"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":"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","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":"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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"0"}]},{"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":"6"}]},{"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":"0"}]},{"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":"4"}]},{"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":"0"}]},{"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":"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","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","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","number"]},"children":[{"type":"text","value":"7"}]},{"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":"8"}]},{"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":"0"}]},{"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":"3"}]},{"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":"9"}]},{"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":"0"}]},{"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":"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","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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"3"}]},{"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","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":" "},{"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","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","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","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","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","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","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","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","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here the value zero represents an empty square, as zero is not an acceptable value in a finished sudoku puzzle."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here is a simple function for printing out the above sudoku grid:"}]},{"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_grid"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"for"}]},{"type":"text","value":" row "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sudoku"},{"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":" square "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" row"},{"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":"if"}]},{"type":"text","value":" square "},{"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":"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":"square"},{"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":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"\" _\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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 "},{"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\nprint_grid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The printout should look like this::"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":"\n 9 _ _ _ 8 _ 3 _ _\n _ _ _ 2 5 _ 7 _ _\n _ 2 _ 3 _ _ _ _ 4\n _ 9 4 _ _ _ _ _ _\n _ _ _ 7 3 _ 5 6 _\n 7 _ 5 _ 6 _ 4 _ _\n _ _ 7 8 _ 3 9 _ _\n _ _ 1 _ _ _ _ _ 3\n 3 _ _ _ _ _ _ _ 2\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Any common game with a gameboard layout can be modelled in a similar fashion. Among others, chess, Minesweeper, Battleship or Mastermind are all based on a two-dimensional grid. For sudoku, it is natural to use numbers to represent the game state, but for other games, different methods may be better."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Go","tmcname":"part05-03_go"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In a game of Go two players take turns to place black and white stones on a game board. The winner is the player who manages to encircle a bigger area on the board with their own game pieces."}]},{"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":"who_won(game_board: list)"}]},{"type":"text","value":", which takes a two-dimensional array as its argument. The array consists of integer values, which represent the following situations:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"0: empty square"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"1: player 1 game piece"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"2: player 2 game piece"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The scoring rules of Go can be quite complex, but in this exercise it is enough to compare the number of pieces each player has on the game board. Also, the size of the game board is not limited."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return the value 1 if player 1 won, and the value 2 if player 2 won. If both players have the same number of pieces on the board, the function should return the value 0."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: check row","tmcname":"part05-04_sudoku_row"},"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":"row_correct(sudoku: list, row_no: int)"}]},{"type":"text","value":", which takes a two-dimensional array representing a sudoku grid, and an integer referring to a single row, as its arguments. Rows are indexed from 0."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":", depending on whether the row is filled in correctly, that is, whether it contains each of the numbers 1 to 9 at most once."}]},{"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":"sudoku "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"8"}]},{"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":"0"}]},{"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":"3"}]},{"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":"0"}]},{"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":"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","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":" "},{"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","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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"5"}]},{"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":"0"}]},{"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":"7"}]},{"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":"0"}]},{"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":"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","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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"0"}]},{"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":"3"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"4"}]},{"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","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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"4"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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","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","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","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","number"]},"children":[{"type":"text","value":"7"}]},{"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":"3"}]},{"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":"0"}]},{"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":"5"}]},{"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":"6"}]},{"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":"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","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":"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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"0"}]},{"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":"6"}]},{"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":"0"}]},{"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":"4"}]},{"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":"0"}]},{"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":"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","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","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","number"]},"children":[{"type":"text","value":"7"}]},{"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":"8"}]},{"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":"0"}]},{"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":"3"}]},{"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":"9"}]},{"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":"0"}]},{"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":"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","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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"3"}]},{"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","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":" "},{"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","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","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","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","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","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","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","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","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":"row_correct"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"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":"row_correct"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"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":"True\nFalse"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: check column","tmcname":"part05-05_sudoku_column"},"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":"column_correct(sudoku: list, column_no: int)"}]},{"type":"text","value":", which takes a two-dimensional array representing a sudoku grid, and an integer referring to a single column, as its arguments. Columns are indexed from 0."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":", depending on whether the column is filled in correctly, that is, whether it contains each of the numbers 1 to 9 at most once."}]},{"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":"sudoku "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"8"}]},{"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":"0"}]},{"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":"3"}]},{"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":"0"}]},{"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":"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","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":" "},{"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","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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"5"}]},{"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":"0"}]},{"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":"7"}]},{"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":"0"}]},{"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":"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","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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"0"}]},{"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":"3"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"4"}]},{"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","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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"4"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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","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","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","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","number"]},"children":[{"type":"text","value":"7"}]},{"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":"3"}]},{"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":"0"}]},{"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":"5"}]},{"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":"6"}]},{"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":"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","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":"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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"0"}]},{"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":"6"}]},{"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":"0"}]},{"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":"4"}]},{"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":"0"}]},{"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":"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","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","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","number"]},"children":[{"type":"text","value":"7"}]},{"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":"8"}]},{"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":"0"}]},{"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":"3"}]},{"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":"9"}]},{"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":"0"}]},{"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":"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","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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"3"}]},{"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","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":" "},{"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","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","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","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","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","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","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","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","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":"column_correct"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"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":"column_correct"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"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":"False\nTrue"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: check block","tmcname":"part05-06_sudoku_block"},"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":"block_correct(sudoku: list, row_no: int, column_no: int)"}]},{"type":"text","value":", which takes a two-dimensional array representing a sudoku grid, and two integers referring to the row and column indexes of a single square, as its arguments. Rows and columns are indexed from 0."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" depending on whether the 3 by 3 block to the right and down from the given indexes is filled in correctly. That is, whether the block contains each of the numbers 1 to 9 at most once."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice that this function does not strictly follow the rules of sudoku. In a real game of sudoku there are only 9 blocks to check, and these are located at indexes (0, 0), (0, 3), (0, 6), (3, 0), (3, 3), (3, 6), (6, 0), (6, 3) and (6, 6). Such restrictions on indexes should not be implemented here."}]},{"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":"sudoku "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"8"}]},{"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":"0"}]},{"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":"3"}]},{"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":"0"}]},{"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":"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","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":" "},{"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","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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"5"}]},{"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":"0"}]},{"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":"7"}]},{"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":"0"}]},{"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":"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","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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"0"}]},{"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":"3"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"4"}]},{"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","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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"4"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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","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","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","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","number"]},"children":[{"type":"text","value":"7"}]},{"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":"3"}]},{"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":"0"}]},{"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":"5"}]},{"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":"6"}]},{"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":"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","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":"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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"0"}]},{"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":"6"}]},{"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":"0"}]},{"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":"4"}]},{"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":"0"}]},{"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":"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","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","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","number"]},"children":[{"type":"text","value":"7"}]},{"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":"8"}]},{"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":"0"}]},{"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":"3"}]},{"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":"9"}]},{"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":"0"}]},{"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":"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","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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"3"}]},{"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","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":" "},{"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","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","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","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","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","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","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","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","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":"block_correct"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"0"}]},{"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":"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":"block_correct"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"1"}]},{"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":"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"False\nTrue"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The first function call should check the 3 by 3 block beginning with the square at indexes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0, 0"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"9 0 0\n2 0 0\n0 2 0\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The second function call should check the 3 by 3 block beginning with the square at row 1, column 2:"}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"0 2 5\n0 3 0\n4 0 0\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This second block would not be checked in an actual game of sudoku, but your function should allow for it to be checked."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: check grid","tmcname":"part05-07_sudoku_grid"},"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":"sudoku_grid_correct(sudoku: list)"}]},{"type":"text","value":", which takes a two-dimensional array representing a sudoku grid as its argument. The function should use the functions from the three previous exercises to determine whether the complete sudoku grid is filled in correctly. Copy the functions from the exercises above into your Python code file for this exercise."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should check each of the nine rows, columns and 3 by 3 blocks in the grid. If all contain each of the numbers 1 to 9 at most once, the function returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":". If a single one is filled in incorrectly, the function returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The image of a sudoku grid above these exercises has the nine blocks within the grid indicated with thicker borders. These are the blocks the function should check, and they begin at the indexes (0, 0), (0, 3), (0, 6), (3, 0), (3, 3), (3, 6), (6, 0), (6, 3) and (6, 6)."}]},{"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":"sudoku1 "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"8"}]},{"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":"0"}]},{"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":"3"}]},{"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":"0"}]},{"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":"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","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":" "},{"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","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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"5"}]},{"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":"0"}]},{"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":"7"}]},{"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":"0"}]},{"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":"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","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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"0"}]},{"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":"3"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"4"}]},{"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","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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"4"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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","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","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","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","number"]},"children":[{"type":"text","value":"7"}]},{"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":"3"}]},{"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":"0"}]},{"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":"5"}]},{"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":"6"}]},{"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":"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","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":"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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"0"}]},{"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":"6"}]},{"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":"0"}]},{"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":"4"}]},{"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":"0"}]},{"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":"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","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","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","number"]},"children":[{"type":"text","value":"7"}]},{"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":"8"}]},{"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":"0"}]},{"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":"3"}]},{"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":"9"}]},{"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":"0"}]},{"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":"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","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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"3"}]},{"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","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":" "},{"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","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","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","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","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","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","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","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","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":"sudoku_grid_correct"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku1"},{"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\nsudoku2 "},{"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":"text","value":"\n "},{"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":" "},{"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":" "},{"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":"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":" "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"5"}]},{"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":"0"}]},{"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":"4"}]},{"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","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"0"}]},{"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":"3"}]},{"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":"5"}]},{"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":"1"}]},{"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":"0"}]},{"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":"6"}]},{"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":"0"}]},{"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":"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","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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"1"}]},{"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":"6"}]},{"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":"0"}]},{"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":"0"}]},{"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":"8"}]},{"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":"3"}]},{"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":"9"}]},{"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","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":" "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"0"}]},{"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":"4"}]},{"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":"6"}]},{"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":"3"}]},{"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":"2"}]},{"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":"8"}]},{"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","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":" "},{"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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"1"}]},{"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":"0"}]},{"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":"5"}]},{"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":"7"}]},{"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":"0"}]},{"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":"6"}]},{"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","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":" "},{"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":"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":"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":" "},{"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":" "},{"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","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","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","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","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"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","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","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","number"]},"children":[{"type":"text","value":"4"}]},{"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":"5"}]},{"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":"7"}]},{"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":"2"}]},{"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":"6"}]},{"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":"3"}]},{"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","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":" "},{"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":" "},{"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","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","number"]},"children":[{"type":"text","value":"8"}]},{"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":"0"}]},{"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":"0"}]},{"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":"5"}]},{"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":"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":"text","value":"\n "},{"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":"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":"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":" "},{"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","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","number"]},"children":[{"type":"text","value":"3"}]},{"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":"9"}]},{"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":"0"}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"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":"sudoku_grid_correct"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku2"},{"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":"False\nTrue"}]}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will be able to create lists with different types of items
    • \n
    • You will know how to use lists to organise data
    • \n
    • You will be able to store a matrix as a two-dimensional list
    • \n

    Becoming a proficient programmer requires a lot of practice, sometimes even quite mechanical practice. It also involves developing problem solving skills and applying intuition. This is why there are a lot of exercises of different kinds on this course. Some of them ask you to quite straightforwardly apply what you have learnt in the material, but some of them are intentionally more challenging and open-ended.

    Some of the exercises might at first seem overwhelming, but this is nothing to worry about. None of the exercises is strictly mandatory, and in fact only 25 % of the points in each part is required to pass the course. You can find more details about passing the course on the page on grading.

    The exercises are not in any specific order of difficulty. Each section usually introduces some new programming concepts, and these are then practised with both simpler and more complicated exercises. If you come across an exercise that feels too difficult, move on to the next one. You can always come back to the more difficult exercises if you have time later.

    When the going inevitably gets tough, a word of consolation: a task that seems impossibly difficult this week will likely feel rather easy in about four weeks' time.

    Lists with different types of data

    In the previous part we mainly handled lists with integer items, but any types of values can be stored in lists. A list of strings could look like this:

    names = [\"Marlyn\", \"Ruth\", \"Paul\"]\nprint(names)\nnames.append(\"David\")\nprint(names)\n\nprint(\"Number of names on the list:\", len(names))\nprint(\"Names in alphabetical order:\")\nnames.sort()\nfor name in names:\n  print(name)

    ['Marlyn', 'Ruth', 'Paul']\n['Marlyn', 'Ruth', 'Paul', 'David']\nNumber of names on the list: 4\nNames in alphabetical order:\nDavid\nMarlyn\nPaul\nRuth

    Floating point numbers are also valid list items:

    measurements = [-2.5, 1.1, 7.5, 14.6, 21.0, 19.2]\n\nfor measure in measurements:\n    print(measure)\n\nmean = sum(measurements) / len(measurements)\n\nprint(\"The mean is:\", mean)

    -2.5\n1.1\n7.5\n14.6\n21.0\n19.2\nThe mean is: 10.15

    Reminder: using global variables within functions

    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 global variables.

    Using global variables from within functions is usually a bad idea. Among other issues, doing so may cause bugs which are difficult to trace.

    Below is an example of a function which uses a global variable \"by mistake\":

    def print_reversed(names: list):\n    # using the global variable instead of the parameter by accident\n    i = len(name_list) - 1\n    while i >= 0:\n        print(name_list[i])\n        i -= 1\n\n# here the global variable is assigned\nname_list = [\"Steve\", \"Jean\", \"Katherine\", \"Paul\"]\nprint_reversed(name_list)\nprint()\nprint_reversed([\"Huey\", \"Dewey\", \"Louie\"])

    Paul\nKatherine\nJean\nSteve

    Paul\nKatherine\nJean\nSteve

    Even though both function calls have the right kind of argument, the function always prints out what is stored in the global variable name_list.

    To make matters even more muddled, remember that all code for testing your functions should be placed within the if __name__ == \"__main__\": block for the automatic tests. The previous example should be modified:

    def print_reversed(names: list):\n    # using the global variable instead of the parameter by accident\n    i = len(name_list) - 1\n    while i>=0:\n        print(name_list[i])\n        i -= 1\n\n# All the code for testing the function should be within this block\nif __name__ == \"__main__\":\n    # here the global variable is assigned\n    name_list = [\"Steve\", \"Jean\", \"Katherine\", \"Paul\"]\n    print_reversed(name_list)\n    print()\n    print_reversed([\"Huey\", \"Dewey\", \"Louie\"])

    Notice the global variable is assigned within the if block now.

    The automatic tests in the TMC system are executed without running any of the code in the if block. So, in this latter example the function wouldn't even theoretically work, since it refers to the variable name_list, which doesn't exist at all when the tests are executed.

    Warning: overwriting a parameter and returning too early

    There are a couple of novel sources of bugs we should look at before jumping into the exercises in this part. Let's have a look at a function which tells us whether an integer is found within a list. Both are defined as parameters of the function:

    def number_in_list(numbers: list, number: int):\n    for number in numbers:\n        if number == number:\n            return True\n        else:\n            return False

    This function seems to always return True. The reason is that the for loop overwrites the value stored in the parameter number. Thus the condition in the if statement is always true.

    Renaming the parameter solves the problem:

    def number_in_list(numbers: list, searched_number: int):\n    for number in numbers:\n        if number == searched_number:\n            return True\n        else:\n            return False

    Now the condition in the if statement looks better. But there is a new problem, since the function still doesn't seem to work correctly. Trying out the following manifests a bug:

    found = number_in_list([1, 2, 3, 4], 3)\nprint(found)  # prints out False

    The issue here is that the function returns too early, without checking all the numbers in the list. In fact, the function takes only the first item in the list, and returns True or False depending on its value. We cannot know whether a number is not present in the list until we have checked all the items in the list. The return False command should be placed outside the for loop:

    def number_in_list(numbers: list, searched_number: int):\n    for number in numbers:\n        if number == searched_number:\n            return True\n\n    return False

    Let's have a look at another faulty function:

    def unique_numbers(numbers: list):\n    # a helper variable to store all the numbers we've already checked\n    numbers = []\n    for number in numbers:\n        # have we seen this number already?\n        if number in numbers:\n            return False\n        numbers.append(number)\n\n    return True\n\nunique = unique_numbers([1, 2, 2])\nprint(unique)  # prints out True

    This function is supposed to check whether all numbers in a list are distinct from each other, but it always returns True.

    Here the function again overwrites the value stored in its parameter by mistake. The function tries to use the variable numbers to store all the numbers already checked, but this overwrites the original argument list. Renaming the helper variable is an easy fix:

    def unique_numbers(numbers: list):\n    # a helper variable to store all the numbers we've already checked\n    numbers_checked = []\n    for number in numbers:\n        # have we seen this number already?\n        if number in numbers_checked:\n            return False\n        numbers_checked.append(number)\n\n    return True\n\nunique = unique_numbers([1, 2, 2])\nprint(unique)  # prints out False

    Problems like this, and many others, can be located and fixed with the help of the debugger or the visualisation tool. Learning to use these efficiently cannot be emphasised enough.

    Please write a function named longest(strings: list), which takes a list of strings as its argument. The function finds and returns the longest string in the list. You may assume there is always a single longest string in the list.

    An example of expected behaviour:

    \nif __name__ == \"__main__\":\n    strings = [\"hi\", \"hiya\", \"hello\", \"howdydoody\", \"hi there\"]\n    print(longest(strings))\n

    howdydoody

    Lists within lists

    The items in a list can be lists themselves:

    my_list = [[5, 2, 3], [4, 1], [2, 2, 5, 1]]\nprint(my_list)\nprint(my_list[1])\nprint(my_list[1][0])

    [[5, 2, 3], [4, 1], [2, 2, 5, 1]]\n[4, 1]\n4

    Why would lists within lists be useful?

    Remember that lists can contain items of different types. You could store information about a person in a list. For instance, you could include their name as the first item, their age as the second item, and their height in meters as the third item:

    [\"Anne\", 12, 1.45]

    A database of persons could then be a list, whose items would be lists containing information about a single person:

    persons = [[\"Betty\", 10, 1.37], [\"Peter\", 7, 1.25], [\"Emily\", 32, 1.64], [\"Alan\", 39, 1.78]]\n\nfor person in persons:\n  name = person[0]\n  age = person[1]\n  height = person[2]\n  print(f\"{name}: age {age} years, height {height} meters\")

    Betty: age 10 years, height 1.37 meters\nPeter: age 7 years, height 1.25 meters\nEmily: age 32 years, height 1.64 meters\nAlan: age 39 years, height 1.78 meters

    The for loop goes through the items in the outer list one by one. That is, each list containing information about a single person is, in turn, assigned to the variable person.

    Lists arent always the best way to present data, such as information about a person. We will soon come across Python dictionaries, which are often better suited to such situations.

    Matrices

    A two-dimensional array, or a matrix, is also a natural application of a list within a list.

    For example, the following matrix

    \n \n \n \n \n \n \"5\n \n \n

    could be presented as a two-dimensional list in Python like so:

    my_matrix = [[1, 2, 3], [3, 2, 1], [4, 5, 6]]

    Since a matrix is a list containing lists, the individual elements within the matrix can be accessed using consecutive square brackets. The first index refers to the row, and the second to the column. Indexing starts from zero, so for example my_matrix[0][1] refers to the second item on the first row.

    my_matrix = [[1, 2, 3], [3, 2, 1], [4, 5, 6]]\n\nprint(my_matrix[0][1])\nmy_matrix[1][0] = 10\nprint(my_matrix)

    2\n[[1, 2, 3], [10, 2, 1], [4, 5, 6]]

    Like any other list, the rows of the matrix can be traversed wth a for loop. The following code prints out each row of the matrix on a separate line:

    my_matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n\nfor row in my_matrix:\n    print(row)

    [1, 2, 3]\n[4, 5, 6]\n[7, 8, 9]

    Likewise, nested loops can be used to access the individual elements. The following code prints out each element in the matrix on a separate line with the help of two for loops:

    my_matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n\nfor row in my_matrix:\n    print(\"a new row\")\n    for element in row:\n        print(element)

    a new row\n1\n2\n3\na new row\n4\n5\n6\na new row\n7\n8\n9

    Visualising code containing lists within lists

    Programs containing lists within lists can feel hard to grasp at first. The visualisation tool from Python Tutor is a great help in understanding how they work. The following is a visualisation of the example above:

    \n \n \n \n \n \n \"5\n \n \n

    The image above reveals that a 3 by 3 matrix technically consists of four lists. The first list represents the entire matrix. The three remaining lists are items in the first list, and represent the rows.

    As multidimensional lists can be traversed with nested loops, it would be natural to think of the lists themselves as nested, but the image above shows us this isn't actually so. Instead, the list representing the whole matrix \"points\" to each individual list representing a row in the matrix. This is called a reference, and in the following section the idea will be explored more thoroughly.

    In the image above the execution has progressed to the second row of the matrix, and this list is what the variable row currently refers to. The variable element contains the element the execution is currently at. The value stored in element is the middle item in the list, i.e. 5.

    Accessing items in a matrix

    Accessing a single row within a matrix is simple - just choose the desired row. The following function calculates the sum of the elements on a chosen row:

    def sum_of_row(my_matrix, row_no: int):\n    # choose the desired row from within the matrix\n    row = my_matrix[row_no]\n    row_sum = 0\n    for item in row:\n        row_sum += item\n\n    return row_sum\n\nm = [[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 5], [2, 9, 15, 1]]\n\nmy_sum = sum_of_row(m, 1)\nprint(my_sum) # prints out 33 (which equals 9 + 1 + 12 + 11)

    Working with columns within a matrix is slightly more complicated, as the matrix is stored by rows:

    def sum_of_column(my_matrix, column_no: int):\n    # go through each row and select the item at the chosen position\n    column_sum = 0\n    for row in my_matrix:\n        column_sum += row[column_no]\n\n    return column_sum\n\nm = [[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 5], [2, 9, 15, 1]]\n\nmy_sum = sum_of_column(m, 2)\nprint(my_sum) # prints out 39 (which equals 3 + 12 + 9 + 15)

    The column handled here consists of the elements at index 2 on each row.

    The visualisation tool is definitely recommended for understanding how these functions work.

    Changing the value of a single element within the matrix is simple: choose a row within the matrix, and then a column within the row:

    def change_value(my_matrix, row_no: int, column_no: int, new_value: int):\n    # choose the desired row\n    row = my_matrix[row_no]\n    # select the correct item within the row\n    row[column_no] = new_value\n\nm = [[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 5], [2, 9, 15, 1]]\n\nprint(m)\nchange_value(m, 2, 3, 1000)\nprint(m)

    [[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 5], [2, 9, 15, 1]]\n[[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 1000], [2, 9, 15, 1]]

    Notice how above we used the indexes of the row and column to access a chosen element. If we want to change the contents of the matrix, we have to access the elements by their indexes. This means that we can't use a simple for item in list loop to traverse the matrix if we want to change the contents of the matrix.

    Instead, we will have to keep track of the indexes of the elements, for example with a while loop, or a for loop using the range function. The following code increases the value of each element in the matrix by one:

    m = [[1,2,3], [4,5,6], [7,8,9]]\n\nfor i in range(len(m)): # using the number of rows in the matrix\n    for j in range(len(m[i])): # using the number of items on each row \n        m[i][j] += 1\n\nprint(m)

    [[2, 3, 4], [5, 6, 7], [8, 9, 10]]

    The outer loop goes through indexes from zero to the length of the matrix, that is, the number of rows in the matrix. The inner loop goes through indexes from zero to the length of each row within the matrix.

    Please write a function named count_matching_elements(my_matrix: list, element: int), which takes a two-dimensional array of integers and a single integer value as its arguments. The function then counts how many elements within the matrix match the argument value.

    An example of how the function should work:

    m = [[1, 2, 1], [0, 3, 4], [1, 0, 0]]\nprint(count_matching_elements(m, 1))

    3

    A two-dimensional array as a data structure in a game

    A matrix can be a very useful data structure in many different games. For example, the grid of a sudoku game in the image below

    \n \n \n \n \n \n \"5\n \n \n

    can be represented in matrix form like so:

    sudoku = [\n  [9, 0, 0, 0, 8, 0, 3, 0, 0],\n  [0, 0, 0, 2, 5, 0, 7, 0, 0],\n  [0, 2, 0, 3, 0, 0, 0, 0, 4],\n  [0, 9, 4, 0, 0, 0, 0, 0, 0],\n  [0, 0, 0, 7, 3, 0, 5, 6, 0],\n  [7, 0, 5, 0, 6, 0, 4, 0, 0],\n  [0, 0, 7, 8, 0, 3, 9, 0, 0],\n  [0, 0, 1, 0, 0, 0, 0, 0, 3],\n  [3, 0, 0, 0, 0, 0, 0, 0, 2]\n]

    Here the value zero represents an empty square, as zero is not an acceptable value in a finished sudoku puzzle.

    Here is a simple function for printing out the above sudoku grid:

    def print_grid(sudoku):\n    for row in sudoku:\n        for square in row:\n            if square > 0:\n                print(f\" {square}\", end=\"\")\n            else:\n                print(\" _\", end=\"\")\n        print()\n\nprint_grid(sudoku)

    The printout should look like this::

    \n 9 _ _ _ 8 _ 3 _ _\n _ _ _ 2 5 _ 7 _ _\n _ 2 _ 3 _ _ _ _ 4\n _ 9 4 _ _ _ _ _ _\n _ _ _ 7 3 _ 5 6 _\n 7 _ 5 _ 6 _ 4 _ _\n _ _ 7 8 _ 3 9 _ _\n _ _ 1 _ _ _ _ _ 3\n 3 _ _ _ _ _ _ _ 2\n

    Any common game with a gameboard layout can be modelled in a similar fashion. Among others, chess, Minesweeper, Battleship or Mastermind are all based on a two-dimensional grid. For sudoku, it is natural to use numbers to represent the game state, but for other games, different methods may be better.

    In a game of Go two players take turns to place black and white stones on a game board. The winner is the player who manages to encircle a bigger area on the board with their own game pieces.

    Please write a function named who_won(game_board: list), which takes a two-dimensional array as its argument. The array consists of integer values, which represent the following situations:

      \n
    • 0: empty square
    • \n
    • 1: player 1 game piece
    • \n
    • 2: player 2 game piece
    • \n

    The scoring rules of Go can be quite complex, but in this exercise it is enough to compare the number of pieces each player has on the game board. Also, the size of the game board is not limited.

    The function should return the value 1 if player 1 won, and the value 2 if player 2 won. If both players have the same number of pieces on the board, the function should return the value 0.

    Please write a function named row_correct(sudoku: list, row_no: int), which takes a two-dimensional array representing a sudoku grid, and an integer referring to a single row, as its arguments. Rows are indexed from 0.

    The function should return True or False, depending on whether the row is filled in correctly, that is, whether it contains each of the numbers 1 to 9 at most once.

    sudoku = [\n  [9, 0, 0, 0, 8, 0, 3, 0, 0],\n  [2, 0, 0, 2, 5, 0, 7, 0, 0],\n  [0, 2, 0, 3, 0, 0, 0, 0, 4],\n  [2, 9, 4, 0, 0, 0, 0, 0, 0],\n  [0, 0, 0, 7, 3, 0, 5, 6, 0],\n  [7, 0, 5, 0, 6, 0, 4, 0, 0],\n  [0, 0, 7, 8, 0, 3, 9, 0, 0],\n  [0, 0, 1, 0, 0, 0, 0, 0, 3],\n  [3, 0, 0, 0, 0, 0, 0, 0, 2]\n]\n\nprint(row_correct(sudoku, 0))\nprint(row_correct(sudoku, 1))

    True\nFalse

    Please write a function named column_correct(sudoku: list, column_no: int), which takes a two-dimensional array representing a sudoku grid, and an integer referring to a single column, as its arguments. Columns are indexed from 0.

    The function should return True or False, depending on whether the column is filled in correctly, that is, whether it contains each of the numbers 1 to 9 at most once.

    sudoku = [\n  [9, 0, 0, 0, 8, 0, 3, 0, 0],\n  [2, 0, 0, 2, 5, 0, 7, 0, 0],\n  [0, 2, 0, 3, 0, 0, 0, 0, 4],\n  [2, 9, 4, 0, 0, 0, 0, 0, 0],\n  [0, 0, 0, 7, 3, 0, 5, 6, 0],\n  [7, 0, 5, 0, 6, 0, 4, 0, 0],\n  [0, 0, 7, 8, 0, 3, 9, 0, 0],\n  [0, 0, 1, 0, 0, 0, 0, 0, 3],\n  [3, 0, 0, 0, 0, 0, 0, 0, 2]\n]\n\nprint(column_correct(sudoku, 0))\nprint(column_correct(sudoku, 1))

    False\nTrue

    Please write a function named block_correct(sudoku: list, row_no: int, column_no: int), which takes a two-dimensional array representing a sudoku grid, and two integers referring to the row and column indexes of a single square, as its arguments. Rows and columns are indexed from 0.

    The function should return True or False depending on whether the 3 by 3 block to the right and down from the given indexes is filled in correctly. That is, whether the block contains each of the numbers 1 to 9 at most once.

    Notice that this function does not strictly follow the rules of sudoku. In a real game of sudoku there are only 9 blocks to check, and these are located at indexes (0, 0), (0, 3), (0, 6), (3, 0), (3, 3), (3, 6), (6, 0), (6, 3) and (6, 6). Such restrictions on indexes should not be implemented here.

    sudoku = [\n  [9, 0, 0, 0, 8, 0, 3, 0, 0],\n  [2, 0, 0, 2, 5, 0, 7, 0, 0],\n  [0, 2, 0, 3, 0, 0, 0, 0, 4],\n  [2, 9, 4, 0, 0, 0, 0, 0, 0],\n  [0, 0, 0, 7, 3, 0, 5, 6, 0],\n  [7, 0, 5, 0, 6, 0, 4, 0, 0],\n  [0, 0, 7, 8, 0, 3, 9, 0, 0],\n  [0, 0, 1, 0, 0, 0, 0, 0, 3],\n  [3, 0, 0, 0, 0, 0, 0, 0, 2]\n]\n\nprint(block_correct(sudoku, 0, 0))\nprint(block_correct(sudoku, 1, 2))

    False\nTrue

    The first function call should check the 3 by 3 block beginning with the square at indexes 0, 0:

    \n9 0 0\n2 0 0\n0 2 0\n

    The second function call should check the 3 by 3 block beginning with the square at row 1, column 2:

    \n0 2 5\n0 3 0\n4 0 0\n

    This second block would not be checked in an actual game of sudoku, but your function should allow for it to be checked.

    Please write a function named sudoku_grid_correct(sudoku: list), which takes a two-dimensional array representing a sudoku grid as its argument. The function should use the functions from the three previous exercises to determine whether the complete sudoku grid is filled in correctly. Copy the functions from the exercises above into your Python code file for this exercise.

    The function should check each of the nine rows, columns and 3 by 3 blocks in the grid. If all contain each of the numbers 1 to 9 at most once, the function returns True. If a single one is filled in incorrectly, the function returns False.

    The image of a sudoku grid above these exercises has the nine blocks within the grid indicated with thicker borders. These are the blocks the function should check, and they begin at the indexes (0, 0), (0, 3), (0, 6), (3, 0), (3, 3), (3, 6), (6, 0), (6, 3) and (6, 6).

    sudoku1 = [\n  [9, 0, 0, 0, 8, 0, 3, 0, 0],\n  [2, 0, 0, 2, 5, 0, 7, 0, 0],\n  [0, 2, 0, 3, 0, 0, 0, 0, 4],\n  [2, 9, 4, 0, 0, 0, 0, 0, 0],\n  [0, 0, 0, 7, 3, 0, 5, 6, 0],\n  [7, 0, 5, 0, 6, 0, 4, 0, 0],\n  [0, 0, 7, 8, 0, 3, 9, 0, 0],\n  [0, 0, 1, 0, 0, 0, 0, 0, 3],\n  [3, 0, 0, 0, 0, 0, 0, 0, 2]\n]\n\nprint(sudoku_grid_correct(sudoku1))\n\nsudoku2 = [\n  [2, 6, 7, 8, 3, 9, 5, 0, 4],\n  [9, 0, 3, 5, 1, 0, 6, 0, 0],\n  [0, 5, 1, 6, 0, 0, 8, 3, 9],\n  [5, 1, 9, 0, 4, 6, 3, 2, 8],\n  [8, 0, 2, 1, 0, 5, 7, 0, 6],\n  [6, 7, 4, 3, 2, 0, 0, 0, 5],\n  [0, 0, 0, 4, 5, 7, 2, 6, 3],\n  [3, 2, 0, 0, 8, 0, 0, 5, 7],\n  [7, 4, 5, 0, 0, 3, 9, 0, 1]\n]\n\nprint(sudoku_grid_correct(sudoku2))

    False\nTrue

    ","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-5/1-more-lists.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 able to create lists with different types of items"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to use lists to organise data"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to store a matrix as a two-dimensional list"}]},{"type":"text","value":"\n"}]}]},{"type":"comment","value":"the same text is in sections 3-1, 5-1 and 6-1, check them all if you're changing this"},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"About the exercises on this course"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Becoming a proficient programmer requires a lot of practice, sometimes even quite mechanical practice. It also involves developing problem solving skills and applying intuition. This is why there are a lot of exercises of different kinds on this course. Some of them ask you to quite straightforwardly apply what you have learnt in the material, but some of them are intentionally more challenging and open-ended."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some of the exercises might at first seem overwhelming, but this is nothing to worry about. None of the exercises is strictly mandatory, and in fact "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"only 25 % of the points in each part is required to pass the course."}]},{"type":"text","value":" You can find more details about passing the course on the "},{"type":"element","tagName":"a","properties":{"href":"/grading-and-exams"},"children":[{"type":"text","value":"page on grading"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"The exercises are not in any specific order of difficulty."}]},{"type":"text","value":" Each section usually introduces some new programming concepts, and these are then practised with both simpler and more complicated exercises. "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"If you come across an exercise that feels too difficult, move on to the next one."}]},{"type":"text","value":" You can always come back to the more difficult exercises if you have time later."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the going inevitably gets tough, a word of consolation: a task that seems impossibly difficult this week will likely feel rather easy in about four weeks' time."}]}]},{"type":"element","tagName":"h2","properties":{"id":"lists-with-different-types-of-data","style":"position:relative;"},"children":[{"type":"text","value":"Lists with different types of data"},{"type":"element","tagName":"a","properties":{"href":"#lists-with-different-types-of-data","ariaLabel":"lists with different types of data 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 the previous part we mainly handled lists with integer items, but any types of values can be stored in lists. A list of strings 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":"text","value":"names "},{"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":"\"Marlyn\""}]},{"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":"\"Ruth\""}]},{"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"},{"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":"names"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnames"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"David\""}]},{"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":"names"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Number of names on the list:\""}]},{"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":"len"}]},{"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":"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":"\"Names in alphabetical order:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnames"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"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":"for"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" names"},{"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"},{"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":"['Marlyn', 'Ruth', 'Paul']\n['Marlyn', 'Ruth', 'Paul', 'David']\nNumber of names on the list: 4\nNames in alphabetical order:\nDavid\nMarlyn\nPaul\nRuth"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Floating point numbers are also valid list items:"}]},{"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":"measurements "},{"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","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2.5"}]},{"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":"1.1"}]},{"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":"7.5"}]},{"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":"14.6"}]},{"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":"21.0"}]},{"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":"19.2"}]},{"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":" measure "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" measurements"},{"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":"measure"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nmean "},{"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":"measurements"},{"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":"measurements"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The mean is:\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"-2.5\n1.1\n7.5\n14.6\n21.0\n19.2\nThe mean is: 10.15"}]}]},{"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":"reminder-using-global-variables-within-functions","style":"position:relative;"},"children":[{"type":"text","value":"Reminder: using global variables within functions"},{"type":"element","tagName":"a","properties":{"href":"#reminder-using-global-variables-within-functions","ariaLabel":"reminder 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":"h2","properties":{"id":"warning-overwriting-a-parameter-and-returning-too-early","style":"position:relative;"},"children":[{"type":"text","value":"Warning: overwriting a parameter and returning too early"},{"type":"element","tagName":"a","properties":{"href":"#warning-overwriting-a-parameter-and-returning-too-early","ariaLabel":"warning overwriting a parameter and returning too early 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":"There are a couple of novel sources of bugs we should look at before jumping into the exercises in this part. Let's have a look at a function which tells us whether an integer is found within a list. Both are defined as parameters of the 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"number_in_list"}]},{"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":"text","value":" number"},{"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":"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":"if"}]},{"type":"text","value":" number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" number"},{"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","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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","boolean"]},"children":[{"type":"text","value":"False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This function seems to always return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":". The reason is that the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop overwrites the value stored in the parameter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":". Thus the condition in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statement is always true."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Renaming the parameter solves the problem:"}]},{"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":"number_in_list"}]},{"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":"text","value":" searched_number"},{"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":"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":"if"}]},{"type":"text","value":" number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" searched_number"},{"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","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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","boolean"]},"children":[{"type":"text","value":"False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now the condition in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statement looks better. But there is a new problem, since the function still doesn't seem to work correctly. Trying out the following manifests a bug:"}]},{"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":"found "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" number_in_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":"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":" "},{"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":" "},{"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":" "},{"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","punctuation"]},"children":[{"type":"text","value":","}]},{"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":"\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":"found"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# prints out False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The issue here is that the function returns too early, without checking all the numbers in the list. In fact, the function takes only the first item in the list, and returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" depending on its value. We cannot know whether a number is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not present"}]},{"type":"text","value":" in the list until we have checked all the items in the list. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return False"}]},{"type":"text","value":" command should be placed outside the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop:"}]},{"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":"number_in_list"}]},{"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":"text","value":" searched_number"},{"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":"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":"if"}]},{"type":"text","value":" number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" searched_number"},{"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","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at another faulty 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"unique_numbers"}]},{"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","comment"]},"children":[{"type":"text","value":"# a helper variable to store all the numbers we've already checked"}]},{"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 "},{"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","comment"]},"children":[{"type":"text","value":"# have we seen this number already?"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"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":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\nunique "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" unique_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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"2"}]},{"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":"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":"\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":"unique"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# prints out True"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This function is supposed to check whether all numbers in a list are distinct from each other, but it always returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here the function again overwrites the value stored in its parameter by mistake. The function tries to use the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"numbers"}]},{"type":"text","value":" to store all the numbers already checked, but this overwrites the original argument list. Renaming the helper variable is an easy fix:"}]},{"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":"unique_numbers"}]},{"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","comment"]},"children":[{"type":"text","value":"# a helper variable to store all the numbers we've already checked"}]},{"type":"text","value":"\n numbers_checked "},{"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","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","comment"]},"children":[{"type":"text","value":"# have we seen this number already?"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" number "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" numbers_checked"},{"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","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n numbers_checked"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\nunique "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" unique_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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"2"}]},{"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":"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":"\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":"unique"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# prints out False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Problems like this, and many others, can be located and fixed with the help of the debugger or the "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":". Learning to use these efficiently cannot be emphasised enough."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The longest string","tmcname":"part05-01_longest_string"},"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":"longest(strings: list)"}]},{"type":"text","value":", which takes a list of strings as its argument. The function finds and returns the longest string in the list. You may assume there is always a single longest string in the list."}]},{"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":"\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 strings "},{"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":"\"hiya\""}]},{"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":"\"hello\""}]},{"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":"\"howdydoody\""}]},{"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":"\"hi there\""}]},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"strings"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"howdydoody"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"lists-within-lists","style":"position:relative;"},"children":[{"type":"text","value":"Lists within lists"},{"type":"element","tagName":"a","properties":{"href":"#lists-within-lists","ariaLabel":"lists within lists 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 items in a list can be lists themselves:"}]},{"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","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":" "},{"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":" "},{"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","punctuation"]},"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":"4"}]},{"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":"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":"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":"2"}]},{"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":"2"}]},{"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":"5"}]},{"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":"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":"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":"\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":"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":"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":"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":"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[[5, 2, 3], [4, 1], [2, 2, 5, 1]]\n[4, 1]\n4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Why would lists within lists be useful?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Remember that lists can contain items of different types. You could store information about a person in a list. For instance, you could include their name as the first item, their age as the second item, and their height in meters as the third item:"}]},{"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","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Anne\""}]},{"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":"12"}]},{"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":"1.45"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A database of persons could then be a list, whose items would be lists containing information about a single person:"}]},{"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":"persons "},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betty\""}]},{"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":"10"}]},{"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":"1.37"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"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":"7"}]},{"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":"1.25"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"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":"32"}]},{"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":"1.64"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Alan\""}]},{"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":"39"}]},{"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":"1.78"}]},{"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":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" persons"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" person"},{"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":"\n age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" person"},{"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":"text","value":"\n height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" person"},{"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":"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":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": age "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"age"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" years, height "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" meters\""}]}]},{"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":"Betty: age 10 years, height 1.37 meters\nPeter: age 7 years, height 1.25 meters\nEmily: age 32 years, height 1.64 meters\nAlan: age 39 years, height 1.78 meters"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop goes through the items in the outer list one by one. That is, each list containing information about a single person is, in turn, assigned to the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"person"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lists arent always the best way to present data, such as information about a person. We will soon come across Python "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"dictionaries"}]},{"type":"text","value":", which are often better suited to such situations."}]},{"type":"element","tagName":"h2","properties":{"id":"matrices","style":"position:relative;"},"children":[{"type":"text","value":"Matrices"},{"type":"element","tagName":"a","properties":{"href":"#matrices","ariaLabel":"matrices 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":"A two-dimensional array, or a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"matrix"}]},{"type":"text","value":", is also a natural application of a list within a list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, the following matrix"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 151px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/57e8504bd70c2538405be5b5e1572347/29fe9/5_1_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: 100%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAYAAACNiR0NAAAACXBIWXMAAAsTAAALEwEAmpwYAAABw0lEQVQ4y62UyYoCQQyG+3EF38YHUPHoyYsLeFLw4smDO654UVBxAXfN8AVS1LSt4zATCJ1OUn+l8qcqKBQKMhwOpd1uq3Y6Hen3+1Iul6XZbEq323UxbHzEyCHX1pgGgEXJbDaTV/IuFrADst/vpdFouMBgMJDT6aR2r9fTyhB8xJDH4yH1el02m43+3+93CSgT2W63kkwmHeBoNHKAuVxOstmsA/RPlUgktBVPgPzsdjv9Iiw6n89qT6dTmc/nauMzwNvtpjHLo+KA5kbJZDJx4L7gI/ayhzBGk+kLO6MsqFQqyuR4PHYxbHzEyMFnMVqEBtZsyr5cLqpUwcLD4SDX61UVP198xMixfF8Dv6G+UA0AYcFHJS+PbKSs12tJpVIOxCelWq1KsViMZJkJYK0jxeZwtVpJOp2W4/HoAG1s8vm8lEqlSED6+Q3wt0dmVN4eOUwK3z+R8snYmP+jsfn3wTaW7T7TozDLy+VSSQtfPUhYLBZ61CeWuZPxeNyB+CzDfiaTeWKZFyoWi2krIl+bWq3mdvMr5PlqtVpPFQLAjNrzpRX6RzanPV8/3RTL9e1vzxf98wGtWmJGED4f0F9DhV9fOel+Sn/2BgAAAABJRU5ErkJggg=='); 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":["/static/57e8504bd70c2538405be5b5e1572347/8d139/5_1_1.webp 151w"],"sizes":"(max-width: 151px) 100vw, 151px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/57e8504bd70c2538405be5b5e1572347/29fe9/5_1_1.png 151w"],"sizes":"(max-width: 151px) 100vw, 151px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/57e8504bd70c2538405be5b5e1572347/29fe9/5_1_1.png","alt":"5 1 1","title":"5 1 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":"p","properties":{},"children":[{"type":"text","value":"could be presented as a two-dimensional list in Python like so:"}]},{"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_matrix "},{"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":"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":" "},{"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":" "},{"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","punctuation"]},"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":"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":" "},{"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":"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":"4"}]},{"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":"5"}]},{"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":"6"}]},{"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":"Since a matrix is a list containing lists, the individual elements within the matrix can be accessed using consecutive square brackets. The first index refers to the row, and the second to the column. Indexing starts from zero, so for example "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"my_matrix[0][1]"}]},{"type":"text","value":" refers to the second item on the first row."}]},{"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_matrix "},{"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":"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":" "},{"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":" "},{"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","punctuation"]},"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":"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":" "},{"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":"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":"4"}]},{"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":"5"}]},{"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":"6"}]},{"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":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_matrix"},{"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":"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":"text","value":"\nmy_matrix"},{"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":"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":"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_matrix"},{"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":"2\n[[1, 2, 3], [10, 2, 1], [4, 5, 6]]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Like any other list, the rows of the matrix can be traversed wth a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop. The following code prints out each row of the matrix on a separate line:"}]},{"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_matrix "},{"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":"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":" "},{"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":" "},{"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","punctuation"]},"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":"4"}]},{"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":"5"}]},{"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":"6"}]},{"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":" "},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":" row "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_matrix"},{"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":"row"},{"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":"[1, 2, 3]\n[4, 5, 6]\n[7, 8, 9]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Likewise, nested loops can be used to access the individual elements. The following code prints out each element in the matrix on a separate line with the help of two "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loops:"}]},{"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_matrix "},{"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":"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":" "},{"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":" "},{"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","punctuation"]},"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":"4"}]},{"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":"5"}]},{"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":"6"}]},{"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":" "},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":" row "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_matrix"},{"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":"\"a new row\""}]},{"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":" element "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" row"},{"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":"element"},{"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":"a new row\n1\n2\n3\na new row\n4\n5\n6\na new row\n7\n8\n9"}]}]},{"type":"element","tagName":"h2","properties":{"id":"visualising-code-containing-lists-within-lists","style":"position:relative;"},"children":[{"type":"text","value":"Visualising code containing lists within lists"},{"type":"element","tagName":"a","properties":{"href":"#visualising-code-containing-lists-within-lists","ariaLabel":"visualising code containing lists within lists 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":"Programs containing lists within lists can feel hard to grasp at first. The "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":" from Python Tutor is a great help in understanding how they work. The following is a visualisation of the example above:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/10e6583e2d3db7e04fba937644e94e43/87a80/5_1_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: 45.21739130434783%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAABV0lEQVQoz21R2U4DMQzc//82UB+gII6XFtru0e0m2dxxdpCTXiAsWbbla8ZulmXBvQSfMB4l9OzwJ4WUEpxzRf3Feo+cc8mxbbiQiGBmjRACtDIYB4lxEPDO10brSg03i9OEGGOJq6ayTClV8nVgztDzjOk0lQajddm25IxMrARmwgNiCFc0bFk5x2jZb7gpWVuCoeux235haNsSs9yfhBf7QBBiBlGGs7YsuJembD5TyJlgjcc4zJiVL8MYCWuhpR1WTxu8fBxgXMZsOnTiEZPaQOgHhNShMVJCSgkhxBUV0YJMyxVhRbnAesL6fY/12w7WJ4ToMIgWJ/kNF7ZIZNFQrHeIkb97xND3OOz36NoWfd/DGFMQVgYZMRIS5espfMg4jCto91kpW5ugVYB39dCXT/Jj6PyMi/z2b7HSRyjzjERTveGN1v9yq7n3bz0hSkj9ipgEfgBiZr5+y7pYPQAAAABJRU5ErkJggg=='); 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":["/static/10e6583e2d3db7e04fba937644e94e43/a0b58/5_1_2.webp 230w","/static/10e6583e2d3db7e04fba937644e94e43/bc10c/5_1_2.webp 460w","/static/10e6583e2d3db7e04fba937644e94e43/966d8/5_1_2.webp 920w","/static/10e6583e2d3db7e04fba937644e94e43/0ea8a/5_1_2.webp 973w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/10e6583e2d3db7e04fba937644e94e43/81c8e/5_1_2.png 230w","/static/10e6583e2d3db7e04fba937644e94e43/08a84/5_1_2.png 460w","/static/10e6583e2d3db7e04fba937644e94e43/c0255/5_1_2.png 920w","/static/10e6583e2d3db7e04fba937644e94e43/87a80/5_1_2.png 973w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/10e6583e2d3db7e04fba937644e94e43/c0255/5_1_2.png","alt":"5 1 2","title":"5 1 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 image above reveals that a 3 by 3 matrix technically consists of four lists. The first list represents the entire matrix. The three remaining lists are items in the first list, and represent the rows."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As multidimensional lists can be traversed with nested loops, it would be natural to think of the lists themselves as nested, but the image above shows us this isn't actually so. Instead, the list representing the whole matrix \"points\" to each individual list representing a row in the matrix. This is called a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reference"}]},{"type":"text","value":", and in the "},{"type":"element","tagName":"a","properties":{"href":"/part-5/2-references"},"children":[{"type":"text","value":"following section"}]},{"type":"text","value":" the idea will be explored more thoroughly."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the image above the execution has progressed to the second row of the matrix, and this list is what the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"row"}]},{"type":"text","value":" currently refers to. The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"element"}]},{"type":"text","value":" contains the element the execution is currently at. The value stored in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"element"}]},{"type":"text","value":" is the middle item in the list, i.e. 5."}]},{"type":"element","tagName":"h2","properties":{"id":"accessing-items-in-a-matrix","style":"position:relative;"},"children":[{"type":"text","value":"Accessing items in a matrix"},{"type":"element","tagName":"a","properties":{"href":"#accessing-items-in-a-matrix","ariaLabel":"accessing items in a matrix 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":"Accessing a single row within a matrix is simple - just choose the desired row. The following function calculates the sum of the elements on a chosen row:"}]},{"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":"sum_of_row"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_matrix"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" row_no"},{"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","comment"]},"children":[{"type":"text","value":"# choose the desired row from within the matrix"}]},{"type":"text","value":"\n row "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_matrix"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"row_no"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n row_sum "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" row"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n row_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" item\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" row_sum\n\nm "},{"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":"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":"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":" "},{"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":" "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"1"}]},{"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":"12"}]},{"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":"11"}]},{"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":" "},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"5"}]},{"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":" "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"15"}]},{"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":"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":"text","value":"\n\nmy_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sum_of_row"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"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":"1"}]},{"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_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# prints out 33 (which equals 9 + 1 + 12 + 11)"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Working with columns within a matrix is slightly more complicated, as the matrix is stored by rows:"}]},{"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":"sum_of_column"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_matrix"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" column_no"},{"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","comment"]},"children":[{"type":"text","value":"# go through each row and select the item at the chosen position"}]},{"type":"text","value":"\n column_sum "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" row "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_matrix"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n column_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" row"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"column_no"},{"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":" column_sum\n\nm "},{"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":"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":"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":" "},{"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":" "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"1"}]},{"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":"12"}]},{"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":"11"}]},{"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":" "},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"5"}]},{"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":" "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"15"}]},{"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":"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":"text","value":"\n\nmy_sum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sum_of_column"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"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":"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_sum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# prints out 39 (which equals 3 + 12 + 9 + 15)"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The column handled here consists of the elements at index 2 on "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"each row"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"The visualisation tool"}]},{"type":"text","value":" is definitely recommended for understanding how these functions work."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Changing the value of a single element within the matrix is simple: choose a row within the matrix, and then a column within the row:"}]},{"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":"change_value"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_matrix"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" row_no"},{"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":"text","value":" column_no"},{"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":"text","value":" new_value"},{"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","comment"]},"children":[{"type":"text","value":"# choose the desired row"}]},{"type":"text","value":"\n row "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_matrix"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"row_no"},{"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":"# select the correct item within the row"}]},{"type":"text","value":"\n row"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"column_no"},{"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":" new_value\n\nm "},{"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":"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":"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":" "},{"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":" "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"1"}]},{"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":"12"}]},{"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":"11"}]},{"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":" "},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"5"}]},{"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":" "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"15"}]},{"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":"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":"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":"m"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nchange_value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"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":"2"}]},{"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":"3"}]},{"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":"1000"}]},{"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":"m"},{"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, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 5], [2, 9, 15, 1]]\n[[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 1000], [2, 9, 15, 1]]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice how above we used the indexes of the row and column to access a chosen element. If we want to change the contents of the matrix, we have to access the elements by their indexes. This means that we can't use a simple "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for item in list"}]},{"type":"text","value":" loop to traverse the matrix if we want to change the contents of the matrix."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Instead, we will have to keep track of the indexes of the elements, for example with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" loop, or a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":" function. The following code increases the value of each element in the matrix by one:"}]},{"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":"m "},{"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":"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","punctuation"]},"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":"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","punctuation"]},"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":"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"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":"m"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# using the number of rows in the matrix"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" j "},{"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":"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":"m"},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# using the number of items on each row "}]},{"type":"text","value":"\n m"},{"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":"j"},{"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\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":"m"},{"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":"[[2, 3, 4], [5, 6, 7], [8, 9, 10]]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The outer loop goes through indexes from zero to the length of the matrix, that is, the number of rows in the matrix. The inner loop goes through indexes from zero to the length of each row within the matrix."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Number of matching elements","tmcname":"part05-02_number_of_elements"},"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":"count_matching_elements(my_matrix: list, element: int)"}]},{"type":"text","value":", which takes a two-dimensional array of integers and a single integer value as its arguments. The function then counts how many elements within the matrix match the argument value."}]},{"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":"m "},{"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":"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":" "},{"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":" "},{"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":"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":"0"}]},{"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":"3"}]},{"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":"4"}]},{"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":" "},{"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":"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","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":"count_matching_elements"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"m"},{"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":"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":"3"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"a-two-dimensional-array-as-a-data-structure-in-a-game","style":"position:relative;"},"children":[{"type":"text","value":"A two-dimensional array as a data structure in a game"},{"type":"element","tagName":"a","properties":{"href":"#a-two-dimensional-array-as-a-data-structure-in-a-game","ariaLabel":"a two dimensional array as a data structure in a game 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":"A matrix can be a very useful data structure in many different games. For example, the grid of a sudoku game in the image below"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 410px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/5a4d938ae5202e8db72b179ff4d76293/d68e4/5_1_3.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: 100.43478260869566%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAUCAIAAAAC64paAAAACXBIWXMAABYlAAAWJQFJUiTwAAADmUlEQVQ4y0WUWXOqPACG/fX9B163vTg67dQuap2OYBfbutQFRSmQoDGQQBAQqsXd5hvLmfO915l5kndJStM0XdcBAJ7nfX5+uq5bKpUIIQghTdMAAISQfr//8vJCCNE0zbZtVVWn0ylCKAV+5bou59w0zcPhUCgUVqtVFEWKomCMV6uVYRjPz8/T6dS27c1mY1kW55wQktJ13XGc4XAYBIEsy9Pp9OLiAmMMITQMAwBgWVar1SqXy8kxx3EGg4Hv+xDClOu6+/3e933OOWOMcy6KIsY4DEP7V7vdbjKZNJvN+Xzued5+v0+OMcZSyd0kSbJtu9PpUEr//PkDIVRVFQCgqup4PK7Vag8PD4wxSZJM05QkiTGmadqRfDgcHMfxPI9SyjkXBIEQ4nnecrn8+vpar9fj8ViSpIR2OBwopaZpHt98f39fLBaz2WyhUMhkMqIonpyc5HK5q6ur29vb6+vrQqFwdnaWTqcFQchms6VSKZvN3t3dvby8pCilm80GITSZTAghEMJ8Pr/ZbGazWRRFQRB8f38DAFqtVuLwer3+322EUBiGSVoAANM0c7lcGIaEEMYYpdTzPFmWX19fk8zm8zmEcLlcHnPGGEdRhBDabreGYaxWq3w+DyG0LAv+KgxDRVFEUXRdV1VVCCFCaL1eY4z/kiGEcRxrmrZYLG5ubizLIoQYv/J9X5blSqUym80Gg0GSfxzHRzJCiFJq2/bhcMAYu66bz+fjOA6CACFECFksFrquV6vVIAgSgzDGPz8/lmWl2u12t9vtdDoY40ajIUnS6enpaDRSFKVSqdTrdcMw3t7eLi8vW61Wu92GEH58fGCMFUVJ1Wq1drstiqIsy+VyuVqtptPpcrn89PTUaDRqtdrj42Mulzs/P6/X66IottttQRAkSep0On/rGQQB59x13SiKyuVyUs/lchnHMWNsOBy+v7/PZjPf9//V03XdlKqqpmkmk5AkiVKayWRs206M0XXdNM1msykIQhAEvV6PECJJ0mQyAQCkHMfZbreO43DOKaWMsWKxuFqtwjCcz+dhGMZxbBhGUhJK6W63M03TdV1K6ZGMMR4Oh57n9Xo927az2WwS8mg0AgBgjBPydDqVZZlS2u/3Hcc5khFClmWFYcg5t22bc14qlXa73WKxiOM4GcZoNOp2u/+GkczGcZzUeDxOps8YSz6GarVqWVbSdoSQaZqKonQ6HcYYAIBSqut60qL/AD7hB6pgN21XAAAAAElFTkSuQmCC'); 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":["/static/5a4d938ae5202e8db72b179ff4d76293/a0b58/5_1_3.webp 230w","/static/5a4d938ae5202e8db72b179ff4d76293/0cb6c/5_1_3.webp 410w"],"sizes":"(max-width: 410px) 100vw, 410px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/5a4d938ae5202e8db72b179ff4d76293/81c8e/5_1_3.png 230w","/static/5a4d938ae5202e8db72b179ff4d76293/d68e4/5_1_3.png 410w"],"sizes":"(max-width: 410px) 100vw, 410px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/5a4d938ae5202e8db72b179ff4d76293/d68e4/5_1_3.png","alt":"5 1 3","title":"5 1 3","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":"can be represented in matrix form like so:"}]},{"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":"sudoku "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"8"}]},{"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":"0"}]},{"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":"3"}]},{"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":"0"}]},{"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":"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","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","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","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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"5"}]},{"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":"0"}]},{"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":"7"}]},{"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":"0"}]},{"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":"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","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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"0"}]},{"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":"3"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"4"}]},{"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","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","number"]},"children":[{"type":"text","value":"9"}]},{"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":"4"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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","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","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","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","number"]},"children":[{"type":"text","value":"7"}]},{"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":"3"}]},{"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":"0"}]},{"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":"5"}]},{"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":"6"}]},{"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":"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","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":"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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"0"}]},{"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":"6"}]},{"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":"0"}]},{"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":"4"}]},{"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":"0"}]},{"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":"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","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","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","number"]},"children":[{"type":"text","value":"7"}]},{"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":"8"}]},{"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":"0"}]},{"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":"3"}]},{"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":"9"}]},{"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":"0"}]},{"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":"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","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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"3"}]},{"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","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":" "},{"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","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","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","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","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","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","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","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","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here the value zero represents an empty square, as zero is not an acceptable value in a finished sudoku puzzle."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here is a simple function for printing out the above sudoku grid:"}]},{"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_grid"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"for"}]},{"type":"text","value":" row "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" sudoku"},{"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":" square "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" row"},{"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":"if"}]},{"type":"text","value":" square "},{"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":"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":"square"},{"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":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"\" _\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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 "},{"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\nprint_grid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The printout should look like this::"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":"\n 9 _ _ _ 8 _ 3 _ _\n _ _ _ 2 5 _ 7 _ _\n _ 2 _ 3 _ _ _ _ 4\n _ 9 4 _ _ _ _ _ _\n _ _ _ 7 3 _ 5 6 _\n 7 _ 5 _ 6 _ 4 _ _\n _ _ 7 8 _ 3 9 _ _\n _ _ 1 _ _ _ _ _ 3\n 3 _ _ _ _ _ _ _ 2\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Any common game with a gameboard layout can be modelled in a similar fashion. Among others, chess, Minesweeper, Battleship or Mastermind are all based on a two-dimensional grid. For sudoku, it is natural to use numbers to represent the game state, but for other games, different methods may be better."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Go","tmcname":"part05-03_go"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In a game of Go two players take turns to place black and white stones on a game board. The winner is the player who manages to encircle a bigger area on the board with their own game pieces."}]},{"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":"who_won(game_board: list)"}]},{"type":"text","value":", which takes a two-dimensional array as its argument. The array consists of integer values, which represent the following situations:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"0: empty square"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"1: player 1 game piece"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"2: player 2 game piece"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The scoring rules of Go can be quite complex, but in this exercise it is enough to compare the number of pieces each player has on the game board. Also, the size of the game board is not limited."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return the value 1 if player 1 won, and the value 2 if player 2 won. If both players have the same number of pieces on the board, the function should return the value 0."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: check row","tmcname":"part05-04_sudoku_row"},"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":"row_correct(sudoku: list, row_no: int)"}]},{"type":"text","value":", which takes a two-dimensional array representing a sudoku grid, and an integer referring to a single row, as its arguments. Rows are indexed from 0."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":", depending on whether the row is filled in correctly, that is, whether it contains each of the numbers 1 to 9 at most once."}]},{"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":"sudoku "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"8"}]},{"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":"0"}]},{"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":"3"}]},{"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":"0"}]},{"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":"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","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":" "},{"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","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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"5"}]},{"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":"0"}]},{"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":"7"}]},{"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":"0"}]},{"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":"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","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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"0"}]},{"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":"3"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"4"}]},{"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","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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"4"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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","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","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","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","number"]},"children":[{"type":"text","value":"7"}]},{"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":"3"}]},{"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":"0"}]},{"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":"5"}]},{"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":"6"}]},{"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":"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","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":"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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"0"}]},{"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":"6"}]},{"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":"0"}]},{"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":"4"}]},{"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":"0"}]},{"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":"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","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","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","number"]},"children":[{"type":"text","value":"7"}]},{"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":"8"}]},{"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":"0"}]},{"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":"3"}]},{"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":"9"}]},{"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":"0"}]},{"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":"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","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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"3"}]},{"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","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":" "},{"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","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","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","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","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","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","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","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","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":"row_correct"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"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":"row_correct"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"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":"True\nFalse"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: check column","tmcname":"part05-05_sudoku_column"},"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":"column_correct(sudoku: list, column_no: int)"}]},{"type":"text","value":", which takes a two-dimensional array representing a sudoku grid, and an integer referring to a single column, as its arguments. Columns are indexed from 0."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":", depending on whether the column is filled in correctly, that is, whether it contains each of the numbers 1 to 9 at most once."}]},{"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":"sudoku "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"8"}]},{"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":"0"}]},{"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":"3"}]},{"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":"0"}]},{"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":"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","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":" "},{"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","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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"5"}]},{"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":"0"}]},{"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":"7"}]},{"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":"0"}]},{"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":"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","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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"0"}]},{"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":"3"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"4"}]},{"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","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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"4"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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","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","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","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","number"]},"children":[{"type":"text","value":"7"}]},{"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":"3"}]},{"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":"0"}]},{"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":"5"}]},{"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":"6"}]},{"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":"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","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":"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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"0"}]},{"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":"6"}]},{"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":"0"}]},{"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":"4"}]},{"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":"0"}]},{"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":"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","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","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","number"]},"children":[{"type":"text","value":"7"}]},{"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":"8"}]},{"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":"0"}]},{"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":"3"}]},{"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":"9"}]},{"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":"0"}]},{"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":"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","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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"3"}]},{"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","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":" "},{"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","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","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","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","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","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","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","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","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":"column_correct"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"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":"column_correct"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"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":"False\nTrue"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: check block","tmcname":"part05-06_sudoku_block"},"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":"block_correct(sudoku: list, row_no: int, column_no: int)"}]},{"type":"text","value":", which takes a two-dimensional array representing a sudoku grid, and two integers referring to the row and column indexes of a single square, as its arguments. Rows and columns are indexed from 0."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" depending on whether the 3 by 3 block to the right and down from the given indexes is filled in correctly. That is, whether the block contains each of the numbers 1 to 9 at most once."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice that this function does not strictly follow the rules of sudoku. In a real game of sudoku there are only 9 blocks to check, and these are located at indexes (0, 0), (0, 3), (0, 6), (3, 0), (3, 3), (3, 6), (6, 0), (6, 3) and (6, 6). Such restrictions on indexes should not be implemented here."}]},{"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":"sudoku "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"8"}]},{"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":"0"}]},{"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":"3"}]},{"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":"0"}]},{"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":"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","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":" "},{"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","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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"5"}]},{"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":"0"}]},{"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":"7"}]},{"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":"0"}]},{"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":"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","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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"0"}]},{"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":"3"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"4"}]},{"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","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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"4"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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","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","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","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","number"]},"children":[{"type":"text","value":"7"}]},{"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":"3"}]},{"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":"0"}]},{"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":"5"}]},{"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":"6"}]},{"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":"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","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":"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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"0"}]},{"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":"6"}]},{"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":"0"}]},{"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":"4"}]},{"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":"0"}]},{"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":"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","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","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","number"]},"children":[{"type":"text","value":"7"}]},{"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":"8"}]},{"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":"0"}]},{"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":"3"}]},{"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":"9"}]},{"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":"0"}]},{"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":"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","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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"3"}]},{"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","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":" "},{"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","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","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","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","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","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","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","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","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":"block_correct"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"0"}]},{"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":"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":"block_correct"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"1"}]},{"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":"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"False\nTrue"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The first function call should check the 3 by 3 block beginning with the square at indexes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0, 0"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"9 0 0\n2 0 0\n0 2 0\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The second function call should check the 3 by 3 block beginning with the square at row 1, column 2:"}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"0 2 5\n0 3 0\n4 0 0\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This second block would not be checked in an actual game of sudoku, but your function should allow for it to be checked."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: check grid","tmcname":"part05-07_sudoku_grid"},"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":"sudoku_grid_correct(sudoku: list)"}]},{"type":"text","value":", which takes a two-dimensional array representing a sudoku grid as its argument. The function should use the functions from the three previous exercises to determine whether the complete sudoku grid is filled in correctly. Copy the functions from the exercises above into your Python code file for this exercise."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should check each of the nine rows, columns and 3 by 3 blocks in the grid. If all contain each of the numbers 1 to 9 at most once, the function returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":". If a single one is filled in incorrectly, the function returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The image of a sudoku grid above these exercises has the nine blocks within the grid indicated with thicker borders. These are the blocks the function should check, and they begin at the indexes (0, 0), (0, 3), (0, 6), (3, 0), (3, 3), (3, 6), (6, 0), (6, 3) and (6, 6)."}]},{"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":"sudoku1 "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"8"}]},{"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":"0"}]},{"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":"3"}]},{"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":"0"}]},{"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":"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","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":" "},{"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","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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"5"}]},{"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":"0"}]},{"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":"7"}]},{"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":"0"}]},{"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":"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","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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"0"}]},{"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":"3"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"4"}]},{"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","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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"4"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"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","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","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","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","number"]},"children":[{"type":"text","value":"7"}]},{"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":"3"}]},{"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":"0"}]},{"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":"5"}]},{"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":"6"}]},{"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":"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","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":"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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"0"}]},{"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":"6"}]},{"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":"0"}]},{"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":"4"}]},{"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":"0"}]},{"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":"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","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","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","number"]},"children":[{"type":"text","value":"7"}]},{"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":"8"}]},{"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":"0"}]},{"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":"3"}]},{"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":"9"}]},{"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":"0"}]},{"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":"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","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","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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"0"}]},{"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":"3"}]},{"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","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":" "},{"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","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","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","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","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","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","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","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","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":"sudoku_grid_correct"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku1"},{"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\nsudoku2 "},{"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":"text","value":"\n "},{"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":" "},{"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":" "},{"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":"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":" "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"5"}]},{"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":"0"}]},{"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":"4"}]},{"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","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"0"}]},{"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":"3"}]},{"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":"5"}]},{"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":"1"}]},{"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":"0"}]},{"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":"6"}]},{"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":"0"}]},{"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":"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","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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"1"}]},{"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":"6"}]},{"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":"0"}]},{"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":"0"}]},{"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":"8"}]},{"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":"3"}]},{"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":"9"}]},{"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","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":" "},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]},{"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":"0"}]},{"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":"4"}]},{"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":"6"}]},{"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":"3"}]},{"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":"2"}]},{"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":"8"}]},{"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","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":" "},{"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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"1"}]},{"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":"0"}]},{"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":"5"}]},{"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":"7"}]},{"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":"0"}]},{"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":"6"}]},{"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","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":" "},{"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":"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":"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":" "},{"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":" "},{"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","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","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","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","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"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","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","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","number"]},"children":[{"type":"text","value":"4"}]},{"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":"5"}]},{"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":"7"}]},{"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":"2"}]},{"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":"6"}]},{"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":"3"}]},{"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","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":" "},{"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":" "},{"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","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","number"]},"children":[{"type":"text","value":"8"}]},{"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":"0"}]},{"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":"0"}]},{"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":"5"}]},{"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":"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":"text","value":"\n "},{"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":"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":"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":" "},{"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","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","number"]},"children":[{"type":"text","value":"3"}]},{"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":"9"}]},{"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":"0"}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"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":"sudoku_grid_correct"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku2"},{"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":"False\nTrue"}]}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will be able to create lists with different types of items
    • \n
    • You will know how to use lists to organise data
    • \n
    • You will be able to store a matrix as a two-dimensional list
    • \n

    Becoming a proficient programmer requires a lot of practice, sometimes even quite mechanical practice. It also involves developing problem solving skills and applying intuition. This is why there are a lot of exercises of different kinds on this course. Some of them ask you to quite straightforwardly apply what you have learnt in the material, but some of them are intentionally more challenging and open-ended.

    Some of the exercises might at first seem overwhelming, but this is nothing to worry about. None of the exercises is strictly mandatory, and in fact only 25 % of the points in each part is required to pass the course. You can find more details about passing the course on the page on grading.

    The exercises are not in any specific order of difficulty. Each section usually introduces some new programming concepts, and these are then practised with both simpler and more complicated exercises. If you come across an exercise that feels too difficult, move on to the next one. You can always come back to the more difficult exercises if you have time later.

    When the going inevitably gets tough, a word of consolation: a task that seems impossibly difficult this week will likely feel rather easy in about four weeks' time.

    Lists with different types of data

    In the previous part we mainly handled lists with integer items, but any types of values can be stored in lists. A list of strings could look like this:

    names = [\"Marlyn\", \"Ruth\", \"Paul\"]\nprint(names)\nnames.append(\"David\")\nprint(names)\n\nprint(\"Number of names on the list:\", len(names))\nprint(\"Names in alphabetical order:\")\nnames.sort()\nfor name in names:\n  print(name)

    ['Marlyn', 'Ruth', 'Paul']\n['Marlyn', 'Ruth', 'Paul', 'David']\nNumber of names on the list: 4\nNames in alphabetical order:\nDavid\nMarlyn\nPaul\nRuth

    Floating point numbers are also valid list items:

    measurements = [-2.5, 1.1, 7.5, 14.6, 21.0, 19.2]\n\nfor measure in measurements:\n    print(measure)\n\nmean = sum(measurements) / len(measurements)\n\nprint(\"The mean is:\", mean)

    -2.5\n1.1\n7.5\n14.6\n21.0\n19.2\nThe mean is: 10.15

    Reminder: using global variables within functions

    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 global variables.

    Using global variables from within functions is usually a bad idea. Among other issues, doing so may cause bugs which are difficult to trace.

    Below is an example of a function which uses a global variable \"by mistake\":

    def print_reversed(names: list):\n    # using the global variable instead of the parameter by accident\n    i = len(name_list) - 1\n    while i >= 0:\n        print(name_list[i])\n        i -= 1\n\n# here the global variable is assigned\nname_list = [\"Steve\", \"Jean\", \"Katherine\", \"Paul\"]\nprint_reversed(name_list)\nprint()\nprint_reversed([\"Huey\", \"Dewey\", \"Louie\"])

    Paul\nKatherine\nJean\nSteve

    Paul\nKatherine\nJean\nSteve

    Even though both function calls have the right kind of argument, the function always prints out what is stored in the global variable name_list.

    To make matters even more muddled, remember that all code for testing your functions should be placed within the if __name__ == \"__main__\": block for the automatic tests. The previous example should be modified:

    def print_reversed(names: list):\n    # using the global variable instead of the parameter by accident\n    i = len(name_list) - 1\n    while i>=0:\n        print(name_list[i])\n        i -= 1\n\n# All the code for testing the function should be within this block\nif __name__ == \"__main__\":\n    # here the global variable is assigned\n    name_list = [\"Steve\", \"Jean\", \"Katherine\", \"Paul\"]\n    print_reversed(name_list)\n    print()\n    print_reversed([\"Huey\", \"Dewey\", \"Louie\"])

    Notice the global variable is assigned within the if block now.

    The automatic tests in the TMC system are executed without running any of the code in the if block. So, in this latter example the function wouldn't even theoretically work, since it refers to the variable name_list, which doesn't exist at all when the tests are executed.

    Warning: overwriting a parameter and returning too early

    There are a couple of novel sources of bugs we should look at before jumping into the exercises in this part. Let's have a look at a function which tells us whether an integer is found within a list. Both are defined as parameters of the function:

    def number_in_list(numbers: list, number: int):\n    for number in numbers:\n        if number == number:\n            return True\n        else:\n            return False

    This function seems to always return True. The reason is that the for loop overwrites the value stored in the parameter number. Thus the condition in the if statement is always true.

    Renaming the parameter solves the problem:

    def number_in_list(numbers: list, searched_number: int):\n    for number in numbers:\n        if number == searched_number:\n            return True\n        else:\n            return False

    Now the condition in the if statement looks better. But there is a new problem, since the function still doesn't seem to work correctly. Trying out the following manifests a bug:

    found = number_in_list([1, 2, 3, 4], 3)\nprint(found)  # prints out False

    The issue here is that the function returns too early, without checking all the numbers in the list. In fact, the function takes only the first item in the list, and returns True or False depending on its value. We cannot know whether a number is not present in the list until we have checked all the items in the list. The return False command should be placed outside the for loop:

    def number_in_list(numbers: list, searched_number: int):\n    for number in numbers:\n        if number == searched_number:\n            return True\n\n    return False

    Let's have a look at another faulty function:

    def unique_numbers(numbers: list):\n    # a helper variable to store all the numbers we've already checked\n    numbers = []\n    for number in numbers:\n        # have we seen this number already?\n        if number in numbers:\n            return False\n        numbers.append(number)\n\n    return True\n\nunique = unique_numbers([1, 2, 2])\nprint(unique)  # prints out True

    This function is supposed to check whether all numbers in a list are distinct from each other, but it always returns True.

    Here the function again overwrites the value stored in its parameter by mistake. The function tries to use the variable numbers to store all the numbers already checked, but this overwrites the original argument list. Renaming the helper variable is an easy fix:

    def unique_numbers(numbers: list):\n    # a helper variable to store all the numbers we've already checked\n    numbers_checked = []\n    for number in numbers:\n        # have we seen this number already?\n        if number in numbers_checked:\n            return False\n        numbers_checked.append(number)\n\n    return True\n\nunique = unique_numbers([1, 2, 2])\nprint(unique)  # prints out False

    Problems like this, and many others, can be located and fixed with the help of the debugger or the visualisation tool. Learning to use these efficiently cannot be emphasised enough.

    Please write a function named longest(strings: list), which takes a list of strings as its argument. The function finds and returns the longest string in the list. You may assume there is always a single longest string in the list.

    An example of expected behaviour:

    \nif __name__ == \"__main__\":\n    strings = [\"hi\", \"hiya\", \"hello\", \"howdydoody\", \"hi there\"]\n    print(longest(strings))\n

    howdydoody

    Lists within lists

    The items in a list can be lists themselves:

    my_list = [[5, 2, 3], [4, 1], [2, 2, 5, 1]]\nprint(my_list)\nprint(my_list[1])\nprint(my_list[1][0])

    [[5, 2, 3], [4, 1], [2, 2, 5, 1]]\n[4, 1]\n4

    Why would lists within lists be useful?

    Remember that lists can contain items of different types. You could store information about a person in a list. For instance, you could include their name as the first item, their age as the second item, and their height in meters as the third item:

    [\"Anne\", 12, 1.45]

    A database of persons could then be a list, whose items would be lists containing information about a single person:

    persons = [[\"Betty\", 10, 1.37], [\"Peter\", 7, 1.25], [\"Emily\", 32, 1.64], [\"Alan\", 39, 1.78]]\n\nfor person in persons:\n  name = person[0]\n  age = person[1]\n  height = person[2]\n  print(f\"{name}: age {age} years, height {height} meters\")

    Betty: age 10 years, height 1.37 meters\nPeter: age 7 years, height 1.25 meters\nEmily: age 32 years, height 1.64 meters\nAlan: age 39 years, height 1.78 meters

    The for loop goes through the items in the outer list one by one. That is, each list containing information about a single person is, in turn, assigned to the variable person.

    Lists arent always the best way to present data, such as information about a person. We will soon come across Python dictionaries, which are often better suited to such situations.

    Matrices

    A two-dimensional array, or a matrix, is also a natural application of a list within a list.

    For example, the following matrix

    \n \n \n \n \n \n \"5\n \n \n

    could be presented as a two-dimensional list in Python like so:

    my_matrix = [[1, 2, 3], [3, 2, 1], [4, 5, 6]]

    Since a matrix is a list containing lists, the individual elements within the matrix can be accessed using consecutive square brackets. The first index refers to the row, and the second to the column. Indexing starts from zero, so for example my_matrix[0][1] refers to the second item on the first row.

    my_matrix = [[1, 2, 3], [3, 2, 1], [4, 5, 6]]\n\nprint(my_matrix[0][1])\nmy_matrix[1][0] = 10\nprint(my_matrix)

    2\n[[1, 2, 3], [10, 2, 1], [4, 5, 6]]

    Like any other list, the rows of the matrix can be traversed wth a for loop. The following code prints out each row of the matrix on a separate line:

    my_matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n\nfor row in my_matrix:\n    print(row)

    [1, 2, 3]\n[4, 5, 6]\n[7, 8, 9]

    Likewise, nested loops can be used to access the individual elements. The following code prints out each element in the matrix on a separate line with the help of two for loops:

    my_matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]\n\nfor row in my_matrix:\n    print(\"a new row\")\n    for element in row:\n        print(element)

    a new row\n1\n2\n3\na new row\n4\n5\n6\na new row\n7\n8\n9

    Visualising code containing lists within lists

    Programs containing lists within lists can feel hard to grasp at first. The visualisation tool from Python Tutor is a great help in understanding how they work. The following is a visualisation of the example above:

    \n \n \n \n \n \n \"5\n \n \n

    The image above reveals that a 3 by 3 matrix technically consists of four lists. The first list represents the entire matrix. The three remaining lists are items in the first list, and represent the rows.

    As multidimensional lists can be traversed with nested loops, it would be natural to think of the lists themselves as nested, but the image above shows us this isn't actually so. Instead, the list representing the whole matrix \"points\" to each individual list representing a row in the matrix. This is called a reference, and in the following section the idea will be explored more thoroughly.

    In the image above the execution has progressed to the second row of the matrix, and this list is what the variable row currently refers to. The variable element contains the element the execution is currently at. The value stored in element is the middle item in the list, i.e. 5.

    Accessing items in a matrix

    Accessing a single row within a matrix is simple - just choose the desired row. The following function calculates the sum of the elements on a chosen row:

    def sum_of_row(my_matrix, row_no: int):\n    # choose the desired row from within the matrix\n    row = my_matrix[row_no]\n    row_sum = 0\n    for item in row:\n        row_sum += item\n\n    return row_sum\n\nm = [[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 5], [2, 9, 15, 1]]\n\nmy_sum = sum_of_row(m, 1)\nprint(my_sum) # prints out 33 (which equals 9 + 1 + 12 + 11)

    Working with columns within a matrix is slightly more complicated, as the matrix is stored by rows:

    def sum_of_column(my_matrix, column_no: int):\n    # go through each row and select the item at the chosen position\n    column_sum = 0\n    for row in my_matrix:\n        column_sum += row[column_no]\n\n    return column_sum\n\nm = [[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 5], [2, 9, 15, 1]]\n\nmy_sum = sum_of_column(m, 2)\nprint(my_sum) # prints out 39 (which equals 3 + 12 + 9 + 15)

    The column handled here consists of the elements at index 2 on each row.

    The visualisation tool is definitely recommended for understanding how these functions work.

    Changing the value of a single element within the matrix is simple: choose a row within the matrix, and then a column within the row:

    def change_value(my_matrix, row_no: int, column_no: int, new_value: int):\n    # choose the desired row\n    row = my_matrix[row_no]\n    # select the correct item within the row\n    row[column_no] = new_value\n\nm = [[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 5], [2, 9, 15, 1]]\n\nprint(m)\nchange_value(m, 2, 3, 1000)\nprint(m)

    [[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 5], [2, 9, 15, 1]]\n[[4, 2, 3, 2], [9, 1, 12, 11], [7, 8, 9, 1000], [2, 9, 15, 1]]

    Notice how above we used the indexes of the row and column to access a chosen element. If we want to change the contents of the matrix, we have to access the elements by their indexes. This means that we can't use a simple for item in list loop to traverse the matrix if we want to change the contents of the matrix.

    Instead, we will have to keep track of the indexes of the elements, for example with a while loop, or a for loop using the range function. The following code increases the value of each element in the matrix by one:

    m = [[1,2,3], [4,5,6], [7,8,9]]\n\nfor i in range(len(m)): # using the number of rows in the matrix\n    for j in range(len(m[i])): # using the number of items on each row \n        m[i][j] += 1\n\nprint(m)

    [[2, 3, 4], [5, 6, 7], [8, 9, 10]]

    The outer loop goes through indexes from zero to the length of the matrix, that is, the number of rows in the matrix. The inner loop goes through indexes from zero to the length of each row within the matrix.

    Please write a function named count_matching_elements(my_matrix: list, element: int), which takes a two-dimensional array of integers and a single integer value as its arguments. The function then counts how many elements within the matrix match the argument value.

    An example of how the function should work:

    m = [[1, 2, 1], [0, 3, 4], [1, 0, 0]]\nprint(count_matching_elements(m, 1))

    3

    A two-dimensional array as a data structure in a game

    A matrix can be a very useful data structure in many different games. For example, the grid of a sudoku game in the image below

    \n \n \n \n \n \n \"5\n \n \n

    can be represented in matrix form like so:

    sudoku = [\n  [9, 0, 0, 0, 8, 0, 3, 0, 0],\n  [0, 0, 0, 2, 5, 0, 7, 0, 0],\n  [0, 2, 0, 3, 0, 0, 0, 0, 4],\n  [0, 9, 4, 0, 0, 0, 0, 0, 0],\n  [0, 0, 0, 7, 3, 0, 5, 6, 0],\n  [7, 0, 5, 0, 6, 0, 4, 0, 0],\n  [0, 0, 7, 8, 0, 3, 9, 0, 0],\n  [0, 0, 1, 0, 0, 0, 0, 0, 3],\n  [3, 0, 0, 0, 0, 0, 0, 0, 2]\n]

    Here the value zero represents an empty square, as zero is not an acceptable value in a finished sudoku puzzle.

    Here is a simple function for printing out the above sudoku grid:

    def print_grid(sudoku):\n    for row in sudoku:\n        for square in row:\n            if square > 0:\n                print(f\" {square}\", end=\"\")\n            else:\n                print(\" _\", end=\"\")\n        print()\n\nprint_grid(sudoku)

    The printout should look like this::

    \n 9 _ _ _ 8 _ 3 _ _\n _ _ _ 2 5 _ 7 _ _\n _ 2 _ 3 _ _ _ _ 4\n _ 9 4 _ _ _ _ _ _\n _ _ _ 7 3 _ 5 6 _\n 7 _ 5 _ 6 _ 4 _ _\n _ _ 7 8 _ 3 9 _ _\n _ _ 1 _ _ _ _ _ 3\n 3 _ _ _ _ _ _ _ 2\n

    Any common game with a gameboard layout can be modelled in a similar fashion. Among others, chess, Minesweeper, Battleship or Mastermind are all based on a two-dimensional grid. For sudoku, it is natural to use numbers to represent the game state, but for other games, different methods may be better.

    In a game of Go two players take turns to place black and white stones on a game board. The winner is the player who manages to encircle a bigger area on the board with their own game pieces.

    Please write a function named who_won(game_board: list), which takes a two-dimensional array as its argument. The array consists of integer values, which represent the following situations:

      \n
    • 0: empty square
    • \n
    • 1: player 1 game piece
    • \n
    • 2: player 2 game piece
    • \n

    The scoring rules of Go can be quite complex, but in this exercise it is enough to compare the number of pieces each player has on the game board. Also, the size of the game board is not limited.

    The function should return the value 1 if player 1 won, and the value 2 if player 2 won. If both players have the same number of pieces on the board, the function should return the value 0.

    Please write a function named row_correct(sudoku: list, row_no: int), which takes a two-dimensional array representing a sudoku grid, and an integer referring to a single row, as its arguments. Rows are indexed from 0.

    The function should return True or False, depending on whether the row is filled in correctly, that is, whether it contains each of the numbers 1 to 9 at most once.

    sudoku = [\n  [9, 0, 0, 0, 8, 0, 3, 0, 0],\n  [2, 0, 0, 2, 5, 0, 7, 0, 0],\n  [0, 2, 0, 3, 0, 0, 0, 0, 4],\n  [2, 9, 4, 0, 0, 0, 0, 0, 0],\n  [0, 0, 0, 7, 3, 0, 5, 6, 0],\n  [7, 0, 5, 0, 6, 0, 4, 0, 0],\n  [0, 0, 7, 8, 0, 3, 9, 0, 0],\n  [0, 0, 1, 0, 0, 0, 0, 0, 3],\n  [3, 0, 0, 0, 0, 0, 0, 0, 2]\n]\n\nprint(row_correct(sudoku, 0))\nprint(row_correct(sudoku, 1))

    True\nFalse

    Please write a function named column_correct(sudoku: list, column_no: int), which takes a two-dimensional array representing a sudoku grid, and an integer referring to a single column, as its arguments. Columns are indexed from 0.

    The function should return True or False, depending on whether the column is filled in correctly, that is, whether it contains each of the numbers 1 to 9 at most once.

    sudoku = [\n  [9, 0, 0, 0, 8, 0, 3, 0, 0],\n  [2, 0, 0, 2, 5, 0, 7, 0, 0],\n  [0, 2, 0, 3, 0, 0, 0, 0, 4],\n  [2, 9, 4, 0, 0, 0, 0, 0, 0],\n  [0, 0, 0, 7, 3, 0, 5, 6, 0],\n  [7, 0, 5, 0, 6, 0, 4, 0, 0],\n  [0, 0, 7, 8, 0, 3, 9, 0, 0],\n  [0, 0, 1, 0, 0, 0, 0, 0, 3],\n  [3, 0, 0, 0, 0, 0, 0, 0, 2]\n]\n\nprint(column_correct(sudoku, 0))\nprint(column_correct(sudoku, 1))

    False\nTrue

    Please write a function named block_correct(sudoku: list, row_no: int, column_no: int), which takes a two-dimensional array representing a sudoku grid, and two integers referring to the row and column indexes of a single square, as its arguments. Rows and columns are indexed from 0.

    The function should return True or False depending on whether the 3 by 3 block to the right and down from the given indexes is filled in correctly. That is, whether the block contains each of the numbers 1 to 9 at most once.

    Notice that this function does not strictly follow the rules of sudoku. In a real game of sudoku there are only 9 blocks to check, and these are located at indexes (0, 0), (0, 3), (0, 6), (3, 0), (3, 3), (3, 6), (6, 0), (6, 3) and (6, 6). Such restrictions on indexes should not be implemented here.

    sudoku = [\n  [9, 0, 0, 0, 8, 0, 3, 0, 0],\n  [2, 0, 0, 2, 5, 0, 7, 0, 0],\n  [0, 2, 0, 3, 0, 0, 0, 0, 4],\n  [2, 9, 4, 0, 0, 0, 0, 0, 0],\n  [0, 0, 0, 7, 3, 0, 5, 6, 0],\n  [7, 0, 5, 0, 6, 0, 4, 0, 0],\n  [0, 0, 7, 8, 0, 3, 9, 0, 0],\n  [0, 0, 1, 0, 0, 0, 0, 0, 3],\n  [3, 0, 0, 0, 0, 0, 0, 0, 2]\n]\n\nprint(block_correct(sudoku, 0, 0))\nprint(block_correct(sudoku, 1, 2))

    False\nTrue

    The first function call should check the 3 by 3 block beginning with the square at indexes 0, 0:

    \n9 0 0\n2 0 0\n0 2 0\n

    The second function call should check the 3 by 3 block beginning with the square at row 1, column 2:

    \n0 2 5\n0 3 0\n4 0 0\n

    This second block would not be checked in an actual game of sudoku, but your function should allow for it to be checked.

    Please write a function named sudoku_grid_correct(sudoku: list), which takes a two-dimensional array representing a sudoku grid as its argument. The function should use the functions from the three previous exercises to determine whether the complete sudoku grid is filled in correctly. Copy the functions from the exercises above into your Python code file for this exercise.

    The function should check each of the nine rows, columns and 3 by 3 blocks in the grid. If all contain each of the numbers 1 to 9 at most once, the function returns True. If a single one is filled in incorrectly, the function returns False.

    The image of a sudoku grid above these exercises has the nine blocks within the grid indicated with thicker borders. These are the blocks the function should check, and they begin at the indexes (0, 0), (0, 3), (0, 6), (3, 0), (3, 3), (3, 6), (6, 0), (6, 3) and (6, 6).

    sudoku1 = [\n  [9, 0, 0, 0, 8, 0, 3, 0, 0],\n  [2, 0, 0, 2, 5, 0, 7, 0, 0],\n  [0, 2, 0, 3, 0, 0, 0, 0, 4],\n  [2, 9, 4, 0, 0, 0, 0, 0, 0],\n  [0, 0, 0, 7, 3, 0, 5, 6, 0],\n  [7, 0, 5, 0, 6, 0, 4, 0, 0],\n  [0, 0, 7, 8, 0, 3, 9, 0, 0],\n  [0, 0, 1, 0, 0, 0, 0, 0, 3],\n  [3, 0, 0, 0, 0, 0, 0, 0, 2]\n]\n\nprint(sudoku_grid_correct(sudoku1))\n\nsudoku2 = [\n  [2, 6, 7, 8, 3, 9, 5, 0, 4],\n  [9, 0, 3, 5, 1, 0, 6, 0, 0],\n  [0, 5, 1, 6, 0, 0, 8, 3, 9],\n  [5, 1, 9, 0, 4, 6, 3, 2, 8],\n  [8, 0, 2, 1, 0, 5, 7, 0, 6],\n  [6, 7, 4, 3, 2, 0, 0, 0, 5],\n  [0, 0, 0, 4, 5, 7, 2, 6, 3],\n  [3, 2, 0, 0, 8, 0, 0, 5, 7],\n  [7, 4, 5, 0, 0, 3, 9, 0, 1]\n]\n\nprint(sudoku_grid_correct(sudoku2))

    False\nTrue

    ","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-5/1-more-lists.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-5/2-references/page-data.json b/page-data/part-5/2-references/page-data.json index 345ce8395..de3b295da 100644 --- a/page-data/part-5/2-references/page-data.json +++ b/page-data/part-5/2-references/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-5/2-references", - "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 know what is meant by a reference to a variable"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will understand that there can be multiple references to the same object"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use lists as parameters in functions"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what is meant by a side effect of a function"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Thus far we have thought of a variable as a sort of a \"box\" which contains the value of the variable. Technically this is not true in Python. What is stored in a variable is not the value per se, but a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reference"}]},{"type":"text","value":" to the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"object"}]},{"type":"text","value":" which is the actual value of the variable. The object can be e.g. a number, a string or a list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In practice, this means that the value of the variable "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"is not stored"}]},{"type":"text","value":" in the variable itself. Instead, there is information about the location in computer memory where the value can be found."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A reference is often represented by an arrow from the variable to the actual value in memory:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 261px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/8687855d3e59d8151e4fce49ec9669b0/edf96/5_2_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: 30.869565217391305%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA1ElEQVQY041ROQ6EQAyb7/MBfsA/aCgoEHQcDQ2XOMQlxI1XzgqKbdhIljzJ2JNk1DiOKIoCdV1jnmfEcYymaZBlmSBJEjDO88R1Xa9Qx3FgWRYRMUHTaZoE5Kwx/2+om7yJ+AA7b9tWprl5VVXCu66TiaRDgoY3v8FI0xR93z8idlyWpRhxAtaZZ5imCUXXKIpkj77vCyeCIECe59A0Dbquyw7XdX0m4e750L7vgm3b4Lrut0MeeJGiXwzDIAb8LM/zEIYhbNuG4zjCLcuSPOuGYeADgRnJTrwaxUwAAAAASUVORK5CYII='); 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":["/static/8687855d3e59d8151e4fce49ec9669b0/a0b58/5_2_1.webp 230w","/static/8687855d3e59d8151e4fce49ec9669b0/ad293/5_2_1.webp 261w"],"sizes":"(max-width: 261px) 100vw, 261px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8687855d3e59d8151e4fce49ec9669b0/81c8e/5_2_1.png 230w","/static/8687855d3e59d8151e4fce49ec9669b0/edf96/5_2_1.png 261w"],"sizes":"(max-width: 261px) 100vw, 261px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/8687855d3e59d8151e4fce49ec9669b0/edf96/5_2_1.png","alt":"5 2 1","title":"5 2 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":"p","properties":{},"children":[{"type":"text","value":"So, a reference tells us where the value can be found. The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"id"}]},{"type":"text","value":" can be used to find out the exact location the variable points to:"}]},{"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":"a "},{"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":"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":" "},{"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":"\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","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"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":"\nb "},{"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 a reference, too\""}]},{"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","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b"},{"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":"4538357072\n4537788912"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The reference, or the ID of the variable, is an integer, which can be thought of as the address in computer memory where the value of the variable is stored. If you execute the above code on your own computer, the result will likely be different, as your variables will point to different locations - the references will be different."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The Python Tutor visualisation tool also shows references as arrows from the variable to the actual content, as we saw in the "},{"type":"element","tagName":"a","properties":{"href":"/part-5/1-more-lists#visualising-code-containing-lists-within-lists"},"children":[{"type":"text","value":"previous section"}]},{"type":"text","value":". The tool \"cheats\" a bit when it comes to strings, however. It displays strings as if they are stored in the variables themselves:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/42bf1345e79a21fd456ed6f4bbd595ad/f4281/5_2_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: 28.695652173913043%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAAAsTAAALEwEAmpwYAAABAUlEQVQY002R626DMAxGef+32++1u3RagUAgoZTcc6akg9aSZcnx5fhLA5BzLoEYEqNQTONCCJHXt5QichwRfY9WGmssZtuIMZJSql6sKQ2lxTmHnjVDP3FbNrwPtehYFiPWWqwxeOePQSVWTwUg01QK71m1RrQdSkq8s9zv99q8b94H75ZzqjUlms1i7eOiJqVMCpHgPdY45HBDK3MQ7IOKBJffmctV0Y0GMTuuw4pxiWWVrNuJlDWN1ho1zzjv/0/LpPikeRDmKsHpa+T8LTlfJO+fAx8/E85nXFDMyxsuCJoQQ6WRUtK1HYMQ9F1H17ZM03TotNPWD4jPHLxIkeEP+LrTj7xGy7wAAAAASUVORK5CYII='); 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":["/static/42bf1345e79a21fd456ed6f4bbd595ad/a0b58/5_2_2.webp 230w","/static/42bf1345e79a21fd456ed6f4bbd595ad/bc10c/5_2_2.webp 460w","/static/42bf1345e79a21fd456ed6f4bbd595ad/966d8/5_2_2.webp 920w","/static/42bf1345e79a21fd456ed6f4bbd595ad/adbc1/5_2_2.webp 1016w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/42bf1345e79a21fd456ed6f4bbd595ad/81c8e/5_2_2.png 230w","/static/42bf1345e79a21fd456ed6f4bbd595ad/08a84/5_2_2.png 460w","/static/42bf1345e79a21fd456ed6f4bbd595ad/c0255/5_2_2.png 920w","/static/42bf1345e79a21fd456ed6f4bbd595ad/f4281/5_2_2.png 1016w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/42bf1345e79a21fd456ed6f4bbd595ad/c0255/5_2_2.png","alt":"5 2 2","title":"5 2 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":"In reality, Python strings are handled very much like lists, with references to locations in memory."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Many of the builtin types in Python, such as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":", are "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"immutable"}]},{"type":"text","value":". This means the value of the object, or any part of it, cannot change. The value can be replaced with a new 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":"/static/374387f1b515a88857cfbac235dfb942/b1a44/5_2_3.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":["/static/374387f1b515a88857cfbac235dfb942/a0b58/5_2_3.webp 230w","/static/374387f1b515a88857cfbac235dfb942/f6dfe/5_2_3.webp 296w"],"sizes":"(max-width: 296px) 100vw, 296px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/374387f1b515a88857cfbac235dfb942/81c8e/5_2_3.png 230w","/static/374387f1b515a88857cfbac235dfb942/b1a44/5_2_3.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":"/static/374387f1b515a88857cfbac235dfb942/b1a44/5_2_3.png","alt":"5 2 3","title":"5 2 3","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":"Some of Python types are "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"mutable"}]},{"type":"text","value":". For example, the contents of a list can change without needing to create a whole new list:"}]},{"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":"/static/6a61f09e6f9cd09d51315c7fde142ab9/6b1e2/5_2_4.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":["/static/6a61f09e6f9cd09d51315c7fde142ab9/a0b58/5_2_4.webp 230w","/static/6a61f09e6f9cd09d51315c7fde142ab9/f44a8/5_2_4.webp 281w"],"sizes":"(max-width: 281px) 100vw, 281px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6a61f09e6f9cd09d51315c7fde142ab9/81c8e/5_2_4.png 230w","/static/6a61f09e6f9cd09d51315c7fde142ab9/6b1e2/5_2_4.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":"/static/6a61f09e6f9cd09d51315c7fde142ab9/6b1e2/5_2_4.png","alt":"5 2 4","title":"5 2 4","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":"It may surprise you that also the basic data types "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]},{"type":"text","value":" are immutable in Python. Let's have a look at the following bit of code:"}]},{"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":"\nnumber "},{"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":"2"}]},{"type":"text","value":"\nnumber "},{"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":"p","properties":{},"children":[{"type":"text","value":"It seems that the commands above are just changing the value stored in the variable, but in fact each command creates a whole new number in the computer's memory."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The printout from the following program illuminates the situation:"}]},{"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":"\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","builtin"]},"children":[{"type":"text","value":"id"}]},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnumber "},{"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":"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","builtin"]},"children":[{"type":"text","value":"id"}]},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na "},{"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":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"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":"4535856912\n4535856944\n4535856912"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"At first, the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" points to the memory location 4535856912. When "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" is assigned a new value, it points to the location 4535856944. Now, when the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" is assigned the value 1, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" points to the very same location where "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" was pointing, when it was also assigned the value 1."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It seems Python has stored the value 1 in the memory location 4535856912. Whenever a variable is assigned the value 1, it "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"refers"}]},{"type":"text","value":" to that location in computer memory."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is good to keep in mind that "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"almost everything is a reference"}]},{"type":"text","value":" in Python, but all this is rarely relevant to everyday programming tasks. So let's get back to more practical matters."}]},{"type":"element","tagName":"h2","properties":{"id":"multiple-references-to-the-same-list","style":"position:relative;"},"children":[{"type":"text","value":"Multiple references to the same list"},{"type":"element","tagName":"a","properties":{"href":"#multiple-references-to-the-same-list","ariaLabel":"multiple references to the same list 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":"What actually happens when you assign a list variable to a new variable - is the list copied over?"}]},{"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":"a "},{"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":"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":" "},{"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":"\nb "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" a\nb"},{"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":"p","properties":{},"children":[{"type":"text","value":"The assignment "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b = a"}]},{"type":"text","value":" copies the value stored in variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" to the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":". However, the value stored in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" is not the list "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"itself"}]},{"type":"text","value":", but a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reference"}]},{"type":"text","value":" to the list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, the assignment "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b = a"}]},{"type":"text","value":" copies the reference. As a result there are now two references to the same memory location containing the list."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 271px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/73c7fae5118d6e5b03c576f1a9d14c3f/5fce1/5_2_5.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: 129.99999999999997%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAaCAYAAAC3g3x9AAAACXBIWXMAAAsTAAALEwEAmpwYAAACN0lEQVRIx52VV64qQQxEZ60sgC2wJvhhFXwhkMg555zx07FU3FHf0QVeS1aHoctlu9xE4/HYlsulbbdbm06nNpvNjDPm4XBoj8fDGM/n8yOLdrudX8KOx6MDbzYbOxwOvtc3fvzJiOyLsVqtbL1ee0SLxeLXmu8OeLvd7H6/OxNmGfvT6WSdTscul4ungv18PncQ1gCSHiJi74C1Ws0Gg4H1+32rVCq+xxqNhhWLRUun09Zutz0FXCRNXAZIZ5PJ5CdkMQwTzLher26wxSGXR6ORm4AoHk4IO4on+6/E801s/rIo6WI4f1phD5kw9vu9S4WchCAh8FsdojtydD6fHTQJ8CuGSSEnFYcZ6cggABGtdR594p0KogKkQyV7vZ4ba+SGTiVwZ1iv1730fCiXy67BZrNp1WrV8vm8pVIpB0N7qjSs6AydAfYKmQPoarBvtVoODjBag2G32/U8wwqDFXqksAC+Wk/NT4FgS7eo4qEOYSTNwVJrMXXAUqnkLPCEl/jQa/NVlWm9pEthF32kQ/UqplcGB9i7dkxkSEWpIFUmF0o6r/d/dUr8HdQIXx7tk9IQrr96sXlgiYACskbYrJEOxeThdUD9hyhkGbJAX9ls9vV4sGeme3hUkBpgzETigIgXj3hSPhF2oVCwXC5nmUzGv3OR/MIKzSFsgcLOcxhWkT2XKRYOVG0GOoUZ5zgFiDVOiOAVMh4Jl9ai3QCM61JF4Sz+hyb9xs9fIZMX6IddEj5j73T4DynC3t7bOJziAAAAAElFTkSuQmCC'); 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":["/static/73c7fae5118d6e5b03c576f1a9d14c3f/a0b58/5_2_5.webp 230w","/static/73c7fae5118d6e5b03c576f1a9d14c3f/cadab/5_2_5.webp 271w"],"sizes":"(max-width: 271px) 100vw, 271px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/73c7fae5118d6e5b03c576f1a9d14c3f/81c8e/5_2_5.png 230w","/static/73c7fae5118d6e5b03c576f1a9d14c3f/5fce1/5_2_5.png 271w"],"sizes":"(max-width: 271px) 100vw, 271px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/73c7fae5118d6e5b03c576f1a9d14c3f/5fce1/5_2_5.png","alt":"5 2 5","title":"5 2 5","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 list can be accessed through either of the two references:"}]},{"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":"list1 "},{"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":"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":" "},{"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":" "},{"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":"text","value":"\nlist2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" list1\n\nlist1"},{"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":"text","value":"\nlist2"},{"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":"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":"20"}]},{"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":"list1"},{"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":"list2"},{"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":"[10, 20, 3, 4]\n[10, 20, 3, 4]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If there is more than one reference to the same list, any one of the references can be used to access the list. On the other hand, a change made through any one of the references affects also the other references, as their target is the same."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The visualisation tool is again very useful in figuring out what is happening:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/c80c08ee8666a616ae1b04ea29ec2de4/bf433/5_2_6.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: 32.608695652173914%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAAsTAAALEwEAmpwYAAABCklEQVQoz2WRXU7EMAyEe/9jwRHgAfEEEnRh27TNNn9NE38o2e3SgqVR5Nge2+NGRCgAIaWM6jSqvxBjothvHJy1qF6hpwlrDM65ipQSOeeKZl9greP7pOi+R8zsiMtyJ61xYxjUgDWWGGMlKGQprazrFQfC4D3WzDhrarAUbLblFItLqK/3frcJpJyOhEsIqK7nojXr+n/ly+z5aAc+254sMM8TS9RksYjEmtPIboKikR5HZq3rdPKHUE2ep5cTz68tbhEG/cZ5fKCfHvHL+42wJAtkybiiYas4fw3EuCII+6PFNWNcxPq4jUHO14NsTRulBvquI4Rw0yezhHIxOWh3bSwHTXfu/e8HySgkP7muRjUAAAAASUVORK5CYII='); 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":["/static/c80c08ee8666a616ae1b04ea29ec2de4/a0b58/5_2_6.webp 230w","/static/c80c08ee8666a616ae1b04ea29ec2de4/bc10c/5_2_6.webp 460w","/static/c80c08ee8666a616ae1b04ea29ec2de4/966d8/5_2_6.webp 920w","/static/c80c08ee8666a616ae1b04ea29ec2de4/3697b/5_2_6.webp 971w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/c80c08ee8666a616ae1b04ea29ec2de4/81c8e/5_2_6.png 230w","/static/c80c08ee8666a616ae1b04ea29ec2de4/08a84/5_2_6.png 460w","/static/c80c08ee8666a616ae1b04ea29ec2de4/c0255/5_2_6.png 920w","/static/c80c08ee8666a616ae1b04ea29ec2de4/bf433/5_2_6.png 971w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/c80c08ee8666a616ae1b04ea29ec2de4/c0255/5_2_6.png","alt":"5 2 6","title":"5 2 6","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":"h2","properties":{"id":"copying-a-list","style":"position:relative;"},"children":[{"type":"text","value":"Copying a list"},{"type":"element","tagName":"a","properties":{"href":"#copying-a-list","ariaLabel":"copying a list 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":"If you want to create an actual separate copy of a list, you can create a new list and add each item from the original list in turn:"}]},{"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":"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":" "},{"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":" "},{"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":" "},{"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\nnew_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","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":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n new_list"},{"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":"item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nnew_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":"text","value":"\nnew_list"},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"the original:\""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"the copy:\""}]},{"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":"my_list [1, 2, 3, 3, 5]\nnew_list [10, 2, 3, 3, 5, 6]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A snapshot of the copying process in the visualisation tool:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/c670b9845a531e92d89ab8b711b7c304/38124/5_2_7.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: 41.30434782608695%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAAAsTAAALEwEAmpwYAAABO0lEQVQoz2WR3U7DMAyF+/5vxgUXIMQFGgikbR2sS9ImzY+d5CBnXVdprqyjtM5X+7irtUJCVB7mjMswYjgbZC7t3bYmhAA/z/Der0lEKKUg54yuXishSinBjhZaWRjlYCcHZl6BcknOog2wam4q4K4uMDAjeo9RG8zOgVJErQXbuIFTiiBKyMyYZ6lrPSHncgVK5BiRieCsg1Eaarg0OBNvYBWHXuNj10MZD86M0X4j8QGRejFs6XCBxhAaSF9Ug8oY8k30FrufAU/Pn+gHB2KGse8w7hXGvqCUgG47krMWf/0J/f6I4/7YOtRKgRItXRZMM+H3PMEHQmKGtm8I9LUyOmZCiqltT5YiPmjlYXRASrK9/ODh9jyHA3w8LX5XdEFpTNOEcRwRY1xGzCi13LeZc8ubBffEww/+AbQPcTCbhdzzAAAAAElFTkSuQmCC'); 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":["/static/c670b9845a531e92d89ab8b711b7c304/a0b58/5_2_7.webp 230w","/static/c670b9845a531e92d89ab8b711b7c304/bc10c/5_2_7.webp 460w","/static/c670b9845a531e92d89ab8b711b7c304/966d8/5_2_7.webp 920w","/static/c670b9845a531e92d89ab8b711b7c304/5bf28/5_2_7.webp 953w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/c670b9845a531e92d89ab8b711b7c304/81c8e/5_2_7.png 230w","/static/c670b9845a531e92d89ab8b711b7c304/08a84/5_2_7.png 460w","/static/c670b9845a531e92d89ab8b711b7c304/c0255/5_2_7.png 920w","/static/c670b9845a531e92d89ab8b711b7c304/38124/5_2_7.png 953w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/c670b9845a531e92d89ab8b711b7c304/c0255/5_2_7.png","alt":"5 2 7","title":"5 2 7","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 variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"new_list"}]},{"type":"text","value":" points to a different list than the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"my_list"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An easier way to copy a list is the bracket operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]},{"type":"text","value":", which we used for slices previously. The notation "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[:]"}]},{"type":"text","value":" selects all items in the collection. As a side effect, it creates a copy of the 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":"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":"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":"text","value":"\nnew_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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","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","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":"text","value":"\nnew_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":"1"}]},{"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":"20"}]},{"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":"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":"[10, 2, 3, 4]\n[1, 20, 3, 4]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"using-lists-as-parameters-in-functions","style":"position:relative;"},"children":[{"type":"text","value":"Using lists as parameters in functions"},{"type":"element","tagName":"a","properties":{"href":"#using-lists-as-parameters-in-functions","ariaLabel":"using lists as parameters in 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":"When you pass a list as an argument to a function, you are passing a reference to that list. This means that the function can modify the list directly."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following function takes a list as an argument and adds a new item to the end of the 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":"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":"add_item"}]},{"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":" "},{"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 new_item "},{"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":"text","value":"\n my_list"},{"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":"new_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\na_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":"\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":"a_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadd_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a_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":"a_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\n[1, 2, 3]\n[1, 2, 3, 10]\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_item"}]},{"type":"text","value":" does not have a return value. It only changes the list it takes as an argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The visualisation tool may help you understand what is happening here:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/51be9444713cc1f7896dd2a365ade5cd/d7abb/5_2_8.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: 36.52173913043478%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAAsTAAALEwEAmpwYAAABCklEQVQoz11Ra2/DIAzk//+5fdmHTVq1bmuUtQ0hEB7mlZtMlCztSRZgzJ3PiGVZwNjWGDPu1wlKzqh1afntrpQCozXsbOG93yPnjFprC9GK+VEjixhuA7rvHr/dDc46ENEDYQgBOaW2fw4mFltxjQmFqClOasQ4DK0bbx2eXTTSnJFSgnMOG/heHC1RCFCjgtEGFGgv2kVrxSgl5H3YO9KTQwwrOUPgAGctLl8/6Lse3vlGxCQbKUcTlbLlffDQ8ycCXVHLWid4HjwntsGqKWaM0mE29NAdI+eK0/mG80XB+IKYPJR5gaPTv2U/aWhjoLVuba8k648dB87nlAvePnq8vncwlo7mduE/oO4jC89VVXwAAAAASUVORK5CYII='); 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":["/static/51be9444713cc1f7896dd2a365ade5cd/a0b58/5_2_8.webp 230w","/static/51be9444713cc1f7896dd2a365ade5cd/bc10c/5_2_8.webp 460w","/static/51be9444713cc1f7896dd2a365ade5cd/966d8/5_2_8.webp 920w","/static/51be9444713cc1f7896dd2a365ade5cd/1bae5/5_2_8.webp 959w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/51be9444713cc1f7896dd2a365ade5cd/81c8e/5_2_8.png 230w","/static/51be9444713cc1f7896dd2a365ade5cd/08a84/5_2_8.png 460w","/static/51be9444713cc1f7896dd2a365ade5cd/c0255/5_2_8.png 920w","/static/51be9444713cc1f7896dd2a365ade5cd/d7abb/5_2_8.png 959w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/51be9444713cc1f7896dd2a365ade5cd/c0255/5_2_8.png","alt":"5 2 8","title":"5 2 8","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":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Global frame"}]},{"type":"text","value":" refers to the variables defined in the main function, whereas the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"add_item"}]},{"type":"text","value":" frame with a blue background represents the parameters and variables within that function. As you can see from the visualisation, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_item"}]},{"type":"text","value":" function refers to the very same list as the main function. The changes made within the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_item"}]},{"type":"text","value":" function also affect the main function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Another way to implement this functionality would be to create a new list within the function, and return that:"}]},{"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":"add_item"}]},{"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":" "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n new_item "},{"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":"text","value":"\n my_list_copy "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n my_list_copy"},{"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":"new_item"},{"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":" my_list_copy\n\nnumbers "},{"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":"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":" "},{"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":"\nnumbers2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" add_item"},{"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\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":"\"original list:\""}]},{"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":"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":"\"new list:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" numbers2"},{"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":"original list: [1, 2, 3]\nnew list: [1, 2, 3, 10]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you aren't absolutely sure you understand what is happening in the code above, please go through it in the visualisation tool."}]},{"type":"element","tagName":"h2","properties":{"id":"editing-a-list-given-as-an-argument","style":"position:relative;"},"children":[{"type":"text","value":"Editing a list given as an argument"},{"type":"element","tagName":"a","properties":{"href":"#editing-a-list-given-as-an-argument","ariaLabel":"editing a list given as an argument 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 following is an attempt at a function which should augment each item in a list by ten:"}]},{"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":"augment_all"}]},{"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":" "},{"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 new_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","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":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n new_list"},{"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":"item "},{"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":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" new_list\n\nnumbers "},{"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":"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":" "},{"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":"\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":"\"in the beginning:\""}]},{"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":"\naugment_all"},{"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":"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":"\"after the function is executed:\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"in the beginning: [1, 2, 3]\nafter the function is executed: [1, 2, 3]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For some reason the function doesn't work, so what is going on?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function takes a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reference"}]},{"type":"text","value":" to a list as an argument. This is stored in the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"my_list"}]},{"type":"text","value":". The assignment "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"my_list = new_list"}]},{"type":"text","value":" assigns a new value to that same variable. The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"my_list"}]},{"type":"text","value":" now points to the new list created inside the function, and the reference to the original list is no longer available within the function. This assignment has no effect outside the function, however."}]},{"type":"comment","value":"-\nThe following sequence of images illustrates the states of the variables at different stages during the execution of the function:\n\n\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Furthermore, the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"new_list"}]},{"type":"text","value":", which contains the new, augmented values, is not accessible from outside the function. It is \"lost\" as the execution of the function finishes, and focus returns to the main function. The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"numbers"}]},{"type":"text","value":" in the main function always points to the original list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The visualisation tool is your friend here, too. Please go through the stages carefully, and see how the original list is not affected by the execution of the function at all:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/65ad0c67b682d1c5d27a8cbe274421c3/914c7/5_2_10.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: 46.52173913043478%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAABeUlEQVQoz02S2W7jMAxF/f//1qIYYPqQAHmYJk5iedFia7Ekn4HUOAkBgoB4Sd5Lqtm2jZwzOWWKzcZx+RGIuyLFxJ4vFkJgGifGYURJidEG733Np/SLbQowxsjUD9zaK2M/oLXBWUtc47NhiaWoDn/zPb+ua43NVkAxsoaAuHf0Xcf10rLMM865WrDbrkQrRUoZay2zMTVXcJXhFiMxhMoo+IB3vsoofXbQ3rRIHoRATbK+FayaZpz9rX9KLg3afz91P/f2WhkUxkZp8mM3xWNK9N1AL4bHAI22fwjx/mTZ7JPL7qZxpLsJ2nOHHDVaaexia/TOMS+e7+OV01kyu4QPBr0cCat4NbRao7WuF9t4SPOZGF+72yUr4/j4OvH3eEMvCTX3jOoDtXySs6+YJnrPugZC8CipUEqhtaqypZR1HeUX5BRZfOR8aDkcLoQ1lVHEZEnpdbwmhISz5coJuyzIaaI9X+hFjzGmfocHT3LeyBvV36//ruQ/rtK74EJfuxIAAAAASUVORK5CYII='); 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":["/static/65ad0c67b682d1c5d27a8cbe274421c3/a0b58/5_2_10.webp 230w","/static/65ad0c67b682d1c5d27a8cbe274421c3/bc10c/5_2_10.webp 460w","/static/65ad0c67b682d1c5d27a8cbe274421c3/966d8/5_2_10.webp 920w","/static/65ad0c67b682d1c5d27a8cbe274421c3/0e613/5_2_10.webp 978w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/65ad0c67b682d1c5d27a8cbe274421c3/81c8e/5_2_10.png 230w","/static/65ad0c67b682d1c5d27a8cbe274421c3/08a84/5_2_10.png 460w","/static/65ad0c67b682d1c5d27a8cbe274421c3/c0255/5_2_10.png 920w","/static/65ad0c67b682d1c5d27a8cbe274421c3/914c7/5_2_10.png 978w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/65ad0c67b682d1c5d27a8cbe274421c3/c0255/5_2_10.png","alt":"5 2 10","title":"5 2 10","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":"One way to fix this is to copy all the items from the new list to the old list, one by one:"}]},{"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":"augment_all"}]},{"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":" "},{"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 new_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","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":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n new_list"},{"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":"item "},{"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":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# copy items from the new list into the old list"}]},{"type":"text","value":"\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":"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":"my_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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n my_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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" new_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":"p","properties":{},"children":[{"type":"text","value":"Python also has a nifty shorthand for assigning multiple items in a collection at once:"}]},{"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","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"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":"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":" "},{"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":" "},{"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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"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":"3"}]},{"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","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" my_list\n"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"20"}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the example above a slice of the list is assigned values from another collection."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As we know, a slice can include the entire collection:"}]},{"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","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"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":"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":" "},{"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":" "},{"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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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","punctuation"]},"children":[{"type":"text","value":":"}]},{"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","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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":"99"}]},{"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":"98"}]},{"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":"97"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" my_list\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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":"99"}]},{"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":"98"}]},{"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":"97"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The entire contents of the old list are replaced. Inspired by this, a working version of the augmenting function 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":"augment_all"}]},{"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":" "},{"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 new_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","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":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n new_list"},{"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":"item "},{"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":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n my_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":"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":" new_list"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Actually, there is no need to create a new list within the function at all. We can just assign the new values directly into the original 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":"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":"augment_all"}]},{"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":" "},{"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":"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":"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":"my_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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n my_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":"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":"text","value":"\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Items multiplied by two","tmcname":"part05-08_items_multiplied_by_two"},"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":"double_items(numbers: list)"}]},{"type":"text","value":", which takes a list of integers as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return a new list, which contains all values from the original list doubled. The function should "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" change the original list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function at 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":"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 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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"4"}]},{"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":"5"}]},{"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":"3"}]},{"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":"11"}]},{"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":"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":"text","value":"\n numbers_doubled "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" double_items"},{"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":"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":"\"original:\""}]},{"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":"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":"\"doubled:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" numbers_doubled"},{"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":"original: [2, 4, 5, 3, 11, -4]\ndoubled: [4, 8, 10, 6, 22, -8]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Remove the smallest","tmcname":"part05-09_remove_smallest"},"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":"remove_smallest(numbers: list)"}]},{"type":"text","value":", which takes a list of integers as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The functions should find and remove the smallest item in the list. You may assume there is a single smallest item in the list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should not have a return value - it should directly modify the list it receives as a parameter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how the function works:"}]},{"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":"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 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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"4"}]},{"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":"6"}]},{"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":"1"}]},{"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":"3"}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n remove_smallest"},{"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":"print"}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[2, 4, 6, 3, 5]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: print out the grid and add a number","tmcname":"part05-10_sudoku_print_and_add"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise we will complete two more functions for the sudoku project from the previous section: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print_sudoku"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_number"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print_sudoku(sudoku: list)"}]},{"type":"text","value":" takes a two-dimensional array representing a sudoku grid as its argument. The function should print out the grid in the format specified in the examples below."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_number(sudoku: list, row_no: int, column_no: int, number:int)"}]},{"type":"text","value":" takes a two-dimensional array representing a sudoku grid, two integers referring to the row and column indexes of a single square, and a single digit between 1 and 9, as its arguments. The function should add the digit to the specified location in the grid."}]},{"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":"sudoku "},{"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":"text","value":"\n "},{"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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nprint_sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadd_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"0"}]},{"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":"0"}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadd_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"1"}]},{"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":"2"}]},{"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":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadd_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"5"}]},{"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":"7"}]},{"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":"3"}]},{"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"},{"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":"\"Three numbers added:\""}]},{"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_sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\nThree numbers added:\n\n2 _ _ _ _ _ _ _ _\n_ _ 7 _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ 3 _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n"}]}]},{"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":"Remember it is possible to call the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" function without causing a line change:"}]},{"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":"\"characters \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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"},{"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":"\"without carriage returns\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"characters without carriage returns"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sometimes you need just a new line, which a print statement without any argument will achieve:"}]},{"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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: add number to a copy of the grid","tmcname":"part05-11_sudoku_add_to_copy"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is the very last sudoku task. This time we will create a slightly different version of the function for adding new numbers to the grid."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"copy_and_add(sudoku: list, row_no: int, column_no: int, number: int)"}]},{"type":"text","value":" takes a two-dimensional array representing a sudoku grid, two integers referring to the row and column indexes of a single square, and a single digit between 1 and 9, as its arguments. The function should "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a copy of the original grid with the new digit added in the correct location. The function should "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" change the original grid received as a parameter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print_sudoku"}]},{"type":"text","value":" function from the previous exercise could be useful for testing, and it is used in the example below:"}]},{"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":"sudoku "},{"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":"text","value":"\n "},{"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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\ngrid_copy "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" copy_and_add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"0"}]},{"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":"0"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Original:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprint_sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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"},{"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":"\"Copy:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprint_sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"grid_copy"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Original:\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\nCopy:\n2 _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint"}]},{"type":"text","value":"\nWhen dealing with nested lists you should be extra careful when copying. What all needs to be explicitly copied, and where do changes actually have an effect? The "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":" is a great help here, too, although the size of the sudoku grid will make the view less orderly than usual."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Tic-Tac-Toe","tmcname":"part05-12_tic_tac_toe"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tic-Tac-Toe is played on a 3 by 3 grid, by two players who take turns inputting noughts and crosses. If either player succeeds in placing three of their own symbols on any row, column or diagonal, they win. If neither player manages this, it is a draw."}]},{"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":"play_turn(game_board: list, x: int, y: int, piece: str)"}]},{"type":"text","value":", which places the given symbol at the given coordinates on the board. The values of the coordinates on the board are between 0 and 2."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" when compared to the sudoku exercises, the arguments the function takes are the other way around here. The column "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" comes first, and the row "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" second."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The board consists of the following strings:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":": empty square"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"X\""}]},{"type":"text","value":": player 1 symbol"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"O\""}]},{"type":"text","value":": player 2 symbol"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" if the square was empty and the symbol was successfully placed on the game board. The function should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" if the square was occupied, or if the coordinates weren't valid."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example execution of the 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":"text","value":"game_board "},{"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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"},{"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":"play_turn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"game_board"},{"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":"2"}]},{"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":"0"}]},{"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":"\"X\""}]},{"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":"game_board"},{"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\n[['', '', 'X'], ['', '', ''], ['', '', '']]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Transpose a matrix","tmcname":"part05-13_transpose_matrix"},"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":"transpose(matrix: list)"}]},{"type":"text","value":", which takes a two-dimensional integer array, i.e., a matrix, as its argument. The function should "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"transpose"}]},{"type":"text","value":" the matrix. Transposing means essentially flipping the matrix over its diagonal: columns become rows, and rows become columns."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume the matrix is a square matrix, so it will have an equal number of rows and columns."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following matrix"}]},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"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","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"transposed looks 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","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should not have a return value. The matrix should be modified directly through the reference."}]}]},{"type":"element","tagName":"h2","properties":{"id":"side-effects-of-functions","style":"position:relative;"},"children":[{"type":"text","value":"Side effects of functions"},{"type":"element","tagName":"a","properties":{"href":"#side-effects-of-functions","ariaLabel":"side effects of 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":"If a function takes a reference to a list as an argument, it will be able to modify that list. If direct modifications were not intended by the programmer, accidentally modifying the list received as a parameter could cause problems elsewhere in the program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's take a look at a function which is supposed to find the second smallest item in 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":"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":"second_smallest"}]},{"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":" "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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":"int"}]},{"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":"# in an ordered list, the second smallest item is at index 1"}]},{"type":"text","value":"\n my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"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":" 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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nnumbers "},{"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":"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"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":"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":"second_smallest"},{"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":"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":"numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\n2\n[1, 2, 3, 4, 4, 5, 6, 7]\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function does find the second smallest item reliably, but it additionally sorts the list in place, changing the order of the items. If the order is significant elsewhere in the program, calling the function could cause errors. Unintentional modifications to an object accessed through a reference is called a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"side effect"}]},{"type":"text","value":" of a function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We can avoid the side effect by making a small change to the 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"second_smallest"}]},{"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":" "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n list_copy "},{"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":"sorted"}]},{"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":"return"}]},{"type":"text","value":" list_copy"},{"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":"text","value":"\n\nnumbers "},{"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":"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"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":"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":"second_smallest"},{"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":"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":"numbers"},{"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":"2\n[1, 4, 2, 5, 3, 6, 4, 7]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" returns a new, sorted copy of the list, so looking for the second smallest item no longer messes with the order of the original list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is generally considered a good programming practice to avoid causing side effects with functions. Side effects can make it more difficult to verify that the program functions as intended in all situations."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Functions free of side effects are also called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"pure functions"}]},{"type":"text","value":". Especially when adhering to a functional programming style, this is a common ideal to follow. We will explore this topic further in "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Advanced Course in Programming"}]},{"type":"text","value":", which is the course following this one."}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will know what is meant by a reference to a variable
    • \n
    • You will understand that there can be multiple references to the same object
    • \n
    • You will be able to use lists as parameters in functions
    • \n
    • You will know what is meant by a side effect of a function
    • \n

    Thus far we have thought of a variable as a sort of a \"box\" which contains the value of the variable. Technically this is not true in Python. What is stored in a variable is not the value per se, but a reference to the object which is the actual value of the variable. The object can be e.g. a number, a string or a list.

    In practice, this means that the value of the variable is not stored in the variable itself. Instead, there is information about the location in computer memory where the value can be found.

    A reference is often represented by an arrow from the variable to the actual value in memory:

    \n \n \n \n \n \n \"5\n \n \n

    Some of Python types are mutable. For example, the contents of a list can change without needing to create a whole new list:

    \n \n
    number = 1\nnumber = 2\nnumber += 10

    It seems that the commands above are just changing the value stored in the variable, but in fact each command creates a whole new number in the computer's memory.

    The printout from the following program illuminates the situation:

    number = 1\nprint(id(number))\nnumber += 10\nprint(id(number))\na = 1\nprint(id(a))

    4535856912\n4535856944\n4535856912

    At first, the variable number points to the memory location 4535856912. When number is assigned a new value, it points to the location 4535856944. Now, when the variable a is assigned the value 1, a points to the very same location where number was pointing, when it was also assigned the value 1.

    It seems Python has stored the value 1 in the memory location 4535856912. Whenever a variable is assigned the value 1, it refers to that location in computer memory.

    It is good to keep in mind that almost everything is a reference in Python, but all this is rarely relevant to everyday programming tasks. So let's get back to more practical matters.

    Multiple references to the same list

    What actually happens when you assign a list variable to a new variable - is the list copied over?

    a = [1, 2, 3]\nb = a\nb[0] = 10

    The assignment b = a copies the value stored in variable a to the variable b. However, the value stored in a is not the list itself, but a reference to the list.

    So, the assignment b = a copies the reference. As a result there are now two references to the same memory location containing the list.

    \n \n \n \n \n \n \"5\n \n \n

    The list can be accessed through either of the two references:

    list1 = [1, 2, 3, 4]\nlist2 = list1\n\nlist1[0] = 10\nlist2[1] = 20\n\nprint(list1)\nprint(list2)

    [10, 20, 3, 4]\n[10, 20, 3, 4]

    If there is more than one reference to the same list, any one of the references can be used to access the list. On the other hand, a change made through any one of the references affects also the other references, as their target is the same.

    The visualisation tool is again very useful in figuring out what is happening:

    \n \n \n \n \n \n \"5\n \n \n

    The variable new_list points to a different list than the variable my_list.

    An easier way to copy a list is the bracket operator [], which we used for slices previously. The notation [:] selects all items in the collection. As a side effect, it creates a copy of the list:

    my_list = [1,2,3,4]\nnew_list = my_list[:]\n\nmy_list[0] = 10\nnew_list[1] = 20\n\nprint(my_list)\nprint(new_list)

    [10, 2, 3, 4]\n[1, 20, 3, 4]

    Using lists as parameters in functions

    When you pass a list as an argument to a function, you are passing a reference to that list. This means that the function can modify the list directly.

    The following function takes a list as an argument and adds a new item to the end of the list:

    def add_item(my_list: list):\n    new_item = 10\n    my_list.append(new_item)\n\na_list = [1,2,3]\nprint(a_list)\nadd_item(a_list)\nprint(a_list)
    \n[1, 2, 3]\n[1, 2, 3, 10]\n

    Notice the function add_item does not have a return value. It only changes the list it takes as an argument.

    The visualisation tool may help you understand what is happening here:

    \n \n

    Editing a list given as an argument

    The following is an attempt at a function which should augment each item in a list by ten:

    def augment_all(my_list: list):\n    new_list = []\n    for item in my_list:\n        new_list.append(item + 10)\n    my_list = new_list\n\nnumbers = [1, 2, 3]\nprint(\"in the beginning:\", numbers)\naugment_all(numbers)\nprint(\"after the function is executed:\", numbers)

    in the beginning: [1, 2, 3]\nafter the function is executed: [1, 2, 3]

    For some reason the function doesn't work, so what is going on?

    The function takes a reference to a list as an argument. This is stored in the variable my_list. The assignment my_list = new_list assigns a new value to that same variable. The variable my_list now points to the new list created inside the function, and the reference to the original list is no longer available within the function. This assignment has no effect outside the function, however.

    Furthermore, the variable new_list, which contains the new, augmented values, is not accessible from outside the function. It is \"lost\" as the execution of the function finishes, and focus returns to the main function. The variable numbers in the main function always points to the original list.

    The visualisation tool is your friend here, too. Please go through the stages carefully, and see how the original list is not affected by the execution of the function at all:

    \n \n

    Please write a function named double_items(numbers: list), which takes a list of integers as its argument.

    The function should return a new list, which contains all values from the original list doubled. The function should not change the original list.

    An example of the function at work:

    if __name__ == \"__main__\":\n    numbers = [2, 4, 5, 3, 11, -4]\n    numbers_doubled = double_items(numbers)\n    print(\"original:\", numbers)\n    print(\"doubled:\", numbers_doubled)

    original: [2, 4, 5, 3, 11, -4]\ndoubled: [4, 8, 10, 6, 22, -8]

    Please write a function named remove_smallest(numbers: list), which takes a list of integers as its argument.

    The functions should find and remove the smallest item in the list. You may assume there is a single smallest item in the list.

    The function should not have a return value - it should directly modify the list it receives as a parameter.

    An example of how the function works:

    if __name__ == \"__main__\":\n    numbers = [2, 4, 6, 1, 3, 5]\n    remove_smallest(numbers)\n    print(numbers)

    [2, 4, 6, 3, 5]

    In this exercise we will complete two more functions for the sudoku project from the previous section: print_sudoku and add_number.

    The function print_sudoku(sudoku: list) takes a two-dimensional array representing a sudoku grid as its argument. The function should print out the grid in the format specified in the examples below.

    The function add_number(sudoku: list, row_no: int, column_no: int, number:int) takes a two-dimensional array representing a sudoku grid, two integers referring to the row and column indexes of a single square, and a single digit between 1 and 9, as its arguments. The function should add the digit to the specified location in the grid.

    sudoku  = [\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0]\n]\n\nprint_sudoku(sudoku)\nadd_number(sudoku, 0, 0, 2)\nadd_number(sudoku, 1, 2, 7)\nadd_number(sudoku, 5, 7, 3)\nprint()\nprint(\"Three numbers added:\")\nprint()\nprint_sudoku(sudoku)
    \n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\nThree numbers added:\n\n2 _ _  _ _ _  _ _ _\n_ _ 7  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ 3 _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n

    Hint

    Remember it is possible to call the print function without causing a line change:

    print(\"characters \", end=\"\")\nprint(\"without carriage returns\", end=\"\")

    characters without carriage returns

    Sometimes you need just a new line, which a print statement without any argument will achieve:

    print()

    This is the very last sudoku task. This time we will create a slightly different version of the function for adding new numbers to the grid.

    The function copy_and_add(sudoku: list, row_no: int, column_no: int, number: int) takes a two-dimensional array representing a sudoku grid, two integers referring to the row and column indexes of a single square, and a single digit between 1 and 9, as its arguments. The function should return a copy of the original grid with the new digit added in the correct location. The function should not change the original grid received as a parameter.

    The print_sudoku function from the previous exercise could be useful for testing, and it is used in the example below:

    sudoku  = [\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0]\n]\n\ngrid_copy = copy_and_add(sudoku, 0, 0, 2)\nprint(\"Original:\")\nprint_sudoku(sudoku)\nprint()\nprint(\"Copy:\")\nprint_sudoku(grid_copy)
    \nOriginal:\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\nCopy:\n2 _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n

    Hint\nWhen dealing with nested lists you should be extra careful when copying. What all needs to be explicitly copied, and where do changes actually have an effect? The visualisation tool is a great help here, too, although the size of the sudoku grid will make the view less orderly than usual.

    Tic-Tac-Toe is played on a 3 by 3 grid, by two players who take turns inputting noughts and crosses. If either player succeeds in placing three of their own symbols on any row, column or diagonal, they win. If neither player manages this, it is a draw.

    Please write a function named play_turn(game_board: list, x: int, y: int, piece: str), which places the given symbol at the given coordinates on the board. The values of the coordinates on the board are between 0 and 2.

    NB: when compared to the sudoku exercises, the arguments the function takes are the other way around here. The column x comes first, and the row y second.

    The board consists of the following strings:

      \n
    • \"\": empty square
    • \n
    • \"X\": player 1 symbol
    • \n
    • \"O\": player 2 symbol
    • \n

    The function should return True if the square was empty and the symbol was successfully placed on the game board. The function should return False if the square was occupied, or if the coordinates weren't valid.

    An example execution of the function:

    game_board = [[\"\", \"\", \"\"], [\"\", \"\", \"\"], [\"\", \"\", \"\"]]\nprint(play_turn(game_board, 2, 0, \"X\"))\nprint(game_board)

    True\n[['', '', 'X'], ['', '', ''], ['', '', '']]

    Please write a function named transpose(matrix: list), which takes a two-dimensional integer array, i.e., a matrix, as its argument. The function should transpose the matrix. Transposing means essentially flipping the matrix over its diagonal: columns become rows, and rows become columns.

    You may assume the matrix is a square matrix, so it will have an equal number of rows and columns.

    The following matrix

    1 2 3\n4 5 6\n7 8 9

    transposed looks like this:

    1 4 7\n2 5 8\n3 6 9

    The function should not have a return value. The matrix should be modified directly through the reference.

    Side effects of functions

    If a function takes a reference to a list as an argument, it will be able to modify that list. If direct modifications were not intended by the programmer, accidentally modifying the list received as a parameter could cause problems elsewhere in the program.

    Let's take a look at a function which is supposed to find the second smallest item in a list:

    def second_smallest(my_list: list) -> int:\n    # in an ordered list, the second smallest item is at index 1\n    my_list.sort()\n    return my_list[1]\n\nnumbers = [1, 4, 2, 5, 3, 6, 4, 7]\nprint(second_smallest(numbers))\nprint(numbers)
    \n2\n[1, 2, 3, 4, 4, 5, 6, 7]\n

    The function does find the second smallest item reliably, but it additionally sorts the list in place, changing the order of the items. If the order is significant elsewhere in the program, calling the function could cause errors. Unintentional modifications to an object accessed through a reference is called a side effect of a function.

    We can avoid the side effect by making a small change to the function:

    def second_smallest(my_list: list) -> int:\n    list_copy = sorted(my_list)\n    return list_copy[1]\n\nnumbers = [1, 4, 2, 5, 3, 6, 4, 7]\nprint(second_smallest(numbers))\nprint(numbers)

    2\n[1, 4, 2, 5, 3, 6, 4, 7]

    The function sorted returns a new, sorted copy of the list, so looking for the second smallest item no longer messes with the order of the original list.

    It is generally considered a good programming practice to avoid causing side effects with functions. Side effects can make it more difficult to verify that the program functions as intended in all situations.

    Functions free of side effects are also called pure functions. Especially when adhering to a functional programming style, this is a common ideal to follow. We will explore this topic further in Advanced Course in Programming, which is the course following this one.

    ","frontmatter":{"path":"/part-5/2-references","title":"References"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-5/2-references.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know what is meant by a reference to a variable"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will understand that there can be multiple references to the same object"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use lists as parameters in functions"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what is meant by a side effect of a function"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Thus far we have thought of a variable as a sort of a \"box\" which contains the value of the variable. Technically this is not true in Python. What is stored in a variable is not the value per se, but a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reference"}]},{"type":"text","value":" to the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"object"}]},{"type":"text","value":" which is the actual value of the variable. The object can be e.g. a number, a string or a list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In practice, this means that the value of the variable "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"is not stored"}]},{"type":"text","value":" in the variable itself. Instead, there is information about the location in computer memory where the value can be found."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A reference is often represented by an arrow from the variable to the actual value in memory:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 261px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/8687855d3e59d8151e4fce49ec9669b0/edf96/5_2_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: 30.869565217391305%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA1ElEQVQY041ROQ6EQAyb7/MBfsA/aCgoEHQcDQ2XOMQlxI1XzgqKbdhIljzJ2JNk1DiOKIoCdV1jnmfEcYymaZBlmSBJEjDO88R1Xa9Qx3FgWRYRMUHTaZoE5Kwx/2+om7yJ+AA7b9tWprl5VVXCu66TiaRDgoY3v8FI0xR93z8idlyWpRhxAtaZZ5imCUXXKIpkj77vCyeCIECe59A0Dbquyw7XdX0m4e750L7vgm3b4Lrut0MeeJGiXwzDIAb8LM/zEIYhbNuG4zjCLcuSPOuGYeADgRnJTrwaxUwAAAAASUVORK5CYII='); 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":["/static/8687855d3e59d8151e4fce49ec9669b0/a0b58/5_2_1.webp 230w","/static/8687855d3e59d8151e4fce49ec9669b0/ad293/5_2_1.webp 261w"],"sizes":"(max-width: 261px) 100vw, 261px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/8687855d3e59d8151e4fce49ec9669b0/81c8e/5_2_1.png 230w","/static/8687855d3e59d8151e4fce49ec9669b0/edf96/5_2_1.png 261w"],"sizes":"(max-width: 261px) 100vw, 261px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/8687855d3e59d8151e4fce49ec9669b0/edf96/5_2_1.png","alt":"5 2 1","title":"5 2 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":"p","properties":{},"children":[{"type":"text","value":"So, a reference tells us where the value can be found. The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"id"}]},{"type":"text","value":" can be used to find out the exact location the variable points to:"}]},{"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":"a "},{"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":"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":" "},{"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":"\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","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"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":"\nb "},{"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 a reference, too\""}]},{"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","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"b"},{"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":"4538357072\n4537788912"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The reference, or the ID of the variable, is an integer, which can be thought of as the address in computer memory where the value of the variable is stored. If you execute the above code on your own computer, the result will likely be different, as your variables will point to different locations - the references will be different."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The Python Tutor visualisation tool also shows references as arrows from the variable to the actual content, as we saw in the "},{"type":"element","tagName":"a","properties":{"href":"/part-5/1-more-lists#visualising-code-containing-lists-within-lists"},"children":[{"type":"text","value":"previous section"}]},{"type":"text","value":". The tool \"cheats\" a bit when it comes to strings, however. It displays strings as if they are stored in the variables themselves:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/42bf1345e79a21fd456ed6f4bbd595ad/f4281/5_2_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: 28.695652173913043%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAYAAADDl76dAAAACXBIWXMAAAsTAAALEwEAmpwYAAABAUlEQVQY002R626DMAxGef+32++1u3RagUAgoZTcc6akg9aSZcnx5fhLA5BzLoEYEqNQTONCCJHXt5QichwRfY9WGmssZtuIMZJSql6sKQ2lxTmHnjVDP3FbNrwPtehYFiPWWqwxeOePQSVWTwUg01QK71m1RrQdSkq8s9zv99q8b94H75ZzqjUlms1i7eOiJqVMCpHgPdY45HBDK3MQ7IOKBJffmctV0Y0GMTuuw4pxiWWVrNuJlDWN1ho1zzjv/0/LpPikeRDmKsHpa+T8LTlfJO+fAx8/E85nXFDMyxsuCJoQQ6WRUtK1HYMQ9F1H17ZM03TotNPWD4jPHLxIkeEP+LrTj7xGy7wAAAAASUVORK5CYII='); 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":["/static/42bf1345e79a21fd456ed6f4bbd595ad/a0b58/5_2_2.webp 230w","/static/42bf1345e79a21fd456ed6f4bbd595ad/bc10c/5_2_2.webp 460w","/static/42bf1345e79a21fd456ed6f4bbd595ad/966d8/5_2_2.webp 920w","/static/42bf1345e79a21fd456ed6f4bbd595ad/adbc1/5_2_2.webp 1016w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/42bf1345e79a21fd456ed6f4bbd595ad/81c8e/5_2_2.png 230w","/static/42bf1345e79a21fd456ed6f4bbd595ad/08a84/5_2_2.png 460w","/static/42bf1345e79a21fd456ed6f4bbd595ad/c0255/5_2_2.png 920w","/static/42bf1345e79a21fd456ed6f4bbd595ad/f4281/5_2_2.png 1016w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/42bf1345e79a21fd456ed6f4bbd595ad/c0255/5_2_2.png","alt":"5 2 2","title":"5 2 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":"In reality, Python strings are handled very much like lists, with references to locations in memory."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Many of the builtin types in Python, such as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":", are "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"immutable"}]},{"type":"text","value":". This means the value of the object, or any part of it, cannot change. The value can be replaced with a new 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":"/static/374387f1b515a88857cfbac235dfb942/b1a44/5_2_3.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":["/static/374387f1b515a88857cfbac235dfb942/a0b58/5_2_3.webp 230w","/static/374387f1b515a88857cfbac235dfb942/f6dfe/5_2_3.webp 296w"],"sizes":"(max-width: 296px) 100vw, 296px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/374387f1b515a88857cfbac235dfb942/81c8e/5_2_3.png 230w","/static/374387f1b515a88857cfbac235dfb942/b1a44/5_2_3.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":"/static/374387f1b515a88857cfbac235dfb942/b1a44/5_2_3.png","alt":"5 2 3","title":"5 2 3","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":"Some of Python types are "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"mutable"}]},{"type":"text","value":". For example, the contents of a list can change without needing to create a whole new list:"}]},{"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":"/static/6a61f09e6f9cd09d51315c7fde142ab9/6b1e2/5_2_4.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":["/static/6a61f09e6f9cd09d51315c7fde142ab9/a0b58/5_2_4.webp 230w","/static/6a61f09e6f9cd09d51315c7fde142ab9/f44a8/5_2_4.webp 281w"],"sizes":"(max-width: 281px) 100vw, 281px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6a61f09e6f9cd09d51315c7fde142ab9/81c8e/5_2_4.png 230w","/static/6a61f09e6f9cd09d51315c7fde142ab9/6b1e2/5_2_4.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":"/static/6a61f09e6f9cd09d51315c7fde142ab9/6b1e2/5_2_4.png","alt":"5 2 4","title":"5 2 4","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":"It may surprise you that also the basic data types "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bool"}]},{"type":"text","value":" are immutable in Python. Let's have a look at the following bit of code:"}]},{"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":"\nnumber "},{"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":"2"}]},{"type":"text","value":"\nnumber "},{"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":"p","properties":{},"children":[{"type":"text","value":"It seems that the commands above are just changing the value stored in the variable, but in fact each command creates a whole new number in the computer's memory."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The printout from the following program illuminates the situation:"}]},{"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":"\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","builtin"]},"children":[{"type":"text","value":"id"}]},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnumber "},{"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":"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","builtin"]},"children":[{"type":"text","value":"id"}]},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\na "},{"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":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"id"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"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":"4535856912\n4535856944\n4535856912"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"At first, the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" points to the memory location 4535856912. When "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" is assigned a new value, it points to the location 4535856944. Now, when the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" is assigned the value 1, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" points to the very same location where "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number"}]},{"type":"text","value":" was pointing, when it was also assigned the value 1."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It seems Python has stored the value 1 in the memory location 4535856912. Whenever a variable is assigned the value 1, it "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"refers"}]},{"type":"text","value":" to that location in computer memory."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is good to keep in mind that "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"almost everything is a reference"}]},{"type":"text","value":" in Python, but all this is rarely relevant to everyday programming tasks. So let's get back to more practical matters."}]},{"type":"element","tagName":"h2","properties":{"id":"multiple-references-to-the-same-list","style":"position:relative;"},"children":[{"type":"text","value":"Multiple references to the same list"},{"type":"element","tagName":"a","properties":{"href":"#multiple-references-to-the-same-list","ariaLabel":"multiple references to the same list 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":"What actually happens when you assign a list variable to a new variable - is the list copied over?"}]},{"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":"a "},{"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":"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":" "},{"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":"\nb "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" a\nb"},{"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":"p","properties":{},"children":[{"type":"text","value":"The assignment "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b = a"}]},{"type":"text","value":" copies the value stored in variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" to the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":". However, the value stored in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" is not the list "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"itself"}]},{"type":"text","value":", but a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reference"}]},{"type":"text","value":" to the list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, the assignment "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b = a"}]},{"type":"text","value":" copies the reference. As a result there are now two references to the same memory location containing the list."}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 271px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/73c7fae5118d6e5b03c576f1a9d14c3f/5fce1/5_2_5.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: 129.99999999999997%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAaCAYAAAC3g3x9AAAACXBIWXMAAAsTAAALEwEAmpwYAAACN0lEQVRIx52VV64qQQxEZ60sgC2wJvhhFXwhkMg555zx07FU3FHf0QVeS1aHoctlu9xE4/HYlsulbbdbm06nNpvNjDPm4XBoj8fDGM/n8yOLdrudX8KOx6MDbzYbOxwOvtc3fvzJiOyLsVqtbL1ee0SLxeLXmu8OeLvd7H6/OxNmGfvT6WSdTscul4ungv18PncQ1gCSHiJi74C1Ws0Gg4H1+32rVCq+xxqNhhWLRUun09Zutz0FXCRNXAZIZ5PJ5CdkMQwTzLher26wxSGXR6ORm4AoHk4IO4on+6/E801s/rIo6WI4f1phD5kw9vu9S4WchCAh8FsdojtydD6fHTQJ8CuGSSEnFYcZ6cggABGtdR594p0KogKkQyV7vZ4ba+SGTiVwZ1iv1730fCiXy67BZrNp1WrV8vm8pVIpB0N7qjSs6AydAfYKmQPoarBvtVoODjBag2G32/U8wwqDFXqksAC+Wk/NT4FgS7eo4qEOYSTNwVJrMXXAUqnkLPCEl/jQa/NVlWm9pEthF32kQ/UqplcGB9i7dkxkSEWpIFUmF0o6r/d/dUr8HdQIXx7tk9IQrr96sXlgiYACskbYrJEOxeThdUD9hyhkGbJAX9ls9vV4sGeme3hUkBpgzETigIgXj3hSPhF2oVCwXC5nmUzGv3OR/MIKzSFsgcLOcxhWkT2XKRYOVG0GOoUZ5zgFiDVOiOAVMh4Jl9ai3QCM61JF4Sz+hyb9xs9fIZMX6IddEj5j73T4DynC3t7bOJziAAAAAElFTkSuQmCC'); 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":["/static/73c7fae5118d6e5b03c576f1a9d14c3f/a0b58/5_2_5.webp 230w","/static/73c7fae5118d6e5b03c576f1a9d14c3f/cadab/5_2_5.webp 271w"],"sizes":"(max-width: 271px) 100vw, 271px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/73c7fae5118d6e5b03c576f1a9d14c3f/81c8e/5_2_5.png 230w","/static/73c7fae5118d6e5b03c576f1a9d14c3f/5fce1/5_2_5.png 271w"],"sizes":"(max-width: 271px) 100vw, 271px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/73c7fae5118d6e5b03c576f1a9d14c3f/5fce1/5_2_5.png","alt":"5 2 5","title":"5 2 5","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 list can be accessed through either of the two references:"}]},{"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":"list1 "},{"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":"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":" "},{"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":" "},{"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":"text","value":"\nlist2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" list1\n\nlist1"},{"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":"text","value":"\nlist2"},{"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":"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":"20"}]},{"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":"list1"},{"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":"list2"},{"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":"[10, 20, 3, 4]\n[10, 20, 3, 4]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If there is more than one reference to the same list, any one of the references can be used to access the list. On the other hand, a change made through any one of the references affects also the other references, as their target is the same."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The visualisation tool is again very useful in figuring out what is happening:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/c80c08ee8666a616ae1b04ea29ec2de4/bf433/5_2_6.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: 32.608695652173914%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAAsTAAALEwEAmpwYAAABCklEQVQoz2WRXU7EMAyEe/9jwRHgAfEEEnRh27TNNn9NE38o2e3SgqVR5Nge2+NGRCgAIaWM6jSqvxBjothvHJy1qF6hpwlrDM65ipQSOeeKZl9greP7pOi+R8zsiMtyJ61xYxjUgDWWGGMlKGQprazrFQfC4D3WzDhrarAUbLblFItLqK/3frcJpJyOhEsIqK7nojXr+n/ly+z5aAc+254sMM8TS9RksYjEmtPIboKikR5HZq3rdPKHUE2ep5cTz68tbhEG/cZ5fKCfHvHL+42wJAtkybiiYas4fw3EuCII+6PFNWNcxPq4jUHO14NsTRulBvquI4Rw0yezhHIxOWh3bSwHTXfu/e8HySgkP7muRjUAAAAASUVORK5CYII='); 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":["/static/c80c08ee8666a616ae1b04ea29ec2de4/a0b58/5_2_6.webp 230w","/static/c80c08ee8666a616ae1b04ea29ec2de4/bc10c/5_2_6.webp 460w","/static/c80c08ee8666a616ae1b04ea29ec2de4/966d8/5_2_6.webp 920w","/static/c80c08ee8666a616ae1b04ea29ec2de4/3697b/5_2_6.webp 971w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/c80c08ee8666a616ae1b04ea29ec2de4/81c8e/5_2_6.png 230w","/static/c80c08ee8666a616ae1b04ea29ec2de4/08a84/5_2_6.png 460w","/static/c80c08ee8666a616ae1b04ea29ec2de4/c0255/5_2_6.png 920w","/static/c80c08ee8666a616ae1b04ea29ec2de4/bf433/5_2_6.png 971w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/c80c08ee8666a616ae1b04ea29ec2de4/c0255/5_2_6.png","alt":"5 2 6","title":"5 2 6","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":"h2","properties":{"id":"copying-a-list","style":"position:relative;"},"children":[{"type":"text","value":"Copying a list"},{"type":"element","tagName":"a","properties":{"href":"#copying-a-list","ariaLabel":"copying a list 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":"If you want to create an actual separate copy of a list, you can create a new list and add each item from the original list in turn:"}]},{"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":"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":" "},{"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":" "},{"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":" "},{"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\nnew_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","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":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n new_list"},{"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":"item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nnew_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":"text","value":"\nnew_list"},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"the original:\""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"the copy:\""}]},{"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":"my_list [1, 2, 3, 3, 5]\nnew_list [10, 2, 3, 3, 5, 6]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A snapshot of the copying process in the visualisation tool:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/c670b9845a531e92d89ab8b711b7c304/38124/5_2_7.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: 41.30434782608695%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAAAsTAAALEwEAmpwYAAABO0lEQVQoz2WR3U7DMAyF+/5vxgUXIMQFGgikbR2sS9ImzY+d5CBnXVdprqyjtM5X+7irtUJCVB7mjMswYjgbZC7t3bYmhAA/z/Der0lEKKUg54yuXishSinBjhZaWRjlYCcHZl6BcknOog2wam4q4K4uMDAjeo9RG8zOgVJErQXbuIFTiiBKyMyYZ6lrPSHncgVK5BiRieCsg1Eaarg0OBNvYBWHXuNj10MZD86M0X4j8QGRejFs6XCBxhAaSF9Ug8oY8k30FrufAU/Pn+gHB2KGse8w7hXGvqCUgG47krMWf/0J/f6I4/7YOtRKgRItXRZMM+H3PMEHQmKGtm8I9LUyOmZCiqltT5YiPmjlYXRASrK9/ODh9jyHA3w8LX5XdEFpTNOEcRwRY1xGzCi13LeZc8ubBffEww/+AbQPcTCbhdzzAAAAAElFTkSuQmCC'); 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":["/static/c670b9845a531e92d89ab8b711b7c304/a0b58/5_2_7.webp 230w","/static/c670b9845a531e92d89ab8b711b7c304/bc10c/5_2_7.webp 460w","/static/c670b9845a531e92d89ab8b711b7c304/966d8/5_2_7.webp 920w","/static/c670b9845a531e92d89ab8b711b7c304/5bf28/5_2_7.webp 953w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/c670b9845a531e92d89ab8b711b7c304/81c8e/5_2_7.png 230w","/static/c670b9845a531e92d89ab8b711b7c304/08a84/5_2_7.png 460w","/static/c670b9845a531e92d89ab8b711b7c304/c0255/5_2_7.png 920w","/static/c670b9845a531e92d89ab8b711b7c304/38124/5_2_7.png 953w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/c670b9845a531e92d89ab8b711b7c304/c0255/5_2_7.png","alt":"5 2 7","title":"5 2 7","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 variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"new_list"}]},{"type":"text","value":" points to a different list than the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"my_list"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An easier way to copy a list is the bracket operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]},{"type":"text","value":", which we used for slices previously. The notation "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[:]"}]},{"type":"text","value":" selects all items in the collection. As a side effect, it creates a copy of the 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":"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":"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":"text","value":"\nnew_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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","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","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":"text","value":"\nnew_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":"1"}]},{"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":"20"}]},{"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":"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":"[10, 2, 3, 4]\n[1, 20, 3, 4]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"using-lists-as-parameters-in-functions","style":"position:relative;"},"children":[{"type":"text","value":"Using lists as parameters in functions"},{"type":"element","tagName":"a","properties":{"href":"#using-lists-as-parameters-in-functions","ariaLabel":"using lists as parameters in 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":"When you pass a list as an argument to a function, you are passing a reference to that list. This means that the function can modify the list directly."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following function takes a list as an argument and adds a new item to the end of the 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":"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":"add_item"}]},{"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":" "},{"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 new_item "},{"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":"text","value":"\n my_list"},{"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":"new_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\na_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":"\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":"a_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadd_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a_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":"a_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\n[1, 2, 3]\n[1, 2, 3, 10]\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_item"}]},{"type":"text","value":" does not have a return value. It only changes the list it takes as an argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The visualisation tool may help you understand what is happening here:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/51be9444713cc1f7896dd2a365ade5cd/d7abb/5_2_8.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: 36.52173913043478%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAHCAYAAAAIy204AAAACXBIWXMAAAsTAAALEwEAmpwYAAABCklEQVQoz11Ra2/DIAzk//+5fdmHTVq1bmuUtQ0hEB7mlZtMlCztSRZgzJ3PiGVZwNjWGDPu1wlKzqh1afntrpQCozXsbOG93yPnjFprC9GK+VEjixhuA7rvHr/dDc46ENEDYQgBOaW2fw4mFltxjQmFqClOasQ4DK0bbx2eXTTSnJFSgnMOG/heHC1RCFCjgtEGFGgv2kVrxSgl5H3YO9KTQwwrOUPgAGctLl8/6Lse3vlGxCQbKUcTlbLlffDQ8ycCXVHLWid4HjwntsGqKWaM0mE29NAdI+eK0/mG80XB+IKYPJR5gaPTv2U/aWhjoLVuba8k648dB87nlAvePnq8vncwlo7mduE/oO4jC89VVXwAAAAASUVORK5CYII='); 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":["/static/51be9444713cc1f7896dd2a365ade5cd/a0b58/5_2_8.webp 230w","/static/51be9444713cc1f7896dd2a365ade5cd/bc10c/5_2_8.webp 460w","/static/51be9444713cc1f7896dd2a365ade5cd/966d8/5_2_8.webp 920w","/static/51be9444713cc1f7896dd2a365ade5cd/1bae5/5_2_8.webp 959w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/51be9444713cc1f7896dd2a365ade5cd/81c8e/5_2_8.png 230w","/static/51be9444713cc1f7896dd2a365ade5cd/08a84/5_2_8.png 460w","/static/51be9444713cc1f7896dd2a365ade5cd/c0255/5_2_8.png 920w","/static/51be9444713cc1f7896dd2a365ade5cd/d7abb/5_2_8.png 959w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/51be9444713cc1f7896dd2a365ade5cd/c0255/5_2_8.png","alt":"5 2 8","title":"5 2 8","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":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Global frame"}]},{"type":"text","value":" refers to the variables defined in the main function, whereas the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"add_item"}]},{"type":"text","value":" frame with a blue background represents the parameters and variables within that function. As you can see from the visualisation, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_item"}]},{"type":"text","value":" function refers to the very same list as the main function. The changes made within the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_item"}]},{"type":"text","value":" function also affect the main function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Another way to implement this functionality would be to create a new list within the function, and return that:"}]},{"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":"add_item"}]},{"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":" "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n new_item "},{"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":"text","value":"\n my_list_copy "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n my_list_copy"},{"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":"new_item"},{"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":" my_list_copy\n\nnumbers "},{"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":"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":" "},{"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":"\nnumbers2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" add_item"},{"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\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":"\"original list:\""}]},{"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":"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":"\"new list:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" numbers2"},{"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":"original list: [1, 2, 3]\nnew list: [1, 2, 3, 10]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you aren't absolutely sure you understand what is happening in the code above, please go through it in the visualisation tool."}]},{"type":"element","tagName":"h2","properties":{"id":"editing-a-list-given-as-an-argument","style":"position:relative;"},"children":[{"type":"text","value":"Editing a list given as an argument"},{"type":"element","tagName":"a","properties":{"href":"#editing-a-list-given-as-an-argument","ariaLabel":"editing a list given as an argument 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 following is an attempt at a function which should augment each item in a list by ten:"}]},{"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":"augment_all"}]},{"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":" "},{"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 new_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","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":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n new_list"},{"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":"item "},{"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":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" new_list\n\nnumbers "},{"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":"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":" "},{"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":"\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":"\"in the beginning:\""}]},{"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":"\naugment_all"},{"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":"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":"\"after the function is executed:\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"in the beginning: [1, 2, 3]\nafter the function is executed: [1, 2, 3]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For some reason the function doesn't work, so what is going on?"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function takes a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reference"}]},{"type":"text","value":" to a list as an argument. This is stored in the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"my_list"}]},{"type":"text","value":". The assignment "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"my_list = new_list"}]},{"type":"text","value":" assigns a new value to that same variable. The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"my_list"}]},{"type":"text","value":" now points to the new list created inside the function, and the reference to the original list is no longer available within the function. This assignment has no effect outside the function, however."}]},{"type":"comment","value":"-\nThe following sequence of images illustrates the states of the variables at different stages during the execution of the function:\n\n\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Furthermore, the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"new_list"}]},{"type":"text","value":", which contains the new, augmented values, is not accessible from outside the function. It is \"lost\" as the execution of the function finishes, and focus returns to the main function. The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"numbers"}]},{"type":"text","value":" in the main function always points to the original list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The visualisation tool is your friend here, too. Please go through the stages carefully, and see how the original list is not affected by the execution of the function at all:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/65ad0c67b682d1c5d27a8cbe274421c3/914c7/5_2_10.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: 46.52173913043478%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAABeUlEQVQoz02S2W7jMAxF/f//1qIYYPqQAHmYJk5iedFia7Ekn4HUOAkBgoB4Sd5Lqtm2jZwzOWWKzcZx+RGIuyLFxJ4vFkJgGifGYURJidEG733Np/SLbQowxsjUD9zaK2M/oLXBWUtc47NhiaWoDn/zPb+ua43NVkAxsoaAuHf0Xcf10rLMM865WrDbrkQrRUoZay2zMTVXcJXhFiMxhMoo+IB3vsoofXbQ3rRIHoRATbK+FayaZpz9rX9KLg3afz91P/f2WhkUxkZp8mM3xWNK9N1AL4bHAI22fwjx/mTZ7JPL7qZxpLsJ2nOHHDVaaexia/TOMS+e7+OV01kyu4QPBr0cCat4NbRao7WuF9t4SPOZGF+72yUr4/j4OvH3eEMvCTX3jOoDtXySs6+YJnrPugZC8CipUEqhtaqypZR1HeUX5BRZfOR8aDkcLoQ1lVHEZEnpdbwmhISz5coJuyzIaaI9X+hFjzGmfocHT3LeyBvV36//ruQ/rtK74EJfuxIAAAAASUVORK5CYII='); 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":["/static/65ad0c67b682d1c5d27a8cbe274421c3/a0b58/5_2_10.webp 230w","/static/65ad0c67b682d1c5d27a8cbe274421c3/bc10c/5_2_10.webp 460w","/static/65ad0c67b682d1c5d27a8cbe274421c3/966d8/5_2_10.webp 920w","/static/65ad0c67b682d1c5d27a8cbe274421c3/0e613/5_2_10.webp 978w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/65ad0c67b682d1c5d27a8cbe274421c3/81c8e/5_2_10.png 230w","/static/65ad0c67b682d1c5d27a8cbe274421c3/08a84/5_2_10.png 460w","/static/65ad0c67b682d1c5d27a8cbe274421c3/c0255/5_2_10.png 920w","/static/65ad0c67b682d1c5d27a8cbe274421c3/914c7/5_2_10.png 978w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/65ad0c67b682d1c5d27a8cbe274421c3/c0255/5_2_10.png","alt":"5 2 10","title":"5 2 10","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":"One way to fix this is to copy all the items from the new list to the old list, one by one:"}]},{"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":"augment_all"}]},{"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":" "},{"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 new_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","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":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n new_list"},{"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":"item "},{"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":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# copy items from the new list into the old list"}]},{"type":"text","value":"\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":"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":"my_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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n my_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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" new_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":"p","properties":{},"children":[{"type":"text","value":"Python also has a nifty shorthand for assigning multiple items in a collection at once:"}]},{"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","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"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":"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":" "},{"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":" "},{"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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"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":"3"}]},{"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","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" my_list\n"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"20"}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the example above a slice of the list is assigned values from another collection."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As we know, a slice can include the entire collection:"}]},{"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","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"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":"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":" "},{"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":" "},{"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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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","punctuation"]},"children":[{"type":"text","value":":"}]},{"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","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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":"99"}]},{"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":"98"}]},{"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":"97"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" my_list\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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":"99"}]},{"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":"98"}]},{"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":"97"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The entire contents of the old list are replaced. Inspired by this, a working version of the augmenting function 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":"augment_all"}]},{"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":" "},{"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 new_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","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":" item "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n new_list"},{"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":"item "},{"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":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n my_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":"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":" new_list"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Actually, there is no need to create a new list within the function at all. We can just assign the new values directly into the original 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":"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":"augment_all"}]},{"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":" "},{"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":"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":"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":"my_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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n my_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":"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":"text","value":"\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Items multiplied by two","tmcname":"part05-08_items_multiplied_by_two"},"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":"double_items(numbers: list)"}]},{"type":"text","value":", which takes a list of integers as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return a new list, which contains all values from the original list doubled. The function should "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" change the original list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function at 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":"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 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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"4"}]},{"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":"5"}]},{"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":"3"}]},{"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":"11"}]},{"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":"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":"text","value":"\n numbers_doubled "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" double_items"},{"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":"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":"\"original:\""}]},{"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":"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":"\"doubled:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" numbers_doubled"},{"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":"original: [2, 4, 5, 3, 11, -4]\ndoubled: [4, 8, 10, 6, 22, -8]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Remove the smallest","tmcname":"part05-09_remove_smallest"},"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":"remove_smallest(numbers: list)"}]},{"type":"text","value":", which takes a list of integers as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The functions should find and remove the smallest item in the list. You may assume there is a single smallest item in the list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should not have a return value - it should directly modify the list it receives as a parameter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how the function works:"}]},{"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":"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 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","number"]},"children":[{"type":"text","value":"2"}]},{"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":"4"}]},{"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":"6"}]},{"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":"1"}]},{"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":"3"}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n remove_smallest"},{"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":"print"}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[2, 4, 6, 3, 5]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: print out the grid and add a number","tmcname":"part05-10_sudoku_print_and_add"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise we will complete two more functions for the sudoku project from the previous section: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print_sudoku"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_number"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print_sudoku(sudoku: list)"}]},{"type":"text","value":" takes a two-dimensional array representing a sudoku grid as its argument. The function should print out the grid in the format specified in the examples below."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_number(sudoku: list, row_no: int, column_no: int, number:int)"}]},{"type":"text","value":" takes a two-dimensional array representing a sudoku grid, two integers referring to the row and column indexes of a single square, and a single digit between 1 and 9, as its arguments. The function should add the digit to the specified location in the grid."}]},{"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":"sudoku "},{"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":"text","value":"\n "},{"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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nprint_sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadd_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"0"}]},{"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":"0"}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadd_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"1"}]},{"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":"2"}]},{"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":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadd_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"5"}]},{"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":"7"}]},{"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":"3"}]},{"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"},{"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":"\"Three numbers added:\""}]},{"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_sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\nThree numbers added:\n\n2 _ _ _ _ _ _ _ _\n_ _ 7 _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ 3 _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n"}]}]},{"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":"Remember it is possible to call the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" function without causing a line change:"}]},{"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":"\"characters \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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"},{"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":"\"without carriage returns\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"characters without carriage returns"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sometimes you need just a new line, which a print statement without any argument will achieve:"}]},{"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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Sudoku: add number to a copy of the grid","tmcname":"part05-11_sudoku_add_to_copy"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is the very last sudoku task. This time we will create a slightly different version of the function for adding new numbers to the grid."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"copy_and_add(sudoku: list, row_no: int, column_no: int, number: int)"}]},{"type":"text","value":" takes a two-dimensional array representing a sudoku grid, two integers referring to the row and column indexes of a single square, and a single digit between 1 and 9, as its arguments. The function should "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" a copy of the original grid with the new digit added in the correct location. The function should "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" change the original grid received as a parameter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print_sudoku"}]},{"type":"text","value":" function from the previous exercise could be useful for testing, and it is used in the example below:"}]},{"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":"sudoku "},{"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":"text","value":"\n "},{"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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","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","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\ngrid_copy "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" copy_and_add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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":"0"}]},{"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":"0"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Original:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprint_sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sudoku"},{"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"},{"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":"\"Copy:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprint_sudoku"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"grid_copy"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Original:\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\nCopy:\n2 _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n_ _ _ _ _ _ _ _ _\n\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint"}]},{"type":"text","value":"\nWhen dealing with nested lists you should be extra careful when copying. What all needs to be explicitly copied, and where do changes actually have an effect? The "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":" is a great help here, too, although the size of the sudoku grid will make the view less orderly than usual."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Tic-Tac-Toe","tmcname":"part05-12_tic_tac_toe"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tic-Tac-Toe is played on a 3 by 3 grid, by two players who take turns inputting noughts and crosses. If either player succeeds in placing three of their own symbols on any row, column or diagonal, they win. If neither player manages this, it is a draw."}]},{"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":"play_turn(game_board: list, x: int, y: int, piece: str)"}]},{"type":"text","value":", which places the given symbol at the given coordinates on the board. The values of the coordinates on the board are between 0 and 2."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" when compared to the sudoku exercises, the arguments the function takes are the other way around here. The column "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" comes first, and the row "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" second."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The board consists of the following strings:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":": empty square"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"X\""}]},{"type":"text","value":": player 1 symbol"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"O\""}]},{"type":"text","value":": player 2 symbol"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" if the square was empty and the symbol was successfully placed on the game board. The function should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" if the square was occupied, or if the coordinates weren't valid."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example execution of the 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":"text","value":"game_board "},{"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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"},{"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":"play_turn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"game_board"},{"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":"2"}]},{"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":"0"}]},{"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":"\"X\""}]},{"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":"game_board"},{"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\n[['', '', 'X'], ['', '', ''], ['', '', '']]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Transpose a matrix","tmcname":"part05-13_transpose_matrix"},"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":"transpose(matrix: list)"}]},{"type":"text","value":", which takes a two-dimensional integer array, i.e., a matrix, as its argument. The function should "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"transpose"}]},{"type":"text","value":" the matrix. Transposing means essentially flipping the matrix over its diagonal: columns become rows, and rows become columns."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume the matrix is a square matrix, so it will have an equal number of rows and columns."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following matrix"}]},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"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","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"transposed looks 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","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"7"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"6"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should not have a return value. The matrix should be modified directly through the reference."}]}]},{"type":"element","tagName":"h2","properties":{"id":"side-effects-of-functions","style":"position:relative;"},"children":[{"type":"text","value":"Side effects of functions"},{"type":"element","tagName":"a","properties":{"href":"#side-effects-of-functions","ariaLabel":"side effects of 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":"If a function takes a reference to a list as an argument, it will be able to modify that list. If direct modifications were not intended by the programmer, accidentally modifying the list received as a parameter could cause problems elsewhere in the program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's take a look at a function which is supposed to find the second smallest item in 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":"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":"second_smallest"}]},{"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":" "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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":"int"}]},{"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":"# in an ordered list, the second smallest item is at index 1"}]},{"type":"text","value":"\n my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"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":" 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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nnumbers "},{"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":"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"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":"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":"second_smallest"},{"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":"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":"numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\n2\n[1, 2, 3, 4, 4, 5, 6, 7]\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function does find the second smallest item reliably, but it additionally sorts the list in place, changing the order of the items. If the order is significant elsewhere in the program, calling the function could cause errors. Unintentional modifications to an object accessed through a reference is called a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"side effect"}]},{"type":"text","value":" of a function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We can avoid the side effect by making a small change to the 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"second_smallest"}]},{"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":" "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n list_copy "},{"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":"sorted"}]},{"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":"return"}]},{"type":"text","value":" list_copy"},{"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":"text","value":"\n\nnumbers "},{"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":"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":"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":" "},{"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":" "},{"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":" "},{"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":" "},{"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":"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":"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":"second_smallest"},{"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":"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":"numbers"},{"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":"2\n[1, 4, 2, 5, 3, 6, 4, 7]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sorted"}]},{"type":"text","value":" returns a new, sorted copy of the list, so looking for the second smallest item no longer messes with the order of the original list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is generally considered a good programming practice to avoid causing side effects with functions. Side effects can make it more difficult to verify that the program functions as intended in all situations."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Functions free of side effects are also called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"pure functions"}]},{"type":"text","value":". Especially when adhering to a functional programming style, this is a common ideal to follow. We will explore this topic further in "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Advanced Course in Programming"}]},{"type":"text","value":", which is the course following this one."}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will know what is meant by a reference to a variable
    • \n
    • You will understand that there can be multiple references to the same object
    • \n
    • You will be able to use lists as parameters in functions
    • \n
    • You will know what is meant by a side effect of a function
    • \n

    Thus far we have thought of a variable as a sort of a \"box\" which contains the value of the variable. Technically this is not true in Python. What is stored in a variable is not the value per se, but a reference to the object which is the actual value of the variable. The object can be e.g. a number, a string or a list.

    In practice, this means that the value of the variable is not stored in the variable itself. Instead, there is information about the location in computer memory where the value can be found.

    A reference is often represented by an arrow from the variable to the actual value in memory:

    \n \n \n \n \n \n \"5\n \n \n

    Some of Python types are mutable. For example, the contents of a list can change without needing to create a whole new list:

    \n \n
    number = 1\nnumber = 2\nnumber += 10

    It seems that the commands above are just changing the value stored in the variable, but in fact each command creates a whole new number in the computer's memory.

    The printout from the following program illuminates the situation:

    number = 1\nprint(id(number))\nnumber += 10\nprint(id(number))\na = 1\nprint(id(a))

    4535856912\n4535856944\n4535856912

    At first, the variable number points to the memory location 4535856912. When number is assigned a new value, it points to the location 4535856944. Now, when the variable a is assigned the value 1, a points to the very same location where number was pointing, when it was also assigned the value 1.

    It seems Python has stored the value 1 in the memory location 4535856912. Whenever a variable is assigned the value 1, it refers to that location in computer memory.

    It is good to keep in mind that almost everything is a reference in Python, but all this is rarely relevant to everyday programming tasks. So let's get back to more practical matters.

    Multiple references to the same list

    What actually happens when you assign a list variable to a new variable - is the list copied over?

    a = [1, 2, 3]\nb = a\nb[0] = 10

    The assignment b = a copies the value stored in variable a to the variable b. However, the value stored in a is not the list itself, but a reference to the list.

    So, the assignment b = a copies the reference. As a result there are now two references to the same memory location containing the list.

    \n \n \n \n \n \n \"5\n \n \n

    The list can be accessed through either of the two references:

    list1 = [1, 2, 3, 4]\nlist2 = list1\n\nlist1[0] = 10\nlist2[1] = 20\n\nprint(list1)\nprint(list2)

    [10, 20, 3, 4]\n[10, 20, 3, 4]

    If there is more than one reference to the same list, any one of the references can be used to access the list. On the other hand, a change made through any one of the references affects also the other references, as their target is the same.

    The visualisation tool is again very useful in figuring out what is happening:

    \n \n \n \n \n \n \"5\n \n \n

    The variable new_list points to a different list than the variable my_list.

    An easier way to copy a list is the bracket operator [], which we used for slices previously. The notation [:] selects all items in the collection. As a side effect, it creates a copy of the list:

    my_list = [1,2,3,4]\nnew_list = my_list[:]\n\nmy_list[0] = 10\nnew_list[1] = 20\n\nprint(my_list)\nprint(new_list)

    [10, 2, 3, 4]\n[1, 20, 3, 4]

    Using lists as parameters in functions

    When you pass a list as an argument to a function, you are passing a reference to that list. This means that the function can modify the list directly.

    The following function takes a list as an argument and adds a new item to the end of the list:

    def add_item(my_list: list):\n    new_item = 10\n    my_list.append(new_item)\n\na_list = [1,2,3]\nprint(a_list)\nadd_item(a_list)\nprint(a_list)
    \n[1, 2, 3]\n[1, 2, 3, 10]\n

    Notice the function add_item does not have a return value. It only changes the list it takes as an argument.

    The visualisation tool may help you understand what is happening here:

    \n \n

    Editing a list given as an argument

    The following is an attempt at a function which should augment each item in a list by ten:

    def augment_all(my_list: list):\n    new_list = []\n    for item in my_list:\n        new_list.append(item + 10)\n    my_list = new_list\n\nnumbers = [1, 2, 3]\nprint(\"in the beginning:\", numbers)\naugment_all(numbers)\nprint(\"after the function is executed:\", numbers)

    in the beginning: [1, 2, 3]\nafter the function is executed: [1, 2, 3]

    For some reason the function doesn't work, so what is going on?

    The function takes a reference to a list as an argument. This is stored in the variable my_list. The assignment my_list = new_list assigns a new value to that same variable. The variable my_list now points to the new list created inside the function, and the reference to the original list is no longer available within the function. This assignment has no effect outside the function, however.

    Furthermore, the variable new_list, which contains the new, augmented values, is not accessible from outside the function. It is \"lost\" as the execution of the function finishes, and focus returns to the main function. The variable numbers in the main function always points to the original list.

    The visualisation tool is your friend here, too. Please go through the stages carefully, and see how the original list is not affected by the execution of the function at all:

    \n \n

    Please write a function named double_items(numbers: list), which takes a list of integers as its argument.

    The function should return a new list, which contains all values from the original list doubled. The function should not change the original list.

    An example of the function at work:

    if __name__ == \"__main__\":\n    numbers = [2, 4, 5, 3, 11, -4]\n    numbers_doubled = double_items(numbers)\n    print(\"original:\", numbers)\n    print(\"doubled:\", numbers_doubled)

    original: [2, 4, 5, 3, 11, -4]\ndoubled: [4, 8, 10, 6, 22, -8]

    Please write a function named remove_smallest(numbers: list), which takes a list of integers as its argument.

    The functions should find and remove the smallest item in the list. You may assume there is a single smallest item in the list.

    The function should not have a return value - it should directly modify the list it receives as a parameter.

    An example of how the function works:

    if __name__ == \"__main__\":\n    numbers = [2, 4, 6, 1, 3, 5]\n    remove_smallest(numbers)\n    print(numbers)

    [2, 4, 6, 3, 5]

    In this exercise we will complete two more functions for the sudoku project from the previous section: print_sudoku and add_number.

    The function print_sudoku(sudoku: list) takes a two-dimensional array representing a sudoku grid as its argument. The function should print out the grid in the format specified in the examples below.

    The function add_number(sudoku: list, row_no: int, column_no: int, number:int) takes a two-dimensional array representing a sudoku grid, two integers referring to the row and column indexes of a single square, and a single digit between 1 and 9, as its arguments. The function should add the digit to the specified location in the grid.

    sudoku  = [\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0]\n]\n\nprint_sudoku(sudoku)\nadd_number(sudoku, 0, 0, 2)\nadd_number(sudoku, 1, 2, 7)\nadd_number(sudoku, 5, 7, 3)\nprint()\nprint(\"Three numbers added:\")\nprint()\nprint_sudoku(sudoku)
    \n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\nThree numbers added:\n\n2 _ _  _ _ _  _ _ _\n_ _ 7  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ 3 _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n

    Hint

    Remember it is possible to call the print function without causing a line change:

    print(\"characters \", end=\"\")\nprint(\"without carriage returns\", end=\"\")

    characters without carriage returns

    Sometimes you need just a new line, which a print statement without any argument will achieve:

    print()

    This is the very last sudoku task. This time we will create a slightly different version of the function for adding new numbers to the grid.

    The function copy_and_add(sudoku: list, row_no: int, column_no: int, number: int) takes a two-dimensional array representing a sudoku grid, two integers referring to the row and column indexes of a single square, and a single digit between 1 and 9, as its arguments. The function should return a copy of the original grid with the new digit added in the correct location. The function should not change the original grid received as a parameter.

    The print_sudoku function from the previous exercise could be useful for testing, and it is used in the example below:

    sudoku  = [\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0],\n    [0, 0, 0, 0, 0, 0, 0, 0, 0]\n]\n\ngrid_copy = copy_and_add(sudoku, 0, 0, 2)\nprint(\"Original:\")\nprint_sudoku(sudoku)\nprint()\nprint(\"Copy:\")\nprint_sudoku(grid_copy)
    \nOriginal:\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\nCopy:\n2 _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n_ _ _  _ _ _  _ _ _\n\n

    Hint\nWhen dealing with nested lists you should be extra careful when copying. What all needs to be explicitly copied, and where do changes actually have an effect? The visualisation tool is a great help here, too, although the size of the sudoku grid will make the view less orderly than usual.

    Tic-Tac-Toe is played on a 3 by 3 grid, by two players who take turns inputting noughts and crosses. If either player succeeds in placing three of their own symbols on any row, column or diagonal, they win. If neither player manages this, it is a draw.

    Please write a function named play_turn(game_board: list, x: int, y: int, piece: str), which places the given symbol at the given coordinates on the board. The values of the coordinates on the board are between 0 and 2.

    NB: when compared to the sudoku exercises, the arguments the function takes are the other way around here. The column x comes first, and the row y second.

    The board consists of the following strings:

      \n
    • \"\": empty square
    • \n
    • \"X\": player 1 symbol
    • \n
    • \"O\": player 2 symbol
    • \n

    The function should return True if the square was empty and the symbol was successfully placed on the game board. The function should return False if the square was occupied, or if the coordinates weren't valid.

    An example execution of the function:

    game_board = [[\"\", \"\", \"\"], [\"\", \"\", \"\"], [\"\", \"\", \"\"]]\nprint(play_turn(game_board, 2, 0, \"X\"))\nprint(game_board)

    True\n[['', '', 'X'], ['', '', ''], ['', '', '']]

    Please write a function named transpose(matrix: list), which takes a two-dimensional integer array, i.e., a matrix, as its argument. The function should transpose the matrix. Transposing means essentially flipping the matrix over its diagonal: columns become rows, and rows become columns.

    You may assume the matrix is a square matrix, so it will have an equal number of rows and columns.

    The following matrix

    1 2 3\n4 5 6\n7 8 9

    transposed looks like this:

    1 4 7\n2 5 8\n3 6 9

    The function should not have a return value. The matrix should be modified directly through the reference.

    Side effects of functions

    If a function takes a reference to a list as an argument, it will be able to modify that list. If direct modifications were not intended by the programmer, accidentally modifying the list received as a parameter could cause problems elsewhere in the program.

    Let's take a look at a function which is supposed to find the second smallest item in a list:

    def second_smallest(my_list: list) -> int:\n    # in an ordered list, the second smallest item is at index 1\n    my_list.sort()\n    return my_list[1]\n\nnumbers = [1, 4, 2, 5, 3, 6, 4, 7]\nprint(second_smallest(numbers))\nprint(numbers)
    \n2\n[1, 2, 3, 4, 4, 5, 6, 7]\n

    The function does find the second smallest item reliably, but it additionally sorts the list in place, changing the order of the items. If the order is significant elsewhere in the program, calling the function could cause errors. Unintentional modifications to an object accessed through a reference is called a side effect of a function.

    We can avoid the side effect by making a small change to the function:

    def second_smallest(my_list: list) -> int:\n    list_copy = sorted(my_list)\n    return list_copy[1]\n\nnumbers = [1, 4, 2, 5, 3, 6, 4, 7]\nprint(second_smallest(numbers))\nprint(numbers)

    2\n[1, 4, 2, 5, 3, 6, 4, 7]

    The function sorted returns a new, sorted copy of the list, so looking for the second smallest item no longer messes with the order of the original list.

    It is generally considered a good programming practice to avoid causing side effects with functions. Side effects can make it more difficult to verify that the program functions as intended in all situations.

    Functions free of side effects are also called pure functions. Especially when adhering to a functional programming style, this is a common ideal to follow. We will explore this topic further in Advanced Course in Programming, which is the course following this one.

    ","frontmatter":{"path":"/part-5/2-references","title":"References"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-5/2-references.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-5/3-dictionary/page-data.json b/page-data/part-5/3-dictionary/page-data.json index 4decdfca2..02a35ebb9 100644 --- a/page-data/part-5/3-dictionary/page-data.json +++ b/page-data/part-5/3-dictionary/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-5/3-dictionary", - "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 the dictionary data structure"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use a dictionary with different types of keys and values"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to traverse through the contents of a dictionary"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to name some typical use cases for dictionaries"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lists can be handy in many situations, but they are limited by the fact that the items are accessed through indexes; 0, 1, 2, and so forth. If you want to find some item in a list, you will either have to know its index, or, at worst, traverse through the entire list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Another central data structure in Python is the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"dictionary"}]},{"type":"text","value":". In a dictionary, the items are indexed by "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"keys"}]},{"type":"text","value":". Each key maps to a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"value"}]},{"type":"text","value":". The values stored in the dictionary can be accessed and changed using the key."}]},{"type":"element","tagName":"h2","properties":{"id":"using-a-dictionary","style":"position:relative;"},"children":[{"type":"text","value":"Using a dictionary"},{"type":"element","tagName":"a","properties":{"href":"#using-a-dictionary","ariaLabel":"using a dictionary 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 following example shows you how the dictionary data structure works. Here is a simple dictionary from Finnish to English:"}]},{"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_dictionary "},{"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\nmy_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apina\""}]},{"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":"\"monkey\""}]},{"type":"text","value":"\nmy_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banaani\""}]},{"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":"\"banana\""}]},{"type":"text","value":"\nmy_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"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":"\"harpsichord\""}]},{"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":"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":"my_dictionary"},{"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":"my_dictionary"},{"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_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apina\""}]},{"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":"3\n{'apina': 'monkey', 'banaani': 'banana', 'cembalo': 'harpsichord'}\nmonkey"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The notation "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"{}"}]},{"type":"text","value":" creates an empty dictionary, to which we can now add content. Three key-value pairs are added:"},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"apina\""}]},{"type":"text","value":" maps to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"monkey\""}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"banaani\""}]},{"type":"text","value":" maps to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"banana\""}]},{"type":"text","value":", and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"text","value":" maps to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"harpsichord\""}]},{"type":"text","value":". Finally, the number of key-value pairs in the dictionary is printed, along with the entire dictionary, and the value mapped to the key "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"apina\""}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"After defining the dictionary we could also use it with user input:"}]},{"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":"word "},{"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 word: \""}]},{"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":"if"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_dictionary"},{"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":"\"Translation: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" my_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"word"},{"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":"else"}]},{"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":"\"Word not found\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice the use of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" operator above. When used on a variable of type dictionary, it checks whether the first operand is among the keys stored in the dictionary. Given different inputs, this program might print out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"apina"}]},{"type":"text","value":"\nTranslation: monkey"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pöllö"}]},{"type":"text","value":"\nWord not found"}]}]},{"type":"element","tagName":"h2","properties":{"id":"what-can-be-stored-in-a-dictionary","style":"position:relative;"},"children":[{"type":"text","value":"What can be stored in a dictionary?"},{"type":"element","tagName":"a","properties":{"href":"#what-can-be-stored-in-a-dictionary","ariaLabel":"what can be stored in a dictionary 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 data type is called dictionary, but it does not have to contain only strings. For example, in the following dictionary the keys are strings, but the values are integers:"}]},{"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":"results "},{"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":"\nresults"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mary\""}]},{"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":"4"}]},{"type":"text","value":"\nresults"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Alice\""}]},{"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":"5"}]},{"type":"text","value":"\nresults"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Larry\""}]},{"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":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here the keys are integers and the values are 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":"lists "},{"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":"\nlists"},{"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":" "},{"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":"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":" "},{"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":"\nlists"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"42"}]},{"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","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":" "},{"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":"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":" "},{"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":"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":"\nlists"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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","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":" "},{"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":" "},{"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":"h2","properties":{"id":"how-keys-and-values-work","style":"position:relative;"},"children":[{"type":"text","value":"How keys and values work"},{"type":"element","tagName":"a","properties":{"href":"#how-keys-and-values-work","ariaLabel":"how keys and values work 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":"Each key can appear only once in the dictionary. If you add an entry using a key that already exists in the dictionary, the original value mapped to that key is replaced with the new value:"}]},{"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_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"suuri\""}]},{"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":"\"big\""}]},{"type":"text","value":"\nmy_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"suuri\""}]},{"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":"\"large\""}]},{"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_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"suuri\""}]},{"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":"large"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"All keys in a dictionary must be "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"immutable"}]},{"type":"text","value":". So, a list cannot be used as a key, because it can be changed. For example, executing the following code causes an error:"}]},{"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_dictionary"},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"2"}]},{"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":"3"}]},{"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":" "},{"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":"5"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TypeError: unhashable type: 'list'"}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Hash table"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice the word 'unhashable' in the error message above. This is a reference to the inner workings of the dictionary data type. Python stores the contents of a dictionary in a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"hash table"}]},{"type":"text","value":". Each key is reduced to a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"hash value"}]},{"type":"text","value":", which determines where the key is stored in computer memory. The error message above indicates that a list cannot be processed into a hash value, so it cannot be used as a key in a dictionary."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Data Structures and Algorithms"}]},{"type":"text","value":" courses will further explore hash tables."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Unlike keys, the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"values"}]},{"type":"text","value":" stored in a dictionary can change, so any type of data is acceptable as a value. A value can also be mapped to more than one key in the same dictionary."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Times ten","tmcname":"part05-14_times_ten"},"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":"times_ten(start_index: int, end_index: int)"}]},{"type":"text","value":", which creates and returns a new dictionary. The keys of the dictionary should be the numbers between "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"start_index"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"end_index"}]},{"type":"text","value":" inclusive"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The value mapped to each key should be the key times ten."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"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":"d "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" times_ten"},{"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":" "},{"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":"d"},{"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":"{3: 30, 4: 40, 5: 50, 6: 60}"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Factorials","tmcname":"part05-15_factorials"},"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":"factorials(n: int)"}]},{"type":"text","value":", which returns the factorials of the numbers 1 to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" in a dictionary. The number is the key, and the factorial of that number is the value mapped to it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A reminder: the factorial of the number "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" is written "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":"! and is calculated by multiplying the number by each integer smaller than itself. For example, the factorial of 4 is 4 * 3 * 2 * 1 = 24."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" factorials"},{"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"},{"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":"k"},{"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":"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":"k"},{"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","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":"k"},{"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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1\n6\n120"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"traversing-a-dictionary","style":"position:relative;"},"children":[{"type":"text","value":"Traversing a dictionary"},{"type":"element","tagName":"a","properties":{"href":"#traversing-a-dictionary","ariaLabel":"traversing a dictionary 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 familiar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for item in collection"}]},{"type":"text","value":" loop can be used to traverse a dictionary, too. When used on the dictionary directly, the loop goes through the keys stored in the dictionary, one by one. In the following example, all keys and values stored in the dictionary are printed out:"}]},{"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_dictionary "},{"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\nmy_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apina\""}]},{"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":"\"monkey\""}]},{"type":"text","value":"\nmy_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banaani\""}]},{"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":"\"banana\""}]},{"type":"text","value":"\nmy_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"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":"\"harpsichord\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" key "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_dictionary"},{"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":"\"key:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"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":"\"value:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" my_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"key"},{"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":"key: apina\nvalue: monkey\nkey: banaani\nvalue: banana\nkey: cembalo\nvalue: harpsichord"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sometimes you need to traverse the entire contents of a dictionary. The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"items"}]},{"type":"text","value":" returns all the keys and values stored in the dictionary, one pair at a time:"}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" value "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_dictionary"},{"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":"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":"\"key:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"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":"\"value:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the examples above, you may have noticed that the keys are processed in the same order as they were added to the dictionary. As the keys are processed based on a hash value, the order should not usually matter in applications. In fact, in many older versions of Python the order is not guaranteed to follow the time of insertion."}]},{"type":"element","tagName":"h2","properties":{"id":"some-more-advanced-ways-to-use-dictionaries","style":"position:relative;"},"children":[{"type":"text","value":"Some more advanced ways to use dictionaries"},{"type":"element","tagName":"a","properties":{"href":"#some-more-advanced-ways-to-use-dictionaries","ariaLabel":"some more advanced ways to use dictionaries 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":"Let's have a look at a list of words:"}]},{"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":"word_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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banana\""}]},{"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":"\"milk\""}]},{"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":"\"beer\""}]},{"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":"\"cheese\""}]},{"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":"\"sourmilk\""}]},{"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":"\"juice\""}]},{"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":"\"sausage\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tomato\""}]},{"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":"\"cucumber\""}]},{"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":"\"butter\""}]},{"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":"\"margarine\""}]},{"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":"\"cheese\""}]},{"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":"\"sausage\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"beer\""}]},{"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":"\"sourmilk\""}]},{"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":"\"sourmilk\""}]},{"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":"\"butter\""}]},{"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":"\"beer\""}]},{"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":"\"chocolate\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We would like to analyze this list of words in different ways. For instance, we would like to know how many times each word appears in the list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A dictionary can be a useful tool in managing this kind of information. In the example below, we go through the items in the list one by one. Using the words in the list as keys in a new dictionary, the value mapped to each key is the number of times the word has appeared:"}]},{"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":"counts"}]},{"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","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n words "},{"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","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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","comment"]},"children":[{"type":"text","value":"# if the word is not yet in the dictionary, initialize the value to zero"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"word"},{"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":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# increment the value"}]},{"type":"text","value":"\n words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"word"},{"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":"return"}]},{"type":"text","value":" words\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# call the function"}]},{"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":"counts"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"word_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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'banana': 1, 'milk': 1, 'beer': 3, 'cheese': 2, 'sourmilk': 3, 'juice': 1, 'sausage': 2, 'tomato': 1, 'cucumber': 1, 'butter': 2, 'margarine': 1, 'chocolate': 1}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What if we wanted to categorize the words based on the initial letter in each word? One way to accomplish this would be to use dictionaries:"}]},{"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":"categorize_by_initial"}]},{"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","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n groups "},{"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","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n initial "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" word"},{"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":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# initialize a new list when the letter is first encountered"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" initial "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" groups"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n groups"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"initial"},{"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","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":"# add the word to the appropriate list"}]},{"type":"text","value":"\n groups"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"initial"},{"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":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"word"},{"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":" groups\n\ngroups "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" categorize_by_initial"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"word_list"},{"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":" key"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" value "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" groups"},{"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":"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\"words beginning with "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"key"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" 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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The structure of the function is very similar to the previous exercise but this time the values mapped to the keys are lists. The program prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"words beginning with b:\nbanana\nbeer\nbutter\nbeer\nbutter\nbeer\nwords beginning with m:\nmilk\nmargarine\nwords beginning with c:\ncheese\ncucumber\ncheese\nchocolate\nwords beginning with s:\nsourmilk\nsausage\nsausage\nsourmilk\nsourmilk\nwords beginning with j:\njuice\nwords beginning with t:\ntomato"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Histogram","tmcname":"part05-16_histogram"},"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":"histogram"}]},{"type":"text","value":", which takes a string as its argument. The function should print out a histogram representing the number of times each letter occurs in the string. Each occurrence of a letter should be represented by a star on the specific line for that letter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, the function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"histogram(\"abba\")"}]},{"type":"text","value":" should print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"a **\nb **\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"while "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"histogram(\"statistically\")"}]},{"type":"text","value":" should print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"s **\nt ***\na **\ni **\nc *\nl **\ny *\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Phone book, version 1","tmcname":"part05-17_phone_book_v1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a phone book application. It should work as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"peter"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"040-5466745"}]},{"type":"text","value":"\nok!\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"emily"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"045-1212344"}]},{"type":"text","value":"\nok!\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"peter"}]},{"type":"text","value":"\n040-5466745\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"mary"}]},{"type":"text","value":"\nno number\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"peter"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"09-22223333"}]},{"type":"text","value":"\nok!\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"peter"}]},{"type":"text","value":"\n09-22223333\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nquitting..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, each name can be attached to a single number only. If a new entry with the same name is added, the number attached to the old entry is replaced with the new number."}]},{"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 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."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Phone book, version 2","tmcname":"part05-18_phone_book_v2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write an improved version of the phone book application. Each entry should now accommodate multiple phone numbers. The application should work otherwise exactly as above, but this time "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"all"}]},{"type":"text","value":" numbers attached to a name should be printed."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"peter"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"040-5466745"}]},{"type":"text","value":"\nok!\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"emily"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"045-1212344"}]},{"type":"text","value":"\nok!\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"peter"}]},{"type":"text","value":"\n040-5466745\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"mary"}]},{"type":"text","value":"\nno number\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"peter"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"09-22223333"}]},{"type":"text","value":"\nok!\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"peter"}]},{"type":"text","value":"\n040-5466745\n09-22223333\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nquitting..."}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"removing-keys-and-values-from-a-dictionary","style":"position:relative;"},"children":[{"type":"text","value":"Removing keys and values from a dictionary"},{"type":"element","tagName":"a","properties":{"href":"#removing-keys-and-values-from-a-dictionary","ariaLabel":"removing keys and values from a dictionary 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":"It is naturally possible to also remove key-value paris from the dictionary. There are two ways to accomplish this. The first is the command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"del"}]},{"type":"text","value":":"}]},{"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":"staff "},{"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":"\"Alan\""}]},{"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":"\"lecturer\""}]},{"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":"\"Emily\""}]},{"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":"\"professor\""}]},{"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":"\"David\""}]},{"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":"\"lecturer\""}]},{"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":"del"}]},{"type":"text","value":" staff"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"David\""}]},{"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":"staff"},{"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":"{'Alan': 'lecturer', 'Emily': 'professor'}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you try to use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"del"}]},{"type":"text","value":" command to delete a key which doesn't exist in the dictionary, there will be an error:"}]},{"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":"staff "},{"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":"\"Alan\""}]},{"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":"\"lecturer\""}]},{"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":"\"Emily\""}]},{"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":"\"professor\""}]},{"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":"\"David\""}]},{"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":"\"lecturer\""}]},{"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":"del"}]},{"type":"text","value":" staff"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":">>> del staff[\"Paul\"]\nTraceback (most recent call last):\n File \""},{"type":"element","tagName":"stdin","properties":{},"children":[{"type":"text","value":"\", line 1, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nKeyError: 'Paul'\n"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, before deleting a key you should check if it is present in the dictionary:"}]},{"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":"staff "},{"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":"\"Alan\""}]},{"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":"\"lecturer\""}]},{"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":"\"Emily\""}]},{"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":"\"professor\""}]},{"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":"\"David\""}]},{"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":"\"lecturer\""}]},{"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":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Paul\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" staff"},{"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":"del"}]},{"type":"text","value":" staff"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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 "},{"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":"\"Deleted\""}]},{"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":"else"}]},{"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":"\"This person is not a staff member\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The other way to delete entries in a dictionary is the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":":"}]},{"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":"staff "},{"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":"\"Alan\""}]},{"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":"\"lecturer\""}]},{"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":"\"Emily\""}]},{"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":"\"professor\""}]},{"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":"\"David\""}]},{"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":"\"lecturer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\ndeleted "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" staff"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"David\""}]},{"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":"staff"},{"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":"deleted"},{"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":"\"deleted\""}]},{"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":"{'Alan': 'lecturer', 'Emily': 'professor'}\nlecturer deleted"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":" also returns the value from the deleted entry."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"By default, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":" will also cause an error if you try to delete a key which is not present in the dictionary. It is possible to avoid this by giving the method a second argument, which contains a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"default return value"}]},{"type":"text","value":". This value is returned in case the key is not found in the dictionary. The special Python value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":" will work here:"}]},{"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":"staff "},{"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":"\"Alan\""}]},{"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":"\"lecturer\""}]},{"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":"\"Emily\""}]},{"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":"\"professor\""}]},{"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":"\"David\""}]},{"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":"\"lecturer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\ndeleted "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" staff"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"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":"if"}]},{"type":"text","value":" deleted "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"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":"\"This person is not a staff member\""}]},{"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":"else"}]},{"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":"deleted"},{"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":"\"deleted\""}]},{"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":"This person is not a staff member"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: if you need to delete the contents of the entire dictionary, and try to do it with a for loop, like so"}]},{"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":"staff "},{"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":"\"Alan\""}]},{"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":"\"lecturer\""}]},{"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":"\"Emily\""}]},{"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":"\"professor\""}]},{"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":"\"David\""}]},{"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":"\"lecturer\""}]},{"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":" key "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" staff"},{"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":"del"}]},{"type":"text","value":" staff"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"key"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"you will receive an error message:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"RuntimeError: dictionary changed size during iteration"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When traversing a collection with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop, the contents may not change while the loop is in progress."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Luckily, there is a dictionary method for just this purpose:"}]},{"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":"staff"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"clear"},{"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":"Invert a dictionary","tmcname":"part05-19_invert_dictionary"},"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":"invert(dictionary: dict)"}]},{"type":"text","value":", which takes a dictionary as its argument. The dictionary should be inverted in place so that values become keys and keys become values."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of its use:"}]},{"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":"s "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"first\""}]},{"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":"2"}]},{"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":"\"second\""}]},{"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":"3"}]},{"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":"\"third\""}]},{"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":"4"}]},{"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":"\"fourth\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\ninvert"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"s"},{"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":"s"},{"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":"{\"first\": 1, \"second\": 2, \"third\": 3, \"fourth\": 4}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" the principles regarding lists covered "},{"type":"element","tagName":"a","properties":{"href":"/part-5/2-references#using-lists-as-parameters-in-functions"},"children":[{"type":"text","value":"here"}]},{"type":"text","value":" also hold for dictionaries passed as arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you have trouble completing this exercise, the "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":" might help you understand what your code is or isn't doing."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Numbers spelled out","tmcname":"part05-20_numbers_spelled_out"},"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":"dict_of_numbers()"}]},{"type":"text","value":", which returns a new dictionary. The dictionary should have the numbers from 0 to 99 as its keys. The value attached to each key should be the number spelled out in words. Please have a look at the example below:"}]},{"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":"numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" dict_of_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":"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":"numbers"},{"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":"\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":"numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"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":"numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"45"}]},{"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":"numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"99"}]},{"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":"numbers"},{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"two\neleven\nforty-five\nninety-nine\nzero"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: Please don't formulate each spelled out number by hand. Figure out how you can use loops and dictionaries in your solution."}]}]},{"type":"element","tagName":"h2","properties":{"id":"using-dictionaries-for-structured-data","style":"position:relative;"},"children":[{"type":"text","value":"Using dictionaries for structured data"},{"type":"element","tagName":"a","properties":{"href":"#using-dictionaries-for-structured-data","ariaLabel":"using dictionaries for structured data 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":"Dictionaries are very useful for structuring data. The following code will create a dictionary which contains some personal data:"}]},{"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":"person "},{"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":"\"name\""}]},{"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":"\"Pippa 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":"\"height\""}]},{"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":"154"}]},{"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":"\"weight\""}]},{"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":"61"}]},{"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":"\"age:\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"44"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This means that we have here a person named Pippa Python, whose height is 154, weight 61, and age 44. The same information could just as well be stored in 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":"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":"\"Pippa Python\""}]},{"type":"text","value":"\nheight "},{"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":"154"}]},{"type":"text","value":"\nweight "},{"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":"61"}]},{"type":"text","value":"\nage "},{"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":"44"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The advantage of a dictionary is that it is a collection. It collects related data under one variable, so it is easy to access the different components. This same advantage is offered by 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":"person "},{"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":"\"Pippa Python\""}]},{"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":"153"}]},{"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":"61"}]},{"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":"44"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With lists, the programmer will have to remember what is stored at each index in the list. There is nothing to indicate that "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"person[2]"}]},{"type":"text","value":" contains the weight and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"person[3]"}]},{"type":"text","value":" the age of the person. When using a dictionary this problem is avoided, as each bit of data is accessed through a named key."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Assuming we have defined multiple people using the same format, we can access their data in the following manner:"}]},{"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":"person1 "},{"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":"\"name\""}]},{"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":"\"Pippa 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":"\"height\""}]},{"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":"154"}]},{"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":"\"weight\""}]},{"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":"61"}]},{"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":"\"age\""}]},{"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":"44"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nperson2 "},{"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":"\"name\""}]},{"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":"\"Peter Pythons\""}]},{"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":"\"height\""}]},{"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":"174"}]},{"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":"\"weight\""}]},{"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":"103"}]},{"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":"\"age\""}]},{"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":"31"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nperson3 "},{"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":"\"name\""}]},{"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":"\"Pedro 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":"\"height\""}]},{"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":"191"}]},{"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":"\"weight\""}]},{"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":"71"}]},{"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":"\"age\""}]},{"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":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\npeople "},{"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":"text","value":"person1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person3"},{"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":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" people"},{"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":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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\ncombined_height "},{"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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" people"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n combined_height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"height\""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The average height is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" combined_height "},{"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":"people"},{"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":"Pippa Python\nPeter Pythons\nPedro Python\nThe average height is 173.0"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Movie database","tmcname":"part05-21_movie_database"},"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":"add_movie(database: list, name: str, director: str, year: int, runtime: int)"}]},{"type":"text","value":", which adds a new movie object into a movie database."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The database is a list, and each movie object in the list is a dictionary. The dictionary should contain the following keys."}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"name"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"director"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"year"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"runtime"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The values attached to these keys are given as arguments to the function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of its use:"}]},{"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":"database "},{"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":"\nadd_movie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"database"},{"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":"\"Gone with the 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":"\"Victor Pything\""}]},{"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":"2017"}]},{"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":"116"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadd_movie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"database"},{"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":"\"Pythons on a Plane\""}]},{"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":"\"Renny Pytholin\""}]},{"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":"2001"}]},{"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":"94"}]},{"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":"database"},{"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":"[{\"name\": \"Gone with the Python\", \"director\": \"Victor Pything\", \"year\": 2017, \"runtime\": 116}, {\"name\": \"Pythons on a Plane\", \"director\": \"Renny Pytholin\", \"year\": 2001, \"runtime\": 94}]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Find movies","tmcname":"part05-22_find_movies"},"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":"find_movies(database: list, search_term: str)"}]},{"type":"text","value":", which processes the movie database created in the previous exercise. The function should formulate a new list, which contains only the movies whose title includes the word searched for. Capitalisation is irrelevant here. A search for "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ana"}]},{"type":"text","value":" should return a list containing both "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Anaconda"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Management"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of its use:"}]},{"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":"database "},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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":"\"Gone with the 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":"\"director\""}]},{"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":"\"Victor Pything\""}]},{"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":"\"year\""}]},{"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":"2017"}]},{"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":"\"runtime\""}]},{"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":"116"}]},{"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","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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":"\"Pythons on a Plane\""}]},{"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":"\"director\""}]},{"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":"\"Renny Pytholin\""}]},{"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":"\"year\""}]},{"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":"2001"}]},{"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":"\"runtime\""}]},{"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":"94"}]},{"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","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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":"\"Dawn of the Dead Programmers\""}]},{"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":"\"director\""}]},{"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":"\"M. Night 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":"\"year\""}]},{"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":"2011"}]},{"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":"\"runtime\""}]},{"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":"101"}]},{"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_movies "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" find_movies"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"database"},{"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":"\"python\""}]},{"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_movies"},{"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":"[{\"name\": \"Gone with the Python\", \"director\": \"Victor Pything\", \"year\": 2017, \"runtime\": 116}, {\"name\": \"Pythons on a Plane\", \"director\": \"Renny Pytholin\", \"year\": 2001, \"runtime\": 94}]"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"At this point in the course, you can choose to participate in a research study related to learning programming. Participation is voluntary and individual participants cannot be identified from the data gathered in the study. You can freely quit the experiment at any point. "},{"type":"element","tagName":"a","properties":{"href":"https://runestone.academy/ns/books/published/p3pt/index.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Click here to begin the study!"}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will be familiar with the dictionary data structure
    • \n
    • You will be able to use a dictionary with different types of keys and values
    • \n
    • You will know how to traverse through the contents of a dictionary
    • \n
    • You will be able to name some typical use cases for dictionaries
    • \n

    Lists can be handy in many situations, but they are limited by the fact that the items are accessed through indexes; 0, 1, 2, and so forth. If you want to find some item in a list, you will either have to know its index, or, at worst, traverse through the entire list.

    Another central data structure in Python is the dictionary. In a dictionary, the items are indexed by keys. Each key maps to a value. The values stored in the dictionary can be accessed and changed using the key.

    Using a dictionary

    The following example shows you how the dictionary data structure works. Here is a simple dictionary from Finnish to English:

    my_dictionary = {}\n\nmy_dictionary[\"apina\"] = \"monkey\"\nmy_dictionary[\"banaani\"] = \"banana\"\nmy_dictionary[\"cembalo\"] = \"harpsichord\"\n\nprint(len(my_dictionary))\nprint(my_dictionary)\nprint(my_dictionary[\"apina\"])

    3\n{'apina': 'monkey', 'banaani': 'banana', 'cembalo': 'harpsichord'}\nmonkey

    The notation {} creates an empty dictionary, to which we can now add content. Three key-value pairs are added:\"apina\" maps to \"monkey\", \"banaani\" maps to \"banana\", and \"cembalo\" maps to \"harpsichord\". Finally, the number of key-value pairs in the dictionary is printed, along with the entire dictionary, and the value mapped to the key \"apina\".

    After defining the dictionary we could also use it with user input:

    word = input(\"Please type in a word: \")\nif word in my_dictionary:\n    print(\"Translation: \", my_dictionary[word])\nelse:\n    print(\"Word not found\")

    Notice the use of the in operator above. When used on a variable of type dictionary, it checks whether the first operand is among the keys stored in the dictionary. Given different inputs, this program might print out the following:

    Please type in a word: apina\nTranslation: monkey

    Please type in a word: pöllö\nWord not found

    What can be stored in a dictionary?

    The data type is called dictionary, but it does not have to contain only strings. For example, in the following dictionary the keys are strings, but the values are integers:

    results = {}\nresults[\"Mary\"] = 4\nresults[\"Alice\"] = 5\nresults[\"Larry\"] = 2

    Here the keys are integers and the values are lists:

    lists = {}\nlists[5] = [1, 2, 3]\nlists[42] = [5, 4, 5, 4, 5]\nlists[100] = [5, 2, 3]

    How keys and values work

    Each key can appear only once in the dictionary. If you add an entry using a key that already exists in the dictionary, the original value mapped to that key is replaced with the new value:

    my_dictionary[\"suuri\"] = \"big\"\nmy_dictionary[\"suuri\"] = \"large\"\nprint(my_dictionary[\"suuri\"])

    large

    All keys in a dictionary must be immutable. So, a list cannot be used as a key, because it can be changed. For example, executing the following code causes an error:

    my_dictionary[[1, 2, 3]] = 5

    TypeError: unhashable type: 'list'

    Notice the word 'unhashable' in the error message above. This is a reference to the inner workings of the dictionary data type. Python stores the contents of a dictionary in a hash table. Each key is reduced to a hash value, which determines where the key is stored in computer memory. The error message above indicates that a list cannot be processed into a hash value, so it cannot be used as a key in a dictionary.

    The Data Structures and Algorithms courses will further explore hash tables.

    Unlike keys, the values stored in a dictionary can change, so any type of data is acceptable as a value. A value can also be mapped to more than one key in the same dictionary.

    Please write a function named times_ten(start_index: int, end_index: int), which creates and returns a new dictionary. The keys of the dictionary should be the numbers between start_index and end_index inclusive

    The value mapped to each key should be the key times ten.

    For example:

    d = times_ten(3, 6)\nprint(d)

    {3: 30, 4: 40, 5: 50, 6: 60}

    Please write a function named factorials(n: int), which returns the factorials of the numbers 1 to n in a dictionary. The number is the key, and the factorial of that number is the value mapped to it.

    A reminder: the factorial of the number n is written n! and is calculated by multiplying the number by each integer smaller than itself. For example, the factorial of 4 is 4 * 3 * 2 * 1 = 24.

    An example of the function in action:

    k = factorials(5)\nprint(k[1])\nprint(k[3])\nprint(k[5])

    1\n6\n120

    Traversing a dictionary

    The familiar for item in collection loop can be used to traverse a dictionary, too. When used on the dictionary directly, the loop goes through the keys stored in the dictionary, one by one. In the following example, all keys and values stored in the dictionary are printed out:

    my_dictionary = {}\n\nmy_dictionary[\"apina\"] = \"monkey\"\nmy_dictionary[\"banaani\"] = \"banana\"\nmy_dictionary[\"cembalo\"] = \"harpsichord\"\n\nfor key in my_dictionary:\n    print(\"key:\", key)\n    print(\"value:\", my_dictionary[key])

    key: apina\nvalue: monkey\nkey: banaani\nvalue: banana\nkey: cembalo\nvalue: harpsichord

    Sometimes you need to traverse the entire contents of a dictionary. The method items returns all the keys and values stored in the dictionary, one pair at a time:

    \nfor key, value in my_dictionary.items():\n    print(\"key:\", key)\n    print(\"value:\", value)

    In the examples above, you may have noticed that the keys are processed in the same order as they were added to the dictionary. As the keys are processed based on a hash value, the order should not usually matter in applications. In fact, in many older versions of Python the order is not guaranteed to follow the time of insertion.

    Some more advanced ways to use dictionaries

    Let's have a look at a list of words:

    word_list = [\n  \"banana\", \"milk\", \"beer\", \"cheese\", \"sourmilk\", \"juice\", \"sausage\",\n  \"tomato\", \"cucumber\", \"butter\", \"margarine\", \"cheese\", \"sausage\",\n  \"beer\", \"sourmilk\", \"sourmilk\", \"butter\", \"beer\", \"chocolate\"\n]

    We would like to analyze this list of words in different ways. For instance, we would like to know how many times each word appears in the list.

    A dictionary can be a useful tool in managing this kind of information. In the example below, we go through the items in the list one by one. Using the words in the list as keys in a new dictionary, the value mapped to each key is the number of times the word has appeared:

    def counts(my_list):\n    words = {}\n    for word in my_list:\n        # if the word is not yet in the dictionary, initialize the value to zero\n        if word not in words:\n            words[word] = 0\n        # increment the value\n        words[word] += 1\n    return words\n\n# call the function\nprint(counts(word_list))

    The program prints out the following:

    {'banana': 1, 'milk': 1, 'beer': 3, 'cheese': 2, 'sourmilk': 3, 'juice': 1, 'sausage': 2, 'tomato': 1, 'cucumber': 1, 'butter': 2, 'margarine': 1, 'chocolate': 1}

    What if we wanted to categorize the words based on the initial letter in each word? One way to accomplish this would be to use dictionaries:

    def categorize_by_initial(my_list):\n    groups = {}\n    for word in my_list:\n        initial = word[0]\n        # initialize a new list when the letter is first encountered\n        if initial not in groups:\n            groups[initial] = []\n        # add the word to the appropriate list\n        groups[initial].append(word)\n    return groups\n\ngroups = categorize_by_initial(word_list)\n\nfor key, value in groups.items():\n    print(f\"words beginning with {key}:\")\n    for word in value:\n        print(word)

    The structure of the function is very similar to the previous exercise but this time the values mapped to the keys are lists. The program prints out the following:

    words beginning with b:\nbanana\nbeer\nbutter\nbeer\nbutter\nbeer\nwords beginning with m:\nmilk\nmargarine\nwords beginning with c:\ncheese\ncucumber\ncheese\nchocolate\nwords beginning with s:\nsourmilk\nsausage\nsausage\nsourmilk\nsourmilk\nwords beginning with j:\njuice\nwords beginning with t:\ntomato

    Please write a function named histogram, which takes a string as its argument. The function should print out a histogram representing the number of times each letter occurs in the string. Each occurrence of a letter should be represented by a star on the specific line for that letter.

    For example, the function call histogram(\"abba\") should print out

    \na **\nb **\n

    while histogram(\"statistically\") should print out

    \ns **\nt ***\na **\ni **\nc *\nl **\ny *\n

    Please write a phone book application. It should work as follows:

    command (1 search, 2 add, 3 quit): 2\nname: peter\nnumber: 040-5466745\nok!\ncommand (1 search, 2 add, 3 quit): 2\nname: emily\nnumber: 045-1212344\nok!\ncommand (1 search, 2 add, 3 quit): 1\nname: peter\n040-5466745\ncommand (1 search, 2 add, 3 quit): 1\nname: mary\nno number\ncommand (1 search, 2 add, 3 quit): 2\nname: peter\nnumber: 09-22223333\nok!\ncommand (1 search, 2 add, 3 quit): 1\nname: peter\n09-22223333\ncommand (1 search, 2 add, 3 quit): 3\nquitting...

    As you can see above, each name can be attached to a single number only. If a new entry with the same name is added, the number attached to the old entry is replaced with the new number.

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    Please write an improved version of the phone book application. Each entry should now accommodate multiple phone numbers. The application should work otherwise exactly as above, but this time all numbers attached to a name should be printed.

    command (1 search, 2 add, 3 quit): 2\nname: peter\nnumber: 040-5466745\nok!\ncommand (1 search, 2 add, 3 quit): 2\nname: emily\nnumber: 045-1212344\nok!\ncommand (1 search, 2 add, 3 quit): 1\nname: peter\n040-5466745\ncommand (1 search, 2 add, 3 quit): 1\nname: mary\nno number\ncommand (1 search, 2 add, 3 quit): 2\nname: peter\nnumber: 09-22223333\nok!\ncommand (1 search, 2 add, 3 quit): 1\nname: peter\n040-5466745\n09-22223333\ncommand (1 search, 2 add, 3 quit): 3\nquitting...

    Removing keys and values from a dictionary

    It is naturally possible to also remove key-value paris from the dictionary. There are two ways to accomplish this. The first is the command del:

    staff = {\"Alan\": \"lecturer\", \"Emily\": \"professor\", \"David\": \"lecturer\"}\ndel staff[\"David\"]\nprint(staff)

    {'Alan': 'lecturer', 'Emily': 'professor'}

    If you try to use the del command to delete a key which doesn't exist in the dictionary, there will be an error:

    staff = {\"Alan\": \"lecturer\", \"Emily\": \"professor\", \"David\": \"lecturer\"}\ndel staff[\"Paul\"]
    \n>>> del staff[\"Paul\"]\nTraceback (most recent call last):\n  File \"\", line 1, in \nKeyError: 'Paul'\n

    So, before deleting a key you should check if it is present in the dictionary:

    staff = {\"Alan\": \"lecturer\", \"Emily\": \"professor\", \"David\": \"lecturer\"}\nif \"Paul\" in staff:\n  del staff[\"Paul\"]\n  print(\"Deleted\")\nelse:\n  print(\"This person is not a staff member\")

    The other way to delete entries in a dictionary is the method pop:

    staff = {\"Alan\": \"lecturer\", \"Emily\": \"professor\", \"David\": \"lecturer\"}\ndeleted = staff.pop(\"David\")\nprint(staff)\nprint(deleted, \"deleted\")

    {'Alan': 'lecturer', 'Emily': 'professor'}\nlecturer deleted

    As you can see above, pop also returns the value from the deleted entry.

    By default, pop will also cause an error if you try to delete a key which is not present in the dictionary. It is possible to avoid this by giving the method a second argument, which contains a default return value. This value is returned in case the key is not found in the dictionary. The special Python value None will work here:

    staff = {\"Alan\": \"lecturer\", \"Emily\": \"professor\", \"David\": \"lecturer\"}\ndeleted = staff.pop(\"Paul\", None)\nif deleted == None:\n  print(\"This person is not a staff member\")\nelse:\n  print(deleted, \"deleted\")

    This person is not a staff member

    NB: if you need to delete the contents of the entire dictionary, and try to do it with a for loop, like so

    staff = {\"Alan\": \"lecturer\", \"Emily\": \"professor\", \"David\": \"lecturer\"}\nfor key in staff:\n  del staff[key]

    you will receive an error message:

    RuntimeError: dictionary changed size during iteration

    When traversing a collection with a for loop, the contents may not change while the loop is in progress.

    Luckily, there is a dictionary method for just this purpose:

    staff.clear()

    Please write a function named invert(dictionary: dict), which takes a dictionary as its argument. The dictionary should be inverted in place so that values become keys and keys become values.

    An example of its use:

    s = {1: \"first\", 2: \"second\", 3: \"third\", 4: \"fourth\"}\ninvert(s)\nprint(s)

    {\"first\": 1, \"second\": 2, \"third\": 3, \"fourth\": 4}

    NB: the principles regarding lists covered here also hold for dictionaries passed as arguments.

    If you have trouble completing this exercise, the visualisation tool might help you understand what your code is or isn't doing.

    Please write a function named dict_of_numbers(), which returns a new dictionary. The dictionary should have the numbers from 0 to 99 as its keys. The value attached to each key should be the number spelled out in words. Please have a look at the example below:

    numbers = dict_of_numbers()\nprint(numbers[2])\nprint(numbers[11])\nprint(numbers[45])\nprint(numbers[99])\nprint(numbers[0])

    two\neleven\nforty-five\nninety-nine\nzero

    NB: Please don't formulate each spelled out number by hand. Figure out how you can use loops and dictionaries in your solution.

    Using dictionaries for structured data

    Dictionaries are very useful for structuring data. The following code will create a dictionary which contains some personal data:

    person = {\"name\": \"Pippa Python\", \"height\": 154, \"weight\": 61, \"age:\" 44}

    This means that we have here a person named Pippa Python, whose height is 154, weight 61, and age 44. The same information could just as well be stored in variables:

    name = \"Pippa Python\"\nheight = 154\nweight = 61\nage = 44

    The advantage of a dictionary is that it is a collection. It collects related data under one variable, so it is easy to access the different components. This same advantage is offered by a list:

    person = [\"Pippa Python\", 153, 61, 44]

    With lists, the programmer will have to remember what is stored at each index in the list. There is nothing to indicate that person[2] contains the weight and person[3] the age of the person. When using a dictionary this problem is avoided, as each bit of data is accessed through a named key.

    Assuming we have defined multiple people using the same format, we can access their data in the following manner:

    person1 = {\"name\": \"Pippa Python\", \"height\": 154, \"weight\": 61, \"age\": 44}\nperson2 = {\"name\": \"Peter Pythons\", \"height\": 174, \"weight\": 103, \"age\": 31}\nperson3 = {\"name\": \"Pedro Python\", \"height\": 191, \"weight\": 71, \"age\": 14}\n\npeople = [person1, person2, person3]\n\nfor person in people:\n    print(person[\"name\"])\n\ncombined_height = 0\nfor person in people:\n    combined_height += person[\"height\"]\n\nprint(\"The average height is\", combined_height / len(people))

    Pippa Python\nPeter Pythons\nPedro Python\nThe average height is 173.0

    Please write a function named add_movie(database: list, name: str, director: str, year: int, runtime: int), which adds a new movie object into a movie database.

    The database is a list, and each movie object in the list is a dictionary. The dictionary should contain the following keys.

      \n
    • name
    • \n
    • director
    • \n
    • year
    • \n
    • runtime
    • \n

    The values attached to these keys are given as arguments to the function.

    An example of its use:

    database = []\nadd_movie(database, \"Gone with the Python\", \"Victor Pything\", 2017, 116)\nadd_movie(database, \"Pythons on a Plane\", \"Renny Pytholin\", 2001, 94)\nprint(database)

    [{\"name\": \"Gone with the Python\", \"director\": \"Victor Pything\", \"year\": 2017, \"runtime\": 116}, {\"name\": \"Pythons on a Plane\", \"director\": \"Renny Pytholin\", \"year\": 2001, \"runtime\": 94}]

    Please write a function named find_movies(database: list, search_term: str), which processes the movie database created in the previous exercise. The function should formulate a new list, which contains only the movies whose title includes the word searched for. Capitalisation is irrelevant here. A search for ana should return a list containing both Anaconda and Management.

    An example of its use:

    database = [{\"name\": \"Gone with the Python\", \"director\": \"Victor Pything\", \"year\": 2017, \"runtime\": 116},\n{\"name\": \"Pythons on a Plane\", \"director\": \"Renny Pytholin\", \"year\": 2001, \"runtime\": 94},\n{\"name\": \"Dawn of the Dead Programmers\", \"director\": \"M. Night Python\", \"year\": 2011, \"runtime\": 101}]\n\nmy_movies = find_movies(database, \"python\")\nprint(my_movies)

    [{\"name\": \"Gone with the Python\", \"director\": \"Victor Pything\", \"year\": 2017, \"runtime\": 116}, {\"name\": \"Pythons on a Plane\", \"director\": \"Renny Pytholin\", \"year\": 2001, \"runtime\": 94}]

    At this point in the course, you can choose to participate in a research study related to learning programming. Participation is voluntary and individual participants cannot be identified from the data gathered in the study. You can freely quit the experiment at any point. Click here to begin the study!

    ","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-5/3-dictionary.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 the dictionary data structure"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use a dictionary with different types of keys and values"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to traverse through the contents of a dictionary"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to name some typical use cases for dictionaries"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lists can be handy in many situations, but they are limited by the fact that the items are accessed through indexes; 0, 1, 2, and so forth. If you want to find some item in a list, you will either have to know its index, or, at worst, traverse through the entire list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Another central data structure in Python is the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"dictionary"}]},{"type":"text","value":". In a dictionary, the items are indexed by "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"keys"}]},{"type":"text","value":". Each key maps to a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"value"}]},{"type":"text","value":". The values stored in the dictionary can be accessed and changed using the key."}]},{"type":"element","tagName":"h2","properties":{"id":"using-a-dictionary","style":"position:relative;"},"children":[{"type":"text","value":"Using a dictionary"},{"type":"element","tagName":"a","properties":{"href":"#using-a-dictionary","ariaLabel":"using a dictionary 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 following example shows you how the dictionary data structure works. Here is a simple dictionary from Finnish to English:"}]},{"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_dictionary "},{"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\nmy_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apina\""}]},{"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":"\"monkey\""}]},{"type":"text","value":"\nmy_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banaani\""}]},{"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":"\"banana\""}]},{"type":"text","value":"\nmy_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"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":"\"harpsichord\""}]},{"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":"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":"my_dictionary"},{"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":"my_dictionary"},{"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_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apina\""}]},{"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":"3\n{'apina': 'monkey', 'banaani': 'banana', 'cembalo': 'harpsichord'}\nmonkey"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The notation "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"{}"}]},{"type":"text","value":" creates an empty dictionary, to which we can now add content. Three key-value pairs are added:"},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"apina\""}]},{"type":"text","value":" maps to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"monkey\""}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"banaani\""}]},{"type":"text","value":" maps to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"banana\""}]},{"type":"text","value":", and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"type":"text","value":" maps to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"harpsichord\""}]},{"type":"text","value":". Finally, the number of key-value pairs in the dictionary is printed, along with the entire dictionary, and the value mapped to the key "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"apina\""}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"After defining the dictionary we could also use it with user input:"}]},{"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":"word "},{"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 word: \""}]},{"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":"if"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_dictionary"},{"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":"\"Translation: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" my_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"word"},{"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":"else"}]},{"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":"\"Word not found\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice the use of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" operator above. When used on a variable of type dictionary, it checks whether the first operand is among the keys stored in the dictionary. Given different inputs, this program might print out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"apina"}]},{"type":"text","value":"\nTranslation: monkey"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a word: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"pöllö"}]},{"type":"text","value":"\nWord not found"}]}]},{"type":"element","tagName":"h2","properties":{"id":"what-can-be-stored-in-a-dictionary","style":"position:relative;"},"children":[{"type":"text","value":"What can be stored in a dictionary?"},{"type":"element","tagName":"a","properties":{"href":"#what-can-be-stored-in-a-dictionary","ariaLabel":"what can be stored in a dictionary 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 data type is called dictionary, but it does not have to contain only strings. For example, in the following dictionary the keys are strings, but the values are integers:"}]},{"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":"results "},{"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":"\nresults"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mary\""}]},{"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":"4"}]},{"type":"text","value":"\nresults"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Alice\""}]},{"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":"5"}]},{"type":"text","value":"\nresults"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Larry\""}]},{"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":"2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here the keys are integers and the values are 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":"lists "},{"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":"\nlists"},{"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":" "},{"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":"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":" "},{"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":"\nlists"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"42"}]},{"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","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":" "},{"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":"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":" "},{"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":"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":"\nlists"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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","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":" "},{"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":" "},{"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":"h2","properties":{"id":"how-keys-and-values-work","style":"position:relative;"},"children":[{"type":"text","value":"How keys and values work"},{"type":"element","tagName":"a","properties":{"href":"#how-keys-and-values-work","ariaLabel":"how keys and values work 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":"Each key can appear only once in the dictionary. If you add an entry using a key that already exists in the dictionary, the original value mapped to that key is replaced with the new value:"}]},{"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_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"suuri\""}]},{"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":"\"big\""}]},{"type":"text","value":"\nmy_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"suuri\""}]},{"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":"\"large\""}]},{"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_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"suuri\""}]},{"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":"large"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"All keys in a dictionary must be "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"immutable"}]},{"type":"text","value":". So, a list cannot be used as a key, because it can be changed. For example, executing the following code causes an error:"}]},{"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_dictionary"},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"2"}]},{"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":"3"}]},{"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":" "},{"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":"5"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TypeError: unhashable type: 'list'"}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"Hash table"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice the word 'unhashable' in the error message above. This is a reference to the inner workings of the dictionary data type. Python stores the contents of a dictionary in a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"hash table"}]},{"type":"text","value":". Each key is reduced to a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"hash value"}]},{"type":"text","value":", which determines where the key is stored in computer memory. The error message above indicates that a list cannot be processed into a hash value, so it cannot be used as a key in a dictionary."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Data Structures and Algorithms"}]},{"type":"text","value":" courses will further explore hash tables."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Unlike keys, the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"values"}]},{"type":"text","value":" stored in a dictionary can change, so any type of data is acceptable as a value. A value can also be mapped to more than one key in the same dictionary."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Times ten","tmcname":"part05-14_times_ten"},"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":"times_ten(start_index: int, end_index: int)"}]},{"type":"text","value":", which creates and returns a new dictionary. The keys of the dictionary should be the numbers between "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"start_index"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"end_index"}]},{"type":"text","value":" inclusive"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The value mapped to each key should be the key times ten."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example:"}]},{"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":"d "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" times_ten"},{"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":" "},{"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":"d"},{"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":"{3: 30, 4: 40, 5: 50, 6: 60}"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Factorials","tmcname":"part05-15_factorials"},"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":"factorials(n: int)"}]},{"type":"text","value":", which returns the factorials of the numbers 1 to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" in a dictionary. The number is the key, and the factorial of that number is the value mapped to it."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A reminder: the factorial of the number "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" is written "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":"! and is calculated by multiplying the number by each integer smaller than itself. For example, the factorial of 4 is 4 * 3 * 2 * 1 = 24."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" factorials"},{"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"},{"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":"k"},{"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":"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":"k"},{"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","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":"k"},{"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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1\n6\n120"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"traversing-a-dictionary","style":"position:relative;"},"children":[{"type":"text","value":"Traversing a dictionary"},{"type":"element","tagName":"a","properties":{"href":"#traversing-a-dictionary","ariaLabel":"traversing a dictionary 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 familiar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for item in collection"}]},{"type":"text","value":" loop can be used to traverse a dictionary, too. When used on the dictionary directly, the loop goes through the keys stored in the dictionary, one by one. In the following example, all keys and values stored in the dictionary are printed out:"}]},{"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_dictionary "},{"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\nmy_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apina\""}]},{"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":"\"monkey\""}]},{"type":"text","value":"\nmy_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banaani\""}]},{"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":"\"banana\""}]},{"type":"text","value":"\nmy_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"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":"\"harpsichord\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" key "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_dictionary"},{"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":"\"key:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"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":"\"value:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" my_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"key"},{"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":"key: apina\nvalue: monkey\nkey: banaani\nvalue: banana\nkey: cembalo\nvalue: harpsichord"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sometimes you need to traverse the entire contents of a dictionary. The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"items"}]},{"type":"text","value":" returns all the keys and values stored in the dictionary, one pair at a time:"}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" value "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_dictionary"},{"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":"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":"\"key:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"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":"\"value:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the examples above, you may have noticed that the keys are processed in the same order as they were added to the dictionary. As the keys are processed based on a hash value, the order should not usually matter in applications. In fact, in many older versions of Python the order is not guaranteed to follow the time of insertion."}]},{"type":"element","tagName":"h2","properties":{"id":"some-more-advanced-ways-to-use-dictionaries","style":"position:relative;"},"children":[{"type":"text","value":"Some more advanced ways to use dictionaries"},{"type":"element","tagName":"a","properties":{"href":"#some-more-advanced-ways-to-use-dictionaries","ariaLabel":"some more advanced ways to use dictionaries 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":"Let's have a look at a list of words:"}]},{"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":"word_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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banana\""}]},{"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":"\"milk\""}]},{"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":"\"beer\""}]},{"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":"\"cheese\""}]},{"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":"\"sourmilk\""}]},{"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":"\"juice\""}]},{"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":"\"sausage\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"tomato\""}]},{"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":"\"cucumber\""}]},{"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":"\"butter\""}]},{"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":"\"margarine\""}]},{"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":"\"cheese\""}]},{"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":"\"sausage\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"beer\""}]},{"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":"\"sourmilk\""}]},{"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":"\"sourmilk\""}]},{"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":"\"butter\""}]},{"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":"\"beer\""}]},{"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":"\"chocolate\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We would like to analyze this list of words in different ways. For instance, we would like to know how many times each word appears in the list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A dictionary can be a useful tool in managing this kind of information. In the example below, we go through the items in the list one by one. Using the words in the list as keys in a new dictionary, the value mapped to each key is the number of times the word has appeared:"}]},{"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":"counts"}]},{"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","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n words "},{"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","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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","comment"]},"children":[{"type":"text","value":"# if the word is not yet in the dictionary, initialize the value to zero"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"word"},{"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":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# increment the value"}]},{"type":"text","value":"\n words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"word"},{"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":"return"}]},{"type":"text","value":" words\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# call the function"}]},{"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":"counts"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"word_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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"{'banana': 1, 'milk': 1, 'beer': 3, 'cheese': 2, 'sourmilk': 3, 'juice': 1, 'sausage': 2, 'tomato': 1, 'cucumber': 1, 'butter': 2, 'margarine': 1, 'chocolate': 1}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What if we wanted to categorize the words based on the initial letter in each word? One way to accomplish this would be to use dictionaries:"}]},{"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":"categorize_by_initial"}]},{"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","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n groups "},{"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","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n initial "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" word"},{"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":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# initialize a new list when the letter is first encountered"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" initial "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" groups"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n groups"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"initial"},{"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","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":"# add the word to the appropriate list"}]},{"type":"text","value":"\n groups"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"initial"},{"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":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"word"},{"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":" groups\n\ngroups "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" categorize_by_initial"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"word_list"},{"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":" key"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" value "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" groups"},{"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":"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\"words beginning with "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"key"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" 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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The structure of the function is very similar to the previous exercise but this time the values mapped to the keys are lists. The program prints out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"words beginning with b:\nbanana\nbeer\nbutter\nbeer\nbutter\nbeer\nwords beginning with m:\nmilk\nmargarine\nwords beginning with c:\ncheese\ncucumber\ncheese\nchocolate\nwords beginning with s:\nsourmilk\nsausage\nsausage\nsourmilk\nsourmilk\nwords beginning with j:\njuice\nwords beginning with t:\ntomato"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Histogram","tmcname":"part05-16_histogram"},"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":"histogram"}]},{"type":"text","value":", which takes a string as its argument. The function should print out a histogram representing the number of times each letter occurs in the string. Each occurrence of a letter should be represented by a star on the specific line for that letter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, the function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"histogram(\"abba\")"}]},{"type":"text","value":" should print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"a **\nb **\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"while "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"histogram(\"statistically\")"}]},{"type":"text","value":" should print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"s **\nt ***\na **\ni **\nc *\nl **\ny *\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Phone book, version 1","tmcname":"part05-17_phone_book_v1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a phone book application. It should work as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"peter"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"040-5466745"}]},{"type":"text","value":"\nok!\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"emily"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"045-1212344"}]},{"type":"text","value":"\nok!\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"peter"}]},{"type":"text","value":"\n040-5466745\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"mary"}]},{"type":"text","value":"\nno number\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"peter"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"09-22223333"}]},{"type":"text","value":"\nok!\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"peter"}]},{"type":"text","value":"\n09-22223333\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nquitting..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, each name can be attached to a single number only. If a new entry with the same name is added, the number attached to the old entry is replaced with the new number."}]},{"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 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."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Phone book, version 2","tmcname":"part05-18_phone_book_v2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write an improved version of the phone book application. Each entry should now accommodate multiple phone numbers. The application should work otherwise exactly as above, but this time "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"all"}]},{"type":"text","value":" numbers attached to a name should be printed."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"command (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"peter"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"040-5466745"}]},{"type":"text","value":"\nok!\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"emily"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"045-1212344"}]},{"type":"text","value":"\nok!\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"peter"}]},{"type":"text","value":"\n040-5466745\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"mary"}]},{"type":"text","value":"\nno number\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"peter"}]},{"type":"text","value":"\nnumber: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"09-22223333"}]},{"type":"text","value":"\nok!\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nname: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"peter"}]},{"type":"text","value":"\n040-5466745\n09-22223333\ncommand (1 search, 2 add, 3 quit): "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nquitting..."}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"removing-keys-and-values-from-a-dictionary","style":"position:relative;"},"children":[{"type":"text","value":"Removing keys and values from a dictionary"},{"type":"element","tagName":"a","properties":{"href":"#removing-keys-and-values-from-a-dictionary","ariaLabel":"removing keys and values from a dictionary 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":"It is naturally possible to also remove key-value paris from the dictionary. There are two ways to accomplish this. The first is the command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"del"}]},{"type":"text","value":":"}]},{"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":"staff "},{"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":"\"Alan\""}]},{"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":"\"lecturer\""}]},{"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":"\"Emily\""}]},{"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":"\"professor\""}]},{"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":"\"David\""}]},{"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":"\"lecturer\""}]},{"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":"del"}]},{"type":"text","value":" staff"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"David\""}]},{"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":"staff"},{"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":"{'Alan': 'lecturer', 'Emily': 'professor'}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you try to use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"del"}]},{"type":"text","value":" command to delete a key which doesn't exist in the dictionary, there will be an error:"}]},{"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":"staff "},{"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":"\"Alan\""}]},{"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":"\"lecturer\""}]},{"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":"\"Emily\""}]},{"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":"\"professor\""}]},{"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":"\"David\""}]},{"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":"\"lecturer\""}]},{"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":"del"}]},{"type":"text","value":" staff"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":">>> del staff[\"Paul\"]\nTraceback (most recent call last):\n File \""},{"type":"element","tagName":"stdin","properties":{},"children":[{"type":"text","value":"\", line 1, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nKeyError: 'Paul'\n"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, before deleting a key you should check if it is present in the dictionary:"}]},{"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":"staff "},{"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":"\"Alan\""}]},{"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":"\"lecturer\""}]},{"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":"\"Emily\""}]},{"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":"\"professor\""}]},{"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":"\"David\""}]},{"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":"\"lecturer\""}]},{"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":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Paul\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" staff"},{"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":"del"}]},{"type":"text","value":" staff"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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 "},{"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":"\"Deleted\""}]},{"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":"else"}]},{"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":"\"This person is not a staff member\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The other way to delete entries in a dictionary is the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":":"}]},{"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":"staff "},{"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":"\"Alan\""}]},{"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":"\"lecturer\""}]},{"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":"\"Emily\""}]},{"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":"\"professor\""}]},{"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":"\"David\""}]},{"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":"\"lecturer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\ndeleted "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" staff"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"David\""}]},{"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":"staff"},{"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":"deleted"},{"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":"\"deleted\""}]},{"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":"{'Alan': 'lecturer', 'Emily': 'professor'}\nlecturer deleted"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":" also returns the value from the deleted entry."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"By default, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":" will also cause an error if you try to delete a key which is not present in the dictionary. It is possible to avoid this by giving the method a second argument, which contains a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"default return value"}]},{"type":"text","value":". This value is returned in case the key is not found in the dictionary. The special Python value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":" will work here:"}]},{"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":"staff "},{"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":"\"Alan\""}]},{"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":"\"lecturer\""}]},{"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":"\"Emily\""}]},{"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":"\"professor\""}]},{"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":"\"David\""}]},{"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":"\"lecturer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\ndeleted "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" staff"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"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":"if"}]},{"type":"text","value":" deleted "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"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":"\"This person is not a staff member\""}]},{"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":"else"}]},{"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":"deleted"},{"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":"\"deleted\""}]},{"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":"This person is not a staff member"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: if you need to delete the contents of the entire dictionary, and try to do it with a for loop, like so"}]},{"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":"staff "},{"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":"\"Alan\""}]},{"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":"\"lecturer\""}]},{"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":"\"Emily\""}]},{"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":"\"professor\""}]},{"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":"\"David\""}]},{"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":"\"lecturer\""}]},{"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":" key "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" staff"},{"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":"del"}]},{"type":"text","value":" staff"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"key"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"you will receive an error message:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"RuntimeError: dictionary changed size during iteration"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When traversing a collection with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop, the contents may not change while the loop is in progress."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Luckily, there is a dictionary method for just this purpose:"}]},{"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":"staff"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"clear"},{"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":"Invert a dictionary","tmcname":"part05-19_invert_dictionary"},"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":"invert(dictionary: dict)"}]},{"type":"text","value":", which takes a dictionary as its argument. The dictionary should be inverted in place so that values become keys and keys become values."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of its use:"}]},{"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":"s "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"first\""}]},{"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":"2"}]},{"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":"\"second\""}]},{"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":"3"}]},{"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":"\"third\""}]},{"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":"4"}]},{"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":"\"fourth\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\ninvert"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"s"},{"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":"s"},{"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":"{\"first\": 1, \"second\": 2, \"third\": 3, \"fourth\": 4}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" the principles regarding lists covered "},{"type":"element","tagName":"a","properties":{"href":"/part-5/2-references#using-lists-as-parameters-in-functions"},"children":[{"type":"text","value":"here"}]},{"type":"text","value":" also hold for dictionaries passed as arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you have trouble completing this exercise, the "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":" might help you understand what your code is or isn't doing."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Numbers spelled out","tmcname":"part05-20_numbers_spelled_out"},"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":"dict_of_numbers()"}]},{"type":"text","value":", which returns a new dictionary. The dictionary should have the numbers from 0 to 99 as its keys. The value attached to each key should be the number spelled out in words. Please have a look at the example below:"}]},{"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":"numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" dict_of_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":"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":"numbers"},{"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":"\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":"numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"11"}]},{"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":"numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"45"}]},{"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":"numbers"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"99"}]},{"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":"numbers"},{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"two\neleven\nforty-five\nninety-nine\nzero"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: Please don't formulate each spelled out number by hand. Figure out how you can use loops and dictionaries in your solution."}]}]},{"type":"element","tagName":"h2","properties":{"id":"using-dictionaries-for-structured-data","style":"position:relative;"},"children":[{"type":"text","value":"Using dictionaries for structured data"},{"type":"element","tagName":"a","properties":{"href":"#using-dictionaries-for-structured-data","ariaLabel":"using dictionaries for structured data 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":"Dictionaries are very useful for structuring data. The following code will create a dictionary which contains some personal data:"}]},{"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":"person "},{"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":"\"name\""}]},{"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":"\"Pippa 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":"\"height\""}]},{"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":"154"}]},{"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":"\"weight\""}]},{"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":"61"}]},{"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":"\"age:\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"44"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This means that we have here a person named Pippa Python, whose height is 154, weight 61, and age 44. The same information could just as well be stored in 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":"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":"\"Pippa Python\""}]},{"type":"text","value":"\nheight "},{"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":"154"}]},{"type":"text","value":"\nweight "},{"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":"61"}]},{"type":"text","value":"\nage "},{"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":"44"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The advantage of a dictionary is that it is a collection. It collects related data under one variable, so it is easy to access the different components. This same advantage is offered by 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":"person "},{"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":"\"Pippa Python\""}]},{"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":"153"}]},{"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":"61"}]},{"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":"44"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With lists, the programmer will have to remember what is stored at each index in the list. There is nothing to indicate that "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"person[2]"}]},{"type":"text","value":" contains the weight and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"person[3]"}]},{"type":"text","value":" the age of the person. When using a dictionary this problem is avoided, as each bit of data is accessed through a named key."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Assuming we have defined multiple people using the same format, we can access their data in the following manner:"}]},{"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":"person1 "},{"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":"\"name\""}]},{"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":"\"Pippa 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":"\"height\""}]},{"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":"154"}]},{"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":"\"weight\""}]},{"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":"61"}]},{"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":"\"age\""}]},{"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":"44"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nperson2 "},{"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":"\"name\""}]},{"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":"\"Peter Pythons\""}]},{"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":"\"height\""}]},{"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":"174"}]},{"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":"\"weight\""}]},{"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":"103"}]},{"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":"\"age\""}]},{"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":"31"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nperson3 "},{"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":"\"name\""}]},{"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":"\"Pedro 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":"\"height\""}]},{"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":"191"}]},{"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":"\"weight\""}]},{"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":"71"}]},{"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":"\"age\""}]},{"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":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\npeople "},{"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":"text","value":"person1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person3"},{"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":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" people"},{"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":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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\ncombined_height "},{"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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" people"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n combined_height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"height\""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The average height is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" combined_height "},{"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":"people"},{"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":"Pippa Python\nPeter Pythons\nPedro Python\nThe average height is 173.0"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Movie database","tmcname":"part05-21_movie_database"},"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":"add_movie(database: list, name: str, director: str, year: int, runtime: int)"}]},{"type":"text","value":", which adds a new movie object into a movie database."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The database is a list, and each movie object in the list is a dictionary. The dictionary should contain the following keys."}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"name"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"director"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"year"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"runtime"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The values attached to these keys are given as arguments to the function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of its use:"}]},{"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":"database "},{"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":"\nadd_movie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"database"},{"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":"\"Gone with the 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":"\"Victor Pything\""}]},{"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":"2017"}]},{"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":"116"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadd_movie"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"database"},{"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":"\"Pythons on a Plane\""}]},{"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":"\"Renny Pytholin\""}]},{"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":"2001"}]},{"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":"94"}]},{"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":"database"},{"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":"[{\"name\": \"Gone with the Python\", \"director\": \"Victor Pything\", \"year\": 2017, \"runtime\": 116}, {\"name\": \"Pythons on a Plane\", \"director\": \"Renny Pytholin\", \"year\": 2001, \"runtime\": 94}]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Find movies","tmcname":"part05-22_find_movies"},"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":"find_movies(database: list, search_term: str)"}]},{"type":"text","value":", which processes the movie database created in the previous exercise. The function should formulate a new list, which contains only the movies whose title includes the word searched for. Capitalisation is irrelevant here. A search for "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ana"}]},{"type":"text","value":" should return a list containing both "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Anaconda"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Management"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of its use:"}]},{"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":"database "},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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":"\"Gone with the 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":"\"director\""}]},{"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":"\"Victor Pything\""}]},{"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":"\"year\""}]},{"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":"2017"}]},{"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":"\"runtime\""}]},{"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":"116"}]},{"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","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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":"\"Pythons on a Plane\""}]},{"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":"\"director\""}]},{"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":"\"Renny Pytholin\""}]},{"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":"\"year\""}]},{"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":"2001"}]},{"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":"\"runtime\""}]},{"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":"94"}]},{"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","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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":"\"Dawn of the Dead Programmers\""}]},{"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":"\"director\""}]},{"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":"\"M. Night 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":"\"year\""}]},{"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":"2011"}]},{"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":"\"runtime\""}]},{"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":"101"}]},{"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_movies "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" find_movies"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"database"},{"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":"\"python\""}]},{"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_movies"},{"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":"[{\"name\": \"Gone with the Python\", \"director\": \"Victor Pything\", \"year\": 2017, \"runtime\": 116}, {\"name\": \"Pythons on a Plane\", \"director\": \"Renny Pytholin\", \"year\": 2001, \"runtime\": 94}]"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"At this point in the course, you can choose to participate in a research study related to learning programming. Participation is voluntary and individual participants cannot be identified from the data gathered in the study. You can freely quit the experiment at any point. "},{"type":"element","tagName":"a","properties":{"href":"https://runestone.academy/ns/books/published/p3pt/index.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Click here to begin the study!"}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will be familiar with the dictionary data structure
    • \n
    • You will be able to use a dictionary with different types of keys and values
    • \n
    • You will know how to traverse through the contents of a dictionary
    • \n
    • You will be able to name some typical use cases for dictionaries
    • \n

    Lists can be handy in many situations, but they are limited by the fact that the items are accessed through indexes; 0, 1, 2, and so forth. If you want to find some item in a list, you will either have to know its index, or, at worst, traverse through the entire list.

    Another central data structure in Python is the dictionary. In a dictionary, the items are indexed by keys. Each key maps to a value. The values stored in the dictionary can be accessed and changed using the key.

    Using a dictionary

    The following example shows you how the dictionary data structure works. Here is a simple dictionary from Finnish to English:

    my_dictionary = {}\n\nmy_dictionary[\"apina\"] = \"monkey\"\nmy_dictionary[\"banaani\"] = \"banana\"\nmy_dictionary[\"cembalo\"] = \"harpsichord\"\n\nprint(len(my_dictionary))\nprint(my_dictionary)\nprint(my_dictionary[\"apina\"])

    3\n{'apina': 'monkey', 'banaani': 'banana', 'cembalo': 'harpsichord'}\nmonkey

    The notation {} creates an empty dictionary, to which we can now add content. Three key-value pairs are added:\"apina\" maps to \"monkey\", \"banaani\" maps to \"banana\", and \"cembalo\" maps to \"harpsichord\". Finally, the number of key-value pairs in the dictionary is printed, along with the entire dictionary, and the value mapped to the key \"apina\".

    After defining the dictionary we could also use it with user input:

    word = input(\"Please type in a word: \")\nif word in my_dictionary:\n    print(\"Translation: \", my_dictionary[word])\nelse:\n    print(\"Word not found\")

    Notice the use of the in operator above. When used on a variable of type dictionary, it checks whether the first operand is among the keys stored in the dictionary. Given different inputs, this program might print out the following:

    Please type in a word: apina\nTranslation: monkey

    Please type in a word: pöllö\nWord not found

    What can be stored in a dictionary?

    The data type is called dictionary, but it does not have to contain only strings. For example, in the following dictionary the keys are strings, but the values are integers:

    results = {}\nresults[\"Mary\"] = 4\nresults[\"Alice\"] = 5\nresults[\"Larry\"] = 2

    Here the keys are integers and the values are lists:

    lists = {}\nlists[5] = [1, 2, 3]\nlists[42] = [5, 4, 5, 4, 5]\nlists[100] = [5, 2, 3]

    How keys and values work

    Each key can appear only once in the dictionary. If you add an entry using a key that already exists in the dictionary, the original value mapped to that key is replaced with the new value:

    my_dictionary[\"suuri\"] = \"big\"\nmy_dictionary[\"suuri\"] = \"large\"\nprint(my_dictionary[\"suuri\"])

    large

    All keys in a dictionary must be immutable. So, a list cannot be used as a key, because it can be changed. For example, executing the following code causes an error:

    my_dictionary[[1, 2, 3]] = 5

    TypeError: unhashable type: 'list'

    Notice the word 'unhashable' in the error message above. This is a reference to the inner workings of the dictionary data type. Python stores the contents of a dictionary in a hash table. Each key is reduced to a hash value, which determines where the key is stored in computer memory. The error message above indicates that a list cannot be processed into a hash value, so it cannot be used as a key in a dictionary.

    The Data Structures and Algorithms courses will further explore hash tables.

    Unlike keys, the values stored in a dictionary can change, so any type of data is acceptable as a value. A value can also be mapped to more than one key in the same dictionary.

    Please write a function named times_ten(start_index: int, end_index: int), which creates and returns a new dictionary. The keys of the dictionary should be the numbers between start_index and end_index inclusive

    The value mapped to each key should be the key times ten.

    For example:

    d = times_ten(3, 6)\nprint(d)

    {3: 30, 4: 40, 5: 50, 6: 60}

    Please write a function named factorials(n: int), which returns the factorials of the numbers 1 to n in a dictionary. The number is the key, and the factorial of that number is the value mapped to it.

    A reminder: the factorial of the number n is written n! and is calculated by multiplying the number by each integer smaller than itself. For example, the factorial of 4 is 4 * 3 * 2 * 1 = 24.

    An example of the function in action:

    k = factorials(5)\nprint(k[1])\nprint(k[3])\nprint(k[5])

    1\n6\n120

    Traversing a dictionary

    The familiar for item in collection loop can be used to traverse a dictionary, too. When used on the dictionary directly, the loop goes through the keys stored in the dictionary, one by one. In the following example, all keys and values stored in the dictionary are printed out:

    my_dictionary = {}\n\nmy_dictionary[\"apina\"] = \"monkey\"\nmy_dictionary[\"banaani\"] = \"banana\"\nmy_dictionary[\"cembalo\"] = \"harpsichord\"\n\nfor key in my_dictionary:\n    print(\"key:\", key)\n    print(\"value:\", my_dictionary[key])

    key: apina\nvalue: monkey\nkey: banaani\nvalue: banana\nkey: cembalo\nvalue: harpsichord

    Sometimes you need to traverse the entire contents of a dictionary. The method items returns all the keys and values stored in the dictionary, one pair at a time:

    \nfor key, value in my_dictionary.items():\n    print(\"key:\", key)\n    print(\"value:\", value)

    In the examples above, you may have noticed that the keys are processed in the same order as they were added to the dictionary. As the keys are processed based on a hash value, the order should not usually matter in applications. In fact, in many older versions of Python the order is not guaranteed to follow the time of insertion.

    Some more advanced ways to use dictionaries

    Let's have a look at a list of words:

    word_list = [\n  \"banana\", \"milk\", \"beer\", \"cheese\", \"sourmilk\", \"juice\", \"sausage\",\n  \"tomato\", \"cucumber\", \"butter\", \"margarine\", \"cheese\", \"sausage\",\n  \"beer\", \"sourmilk\", \"sourmilk\", \"butter\", \"beer\", \"chocolate\"\n]

    We would like to analyze this list of words in different ways. For instance, we would like to know how many times each word appears in the list.

    A dictionary can be a useful tool in managing this kind of information. In the example below, we go through the items in the list one by one. Using the words in the list as keys in a new dictionary, the value mapped to each key is the number of times the word has appeared:

    def counts(my_list):\n    words = {}\n    for word in my_list:\n        # if the word is not yet in the dictionary, initialize the value to zero\n        if word not in words:\n            words[word] = 0\n        # increment the value\n        words[word] += 1\n    return words\n\n# call the function\nprint(counts(word_list))

    The program prints out the following:

    {'banana': 1, 'milk': 1, 'beer': 3, 'cheese': 2, 'sourmilk': 3, 'juice': 1, 'sausage': 2, 'tomato': 1, 'cucumber': 1, 'butter': 2, 'margarine': 1, 'chocolate': 1}

    What if we wanted to categorize the words based on the initial letter in each word? One way to accomplish this would be to use dictionaries:

    def categorize_by_initial(my_list):\n    groups = {}\n    for word in my_list:\n        initial = word[0]\n        # initialize a new list when the letter is first encountered\n        if initial not in groups:\n            groups[initial] = []\n        # add the word to the appropriate list\n        groups[initial].append(word)\n    return groups\n\ngroups = categorize_by_initial(word_list)\n\nfor key, value in groups.items():\n    print(f\"words beginning with {key}:\")\n    for word in value:\n        print(word)

    The structure of the function is very similar to the previous exercise but this time the values mapped to the keys are lists. The program prints out the following:

    words beginning with b:\nbanana\nbeer\nbutter\nbeer\nbutter\nbeer\nwords beginning with m:\nmilk\nmargarine\nwords beginning with c:\ncheese\ncucumber\ncheese\nchocolate\nwords beginning with s:\nsourmilk\nsausage\nsausage\nsourmilk\nsourmilk\nwords beginning with j:\njuice\nwords beginning with t:\ntomato

    Please write a function named histogram, which takes a string as its argument. The function should print out a histogram representing the number of times each letter occurs in the string. Each occurrence of a letter should be represented by a star on the specific line for that letter.

    For example, the function call histogram(\"abba\") should print out

    \na **\nb **\n

    while histogram(\"statistically\") should print out

    \ns **\nt ***\na **\ni **\nc *\nl **\ny *\n

    Please write a phone book application. It should work as follows:

    command (1 search, 2 add, 3 quit): 2\nname: peter\nnumber: 040-5466745\nok!\ncommand (1 search, 2 add, 3 quit): 2\nname: emily\nnumber: 045-1212344\nok!\ncommand (1 search, 2 add, 3 quit): 1\nname: peter\n040-5466745\ncommand (1 search, 2 add, 3 quit): 1\nname: mary\nno number\ncommand (1 search, 2 add, 3 quit): 2\nname: peter\nnumber: 09-22223333\nok!\ncommand (1 search, 2 add, 3 quit): 1\nname: peter\n09-22223333\ncommand (1 search, 2 add, 3 quit): 3\nquitting...

    As you can see above, each name can be attached to a single number only. If a new entry with the same name is added, the number attached to the old entry is replaced with the new number.

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    Please write an improved version of the phone book application. Each entry should now accommodate multiple phone numbers. The application should work otherwise exactly as above, but this time all numbers attached to a name should be printed.

    command (1 search, 2 add, 3 quit): 2\nname: peter\nnumber: 040-5466745\nok!\ncommand (1 search, 2 add, 3 quit): 2\nname: emily\nnumber: 045-1212344\nok!\ncommand (1 search, 2 add, 3 quit): 1\nname: peter\n040-5466745\ncommand (1 search, 2 add, 3 quit): 1\nname: mary\nno number\ncommand (1 search, 2 add, 3 quit): 2\nname: peter\nnumber: 09-22223333\nok!\ncommand (1 search, 2 add, 3 quit): 1\nname: peter\n040-5466745\n09-22223333\ncommand (1 search, 2 add, 3 quit): 3\nquitting...

    Removing keys and values from a dictionary

    It is naturally possible to also remove key-value paris from the dictionary. There are two ways to accomplish this. The first is the command del:

    staff = {\"Alan\": \"lecturer\", \"Emily\": \"professor\", \"David\": \"lecturer\"}\ndel staff[\"David\"]\nprint(staff)

    {'Alan': 'lecturer', 'Emily': 'professor'}

    If you try to use the del command to delete a key which doesn't exist in the dictionary, there will be an error:

    staff = {\"Alan\": \"lecturer\", \"Emily\": \"professor\", \"David\": \"lecturer\"}\ndel staff[\"Paul\"]
    \n>>> del staff[\"Paul\"]\nTraceback (most recent call last):\n  File \"\", line 1, in \nKeyError: 'Paul'\n

    So, before deleting a key you should check if it is present in the dictionary:

    staff = {\"Alan\": \"lecturer\", \"Emily\": \"professor\", \"David\": \"lecturer\"}\nif \"Paul\" in staff:\n  del staff[\"Paul\"]\n  print(\"Deleted\")\nelse:\n  print(\"This person is not a staff member\")

    The other way to delete entries in a dictionary is the method pop:

    staff = {\"Alan\": \"lecturer\", \"Emily\": \"professor\", \"David\": \"lecturer\"}\ndeleted = staff.pop(\"David\")\nprint(staff)\nprint(deleted, \"deleted\")

    {'Alan': 'lecturer', 'Emily': 'professor'}\nlecturer deleted

    As you can see above, pop also returns the value from the deleted entry.

    By default, pop will also cause an error if you try to delete a key which is not present in the dictionary. It is possible to avoid this by giving the method a second argument, which contains a default return value. This value is returned in case the key is not found in the dictionary. The special Python value None will work here:

    staff = {\"Alan\": \"lecturer\", \"Emily\": \"professor\", \"David\": \"lecturer\"}\ndeleted = staff.pop(\"Paul\", None)\nif deleted == None:\n  print(\"This person is not a staff member\")\nelse:\n  print(deleted, \"deleted\")

    This person is not a staff member

    NB: if you need to delete the contents of the entire dictionary, and try to do it with a for loop, like so

    staff = {\"Alan\": \"lecturer\", \"Emily\": \"professor\", \"David\": \"lecturer\"}\nfor key in staff:\n  del staff[key]

    you will receive an error message:

    RuntimeError: dictionary changed size during iteration

    When traversing a collection with a for loop, the contents may not change while the loop is in progress.

    Luckily, there is a dictionary method for just this purpose:

    staff.clear()

    Please write a function named invert(dictionary: dict), which takes a dictionary as its argument. The dictionary should be inverted in place so that values become keys and keys become values.

    An example of its use:

    s = {1: \"first\", 2: \"second\", 3: \"third\", 4: \"fourth\"}\ninvert(s)\nprint(s)

    {\"first\": 1, \"second\": 2, \"third\": 3, \"fourth\": 4}

    NB: the principles regarding lists covered here also hold for dictionaries passed as arguments.

    If you have trouble completing this exercise, the visualisation tool might help you understand what your code is or isn't doing.

    Please write a function named dict_of_numbers(), which returns a new dictionary. The dictionary should have the numbers from 0 to 99 as its keys. The value attached to each key should be the number spelled out in words. Please have a look at the example below:

    numbers = dict_of_numbers()\nprint(numbers[2])\nprint(numbers[11])\nprint(numbers[45])\nprint(numbers[99])\nprint(numbers[0])

    two\neleven\nforty-five\nninety-nine\nzero

    NB: Please don't formulate each spelled out number by hand. Figure out how you can use loops and dictionaries in your solution.

    Using dictionaries for structured data

    Dictionaries are very useful for structuring data. The following code will create a dictionary which contains some personal data:

    person = {\"name\": \"Pippa Python\", \"height\": 154, \"weight\": 61, \"age:\" 44}

    This means that we have here a person named Pippa Python, whose height is 154, weight 61, and age 44. The same information could just as well be stored in variables:

    name = \"Pippa Python\"\nheight = 154\nweight = 61\nage = 44

    The advantage of a dictionary is that it is a collection. It collects related data under one variable, so it is easy to access the different components. This same advantage is offered by a list:

    person = [\"Pippa Python\", 153, 61, 44]

    With lists, the programmer will have to remember what is stored at each index in the list. There is nothing to indicate that person[2] contains the weight and person[3] the age of the person. When using a dictionary this problem is avoided, as each bit of data is accessed through a named key.

    Assuming we have defined multiple people using the same format, we can access their data in the following manner:

    person1 = {\"name\": \"Pippa Python\", \"height\": 154, \"weight\": 61, \"age\": 44}\nperson2 = {\"name\": \"Peter Pythons\", \"height\": 174, \"weight\": 103, \"age\": 31}\nperson3 = {\"name\": \"Pedro Python\", \"height\": 191, \"weight\": 71, \"age\": 14}\n\npeople = [person1, person2, person3]\n\nfor person in people:\n    print(person[\"name\"])\n\ncombined_height = 0\nfor person in people:\n    combined_height += person[\"height\"]\n\nprint(\"The average height is\", combined_height / len(people))

    Pippa Python\nPeter Pythons\nPedro Python\nThe average height is 173.0

    Please write a function named add_movie(database: list, name: str, director: str, year: int, runtime: int), which adds a new movie object into a movie database.

    The database is a list, and each movie object in the list is a dictionary. The dictionary should contain the following keys.

      \n
    • name
    • \n
    • director
    • \n
    • year
    • \n
    • runtime
    • \n

    The values attached to these keys are given as arguments to the function.

    An example of its use:

    database = []\nadd_movie(database, \"Gone with the Python\", \"Victor Pything\", 2017, 116)\nadd_movie(database, \"Pythons on a Plane\", \"Renny Pytholin\", 2001, 94)\nprint(database)

    [{\"name\": \"Gone with the Python\", \"director\": \"Victor Pything\", \"year\": 2017, \"runtime\": 116}, {\"name\": \"Pythons on a Plane\", \"director\": \"Renny Pytholin\", \"year\": 2001, \"runtime\": 94}]

    Please write a function named find_movies(database: list, search_term: str), which processes the movie database created in the previous exercise. The function should formulate a new list, which contains only the movies whose title includes the word searched for. Capitalisation is irrelevant here. A search for ana should return a list containing both Anaconda and Management.

    An example of its use:

    database = [{\"name\": \"Gone with the Python\", \"director\": \"Victor Pything\", \"year\": 2017, \"runtime\": 116},\n{\"name\": \"Pythons on a Plane\", \"director\": \"Renny Pytholin\", \"year\": 2001, \"runtime\": 94},\n{\"name\": \"Dawn of the Dead Programmers\", \"director\": \"M. Night Python\", \"year\": 2011, \"runtime\": 101}]\n\nmy_movies = find_movies(database, \"python\")\nprint(my_movies)

    [{\"name\": \"Gone with the Python\", \"director\": \"Victor Pything\", \"year\": 2017, \"runtime\": 116}, {\"name\": \"Pythons on a Plane\", \"director\": \"Renny Pytholin\", \"year\": 2001, \"runtime\": 94}]

    At this point in the course, you can choose to participate in a research study related to learning programming. Participation is voluntary and individual participants cannot be identified from the data gathered in the study. You can freely quit the experiment at any point. Click here to begin the study!

    ","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-5/3-dictionary.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-5/4-tuple/page-data.json b/page-data/part-5/4-tuple/page-data.json index a0207e258..11106ba3f 100644 --- a/page-data/part-5/4-tuple/page-data.json +++ b/page-data/part-5/4-tuple/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-5/4-tuple", - "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 the tuple data type"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to create tuples from various types of values"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know the difference between a tuple and a list"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to name some typical use cases for tuples"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tuple is a data structure which is, in many ways, similar to a list. The most important differences between the two are:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Tuples are enclosed in parentheses "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"()"}]},{"type":"text","value":", while lists are enclosed in square brackets "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Tuples are "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"immutable"}]},{"type":"text","value":", while the contents of a list may change"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following bit of code creates a tuple containing the coordinates of a 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":"point "},{"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":"10"}]},{"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":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The items stored in a tuple are accessed by index, just like the items stored in 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":"point "},{"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":"10"}]},{"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":"20"}]},{"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":"\"x coordinate:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" point"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"y coordinate:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" point"},{"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":"x coordinate: 10\ny coordinate: 20"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The values stored in a tuple cannot be changed after the tuple has been defined. The following will "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" 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":"point "},{"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":"10"}]},{"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":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npoint"},{"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":"15"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TypeError: 'tuple' object does not support item assignment"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Create a tuple","tmcname":"part05-23_create_tuple"},"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":"create_tuple(x: int, y: int, z: int)"}]},{"type":"text","value":", which takes three integers as its arguments, and creates and returns a tuple based on the following criteria:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The first element in the tuple is the smallest of the arguments"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The second element in the tuple is the greatest of the arguments"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The third element in the tuple is the sum of the arguments"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of its use:"}]},{"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":"\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","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"create_tuple"},{"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":" "},{"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":" "},{"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":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(-1, 5, 7)"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The oldest person","tmcname":"part05-24_oldest_person"},"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":"oldest_person(people: list)"}]},{"type":"text","value":", which takes a list of tuples as its argument. In each tuple, the first element is the name of a person, and the second element is their year of birth. The function should find the oldest person on the list and return their name."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"p1 "},{"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":"\"Adam\""}]},{"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":"1977"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"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":"\"Ellen\""}]},{"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":"1985"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"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":"\"Mary\""}]},{"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":"1953"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np4 "},{"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":"\"Ernest\""}]},{"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":"1997"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeople "},{"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":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p4"},{"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":"oldest_person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"people"},{"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":"Mary"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Older people","tmcname":"part05-25_older_people"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise we are handling tuples just like the ones described in the previous exercise."}]},{"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":"older_people(people: list, year: int)"}]},{"type":"text","value":", which selects all those people on the list who were born "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"before"}]},{"type":"text","value":" the year given as an argument. The function should return the names of these people in a new list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of its use:"}]},{"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":"p1 "},{"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":"\"Adam\""}]},{"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":"1977"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"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":"\"Ellen\""}]},{"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":"1985"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"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":"\"Mary\""}]},{"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":"1953"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np4 "},{"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":"\"Ernest\""}]},{"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":"1997"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeople "},{"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":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nolder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" older_people"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"people"},{"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":"1979"}]},{"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":"older"},{"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":"[ 'Adam', 'Mary' ]"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"what-is-the-purpose-of-a-tuple","style":"position:relative;"},"children":[{"type":"text","value":"What is the purpose of a tuple?"},{"type":"element","tagName":"a","properties":{"href":"#what-is-the-purpose-of-a-tuple","ariaLabel":"what is the purpose of a tuple 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":"Tuples are ideal for when there is a set collection of values which are in some way connected. For example, when there is a need to handle the x and y coordinates of a point, a tuple is a natural choice, because coordinates will always consist of two 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":"point "},{"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":"10"}]},{"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":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Technically it is of course possible to also use a list to store these:"}]},{"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":"point "},{"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":"10"}]},{"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":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A list is a collection of consecutive items in a certain order. The size of a list may also change. When we are storing the coordinates of a point, we want to store the x and y coordinates specifically, not an arbitrary list containing those values."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Because tuples are immutable, unlike lists, they can be used as keys in a dictionary. The following bit of code creates a dictionary, where the keys are coordinate points:"}]},{"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":"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":"\npoints"},{"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","number"]},"children":[{"type":"text","value":"3"}]},{"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":"5"}]},{"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":" "},{"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":"\"monkey\""}]},{"type":"text","value":"\npoints"},{"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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"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":" "},{"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":"\"banana\""}]},{"type":"text","value":"\npoints"},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"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":" "},{"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":"\"harpsichord\""}]},{"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":"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":"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":" "},{"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","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":"text","value":"\nmonkey\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Attempting a similar dictionary definition using lists would "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" 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":"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":"\npoints"},{"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","number"]},"children":[{"type":"text","value":"3"}]},{"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":"5"}]},{"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":" "},{"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":"\"monkey\""}]},{"type":"text","value":"\npoints"},{"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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"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":" "},{"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":"\"banana\""}]},{"type":"text","value":"\npoints"},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"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":" "},{"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":"\"harpsichord\""}]},{"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":"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":"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":" "},{"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","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":"TypeError: unhashable type: 'list'"}]}]},{"type":"element","tagName":"h2","properties":{"id":"tuples-without-parentheses","style":"position:relative;"},"children":[{"type":"text","value":"Tuples without parentheses"},{"type":"element","tagName":"a","properties":{"href":"#tuples-without-parentheses","ariaLabel":"tuples without parentheses 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 parentheses are not strictly necessary when defining tuples. The following two variable assignments are identical in their results:"}]},{"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":"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"2"}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"numbers "},{"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":"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":"2"}]},{"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":"3"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This means we can also easily return multiple values using tuples. Let's have alook at he following example:"}]},{"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":"minmax"}]},{"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","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","builtin"]},"children":[{"type":"text","value":"min"}]},{"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","punctuation"]},"children":[{"type":"text","value":","}]},{"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":"my_list"},{"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":"33"}]},{"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":"5"}]},{"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":"21"}]},{"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":"7"}]},{"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":"88"}]},{"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":"312"}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nmin_value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" max_value "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" minmax"},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"The smallest item 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":"min_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 item 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":"max_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":"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":"The smallest item is 5 and the greatest item is 312"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This function returns two values in a tuple. The return value is assigned to two variables at once:"}]},{"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":"min_value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" max_value "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" minmax"},{"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":"p","properties":{},"children":[{"type":"text","value":"Using parentheses may make the notation more clear. On the left hand side of the assignment statement we also have a tuple, which contains two variable names. The values contained within the tuple returned by the function are assigned to these two 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","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"min_value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" max_value"},{"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":" minmax"},{"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":"p","properties":{},"children":[{"type":"text","value":"You may remember the dictionary method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"items"}]},{"type":"text","value":" in the previous section. We used it to access all the keys and values stored in a dictionary:"}]},{"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_dictionary "},{"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\nmy_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apina\""}]},{"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":"\"monkey\""}]},{"type":"text","value":"\nmy_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banaani\""}]},{"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":"\"banana\""}]},{"type":"text","value":"\nmy_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"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":"\"harpsichord\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" value "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_dictionary"},{"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":"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":"\"key:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"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":"\"value:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tuples are at work here, too. The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"my_dictionary.items()"}]},{"type":"text","value":" returns each key-value pair as a tuple, where the first item is the key and the second item is the value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Another common use case for tuples is swapping the values of two 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":"text","value":"number1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" number2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" number2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" number1"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The assignment statement above swaps the values stored in the variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number1"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number2"}]},{"type":"text","value":". The result is identical to what is achieved with the following bit of code, using a helper 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":"helper_var "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" number1\nnumber1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" number2\nnumber2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" helper_var"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Student database","tmcname":"part05-26_student_database"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this series of exercises you will create a simple student database. Before diving in, please spend a moment reading through the instructions and thinking about what sort of data structures are necessary for organising the data stored by your program."}]},{"type":"element","tagName":"h4","properties":{"id":"adding-students","style":"position:relative;"},"children":[{"type":"text","value":"adding students"},{"type":"element","tagName":"a","properties":{"href":"#adding-students","ariaLabel":"adding students 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":"First write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_student"}]},{"type":"text","value":", which adds a new student to the database. Also write a preliminary version of the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print_student"}]},{"type":"text","value":", which prints out the information of a single student."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"These function are used as follows:"}]},{"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":"students "},{"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":"\nadd_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadd_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Eliza\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprint_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprint_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Eliza\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprint_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Jack\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your program should now print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Peter:\n no completed courses\nEliza:\n no completed courses\nJack: no such person in the database\n"}]}]},{"type":"element","tagName":"h4","properties":{"id":"adding-completed-courses","style":"position:relative;"},"children":[{"type":"text","value":"adding completed courses"},{"type":"element","tagName":"a","properties":{"href":"#adding-completed-courses","ariaLabel":"adding completed courses 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_course"}]},{"type":"text","value":", which adds a completed course to the information of a specific student in the database. The course data is a tuple consisting of the name of the course and the grade:"}]},{"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":"students "},{"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":"\nadd_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Introduction to Programming\""}]},{"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":"3"}]},{"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":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Advanced Course in Programming\""}]},{"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":"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":"\nprint_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When some courses have been added, the information printed out changes:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Peter:\n 2 completed courses:\n Introduction to Programming 3\n Advanced Course in Programming 2\n average grade 2.5\n"}]}]},{"type":"element","tagName":"h4","properties":{"id":"repeating-courses","style":"position:relative;"},"children":[{"type":"text","value":"repeating courses"},{"type":"element","tagName":"a","properties":{"href":"#repeating-courses","ariaLabel":"repeating courses 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":"Courses with grade 0 should be ignored when adding course information. Additionally, if the course is already in the database in that specific student's information, the grade recorded in the database should never be lowered if the course is repeated."}]},{"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":"students "},{"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":"\nadd_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Introduction to Programming\""}]},{"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":"3"}]},{"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":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Advanced Course in Programming\""}]},{"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":"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":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Data Structures and Algorithms\""}]},{"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":"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":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Introduction to Programming\""}]},{"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":"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":"\nprint_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Peter:\n 2 completed courses:\n Introduction to Programming 3\n Advanced Course in Programming 2\n average grade 2.5\n"}]}]},{"type":"element","tagName":"h4","properties":{"id":"summary-of-database","style":"position:relative;"},"children":[{"type":"text","value":"summary of database"},{"type":"element","tagName":"a","properties":{"href":"#summary-of-database","ariaLabel":"summary of database 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"summary"}]},{"type":"text","value":", which prints out a summary based on all the information stored in the database."}]},{"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":"students "},{"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":"\nadd_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadd_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Eliza\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Data Structures and Algorithms\""}]},{"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":"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":"text","value":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Introduction to Programming\""}]},{"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":"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":"text","value":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Advanced Course in Programming\""}]},{"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":"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":"text","value":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Eliza\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Introduction to Programming\""}]},{"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":"5"}]},{"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":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Eliza\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Introduction to Computer Science\""}]},{"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":"4"}]},{"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":"\nsummary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This should print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"students 2\nmost courses completed 3 Peter\nbest average grade 4.5 Eliza\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"A square of letters","tmcname":"part05-27_letter_square"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This final exercise in this part is a relatively demanding problem solving task. It can be solved in many different ways. Even though this current section in the material covers tuples, tuples are not necessarily the best way to go about solving this."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which prints out a square of letters as specified in the examples below. You may assume there will be at most 26 layers."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Layers: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"CCCCC\nCBBBC\nCBABC\nCBBBC\nCCCCC\n"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Layers: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"DDDDDDD\nDCCCCCD\nDCBBBCD\nDCBABCD\nDCBBBCD\nDCCCCCD\nDDDDDDD\n"}]}]},{"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 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."}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this week's materials."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"28af7349-2997-5bb7-bc45-a9defb93914e"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will be familiar with the tuple data type
    • \n
    • You will be able to create tuples from various types of values
    • \n
    • You will know the difference between a tuple and a list
    • \n
    • You will be able to name some typical use cases for tuples
    • \n

    Tuple is a data structure which is, in many ways, similar to a list. The most important differences between the two are:

      \n
    • Tuples are enclosed in parentheses (), while lists are enclosed in square brackets []
    • \n
    • Tuples are immutable, while the contents of a list may change
    • \n

    The following bit of code creates a tuple containing the coordinates of a point:

    point = (10, 20)

    The items stored in a tuple are accessed by index, just like the items stored in a list:

    point = (10, 20)\nprint(\"x coordinate:\", point[0])\nprint(\"y coordinate:\", point[1])

    x coordinate: 10\ny coordinate: 20

    The values stored in a tuple cannot be changed after the tuple has been defined. The following will not work:

    point = (10, 20)\npoint[0] = 15

    TypeError: 'tuple' object does not support item assignment

    Please write a function named create_tuple(x: int, y: int, z: int), which takes three integers as its arguments, and creates and returns a tuple based on the following criteria:

      \n
    1. The first element in the tuple is the smallest of the arguments
    2. \n
    3. The second element in the tuple is the greatest of the arguments
    4. \n
    5. The third element in the tuple is the sum of the arguments
    6. \n

    An example of its use:

    \nif __name__ == \"__main__\":\n    print(create_tuple(5, 3, -1))\n

    (-1, 5, 7)

    Please write a function named oldest_person(people: list), which takes a list of tuples as its argument. In each tuple, the first element is the name of a person, and the second element is their year of birth. The function should find the oldest person on the list and return their name.

    An example of the function in action:

    p1 = (\"Adam\", 1977)\np2 = (\"Ellen\", 1985)\np3 = (\"Mary\", 1953)\np4 = (\"Ernest\", 1997)\npeople = [p1, p2, p3, p4]\n\nprint(oldest_person(people))

    Mary

    In this exercise we are handling tuples just like the ones described in the previous exercise.

    Please write a function named older_people(people: list, year: int), which selects all those people on the list who were born before the year given as an argument. The function should return the names of these people in a new list.

    An example of its use:

    p1 = (\"Adam\", 1977)\np2 = (\"Ellen\", 1985)\np3 = (\"Mary\", 1953)\np4 = (\"Ernest\", 1997)\npeople = [p1, p2, p3, p4]\n\nolder = older_people(people, 1979)\nprint(older)

    [ 'Adam', 'Mary' ]

    What is the purpose of a tuple?

    Tuples are ideal for when there is a set collection of values which are in some way connected. For example, when there is a need to handle the x and y coordinates of a point, a tuple is a natural choice, because coordinates will always consist of two values:

    point = (10, 20)

    Technically it is of course possible to also use a list to store these:

    point = [10, 20]

    A list is a collection of consecutive items in a certain order. The size of a list may also change. When we are storing the coordinates of a point, we want to store the x and y coordinates specifically, not an arbitrary list containing those values.

    Because tuples are immutable, unlike lists, they can be used as keys in a dictionary. The following bit of code creates a dictionary, where the keys are coordinate points:

    points = {}\npoints[(3, 5)] = \"monkey\"\npoints[(5, 0)] = \"banana\"\npoints[(1, 2)] = \"harpsichord\"\nprint(points[(3, 5)])
    \nmonkey\n

    Attempting a similar dictionary definition using lists would not work:

    points = {}\npoints[[3, 5]] = \"monkey\"\npoints[[5, 0]] = \"banana\"\npoints[[1, 2]] = \"harpsichord\"\nprint(points[[3, 5]])

    TypeError: unhashable type: 'list'

    Tuples without parentheses

    The parentheses are not strictly necessary when defining tuples. The following two variable assignments are identical in their results:

    numbers = (1, 2, 3)
    numbers = 1, 2, 3

    This means we can also easily return multiple values using tuples. Let's have alook at he following example:

    def minmax(my_list):\n  return min(my_list), max(my_list)\n\nmy_list = [33, 5, 21, 7, 88, 312, 5]\n\nmin_value, max_value = minmax(my_list)\nprint(f\"The smallest item is {min_value} and the greatest item is {max_value}\")

    The smallest item is 5 and the greatest item is 312

    This function returns two values in a tuple. The return value is assigned to two variables at once:

    min_value, max_value = minmax(my_list)

    Using parentheses may make the notation more clear. On the left hand side of the assignment statement we also have a tuple, which contains two variable names. The values contained within the tuple returned by the function are assigned to these two variables.

    (min_value, max_value) = minmax(my_list)

    You may remember the dictionary method items in the previous section. We used it to access all the keys and values stored in a dictionary:

    my_dictionary = {}\n\nmy_dictionary[\"apina\"] = \"monkey\"\nmy_dictionary[\"banaani\"] = \"banana\"\nmy_dictionary[\"cembalo\"] = \"harpsichord\"\n\nfor key, value in my_dictionary.items():\n    print(\"key:\", key)\n    print(\"value:\", value)

    Tuples are at work here, too. The method my_dictionary.items() returns each key-value pair as a tuple, where the first item is the key and the second item is the value.

    Another common use case for tuples is swapping the values of two variables:

    number1, number2 = number2, number1

    The assignment statement above swaps the values stored in the variables number1 and number2. The result is identical to what is achieved with the following bit of code, using a helper variable:

    helper_var = number1\nnumber1 = number2\nnumber2 = helper_var

    In this series of exercises you will create a simple student database. Before diving in, please spend a moment reading through the instructions and thinking about what sort of data structures are necessary for organising the data stored by your program.

    adding students

    First write a function named add_student, which adds a new student to the database. Also write a preliminary version of the function print_student, which prints out the information of a single student.

    These function are used as follows:

    students = {}\nadd_student(students, \"Peter\")\nadd_student(students, \"Eliza\")\nprint_student(students, \"Peter\")\nprint_student(students, \"Eliza\")\nprint_student(students, \"Jack\")

    Your program should now print out

    \nPeter:\n no completed courses\nEliza:\n no completed courses\nJack: no such person in the database\n

    adding completed courses

    Please write a function named add_course, which adds a completed course to the information of a specific student in the database. The course data is a tuple consisting of the name of the course and the grade:

    students = {}\nadd_student(students, \"Peter\")\nadd_course(students, \"Peter\", (\"Introduction to Programming\", 3))\nadd_course(students, \"Peter\", (\"Advanced Course in Programming\", 2))\nprint_student(students, \"Peter\")

    When some courses have been added, the information printed out changes:

    \nPeter:\n 2 completed courses:\n  Introduction to Programming 3\n  Advanced Course in Programming 2\n average grade 2.5\n

    repeating courses

    Courses with grade 0 should be ignored when adding course information. Additionally, if the course is already in the database in that specific student's information, the grade recorded in the database should never be lowered if the course is repeated.

    students = {}\nadd_student(students, \"Peter\")\nadd_course(students, \"Peter\", (\"Introduction to Programming\", 3))\nadd_course(students, \"Peter\", (\"Advanced Course in Programming\", 2))\nadd_course(students, \"Peter\", (\"Data Structures and Algorithms\", 0))\nadd_course(students, \"Peter\", (\"Introduction to Programming\", 2))\nprint_student(students, \"Peter\")
    \nPeter:\n 2 completed courses:\n  Introduction to Programming 3\n  Advanced Course in Programming 2\n average grade 2.5\n

    summary of database

    Please write a function named summary, which prints out a summary based on all the information stored in the database.

    students = {}\nadd_student(students, \"Peter\")\nadd_student(students, \"Eliza\")\nadd_course(students, \"Peter\", (\"Data Structures and Algorithms\", 1))\nadd_course(students, \"Peter\", (\"Introduction to Programming\", 1))\nadd_course(students, \"Peter\", (\"Advanced Course in Programming\", 1))\nadd_course(students, \"Eliza\", (\"Introduction to Programming\", 5))\nadd_course(students, \"Eliza\", (\"Introduction to Computer Science\", 4))\nsummary(students)

    This should print out

    \nstudents 2\nmost courses completed 3 Peter\nbest average grade 4.5 Eliza\n

    This final exercise in this part is a relatively demanding problem solving task. It can be solved in many different ways. Even though this current section in the material covers tuples, tuples are not necessarily the best way to go about solving this.

    Please write a program which prints out a square of letters as specified in the examples below. You may assume there will be at most 26 layers.

    Layers: 3

    \nCCCCC\nCBBBC\nCBABC\nCBBBC\nCCCCC\n

    Layers: 4

    \nDDDDDDD\nDCCCCCD\nDCBBBCD\nDCBABCD\nDCBBBCD\nDCCCCCD\nDDDDDDD\n

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    Please respond to a quick questionnaire on this week's materials.

    ","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-5/4-tuple.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 the tuple data type"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to create tuples from various types of values"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know the difference between a tuple and a list"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to name some typical use cases for tuples"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tuple is a data structure which is, in many ways, similar to a list. The most important differences between the two are:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Tuples are enclosed in parentheses "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"()"}]},{"type":"text","value":", while lists are enclosed in square brackets "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[]"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Tuples are "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"immutable"}]},{"type":"text","value":", while the contents of a list may change"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following bit of code creates a tuple containing the coordinates of a 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":"point "},{"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":"10"}]},{"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":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The items stored in a tuple are accessed by index, just like the items stored in 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":"point "},{"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":"10"}]},{"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":"20"}]},{"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":"\"x coordinate:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" point"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"y coordinate:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" point"},{"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":"x coordinate: 10\ny coordinate: 20"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The values stored in a tuple cannot be changed after the tuple has been defined. The following will "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" 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":"point "},{"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":"10"}]},{"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":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npoint"},{"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":"15"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"TypeError: 'tuple' object does not support item assignment"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Create a tuple","tmcname":"part05-23_create_tuple"},"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":"create_tuple(x: int, y: int, z: int)"}]},{"type":"text","value":", which takes three integers as its arguments, and creates and returns a tuple based on the following criteria:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The first element in the tuple is the smallest of the arguments"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The second element in the tuple is the greatest of the arguments"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The third element in the tuple is the sum of the arguments"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of its use:"}]},{"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":"\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","keyword"]},"children":[{"type":"text","value":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"create_tuple"},{"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":" "},{"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":" "},{"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":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(-1, 5, 7)"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The oldest person","tmcname":"part05-24_oldest_person"},"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":"oldest_person(people: list)"}]},{"type":"text","value":", which takes a list of tuples as its argument. In each tuple, the first element is the name of a person, and the second element is their year of birth. The function should find the oldest person on the list and return their name."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"p1 "},{"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":"\"Adam\""}]},{"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":"1977"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"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":"\"Ellen\""}]},{"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":"1985"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"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":"\"Mary\""}]},{"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":"1953"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np4 "},{"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":"\"Ernest\""}]},{"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":"1997"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeople "},{"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":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p4"},{"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":"oldest_person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"people"},{"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":"Mary"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Older people","tmcname":"part05-25_older_people"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise we are handling tuples just like the ones described in the previous exercise."}]},{"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":"older_people(people: list, year: int)"}]},{"type":"text","value":", which selects all those people on the list who were born "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"before"}]},{"type":"text","value":" the year given as an argument. The function should return the names of these people in a new list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of its use:"}]},{"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":"p1 "},{"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":"\"Adam\""}]},{"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":"1977"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np2 "},{"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":"\"Ellen\""}]},{"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":"1985"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np3 "},{"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":"\"Mary\""}]},{"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":"1953"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\np4 "},{"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":"\"Ernest\""}]},{"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":"1997"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeople "},{"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":"text","value":"p1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nolder "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" older_people"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"people"},{"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":"1979"}]},{"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":"older"},{"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":"[ 'Adam', 'Mary' ]"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"what-is-the-purpose-of-a-tuple","style":"position:relative;"},"children":[{"type":"text","value":"What is the purpose of a tuple?"},{"type":"element","tagName":"a","properties":{"href":"#what-is-the-purpose-of-a-tuple","ariaLabel":"what is the purpose of a tuple 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":"Tuples are ideal for when there is a set collection of values which are in some way connected. For example, when there is a need to handle the x and y coordinates of a point, a tuple is a natural choice, because coordinates will always consist of two 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":"point "},{"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":"10"}]},{"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":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Technically it is of course possible to also use a list to store these:"}]},{"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":"point "},{"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":"10"}]},{"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":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A list is a collection of consecutive items in a certain order. The size of a list may also change. When we are storing the coordinates of a point, we want to store the x and y coordinates specifically, not an arbitrary list containing those values."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Because tuples are immutable, unlike lists, they can be used as keys in a dictionary. The following bit of code creates a dictionary, where the keys are coordinate points:"}]},{"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":"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":"\npoints"},{"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","number"]},"children":[{"type":"text","value":"3"}]},{"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":"5"}]},{"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":" "},{"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":"\"monkey\""}]},{"type":"text","value":"\npoints"},{"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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"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":" "},{"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":"\"banana\""}]},{"type":"text","value":"\npoints"},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"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":" "},{"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":"\"harpsichord\""}]},{"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":"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":"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":" "},{"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","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":"text","value":"\nmonkey\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Attempting a similar dictionary definition using lists would "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" 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":"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":"\npoints"},{"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","number"]},"children":[{"type":"text","value":"3"}]},{"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":"5"}]},{"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":" "},{"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":"\"monkey\""}]},{"type":"text","value":"\npoints"},{"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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"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":" "},{"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":"\"banana\""}]},{"type":"text","value":"\npoints"},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"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":" "},{"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":"\"harpsichord\""}]},{"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":"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":"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":" "},{"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","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":"TypeError: unhashable type: 'list'"}]}]},{"type":"element","tagName":"h2","properties":{"id":"tuples-without-parentheses","style":"position:relative;"},"children":[{"type":"text","value":"Tuples without parentheses"},{"type":"element","tagName":"a","properties":{"href":"#tuples-without-parentheses","ariaLabel":"tuples without parentheses 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 parentheses are not strictly necessary when defining tuples. The following two variable assignments are identical in their results:"}]},{"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":"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"2"}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"numbers "},{"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":"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":"2"}]},{"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":"3"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This means we can also easily return multiple values using tuples. Let's have alook at he following example:"}]},{"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":"minmax"}]},{"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","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","builtin"]},"children":[{"type":"text","value":"min"}]},{"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","punctuation"]},"children":[{"type":"text","value":","}]},{"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":"my_list"},{"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":"33"}]},{"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":"5"}]},{"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":"21"}]},{"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":"7"}]},{"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":"88"}]},{"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":"312"}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n\nmin_value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" max_value "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" minmax"},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"The smallest item 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":"min_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 item 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":"max_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":"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":"The smallest item is 5 and the greatest item is 312"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This function returns two values in a tuple. The return value is assigned to two variables at once:"}]},{"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":"min_value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" max_value "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" minmax"},{"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":"p","properties":{},"children":[{"type":"text","value":"Using parentheses may make the notation more clear. On the left hand side of the assignment statement we also have a tuple, which contains two variable names. The values contained within the tuple returned by the function are assigned to these two 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","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"min_value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" max_value"},{"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":" minmax"},{"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":"p","properties":{},"children":[{"type":"text","value":"You may remember the dictionary method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"items"}]},{"type":"text","value":" in the previous section. We used it to access all the keys and values stored in a dictionary:"}]},{"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_dictionary "},{"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\nmy_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apina\""}]},{"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":"\"monkey\""}]},{"type":"text","value":"\nmy_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"banaani\""}]},{"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":"\"banana\""}]},{"type":"text","value":"\nmy_dictionary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cembalo\""}]},{"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":"\"harpsichord\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" key"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" value "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_dictionary"},{"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":"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":"\"key:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" key"},{"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":"\"value:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" value"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Tuples are at work here, too. The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"my_dictionary.items()"}]},{"type":"text","value":" returns each key-value pair as a tuple, where the first item is the key and the second item is the value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Another common use case for tuples is swapping the values of two 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":"text","value":"number1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" number2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" number2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" number1"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The assignment statement above swaps the values stored in the variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number1"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number2"}]},{"type":"text","value":". The result is identical to what is achieved with the following bit of code, using a helper 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":"helper_var "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" number1\nnumber1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" number2\nnumber2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" helper_var"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Student database","tmcname":"part05-26_student_database"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this series of exercises you will create a simple student database. Before diving in, please spend a moment reading through the instructions and thinking about what sort of data structures are necessary for organising the data stored by your program."}]},{"type":"element","tagName":"h4","properties":{"id":"adding-students","style":"position:relative;"},"children":[{"type":"text","value":"adding students"},{"type":"element","tagName":"a","properties":{"href":"#adding-students","ariaLabel":"adding students 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":"First write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_student"}]},{"type":"text","value":", which adds a new student to the database. Also write a preliminary version of the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print_student"}]},{"type":"text","value":", which prints out the information of a single student."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"These function are used as follows:"}]},{"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":"students "},{"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":"\nadd_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadd_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Eliza\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprint_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprint_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Eliza\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprint_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Jack\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your program should now print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Peter:\n no completed courses\nEliza:\n no completed courses\nJack: no such person in the database\n"}]}]},{"type":"element","tagName":"h4","properties":{"id":"adding-completed-courses","style":"position:relative;"},"children":[{"type":"text","value":"adding completed courses"},{"type":"element","tagName":"a","properties":{"href":"#adding-completed-courses","ariaLabel":"adding completed courses 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_course"}]},{"type":"text","value":", which adds a completed course to the information of a specific student in the database. The course data is a tuple consisting of the name of the course and the grade:"}]},{"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":"students "},{"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":"\nadd_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Introduction to Programming\""}]},{"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":"3"}]},{"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":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Advanced Course in Programming\""}]},{"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":"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":"\nprint_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When some courses have been added, the information printed out changes:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Peter:\n 2 completed courses:\n Introduction to Programming 3\n Advanced Course in Programming 2\n average grade 2.5\n"}]}]},{"type":"element","tagName":"h4","properties":{"id":"repeating-courses","style":"position:relative;"},"children":[{"type":"text","value":"repeating courses"},{"type":"element","tagName":"a","properties":{"href":"#repeating-courses","ariaLabel":"repeating courses 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":"Courses with grade 0 should be ignored when adding course information. Additionally, if the course is already in the database in that specific student's information, the grade recorded in the database should never be lowered if the course is repeated."}]},{"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":"students "},{"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":"\nadd_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Introduction to Programming\""}]},{"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":"3"}]},{"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":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Advanced Course in Programming\""}]},{"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":"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":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Data Structures and Algorithms\""}]},{"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":"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":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Introduction to Programming\""}]},{"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":"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":"\nprint_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Peter:\n 2 completed courses:\n Introduction to Programming 3\n Advanced Course in Programming 2\n average grade 2.5\n"}]}]},{"type":"element","tagName":"h4","properties":{"id":"summary-of-database","style":"position:relative;"},"children":[{"type":"text","value":"summary of database"},{"type":"element","tagName":"a","properties":{"href":"#summary-of-database","ariaLabel":"summary of database 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"summary"}]},{"type":"text","value":", which prints out a summary based on all the information stored in the database."}]},{"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":"students "},{"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":"\nadd_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadd_student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Eliza\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Data Structures and Algorithms\""}]},{"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":"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":"text","value":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Introduction to Programming\""}]},{"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":"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":"text","value":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Advanced Course in Programming\""}]},{"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":"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":"text","value":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Eliza\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Introduction to Programming\""}]},{"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":"5"}]},{"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":"\nadd_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"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":"\"Eliza\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Introduction to Computer Science\""}]},{"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":"4"}]},{"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":"\nsummary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This should print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"students 2\nmost courses completed 3 Peter\nbest average grade 4.5 Eliza\n"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"A square of letters","tmcname":"part05-27_letter_square"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This final exercise in this part is a relatively demanding problem solving task. It can be solved in many different ways. Even though this current section in the material covers tuples, tuples are not necessarily the best way to go about solving this."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which prints out a square of letters as specified in the examples below. You may assume there will be at most 26 layers."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Layers: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"CCCCC\nCBBBC\nCBABC\nCBBBC\nCCCCC\n"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Layers: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"DDDDDDD\nDCCCCCD\nDCBBBCD\nDCBABCD\nDCBBBCD\nDCCCCCD\nDDDDDDD\n"}]}]},{"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 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."}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this week's materials."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"28af7349-2997-5bb7-bc45-a9defb93914e"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will be familiar with the tuple data type
    • \n
    • You will be able to create tuples from various types of values
    • \n
    • You will know the difference between a tuple and a list
    • \n
    • You will be able to name some typical use cases for tuples
    • \n

    Tuple is a data structure which is, in many ways, similar to a list. The most important differences between the two are:

      \n
    • Tuples are enclosed in parentheses (), while lists are enclosed in square brackets []
    • \n
    • Tuples are immutable, while the contents of a list may change
    • \n

    The following bit of code creates a tuple containing the coordinates of a point:

    point = (10, 20)

    The items stored in a tuple are accessed by index, just like the items stored in a list:

    point = (10, 20)\nprint(\"x coordinate:\", point[0])\nprint(\"y coordinate:\", point[1])

    x coordinate: 10\ny coordinate: 20

    The values stored in a tuple cannot be changed after the tuple has been defined. The following will not work:

    point = (10, 20)\npoint[0] = 15

    TypeError: 'tuple' object does not support item assignment

    Please write a function named create_tuple(x: int, y: int, z: int), which takes three integers as its arguments, and creates and returns a tuple based on the following criteria:

      \n
    1. The first element in the tuple is the smallest of the arguments
    2. \n
    3. The second element in the tuple is the greatest of the arguments
    4. \n
    5. The third element in the tuple is the sum of the arguments
    6. \n

    An example of its use:

    \nif __name__ == \"__main__\":\n    print(create_tuple(5, 3, -1))\n

    (-1, 5, 7)

    Please write a function named oldest_person(people: list), which takes a list of tuples as its argument. In each tuple, the first element is the name of a person, and the second element is their year of birth. The function should find the oldest person on the list and return their name.

    An example of the function in action:

    p1 = (\"Adam\", 1977)\np2 = (\"Ellen\", 1985)\np3 = (\"Mary\", 1953)\np4 = (\"Ernest\", 1997)\npeople = [p1, p2, p3, p4]\n\nprint(oldest_person(people))

    Mary

    In this exercise we are handling tuples just like the ones described in the previous exercise.

    Please write a function named older_people(people: list, year: int), which selects all those people on the list who were born before the year given as an argument. The function should return the names of these people in a new list.

    An example of its use:

    p1 = (\"Adam\", 1977)\np2 = (\"Ellen\", 1985)\np3 = (\"Mary\", 1953)\np4 = (\"Ernest\", 1997)\npeople = [p1, p2, p3, p4]\n\nolder = older_people(people, 1979)\nprint(older)

    [ 'Adam', 'Mary' ]

    What is the purpose of a tuple?

    Tuples are ideal for when there is a set collection of values which are in some way connected. For example, when there is a need to handle the x and y coordinates of a point, a tuple is a natural choice, because coordinates will always consist of two values:

    point = (10, 20)

    Technically it is of course possible to also use a list to store these:

    point = [10, 20]

    A list is a collection of consecutive items in a certain order. The size of a list may also change. When we are storing the coordinates of a point, we want to store the x and y coordinates specifically, not an arbitrary list containing those values.

    Because tuples are immutable, unlike lists, they can be used as keys in a dictionary. The following bit of code creates a dictionary, where the keys are coordinate points:

    points = {}\npoints[(3, 5)] = \"monkey\"\npoints[(5, 0)] = \"banana\"\npoints[(1, 2)] = \"harpsichord\"\nprint(points[(3, 5)])
    \nmonkey\n

    Attempting a similar dictionary definition using lists would not work:

    points = {}\npoints[[3, 5]] = \"monkey\"\npoints[[5, 0]] = \"banana\"\npoints[[1, 2]] = \"harpsichord\"\nprint(points[[3, 5]])

    TypeError: unhashable type: 'list'

    Tuples without parentheses

    The parentheses are not strictly necessary when defining tuples. The following two variable assignments are identical in their results:

    numbers = (1, 2, 3)
    numbers = 1, 2, 3

    This means we can also easily return multiple values using tuples. Let's have alook at he following example:

    def minmax(my_list):\n  return min(my_list), max(my_list)\n\nmy_list = [33, 5, 21, 7, 88, 312, 5]\n\nmin_value, max_value = minmax(my_list)\nprint(f\"The smallest item is {min_value} and the greatest item is {max_value}\")

    The smallest item is 5 and the greatest item is 312

    This function returns two values in a tuple. The return value is assigned to two variables at once:

    min_value, max_value = minmax(my_list)

    Using parentheses may make the notation more clear. On the left hand side of the assignment statement we also have a tuple, which contains two variable names. The values contained within the tuple returned by the function are assigned to these two variables.

    (min_value, max_value) = minmax(my_list)

    You may remember the dictionary method items in the previous section. We used it to access all the keys and values stored in a dictionary:

    my_dictionary = {}\n\nmy_dictionary[\"apina\"] = \"monkey\"\nmy_dictionary[\"banaani\"] = \"banana\"\nmy_dictionary[\"cembalo\"] = \"harpsichord\"\n\nfor key, value in my_dictionary.items():\n    print(\"key:\", key)\n    print(\"value:\", value)

    Tuples are at work here, too. The method my_dictionary.items() returns each key-value pair as a tuple, where the first item is the key and the second item is the value.

    Another common use case for tuples is swapping the values of two variables:

    number1, number2 = number2, number1

    The assignment statement above swaps the values stored in the variables number1 and number2. The result is identical to what is achieved with the following bit of code, using a helper variable:

    helper_var = number1\nnumber1 = number2\nnumber2 = helper_var

    In this series of exercises you will create a simple student database. Before diving in, please spend a moment reading through the instructions and thinking about what sort of data structures are necessary for organising the data stored by your program.

    adding students

    First write a function named add_student, which adds a new student to the database. Also write a preliminary version of the function print_student, which prints out the information of a single student.

    These function are used as follows:

    students = {}\nadd_student(students, \"Peter\")\nadd_student(students, \"Eliza\")\nprint_student(students, \"Peter\")\nprint_student(students, \"Eliza\")\nprint_student(students, \"Jack\")

    Your program should now print out

    \nPeter:\n no completed courses\nEliza:\n no completed courses\nJack: no such person in the database\n

    adding completed courses

    Please write a function named add_course, which adds a completed course to the information of a specific student in the database. The course data is a tuple consisting of the name of the course and the grade:

    students = {}\nadd_student(students, \"Peter\")\nadd_course(students, \"Peter\", (\"Introduction to Programming\", 3))\nadd_course(students, \"Peter\", (\"Advanced Course in Programming\", 2))\nprint_student(students, \"Peter\")

    When some courses have been added, the information printed out changes:

    \nPeter:\n 2 completed courses:\n  Introduction to Programming 3\n  Advanced Course in Programming 2\n average grade 2.5\n

    repeating courses

    Courses with grade 0 should be ignored when adding course information. Additionally, if the course is already in the database in that specific student's information, the grade recorded in the database should never be lowered if the course is repeated.

    students = {}\nadd_student(students, \"Peter\")\nadd_course(students, \"Peter\", (\"Introduction to Programming\", 3))\nadd_course(students, \"Peter\", (\"Advanced Course in Programming\", 2))\nadd_course(students, \"Peter\", (\"Data Structures and Algorithms\", 0))\nadd_course(students, \"Peter\", (\"Introduction to Programming\", 2))\nprint_student(students, \"Peter\")
    \nPeter:\n 2 completed courses:\n  Introduction to Programming 3\n  Advanced Course in Programming 2\n average grade 2.5\n

    summary of database

    Please write a function named summary, which prints out a summary based on all the information stored in the database.

    students = {}\nadd_student(students, \"Peter\")\nadd_student(students, \"Eliza\")\nadd_course(students, \"Peter\", (\"Data Structures and Algorithms\", 1))\nadd_course(students, \"Peter\", (\"Introduction to Programming\", 1))\nadd_course(students, \"Peter\", (\"Advanced Course in Programming\", 1))\nadd_course(students, \"Eliza\", (\"Introduction to Programming\", 5))\nadd_course(students, \"Eliza\", (\"Introduction to Computer Science\", 4))\nsummary(students)

    This should print out

    \nstudents 2\nmost courses completed 3 Peter\nbest average grade 4.5 Eliza\n

    This final exercise in this part is a relatively demanding problem solving task. It can be solved in many different ways. Even though this current section in the material covers tuples, tuples are not necessarily the best way to go about solving this.

    Please write a program which prints out a square of letters as specified in the examples below. You may assume there will be at most 26 layers.

    Layers: 3

    \nCCCCC\nCBBBC\nCBABC\nCBBBC\nCCCCC\n

    Layers: 4

    \nDDDDDDD\nDCCCCCD\nDCBBBCD\nDCBABCD\nDCBBBCD\nDCCCCCD\nDDDDDDD\n

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    Please respond to a quick questionnaire on this week's materials.

    ","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-5/4-tuple.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-5/page-data.json b/page-data/part-5/page-data.json index 18a266771..bd37a8115 100644 --- a/page-data/part-5/page-data.json +++ b/page-data/part-5/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/part-5", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-5","title":"Part 5"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-5/index.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-5","title":"Part 5"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-5/index.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-6/1-reading-files/page-data.json b/page-data/part-6/1-reading-files/page-data.json index 92e2058fa..196019fa3 100644 --- a/page-data/part-6/1-reading-files/page-data.json +++ b/page-data/part-6/1-reading-files/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-6/1-reading-files", - "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 know how to read the contents of files with Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what a text file and a CSV file are"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to process the contents of a CSV file in your programs"}]},{"type":"text","value":"\n"}]}]},{"type":"comment","value":"the same text is in sections 3-1, 5-1 and 6-1, check them all if you're changing this"},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"About the exercises on this course"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Becoming a proficient programmer requires a lot of practice, sometimes even quite mechanical practice. It also involves developing problem solving skills and applying intuition. This is why there are a lot of exercises of different kinds on this course. Some of them ask you to quite straightforwardly apply what you have learnt in the material, but some of them are intentionally more challenging and open-ended."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some of the exercises might at first seem overwhelming, but this is nothing to worry about. None of the exercises is strictly mandatory, and in fact "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"only 25 % of the points in each part is required to pass the course."}]},{"type":"text","value":" You can find more details about passing the course on the "},{"type":"element","tagName":"a","properties":{"href":"/grading-and-exams"},"children":[{"type":"text","value":"page on grading"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"The exercises are not in any specific order of difficulty."}]},{"type":"text","value":" Each section usually introduces some new programming concepts, and these are then practised with both simpler and more complicated exercises. "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"If you come across an exercise that feels too difficult, move on to the next one."}]},{"type":"text","value":" You can always come back to the more difficult exercises if you have time later."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the going inevitably gets tough, a word of consolation: a task that seems impossibly difficult this week will likely feel rather easy in about four weeks' time."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A very common use case for programming is handling data stored in files. Programs can read data from files and write the computed results to files. Even large amounts of data become easy to process automatically when files are used."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"On this course we only deal with "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"text files"}]},{"type":"text","value":". All the files used will consist of lines of text. As an example, the Visual Studio Code editor we use on this course works with text files. NB: even though word processors such as Microsoft Word are usually used with files that contain text, Word documents themselves aren't text files. They also contain formatting information, and are encoded in a way that makes handling them in a program more complicated."}]},{"type":"element","tagName":"h2","properties":{"id":"reading-data-from-a-file","style":"position:relative;"},"children":[{"type":"text","value":"Reading data from a file"},{"type":"element","tagName":"a","properties":{"href":"#reading-data-from-a-file","ariaLabel":"reading data from a file 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":"Let's first work with a file called "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"example.txt"}]},{"type":"text","value":", with the following contents:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hello there!\nThis example file contains three lines of text.\nThis is the last line."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A simple way to include files in a Python program is to use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" statement. The header line opens the file, and the block where the file can be accessed follows. After the block the file is automatically closed, and can no longer be accessed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, the following code opens the file, reads the contents, prints them out, and then closes the file:"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"example.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n contents "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"read"},{"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":"contents"},{"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":"Hello there!\nThis example file contains three lines of text.\nThis is the last line."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"new_file"}]},{"type":"text","value":" above is a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"file handle"}]},{"type":"text","value":". Through it the file can accessed while it is still open. Here we used the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"read"}]},{"type":"text","value":", which returns the contents of the file as a single string. So, in this case the string returned by "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"read"}]},{"type":"text","value":" would be"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"text"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-text"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Hello there!\\nThis example file contains three lines of text.\\nThis is the last line.\""}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"going-through-the-contents-of-a-file","style":"position:relative;"},"children":[{"type":"text","value":"Going through the contents of a file"},{"type":"element","tagName":"a","properties":{"href":"#going-through-the-contents-of-a-file","ariaLabel":"going through the contents of a file 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 "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"read"}]},{"type":"text","value":" method is useful for printing out the contents of the entire file, but more often we will want to go through the file line by line."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Text files can be thought of as lists of strings, each string representing a single line in the file. We can go through the list with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following example reads our example file using a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop, removes line breaks from the end of each line, counts the number of lines, and prints each line with its line number. It also keeps track of the length of the lines:"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"example.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n count "},{"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":"text","value":"\n total_length "},{"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":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n line "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"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":"\"\\n\""}]},{"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n count "},{"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":"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":"\"Line\""}]},{"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":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n length "},{"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":"line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n total_length "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" length\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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Total length of lines:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" total_length"},{"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":"Line 1 Hello there!\nLine 2 This example file contains three lines of text.\nLine 3 This is the last line.\nTotal length of lines: 81"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There is a line break "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\\n"}]},{"type":"text","value":" at the end of each line in the file, but the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" function also adds a line break by default. There are no extra line breaks in the printout above, because the line breaks at the ends of the lines are removed with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"replace"}]},{"type":"text","value":" method. It replaces each line break character with an empty string. This way the lengths of the lines are also calculated correctly."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Largest number","tmcname":"part06-01_largest_number"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"numbers.txt"}]},{"type":"text","value":" contains integer numbers, one number per line. The contents could look like this:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"45"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"108"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","parameter","variable"]},"children":[{"type":"text","value":"-10"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1100"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":".."}]},{"type":"text","value":".etc"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":".."}]},{"type":"text","value":"."}]}]}]},{"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":"largest"}]},{"type":"text","value":", which reads the file and returns the largest number in the file."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice that the function does not take any arguments. The file you are working with is always named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"numbers.txt"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" If Visual Studio Code can't find the file and you have checked that there are no spelling errors, take a look at the instructions following this exercise."}]}]},{"type":"element","tagName":"h2","properties":{"id":"what-if-visual-studio-code-cannot-find-my-file","style":"position:relative;"},"children":[{"type":"text","value":"What if Visual Studio Code cannot find my file?"},{"type":"element","tagName":"a","properties":{"href":"#what-if-visual-studio-code-cannot-find-my-file","ariaLabel":"what if visual studio code cannot find my file 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":"When you execute your code, Visual Studio Code might complain about not finding the file, even though you have checked and double-checked the file name was written correctly, and the file exists. Changing the following setting may fix the problem:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Open the settings from the menu bar: "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"File"}]},{"type":"text","value":" -> "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Preferences"}]},{"type":"text","value":" -> "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Settings"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Find the relevant setting with the search term \"executeinfile\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Choose the tab "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Workspace"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Select the option under "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Python"}]},{"type":"text","value":" -> "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Terminal"}]},{"type":"text","value":" -> "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Execute In File Dir"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your settings window should now look somewhat like this:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/6e5d89f76dfd47af6f9ea67b097452e8/e405b/6_1_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: 16.521739130434785%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAADCAIAAAAcOLh5AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAZElEQVQI11XOQQqAMAwEwP5DknRNkNIilaInvXn3/8+RKIoOOe4uCQAQHYDWWq01/xS/UnLO07rP+9HbEEU8DQT+SCmZmZqlB77GRdvGUCa687+yiDAzVH3i8hb9MyZQJ0xv/gSEjBk5LU20dgAAAABJRU5ErkJggg=='); 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":["/static/6e5d89f76dfd47af6f9ea67b097452e8/a0b58/6_1_1.webp 230w","/static/6e5d89f76dfd47af6f9ea67b097452e8/bc10c/6_1_1.webp 460w","/static/6e5d89f76dfd47af6f9ea67b097452e8/966d8/6_1_1.webp 920w","/static/6e5d89f76dfd47af6f9ea67b097452e8/445df/6_1_1.webp 1380w","/static/6e5d89f76dfd47af6f9ea67b097452e8/9b2c8/6_1_1.webp 1566w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6e5d89f76dfd47af6f9ea67b097452e8/81c8e/6_1_1.png 230w","/static/6e5d89f76dfd47af6f9ea67b097452e8/08a84/6_1_1.png 460w","/static/6e5d89f76dfd47af6f9ea67b097452e8/c0255/6_1_1.png 920w","/static/6e5d89f76dfd47af6f9ea67b097452e8/b1001/6_1_1.png 1380w","/static/6e5d89f76dfd47af6f9ea67b097452e8/e405b/6_1_1.png 1566w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/6e5d89f76dfd47af6f9ea67b097452e8/c0255/6_1_1.png","alt":"6 1 1","title":"6 1 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":"p","properties":{},"children":[{"type":"text","value":"If this doesn't help, you can copy the file in the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"src"}]},{"type":"text","value":" directory"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/a57483e6084d39a596ee24c41a42de70/6297e/6_1_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: 22.17391304347826%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAABYlAAAWJQFJUiTwAAAApklEQVQI123N0QqCQBAFUH/CUVlnt3JnVtcSyx1XikKCHur/PyhQih463KcLl5s87rcQRcLoa98037TO1czsFtba6p/kFESm8+U6HyTuT4FcY11Dtd9WVH4gol7keQ4Aa4OIySBjfxwkyKuLc90P5ImYiHeWtdmYRVmWSqmiKLIsAwCllDFGa50EiW3Xj3F6tjKQr5YlE28tozbrISKmaQoAiPg7fgOYxyLFEKvBSAAAAABJRU5ErkJggg=='); 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":["/static/a57483e6084d39a596ee24c41a42de70/a0b58/6_1_2.webp 230w","/static/a57483e6084d39a596ee24c41a42de70/bc10c/6_1_2.webp 460w","/static/a57483e6084d39a596ee24c41a42de70/966d8/6_1_2.webp 920w","/static/a57483e6084d39a596ee24c41a42de70/445df/6_1_2.webp 1380w","/static/a57483e6084d39a596ee24c41a42de70/78de1/6_1_2.webp 1840w","/static/a57483e6084d39a596ee24c41a42de70/4b7c9/6_1_2.webp 2044w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a57483e6084d39a596ee24c41a42de70/81c8e/6_1_2.png 230w","/static/a57483e6084d39a596ee24c41a42de70/08a84/6_1_2.png 460w","/static/a57483e6084d39a596ee24c41a42de70/c0255/6_1_2.png 920w","/static/a57483e6084d39a596ee24c41a42de70/b1001/6_1_2.png 1380w","/static/a57483e6084d39a596ee24c41a42de70/161ec/6_1_2.png 1840w","/static/a57483e6084d39a596ee24c41a42de70/6297e/6_1_2.png 2044w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/a57483e6084d39a596ee24c41a42de70/c0255/6_1_2.png","alt":"6 1 2","title":"6 1 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":"directly to the root of the exercise directory"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/bc83b27162a67ce98f825821dc304a7e/6297e/6_1_3.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: 26.956521739130434%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAIAAADKYVtkAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAt0lEQVQY023P3Q6CMAwFYN6DjqjtNta1MxlDdILGxBvf/4UMGO/4cm5Pf5pa79N0m+eHSBJJMSqzMKv3vaqklPq+995ba51zdkNkPR2COzallJzL8/lalketc86FWWJUIsfMqtq2bdd1sDF/AOaE2AzDUIYyjmPOWUQRiWidjYgxRlU1xnQ7DBGtmyWdy2ViZqL1ul8QSSQyMwDsVI1BxOZ6md65fsYlyTms3/6iznnd7JYBIITwBcyRLmVWTxqEAAAAAElFTkSuQmCC'); 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":["/static/bc83b27162a67ce98f825821dc304a7e/a0b58/6_1_3.webp 230w","/static/bc83b27162a67ce98f825821dc304a7e/bc10c/6_1_3.webp 460w","/static/bc83b27162a67ce98f825821dc304a7e/966d8/6_1_3.webp 920w","/static/bc83b27162a67ce98f825821dc304a7e/445df/6_1_3.webp 1380w","/static/bc83b27162a67ce98f825821dc304a7e/78de1/6_1_3.webp 1840w","/static/bc83b27162a67ce98f825821dc304a7e/4b7c9/6_1_3.webp 2044w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/bc83b27162a67ce98f825821dc304a7e/81c8e/6_1_3.png 230w","/static/bc83b27162a67ce98f825821dc304a7e/08a84/6_1_3.png 460w","/static/bc83b27162a67ce98f825821dc304a7e/c0255/6_1_3.png 920w","/static/bc83b27162a67ce98f825821dc304a7e/b1001/6_1_3.png 1380w","/static/bc83b27162a67ce98f825821dc304a7e/161ec/6_1_3.png 1840w","/static/bc83b27162a67ce98f825821dc304a7e/6297e/6_1_3.png 2044w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/bc83b27162a67ce98f825821dc304a7e/c0255/6_1_3.png","alt":"6 1 3","title":"6 1 3","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":"h2","properties":{"id":"debugging-code-which-handles-files","style":"position:relative;"},"children":[{"type":"text","value":"Debugging code which handles files"},{"type":"element","tagName":"a","properties":{"href":"#debugging-code-which-handles-files","ariaLabel":"debugging code which handles files 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":"Using the Visual Studio Code "},{"type":"element","tagName":"a","properties":{"href":"/part-4/1-vscode#the-built-in-debugger"},"children":[{"type":"text","value":"debugger"}]},{"type":"text","value":" with programs which handle files will often result in a nasty looking error message:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/b0c7745f7a4cc9c485a52d483e0aa290/eff3b/6_1_4.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: 31.304347826086953%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAABYlAAAWJQFJUiTwAAABMklEQVQY003PyU7CQACA4d7EBWQpLdDS6XSWzkw702VIIBESgyaYWBZRE60QUIzv/wguB0PyX7/Db2itx5PJcDiKYym4ICFXaSKkZFKGMhUyESKmlAEY+BD5EAGIMA5iThhjRpZlOhsooTjlmFAKgaBgPMSjHOS8kwtHx55itgqt4xIllEqNPM85F4xxSmhAyAziDabbiG8F28voBcGnvrNot+ZmY2E252azMBtFvSZ7XQADQ19dI4QJoX+Y3vXhgcbvcbYL5Z6pHYlKQEpAS0DWrd6yZi3r1uK0Hpm26wND6xxj8o9nrv8Zxl/p4CCSQ5R8cFV6aAPwm4eeLWfd7Dy2OqvzRtS2XQB+n4/x1IOvNHrw8dIBSwcUtlPYzsqF9+3e7dnltFKdVqo3JxfC7v7gb6ThPL16POKXAAAAAElFTkSuQmCC'); 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":["/static/b0c7745f7a4cc9c485a52d483e0aa290/a0b58/6_1_4.webp 230w","/static/b0c7745f7a4cc9c485a52d483e0aa290/bc10c/6_1_4.webp 460w","/static/b0c7745f7a4cc9c485a52d483e0aa290/966d8/6_1_4.webp 920w","/static/b0c7745f7a4cc9c485a52d483e0aa290/445df/6_1_4.webp 1380w","/static/b0c7745f7a4cc9c485a52d483e0aa290/78de1/6_1_4.webp 1840w","/static/b0c7745f7a4cc9c485a52d483e0aa290/5340a/6_1_4.webp 2072w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/b0c7745f7a4cc9c485a52d483e0aa290/81c8e/6_1_4.png 230w","/static/b0c7745f7a4cc9c485a52d483e0aa290/08a84/6_1_4.png 460w","/static/b0c7745f7a4cc9c485a52d483e0aa290/c0255/6_1_4.png 920w","/static/b0c7745f7a4cc9c485a52d483e0aa290/b1001/6_1_4.png 1380w","/static/b0c7745f7a4cc9c485a52d483e0aa290/161ec/6_1_4.png 1840w","/static/b0c7745f7a4cc9c485a52d483e0aa290/eff3b/6_1_4.png 2072w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/b0c7745f7a4cc9c485a52d483e0aa290/c0255/6_1_4.png","alt":"6 1 4","title":"6 1 4","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 reason is that the debugger will always look for files in the root of the exercise directory. The "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Execute In File Dir"}]},{"type":"text","value":" setting mentioned above will have no effect on this. The easiest solution is to just copy the file to the root of the directory."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may need to restart Visual Studio Code after copying all the necessary files."}]},{"type":"element","tagName":"h2","properties":{"id":"reading-csv-files","style":"position:relative;"},"children":[{"type":"text","value":"Reading CSV files"},{"type":"element","tagName":"a","properties":{"href":"#reading-csv-files","ariaLabel":"reading csv files 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":"A CSV file, short for "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"comma-separated Values"}]},{"type":"text","value":", is a text file which contains data separated by a predetermined character. The most common characters used for this purpose are the comma "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" and the semicolon "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":", but any character is, in principle, possible."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"CSV files are commonly used to store records of different kinds. Many database and spreadsheet programs, such as Excel, can import and export data in CSV format, which makes data exchange between different systems easy."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We already learnt we can go through the lines in a file with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop, but how can we separate the different fields on a single line? Python has a string method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":" for just this purpose. The method takes the separator character(s) as a string argument, and returns the contents of the target string as a list of strings, separated at the separator."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how the method works:"}]},{"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":"text "},{"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":"\"monkey,banana,harpsichord\""}]},{"type":"text","value":"\nwords "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" words"},{"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":"word"},{"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":"monkey\nbanana\nharpsichord"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's assume we have a file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"grades.csv"}]},{"type":"text","value":", which contains names of students and the grades they received on some courses. Each line has the data of a single student, and the data is separated by a semicolon."}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Paul;5;4;5;3;4;5;5;4;2;4\nBeth;3;4;2;4;4;2;3;1;3;3\nRuth;4;5;5;4;5;5;4;5;4;4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program goes through the file line by line, splits each line into its separate parts, and prints out the name and grades of each student."}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"grades.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n line "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"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":"\"\\n\""}]},{"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" parts"},{"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":"\n grades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" parts"},{"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":"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":"\"Name:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"\"Grades:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" grades"},{"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":"Name: Paul\nGrades: ['5', '4', '5', '3', '4', '5', '5', '4', '2', '4']\nName: Beth\nGrades: ['3', '4', '2', '4', '4', '2', '3', '1', '3', '3']\nName: Ruth\nGrades: ['4', '5', '5', '4', '5', '5', '4', '5', '4', '4']"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Fruit market","tmcname":"part06-02_fruit_market"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fruits.csv"}]},{"type":"text","value":" contains names of fruits, and their prices, in the format specified in this example:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"text","value":"banana"},{"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.50"}]},{"type":"text","value":"\napple"},{"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.95"}]},{"type":"text","value":"\norange"},{"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.0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":".."}]},{"type":"text","value":".etc"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":".."}]},{"type":"text","value":"."}]}]}]},{"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":"read_fruits"}]},{"type":"text","value":", which reads the file and returns a dictionary based on the contents. In the dictionary, the name of the fruit should be the key, and the value should be its price. Prices should be of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the function does not take any arguments. The file you are working with is always named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fruits.csv"}]},{"type":"text","value":"."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Matrix","tmcname":"part06-03_matrix"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"matrix.txt"}]},{"type":"text","value":" contains a matrix in the format specified in the example below:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1,0"}]},{"type":"text","value":",2,8,2,1,3,2,5,2,2,2\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9,2"}]},{"type":"text","value":",4,5,2,4,2,4,1,10,4,2\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":".."}]},{"type":"text","value":".etc"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":".."}]},{"type":"text","value":"."}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write two functions, named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"matrix_sum"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"matrix_max"}]},{"type":"text","value":". Both go through the matrix in the file, and then return the sum of the elements or the element with the greatest value, as the names of the functions imply."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please also write the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"row_sums"}]},{"type":"text","value":", which returns a list containing the sum of each row in the matrix. For example, calling "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"row_sums"}]},{"type":"text","value":" when the matrix in the file is defined as"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1,2"}]},{"type":"text","value":",3\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2,3"}]},{"type":"text","value":",4"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"the function should return the list "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[6, 9]"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hint: you can also include other helper functions in your program. It is very worthwhile to spend a moment considering which functionalities are shared by the three functions you are asked to write. Notice that the three functions named above do not take any arguments, but any helper functions you write may take arguments. The file you are working with is always named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"matrix.txt"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" If Visual Studio Code can't find the file and you have checked that there are no spelling errors, take a look at the instructions before this exercise."}]}]},{"type":"element","tagName":"h2","properties":{"id":"reading-the-same-file-multiple-times","style":"position:relative;"},"children":[{"type":"text","value":"Reading the same file multiple times"},{"type":"element","tagName":"a","properties":{"href":"#reading-the-same-file-multiple-times","ariaLabel":"reading the same file multiple times 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":"Sometimes it is necessary to process the contents of a file more than once in a single program. Let's have a look at a program which works with some personal data stored in a CSV file:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"text","value":"\nPeter;40;Helsinki\nEmily;34;Espoo\nEric;42;London\nAdam;100;Amsterdam\nAlice;58;Paris\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":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"people.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"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":"# print out the names"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 "},{"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":"\"Name:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" parts"},{"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\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# find the oldest"}]},{"type":"text","value":"\n age_of_oldest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" parts"},{"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":"\n age "},{"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":"text","value":"parts"},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" age_of_oldest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n age_of_oldest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" age\n oldest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\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 oldest is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" oldest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Running this will result in a somewhat cryptic error message:"}]},{"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":"Traceback "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"most recent call 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":"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 oldest is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" oldest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nUnboundLocalError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" local variable "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'oldest'"}]},{"type":"text","value":" referenced before assignment"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The reason this happens is that the latter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop is not executed at all, beacuse the file can only be processed once. Once the last line is read, the file handle rests at the end of the file, and the data in the file can no longer be accessed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we want to access the contents in the second "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop, we will have to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"open"}]},{"type":"text","value":" the file a second time:"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"people.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"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":"# print out the names"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 "},{"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":"\"Name:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" parts"},{"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"people.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"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":"# find the oldest"}]},{"type":"text","value":"\n age_of_oldest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" parts"},{"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":"\n age "},{"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":"text","value":"parts"},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" age_of_oldest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n age_of_oldest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" age\n oldest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\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 oldest is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" oldest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"While the above code would work, it contains unnecessary repetition. It is usually best to read the file just once, and store its contents in an appropriate format for further processing:"}]},{"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":"people "},{"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":"# read the contents of the file and store it in a list"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"people.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 people"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"parts"},{"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":" "},{"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":"text","value":"parts"},{"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":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" parts"},{"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":"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":"# print out the names"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" people"},{"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":"\"Name:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person"},{"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# find the oldest"}]},{"type":"text","value":"\nage_of_oldest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" people"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" person"},{"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":"\n age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" person"},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" age_of_oldest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n age_of_oldest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" age\n oldest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\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 oldest is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" oldest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"more-csv-file-processing","style":"position:relative;"},"children":[{"type":"text","value":"More CSV file processing"},{"type":"element","tagName":"a","properties":{"href":"#more-csv-file-processing","ariaLabel":"more csv file processing 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":"Let's continue with the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"grades.csv"}]},{"type":"text","value":", which has the following contents:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Paul;5;4;5;3;4;5;5;4;2;4\nBeth;3;4;2;4;4;2;3;1;3;3\nRuth;4;5;5;4;5;5;4;5;4;4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This following program creates a dictionary "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"grades"}]},{"type":"text","value":" based on the contents of the file. The keys are the names of the students, and the value attached to each key is the list of grades attained by the student. The program converts the grades to integer values, so they can be processed easier."}]},{"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":"grades "},{"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","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"grades.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n line "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"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":"\"\\n\""}]},{"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" parts"},{"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":"\n grades"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"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","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":"for"}]},{"type":"text","value":" grade "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" parts"},{"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":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n grades"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"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":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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":"text","value":"grade"},{"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":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"grades"},{"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': [5, 4, 5, 3, 4, 5, 5, 4, 2, 4], 'Beth': [3, 4, 2, 4, 4, 2, 3, 1, 3, 3], 'Ruth': [4, 5, 5, 4, 5, 5, 4, 5, 4, 4]}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now we can print out some statistics on each student based on the contents of the dictionary "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"grades"}]},{"type":"text","value":":"}]},{"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":"for"}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" grade_list "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" grades"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n best "},{"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":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"grade_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n average "},{"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":"grade_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","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"grade_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","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":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": best grade "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"best"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", average "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"average"},{"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":"Paul: best grade 5, average 4.10\nBeth: best grade 4, average 2.90\nRuth: best grade 5, average 4.50"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please take a careful look at the program in the example above. It may seem a bit complicated at first, but the technique can be used on files containing many different types of data."}]},{"type":"element","tagName":"h2","properties":{"id":"removing-unnecessary-lines-spaces-and-line-breaks","style":"position:relative;"},"children":[{"type":"text","value":"Removing unnecessary lines, spaces and line breaks"},{"type":"element","tagName":"a","properties":{"href":"#removing-unnecessary-lines-spaces-and-line-breaks","ariaLabel":"removing unnecessary lines spaces and line breaks 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":"Let's assume we have a CSV file containing some names, which has been exported from Excel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"text","value":"first"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" last\nPaul"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" Python\nJean"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" Java\nHarry"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" Haskell"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Excel is notorious for adding extra whitespace. Here we have an extra space character between the items, after each semicolon."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We would like to print out the last names of each person on the list. The first line contains the headers for the data, and it can be safely ignored:"}]},{"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":"last_names "},{"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","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"people.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ignore the header line"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" parts"},{"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":"\"first\""}]},{"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":"continue"}]},{"type":"text","value":"\n last_names"},{"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":"parts"},{"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":"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":"last_names"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exectuing this would print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[' Python\\n', ' Java\\n', ' Haskell']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The first two items have a line break character at the end, and all three have an extra leading space character."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We have already used the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"replace"}]},{"type":"text","value":" method to remove extra whitespace, but a more efficient solution is to use the Python string method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"strip"}]},{"type":"text","value":", which removes whitespace from the beginning and end of a string. It removes all spaces, line breaks, tabs and other characters which would not normally be printed out."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can try it out in the Python console:"}]},{"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","operator"]},"children":[{"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":"\" tryout \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strip"},{"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","string"]},"children":[{"type":"text","value":"'tryout'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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":"\"\\n\\ntest\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strip"},{"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","string"]},"children":[{"type":"text","value":"'test'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Stripping the string requires only a small change to the program:"}]},{"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":"last_names "},{"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","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"people.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" parts"},{"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":"\"first\""}]},{"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":"continue"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# this was the header line, so it is ignored"}]},{"type":"text","value":"\n last_names"},{"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":"parts"},{"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":"text","value":"strip"},{"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"},{"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":"last_names"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now we have the desired tidy printout:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['Python', 'Java', 'Haskell']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are also the related string methods "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lstrip"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rstrip"}]},{"type":"text","value":". They remove only the leading or trailing unprintable characters, l for the left edge of the string and r for the right:"}]},{"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","operator"]},"children":[{"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":"\" teststring \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rstrip"},{"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","string"]},"children":[{"type":"text","value":"' teststring'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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":"\" teststring \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lstrip"},{"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","string"]},"children":[{"type":"text","value":"'teststring '"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"combining-data-from-different-files","style":"position:relative;"},"children":[{"type":"text","value":"Combining data from different files"},{"type":"element","tagName":"a","properties":{"href":"#combining-data-from-different-files","ariaLabel":"combining data from different files 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":"It is very common for the data processed by a program to be scattered in multiple files. Lets have a look at a situation where the personal details of the personnel of a company are stored in a file called "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"employees.csv"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"pic;name;address;city"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"080488-123X;Pekka Mikkola;Vilppulantie 7;00700 Helsinki"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"290274-044S;Liisa Marttinen;Mannerheimintie 100 A 10;00100 Helsinki"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"010479-007Z;Arto Vihavainen;Pihapolku 4;01010 Kerava"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"010499-345K;Leevi Hellas;Tapiolantie 11 B;02000 Espoo"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The salaries are stored in a separate file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"salaries.csv"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"pic;salary;bonus"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"080488-123X;3300;0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"290274-044S;4150;200"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"010479-007Z;1300;1200"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each data line in both files contains the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"personal identity code"}]},{"type":"text","value":", which identifies whose data we are dealing with. Using the personal identity code as a common factor, it is easy to connect the names and salaries of each employee. We can, for example, print out the following list of monthly incomes:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"incomes:\nPekka Mikkola 3300 euros\nLiisa Marttinen 4350 euros\nArto Vihavainen 2500 euros\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program uses two dictionaries as helper data structures: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"names"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"salaries"}]},{"type":"text","value":". Both use the PIC as key:"}]},{"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":"names "},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"employees.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" parts"},{"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":"\"pic\""}]},{"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":"continue"}]},{"type":"text","value":"\n names"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"parts"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" parts"},{"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":"text","value":"\n\nsalaries "},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"salaries.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" parts"},{"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":"\"pic\""}]},{"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":"continue"}]},{"type":"text","value":"\n salaries"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"parts"},{"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":" "},{"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":"text","value":"parts"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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":"text","value":"parts"},{"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":"\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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"incomes:\""}]},{"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":" pic"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" names"},{"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":"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":"if"}]},{"type":"text","value":" pic "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" salaries"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n salary "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" salaries"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"pic"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"name"},{"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":"16"}]},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"salary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euros\""}]}]},{"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":"else"}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"name"},{"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":"16"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" 0 euros\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First the program produces the dictionaries "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"names"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"salaries"}]},{"type":"text","value":". They have the following contents:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'080488-123X'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Pekka Mikkola'"}]},{"type":"text","value":",\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'290274-044S'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Liisa Marttinen'"}]},{"type":"text","value":",\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'010479-007Z'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Arto Vihavainen'"}]},{"type":"text","value":",\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'010499-345K'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Leevi Hellas'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'080488-123X'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3300"}]},{"type":"text","value":",\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'290274-044S'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4350"}]},{"type":"text","value":",\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'010479-007Z'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2500"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop at the end of the program combines the names of the employees with their respective salaries."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program also takes care of situations where the employee's pic is not present in the salary file."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Remember, the order in which items are stored in a dictionary does not matter, as the keys are processed based on hash values."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Course grading, part 1","tmcname":"part06-04_course_grading_part_1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program works with two CSV files. One of them contains information about some students on a course:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"id;first;last"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345678;peter;pythons"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345687;jean;javanese"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345699;alice;adder"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The other contains the number of exercises each student has completed each week:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"id;e1;e2;e3;e4;e5;e6;e7"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345678;4;1;1;4;5;2;4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345687;3;5;3;1;5;4;6"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345699;10;2;2;7;10;2;2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, both CSV files also have a header row, which tells you what each column contains."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for the names of these two files, reads the files, and then prints out the total number of exercises completed by each student. If the files have the contents in the examples above, the program should print out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Student information: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"students1.csv"}]},{"type":"text","value":"\nExercises completed: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"exercises1.csv"}]},{"type":"text","value":"\npekka peloton 21\njaana javanainen 27\nliisa virtanen 35"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hint: while testing your program, you may quickly run out of patience if you always have to type in the file names at the prompt. You might want to hard-code the user input, like so:"}]},{"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":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"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":"# this is never executed"}]},{"type":"text","value":"\n student_info "},{"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":"\"Student information: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n exercise_data "},{"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":"\"Exercises completed: \""}]},{"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":"else"}]},{"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":"# hard-coded input"}]},{"type":"text","value":"\n student_info "},{"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":"\"students1.csv\""}]},{"type":"text","value":"\n exercise_data "},{"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":"\"exercises1.csv\""}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The actual functionality of the program is now \"hidden\" in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" branch of an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statement. It will never be executed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, if you want to quickly verify the program works correctly also with user input, you can just replace "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":":"}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n student_info "},{"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":"\"Student information: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n exercise_data "},{"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":"\"Exercises completed: \""}]},{"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":"else"}]},{"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":"# now this is the False branch, and is never executed"}]},{"type":"text","value":"\n student_info "},{"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":"\"students1.csv\""}]},{"type":"text","value":"\n exercise_data "},{"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":"\"exercises1.csv\""}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When you have verified your program works correctly, you can remove the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" structure, keeping the commands asking for input."}]},{"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 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."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB2:"}]},{"type":"text","value":" If Visual Studio can't find the file and you have checked that there are no spelling errors, take a look at "},{"type":"element","tagName":"a","properties":{"href":"/part-6/1-reading-files#what-if-visual-studio-code-cannot-find-my-file"},"children":[{"type":"text","value":"these instructions"}]},{"type":"text","value":"."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Course grading, part 2","tmcname":"part06-05_course_grading_part_2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's expand the program created in the previous exercise. Now also the exam points awarded to each student are contained in a CSV file. The contents of the file follow this format:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"id;e1;e2;e3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345678;4;1;4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345687;3;5;3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345699;10;2;2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the above example the student whose student number is 12345678 was awarded 4+1+4 points in the exam, which equals a total of 9 points."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should again ask the user for the names of the files. Then the program should process the files and print out a grade for each student."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Student information: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"students1.csv"}]},{"type":"text","value":"\nExercises completed: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"exercises1.csv"}]},{"type":"text","value":"\nExam points: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"exam_points1.csv"}]},{"type":"text","value":"\npekka peloton 0\njaana javanainen 1\nliisa virtanen 3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each completed exercise is counted towards "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"exercise points"}]},{"type":"text","value":", so that completing at least 10 % of the total exercices awards 1 point, completing at least 20 % awards 2 points, etc. Completing all 40 exercises awards 10 points. The number of points awarded is always an integer number."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The final grade for the course is determined based on the sum of exam and exercise points according to 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 (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-"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"5"}]}]}]}]},{"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 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."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Course grading, part 3","tmcname":"part06-06_course_grading_part_3"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This exercise will continue from the previous one. Now we shall print out some statistics based on the CSV files."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Student information: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"students1.csv"}]},{"type":"text","value":"\nExercises completed: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"exercises1.csv"}]},{"type":"text","value":"\nExam points: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"exam_points1.csv"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"name exec_nbr exec_pts. exm_pts. tot_pts. grade\npekka peloton 21 5 9 14 0\njaana javanainen 27 6 11 17 1\nliisa virtanen 35 8 14 22 3\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each row contains the information for a single student. The number of exercises completed, the number of exercise points awarded, the number of exam points awarded, the total number of points awarded, and the grade are all displayed in tidy columns. The width of the column for the name should be 30 characters, while the other columns should be 10 characters wide."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You might find the f-strings covered in "},{"type":"element","tagName":"a","properties":{"href":"/part-4/5-print-statement-formatting"},"children":[{"type":"text","value":"part 4"}]},{"type":"text","value":" useful here."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"F-strings differentiate between strings and numbers when it comes to justifying columns:"}]},{"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":"word "},{"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\""}]},{"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":"word"},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"continues\""}]}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"word"},{"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":">10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"continues\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"python continues\n pythoncontinues\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, by default strings are justified to the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"left"}]},{"type":"text","value":" edge of the area specified for them. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" symbol can be used to justify to the right edge."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With number values the logic is reversed:"}]},{"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":"42"}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"continues\""}]}]},{"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\""}]},{"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":"<10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"continues\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":" 42continues\n42 continues\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With numbers the default behaviour is to justify to the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"right"}]},{"type":"text","value":" edge. The symbol "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" can be used to justify to the left edge."}]},{"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 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."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Spell checker","tmcname":"part06-07_spellchecker"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in some text. Your program should then perform a spell check, and print out feedback to the user, so that all misspelled words have stars around them. Please see the two examples below:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Write text: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"We use ptython to make a spell checker"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"We use *ptython* to make a spell checker\n"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Write text: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"This is acually a good and usefull program"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"This is *acually* good and *usefull* program\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The case of the letters should be irrelevant to the functioning of your program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template includes the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"wordlist.txt"}]},{"type":"text","value":", which contains all the words the spell checker should accept as correct."}]},{"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 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."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB2"}]},{"type":"text","value":" If Visual Studio can't find the file and you have checked that there are no spelling errors, take a look at "},{"type":"element","tagName":"a","properties":{"href":"/part-6/1-reading-files#what-if-visual-studio-code-cannot-find-my-file"},"children":[{"type":"text","value":"these instructions"}]},{"type":"text","value":"."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Recipe search","tmcname":"part06-08_recipe_search"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This exercise is about creating a program which allows the user to search for recipes based on their names, preparation times, or ingredients used. The program should read the recipes from a file submitted by the user."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each recipe consists of three or more lines. The first line has the name of the recipe, the second line contains an integer number representing the preparation time in minutes, and the remaining line or lines contain the ingredients used, one on each line. The recipe ends with an empty line, with the exception of the final recipe in the file which just ends with the end of the file. So, there can be more than one recipe in a single file, like in the example below."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"text","value":"Pancakes\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"text","value":"\nmilk\neggs\nflour\nsugar\nsalt\nbutter\n\nMeatballs\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"45"}]},{"type":"text","value":"\nmince\neggs\nbreadcrumbs\n\nTofu rolls\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"text","value":"\ntofu\nrice\nwater\ncarrot\ncucumber\navocado\nwasabi\n\nCake pops\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"text","value":"\nmilk\nbicarbonate\neggs\nsalt\nsugar\ncardamom\nbutter"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint:"}]},{"type":"text","value":" it might be best to first read through all the lines in the file and pop them into a list, which is then easier to manipulate in the way described in the exercise."}]},{"type":"element","tagName":"h4","properties":{"id":"search-for-recipes-based-on-the-name-of-the-recipe","style":"position:relative;"},"children":[{"type":"text","value":"Search for recipes based on the name of the recipe"},{"type":"element","tagName":"a","properties":{"href":"#search-for-recipes-based-on-the-name-of-the-recipe","ariaLabel":"search for recipes based on the name of the recipe 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"search_by_name(filename: str, word: str)"}]},{"type":"text","value":", which takes a filename and a search string as its arguments. The function should go through the file and select all recipes whose "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" contains the given search string. The names of these recipes are then returned in a list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"found_recipes "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" search_by_name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"recipes1.txt\""}]},{"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":"\"cake\""}]},{"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":" recipe "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" found_recipes"},{"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":"recipe"},{"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":"Pancakes\nCake pops"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see in the example above, the case of the letters is irrelevant. The search term "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"cake"}]},{"type":"text","value":" returns both "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Pancakes"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Cake pops"}]},{"type":"text","value":", even though the latter is capitalized."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" If Visual Studio can't find the file and you have checked that there are no spelling errors, take a look at "},{"type":"element","tagName":"a","properties":{"href":"/part-6/1-reading-files#what-if-visual-studio-code-cannot-find-my-file"},"children":[{"type":"text","value":"these instructions"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h4","properties":{"id":"search-for-recipes-based-on-the-preparation-time","style":"position:relative;"},"children":[{"type":"text","value":"Search for recipes based on the preparation time"},{"type":"element","tagName":"a","properties":{"href":"#search-for-recipes-based-on-the-preparation-time","ariaLabel":"search for recipes based on the preparation time 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"search_by_time(filename: str, prep_time: int)"}]},{"type":"text","value":", which takes a filename and an integer as its arguments. The function should go through the file and select all recipes whose preparation time is at most the number given."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The names of these recipes are again returned in a list, but the preparation time should be appended to each name. Please have a look at the example below."}]},{"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":"found_recipes "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" search_by_time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"recipes1.txt\""}]},{"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":"20"}]},{"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":" recipe "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" found_recipes"},{"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":"recipe"},{"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":"Pancakes, preparation time 15 min"}]}]},{"type":"element","tagName":"h4","properties":{"id":"search-for-recipes-based-on-the-ingredients","style":"position:relative;"},"children":[{"type":"text","value":"Search for recipes based on the ingredients"},{"type":"element","tagName":"a","properties":{"href":"#search-for-recipes-based-on-the-ingredients","ariaLabel":"search for recipes based on the ingredients 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":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"A word of caution:"}]},{"type":"text","value":" this third part of the exercise is considerably more demanding than the previous two. If you feel like you aren't making headway, it may be worth your while to move on, complete the other exercises in this part of the material, and then come back to this exercise if you have time later. Remember, you can submit and receive points for the first two parts of this exercise even if you haven't completed the third part."}]},{"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":"search_by_ingredient(filename: str, ingredient: str)"}]},{"type":"text","value":", which takes a filename and a search string as its arguments. The function should go through the file and select all recipes whose "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"ingredients"}]},{"type":"text","value":" contain the given search string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The names of these recipes are returned in a list just like in the second part, with the preparation time appended. Please have a look at the example below."}]},{"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":"found_recipes "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" search_by_ingredient"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"recipes1.txt\""}]},{"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":"\"eggs\""}]},{"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":" recipe "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" found_recipes"},{"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":"recipe"},{"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":"Pancakes, preparation time 15 min\nMeatballs, preparation time 45 min\nCake pops, preparation time 60 min"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"City bikes","tmcname":"part06-09_city_bikes"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise we will write some functions for working on a file containing location data from the stations for "},{"type":"element","tagName":"a","properties":{"href":"https://www.hsl.fi/en/citybikes","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"city bikes in Helsinki"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each file will follow this format:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Longitude;Latitude;FID;name;total_slot;operative;id"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"24.950292890004903;60.155444793742276;1;Kaivopuisto;30;Yes;001"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"24.956347471358754;60.160959093887129;2;Laivasillankatu;12;Yes;002"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"24.944927399779715;60.158189199971673;3;Kapteeninpuistikko;16;Yes;003"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each station has a single line in the file. The line contains the coordinates, name, and other identifying information for the station."}]},{"type":"element","tagName":"h4","properties":{"id":"distance-between-stations","style":"position:relative;"},"children":[{"type":"text","value":"Distance between stations"},{"type":"element","tagName":"a","properties":{"href":"#distance-between-stations","ariaLabel":"distance between stations 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":"First, write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"get_station_data(filename: str)"}]},{"type":"text","value":". This function should read the names and locations of all the stations in the file, and return them in a dictionary with the following format:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"{\n \"Kaivopuisto\": (24.950292890004903, 60.155444793742276),\n \"Laivasillankatu\": (24.956347471358754, 60.160959093887129),\n \"Kapteeninpuistikko\": (24.944927399779715, 60.158189199971673)\n}\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dictionary keys are the names of the stations, and the value attached is a tuple containing the location coordinates of the station. The first element in the tuple is the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Longitude"}]},{"type":"text","value":" field, and the second is the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Latitude"}]},{"type":"text","value":" field."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Next, write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"distance(stations: dict, station1: str, station2: str)"}]},{"type":"text","value":", which returns the distance between the two stations given as arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The distance is calculated using the Pythagorean theorem. The multiplication factors below are approximate values for converting latitudes and longitudes to distances in kilometres in the Helsinki region."}]},{"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":"# we will need the function sqrt from the math module "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" math\n\nx_km "},{"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":"text","value":"longitude1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" longitude2"},{"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":"55.26"}]},{"type":"text","value":"\ny_km "},{"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":"text","value":"latitude1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" latitude2"},{"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":"111.2"}]},{"type":"text","value":"\ndistance_km "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x_km"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" y_km"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"p","properties":{},"children":[{"type":"text","value":"Some examples of the function in action:"}]},{"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":"stations "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" get_station_data"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'stations1.csv'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" distance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"stations"},{"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":"\"Designmuseo\""}]},{"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":"\"Hietalahdentori\""}]},{"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":"d"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" distance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"stations"},{"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":"\"Viiskulma\""}]},{"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":"\"Kaivopuisto\""}]},{"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":"d"},{"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.9032737292463177\n0.7753594392019532"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" If Visual Studio can't find the file and you have checked that there are no spelling errors, take a look at "},{"type":"element","tagName":"a","properties":{"href":"/part-6/1-reading-files#what-if-visual-studio-code-cannot-find-my-file"},"children":[{"type":"text","value":"these instructions"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h4","properties":{"id":"the-greatest-distance","style":"position:relative;"},"children":[{"type":"text","value":"The greatest distance"},{"type":"element","tagName":"a","properties":{"href":"#the-greatest-distance","ariaLabel":"the greatest distance 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"greatest_distance(stations: dict)"}]},{"type":"text","value":", which works out the two stations on the list with the greatest distance from each other. The function should return a tuple, where the first two elements are the names of the two stations, and the third element is the distance between the two."}]},{"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":"stations "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" get_station_data"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'stations1.csv'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstation1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" station2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" greatest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" greatest_distance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"stations"},{"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":"station1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" station2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" greatest"},{"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":"Laivasillankatu Hietalahdentori 1.478708873076181"}]}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will know how to read the contents of files with Python
    • \n
    • You will know what a text file and a CSV file are
    • \n
    • You will be able to process the contents of a CSV file in your programs
    • \n

    Becoming a proficient programmer requires a lot of practice, sometimes even quite mechanical practice. It also involves developing problem solving skills and applying intuition. This is why there are a lot of exercises of different kinds on this course. Some of them ask you to quite straightforwardly apply what you have learnt in the material, but some of them are intentionally more challenging and open-ended.

    Some of the exercises might at first seem overwhelming, but this is nothing to worry about. None of the exercises is strictly mandatory, and in fact only 25 % of the points in each part is required to pass the course. You can find more details about passing the course on the page on grading.

    The exercises are not in any specific order of difficulty. Each section usually introduces some new programming concepts, and these are then practised with both simpler and more complicated exercises. If you come across an exercise that feels too difficult, move on to the next one. You can always come back to the more difficult exercises if you have time later.

    When the going inevitably gets tough, a word of consolation: a task that seems impossibly difficult this week will likely feel rather easy in about four weeks' time.

    A very common use case for programming is handling data stored in files. Programs can read data from files and write the computed results to files. Even large amounts of data become easy to process automatically when files are used.

    On this course we only deal with text files. All the files used will consist of lines of text. As an example, the Visual Studio Code editor we use on this course works with text files. NB: even though word processors such as Microsoft Word are usually used with files that contain text, Word documents themselves aren't text files. They also contain formatting information, and are encoded in a way that makes handling them in a program more complicated.

    Reading data from a file

    Let's first work with a file called example.txt, with the following contents:

    Hello there!\nThis example file contains three lines of text.\nThis is the last line.

    A simple way to include files in a Python program is to use the with statement. The header line opens the file, and the block where the file can be accessed follows. After the block the file is automatically closed, and can no longer be accessed.

    So, the following code opens the file, reads the contents, prints them out, and then closes the file:

    with open(\"example.txt\") as new_file:\n    contents = new_file.read()\n    print(contents)

    Hello there!\nThis example file contains three lines of text.\nThis is the last line.

    The variable new_file above is a file handle. Through it the file can accessed while it is still open. Here we used the method read, which returns the contents of the file as a single string. So, in this case the string returned by read would be

    \"Hello there!\\nThis example file contains three lines of text.\\nThis is the last line.\"

    Going through the contents of a file

    The read method is useful for printing out the contents of the entire file, but more often we will want to go through the file line by line.

    Text files can be thought of as lists of strings, each string representing a single line in the file. We can go through the list with a for loop.

    The following example reads our example file using a for loop, removes line breaks from the end of each line, counts the number of lines, and prints each line with its line number. It also keeps track of the length of the lines:

    with open(\"example.txt\") as new_file:\n    count = 0\n    total_length = 0\n\n    for line in new_file:\n        line = line.replace(\"\\n\", \"\")\n        count += 1\n        print(\"Line\", count, line)\n        length = len(line)\n        total_length += length\n\nprint(\"Total length of lines:\", total_length)

    Line 1 Hello there!\nLine 2 This example file contains three lines of text.\nLine 3 This is the last line.\nTotal length of lines: 81

    There is a line break \\n at the end of each line in the file, but the print function also adds a line break by default. There are no extra line breaks in the printout above, because the line breaks at the ends of the lines are removed with the replace method. It replaces each line break character with an empty string. This way the lengths of the lines are also calculated correctly.

    The file numbers.txt contains integer numbers, one number per line. The contents could look like this:

    2\n45\n108\n3\n-10\n1100\n...etc...

    Please write a function named largest, which reads the file and returns the largest number in the file.

    Notice that the function does not take any arguments. The file you are working with is always named numbers.txt.

    NB: If Visual Studio Code can't find the file and you have checked that there are no spelling errors, take a look at the instructions following this exercise.

    What if Visual Studio Code cannot find my file?

    When you execute your code, Visual Studio Code might complain about not finding the file, even though you have checked and double-checked the file name was written correctly, and the file exists. Changing the following setting may fix the problem:

      \n
    • Open the settings from the menu bar: File -> Preferences -> Settings
    • \n
    • Find the relevant setting with the search term \"executeinfile\"
    • \n
    • Choose the tab Workspace
    • \n
    • Select the option under Python -> Terminal -> Execute In File Dir
    • \n

    Your settings window should now look somewhat like this:

    \n \n \n \n \n \n \"6\n \n \n

    If this doesn't help, you can copy the file in the src directory

    \n \n \n \n \n \n \"6\n \n \n

    directly to the root of the exercise directory

    \n \n \n \n \n \n \"6\n \n \n

    Debugging code which handles files

    Using the Visual Studio Code debugger with programs which handle files will often result in a nasty looking error message:

    \n \n \n \n \n \n \"6\n \n \n

    The reason is that the debugger will always look for files in the root of the exercise directory. The Execute In File Dir setting mentioned above will have no effect on this. The easiest solution is to just copy the file to the root of the directory.

    You may need to restart Visual Studio Code after copying all the necessary files.

    Reading CSV files

    A CSV file, short for comma-separated Values, is a text file which contains data separated by a predetermined character. The most common characters used for this purpose are the comma , and the semicolon ;, but any character is, in principle, possible.

    CSV files are commonly used to store records of different kinds. Many database and spreadsheet programs, such as Excel, can import and export data in CSV format, which makes data exchange between different systems easy.

    We already learnt we can go through the lines in a file with a for loop, but how can we separate the different fields on a single line? Python has a string method split for just this purpose. The method takes the separator character(s) as a string argument, and returns the contents of the target string as a list of strings, separated at the separator.

    An example of how the method works:

    text = \"monkey,banana,harpsichord\"\nwords = text.split(\",\")\nfor word in words:\n    print(word)

    monkey\nbanana\nharpsichord

    Let's assume we have a file grades.csv, which contains names of students and the grades they received on some courses. Each line has the data of a single student, and the data is separated by a semicolon.

    Paul;5;4;5;3;4;5;5;4;2;4\nBeth;3;4;2;4;4;2;3;1;3;3\nRuth;4;5;5;4;5;5;4;5;4;4

    The following program goes through the file line by line, splits each line into its separate parts, and prints out the name and grades of each student.

    with open(\"grades.csv\") as new_file:\n    for line in new_file:\n        line = line.replace(\"\\n\", \"\")\n        parts = line.split(\";\")\n        name = parts[0]\n        grades = parts[1:]\n        print(\"Name:\", name)\n        print(\"Grades:\", grades)

    Name: Paul\nGrades: ['5', '4', '5', '3', '4', '5', '5', '4', '2', '4']\nName: Beth\nGrades: ['3', '4', '2', '4', '4', '2', '3', '1', '3', '3']\nName: Ruth\nGrades: ['4', '5', '5', '4', '5', '5', '4', '5', '4', '4']

    The file fruits.csv contains names of fruits, and their prices, in the format specified in this example:

    banana;6.50\napple;4.95\norange;8.0\n...etc...

    Please write a function named read_fruits, which reads the file and returns a dictionary based on the contents. In the dictionary, the name of the fruit should be the key, and the value should be its price. Prices should be of type float.

    NB: the function does not take any arguments. The file you are working with is always named fruits.csv.

    The file matrix.txt contains a matrix in the format specified in the example below:

    1,0,2,8,2,1,3,2,5,2,2,2\n9,2,4,5,2,4,2,4,1,10,4,2\n...etc...

    Please write two functions, named matrix_sum and matrix_max. Both go through the matrix in the file, and then return the sum of the elements or the element with the greatest value, as the names of the functions imply.

    Please also write the function row_sums, which returns a list containing the sum of each row in the matrix. For example, calling row_sums when the matrix in the file is defined as

    1,2,3\n2,3,4

    the function should return the list [6, 9].

    Hint: you can also include other helper functions in your program. It is very worthwhile to spend a moment considering which functionalities are shared by the three functions you are asked to write. Notice that the three functions named above do not take any arguments, but any helper functions you write may take arguments. The file you are working with is always named matrix.txt.

    NB: If Visual Studio Code can't find the file and you have checked that there are no spelling errors, take a look at the instructions before this exercise.

    Reading the same file multiple times

    Sometimes it is necessary to process the contents of a file more than once in a single program. Let's have a look at a program which works with some personal data stored in a CSV file:

    \nPeter;40;Helsinki\nEmily;34;Espoo\nEric;42;London\nAdam;100;Amsterdam\nAlice;58;Paris\n
    with open(\"people.csv\") as new_file:\n    # print out the names\n    for line in new_file:\n        parts = line.split(\";\")\n        print(\"Name:\", parts[0])\n\n    # find the oldest\n    age_of_oldest = -1\n    for line in new_file:\n        parts = line.split(\";\")\n        name = parts[0]\n        age = int(parts[1])\n        if age > age_of_oldest:\n            age_of_oldest = age\n            oldest = name\n    print(\"the oldest is\", oldest)

    Running this will result in a somewhat cryptic error message:

    Traceback (most recent call last):\n    print(\"the oldest is\"; oldest)\nUnboundLocalError: local variable 'oldest' referenced before assignment

    The reason this happens is that the latter for loop is not executed at all, beacuse the file can only be processed once. Once the last line is read, the file handle rests at the end of the file, and the data in the file can no longer be accessed.

    If we want to access the contents in the second for loop, we will have to open the file a second time:

    with open(\"people.csv\") as new_file:\n    # print out the names\n    for line in new_file:\n        parts = line.split(\";\")\n        print(\"Name:\", parts[0])\n\nwith open(\"people.csv\") as new_file:\n    # find the oldest\n    age_of_oldest = -1\n    for line in new_file:\n        parts = line.split(\";\")\n        name = parts[0]\n        age = int(parts[1])\n        if age > age_of_oldest:\n            age_of_oldest = age\n            oldest = name\n    print(\"the oldest is\", oldest)

    While the above code would work, it contains unnecessary repetition. It is usually best to read the file just once, and store its contents in an appropriate format for further processing:

    people = []\n# read the contents of the file and store it in a list\nwith open(\"people.csv\") as new_file:\n    for line in new_file:\n        parts = line.split(\";\")\n        people.append((parts[0], int(parts[1]), parts[2]))\n\n# print out the names\nfor person in people:\n    print(\"Name:\", person[0])\n\n# find the oldest\nage_of_oldest = -1\nfor person in people:\n    name = person[0]\n    age = person[1]\n    if age > age_of_oldest:\n        age_of_oldest = age\n        oldest = name\nprint(\"the oldest is\", oldest)

    More CSV file processing

    Let's continue with the file grades.csv, which has the following contents:

    Paul;5;4;5;3;4;5;5;4;2;4\nBeth;3;4;2;4;4;2;3;1;3;3\nRuth;4;5;5;4;5;5;4;5;4;4

    This following program creates a dictionary grades based on the contents of the file. The keys are the names of the students, and the value attached to each key is the list of grades attained by the student. The program converts the grades to integer values, so they can be processed easier.

    grades = {}\nwith open(\"grades.csv\") as new_file:\n    for line in new_file:\n        line = line.replace(\"\\n\", \"\")\n        parts = line.split(\";\")\n        name = parts[0]\n        grades[name] = []\n        for grade in parts[1:]:\n            grades[name].append(int(grade))\n\nprint(grades)

    {'Paul': [5, 4, 5, 3, 4, 5, 5, 4, 2, 4], 'Beth': [3, 4, 2, 4, 4, 2, 3, 1, 3, 3], 'Ruth': [4, 5, 5, 4, 5, 5, 4, 5, 4, 4]}

    Now we can print out some statistics on each student based on the contents of the dictionary grades:

    for name, grade_list in grades.items():\n    best = max(grade_list)\n    average = sum(grade_list) / len(grade_list)\n    print(f\"{name}: best grade {best}, average {average:.2f}\")

    Paul: best grade 5, average 4.10\nBeth: best grade 4, average 2.90\nRuth: best grade 5, average 4.50

    Please take a careful look at the program in the example above. It may seem a bit complicated at first, but the technique can be used on files containing many different types of data.

    Removing unnecessary lines, spaces and line breaks

    Let's assume we have a CSV file containing some names, which has been exported from Excel:

    first; last\nPaul; Python\nJean; Java\nHarry; Haskell

    Excel is notorious for adding extra whitespace. Here we have an extra space character between the items, after each semicolon.

    We would like to print out the last names of each person on the list. The first line contains the headers for the data, and it can be safely ignored:

    last_names = []\nwith open(\"people.csv\") as new_file:\n    for line in new_file:\n        parts = line.split(\";\")\n        # ignore the header line\n        if parts[0] == \"first\":\n            continue\n        last_names.append(parts[1])\n\nprint(last_names)

    Exectuing this would print out

    [' Python\\n', ' Java\\n', ' Haskell']

    The first two items have a line break character at the end, and all three have an extra leading space character.

    We have already used the replace method to remove extra whitespace, but a more efficient solution is to use the Python string method strip, which removes whitespace from the beginning and end of a string. It removes all spaces, line breaks, tabs and other characters which would not normally be printed out.

    You can try it out in the Python console:

    >>> \" tryout \".strip()\n'tryout'\n>>> \"\\n\\ntest\\n\".strip()\n'test'\n>>>

    Stripping the string requires only a small change to the program:

    last_names = []\nwith open(\"people.csv\") as new_file:\n    for line in new_file:\n        parts = line.split(';')\n        if parts[0] == \"first\":\n            continue # this was the header line, so it is ignored\n        last_names.append(parts[1].strip())\nprint(last_names)

    Now we have the desired tidy printout:

    ['Python', 'Java', 'Haskell']

    There are also the related string methods lstrip and rstrip. They remove only the leading or trailing unprintable characters, l for the left edge of the string and r for the right:

    >>> \" teststring  \".rstrip()\n' teststring'\n>>> \" teststring  \".lstrip()\n'teststring  '

    Combining data from different files

    It is very common for the data processed by a program to be scattered in multiple files. Lets have a look at a situation where the personal details of the personnel of a company are stored in a file called employees.csv:

    pic;name;address;city\n080488-123X;Pekka Mikkola;Vilppulantie 7;00700 Helsinki\n290274-044S;Liisa Marttinen;Mannerheimintie 100 A 10;00100 Helsinki\n010479-007Z;Arto Vihavainen;Pihapolku 4;01010 Kerava\n010499-345K;Leevi Hellas;Tapiolantie 11 B;02000 Espoo

    The salaries are stored in a separate file salaries.csv:

    pic;salary;bonus\n080488-123X;3300;0\n290274-044S;4150;200\n010479-007Z;1300;1200

    Each data line in both files contains the personal identity code, which identifies whose data we are dealing with. Using the personal identity code as a common factor, it is easy to connect the names and salaries of each employee. We can, for example, print out the following list of monthly incomes:

    \nincomes:\nPekka Mikkola    3300 euros\nLiisa Marttinen  4350 euros\nArto Vihavainen  2500 euros\n

    This program uses two dictionaries as helper data structures: names and salaries. Both use the PIC as key:

    names = {}\n\nwith open(\"employees.csv\") as new_file:\n    for line in new_file:\n        parts = line.split(';')\n        if parts[0] == \"pic\":\n            continue\n        names[parts[0]] = parts[1]\n\nsalaries = {}\n\nwith open(\"salaries.csv\") as new_file:\n    for line in new_file:\n        parts = line.split(';')\n        if parts[0] == \"pic\":\n            continue\n        salaries[parts[0]] = int(parts[1]) +int(parts[2])\n\nprint(\"incomes:\")\n\nfor pic, name in names.items():\n    if pic in salaries:\n        salary = salaries[pic]\n        print(f\"{name:16} {salary} euros\")\n    else:\n        print(f\"{name:16} 0 euros\")

    First the program produces the dictionaries names and salaries. They have the following contents:

    {\n    '080488-123X': 'Pekka Mikkola',\n    '290274-044S': 'Liisa Marttinen',\n    '010479-007Z': 'Arto Vihavainen',\n    '010499-345K': 'Leevi Hellas'\n}\n\n{\n    '080488-123X': 3300,\n    '290274-044S': 4350,\n    '010479-007Z': 2500\n}

    The for loop at the end of the program combines the names of the employees with their respective salaries.

    The program also takes care of situations where the employee's pic is not present in the salary file.

    Remember, the order in which items are stored in a dictionary does not matter, as the keys are processed based on hash values.

    This program works with two CSV files. One of them contains information about some students on a course:

    id;first;last\n12345678;peter;pythons\n12345687;jean;javanese\n12345699;alice;adder

    The other contains the number of exercises each student has completed each week:

    id;e1;e2;e3;e4;e5;e6;e7\n12345678;4;1;1;4;5;2;4\n12345687;3;5;3;1;5;4;6\n12345699;10;2;2;7;10;2;2

    As you can see above, both CSV files also have a header row, which tells you what each column contains.

    Please write a program which asks the user for the names of these two files, reads the files, and then prints out the total number of exercises completed by each student. If the files have the contents in the examples above, the program should print out the following:

    Student information: students1.csv\nExercises completed: exercises1.csv\npekka peloton 21\njaana javanainen 27\nliisa virtanen 35

    Hint: while testing your program, you may quickly run out of patience if you always have to type in the file names at the prompt. You might want to hard-code the user input, like so:

    if False:\n    # this is never executed\n    student_info = input(\"Student information: \")\n    exercise_data = input(\"Exercises completed: \")\nelse:\n    # hard-coded input\n    student_info = \"students1.csv\"\n    exercise_data = \"exercises1.csv\"

    The actual functionality of the program is now \"hidden\" in the False branch of an if statement. It will never be executed.

    Now, if you want to quickly verify the program works correctly also with user input, you can just replace False with True:

    \nif True:\n    student_info = input(\"Student information: \")\n    exercise_data = input(\"Exercises completed: \")\nelse:\n    # now this is the False branch, and is never executed\n    student_info = \"students1.csv\"\n    exercise_data = \"exercises1.csv\"

    When you have verified your program works correctly, you can remove the if structure, keeping the commands asking for input.

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    NB2: If Visual Studio can't find the file and you have checked that there are no spelling errors, take a look at these instructions.

    Let's expand the program created in the previous exercise. Now also the exam points awarded to each student are contained in a CSV file. The contents of the file follow this format:

    id;e1;e2;e3\n12345678;4;1;4\n12345687;3;5;3\n12345699;10;2;2

    In the above example the student whose student number is 12345678 was awarded 4+1+4 points in the exam, which equals a total of 9 points.

    The program should again ask the user for the names of the files. Then the program should process the files and print out a grade for each student.

    Student information: students1.csv\nExercises completed: exercises1.csv\nExam points: exam_points1.csv\npekka peloton 0\njaana javanainen 1\nliisa virtanen 3

    Each completed exercise is counted towards exercise points, so that completing at least 10 % of the total exercices awards 1 point, completing at least 20 % awards 2 points, etc. Completing all 40 exercises awards 10 points. The number of points awarded is always an integer number.

    The final grade for the course is determined based on the sum of exam and exercise points according to the following table:

    \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
    exam points + exercise pointsgrade
    0-140 (fail)
    15-171
    18-202
    21-233
    24-274
    28-5

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    This exercise will continue from the previous one. Now we shall print out some statistics based on the CSV files.

    Student information: students1.csv\nExercises completed: exercises1.csv\nExam points: exam_points1.csv

    \nname                          exec_nbr  exec_pts. exm_pts.  tot_pts.  grade\npekka peloton                 21        5         9         14        0\njaana javanainen              27        6         11        17        1\nliisa virtanen                35        8         14        22        3\n

    Each row contains the information for a single student. The number of exercises completed, the number of exercise points awarded, the number of exam points awarded, the total number of points awarded, and the grade are all displayed in tidy columns. The width of the column for the name should be 30 characters, while the other columns should be 10 characters wide.

    You might find the f-strings covered in part 4 useful here.

    F-strings differentiate between strings and numbers when it comes to justifying columns:

    word = \"python\"\nprint(f\"{word:10}continues\")\nprint(f\"{word:>10}continues\")
    \npython    continues\n    pythoncontinues\n

    As you can see above, by default strings are justified to the left edge of the area specified for them. The > symbol can be used to justify to the right edge.

    With number values the logic is reversed:

    number = 42\nprint(f\"{number:10}continues\")\nprint(f\"{number:<10}continues\")
    \n        42continues\n42        continues\n

    With numbers the default behaviour is to justify to the right edge. The symbol < can be used to justify to the left edge.

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    Please write a program which asks the user to type in some text. Your program should then perform a spell check, and print out feedback to the user, so that all misspelled words have stars around them. Please see the two examples below:

    Write text: We use ptython to make a spell checker

    \nWe use *ptython* to make a spell checker\n

    Write text: This is acually a good and usefull program

    \nThis is *acually* good and *usefull* program\n

    The case of the letters should be irrelevant to the functioning of your program.

    The exercise template includes the file wordlist.txt, which contains all the words the spell checker should accept as correct.

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    NB2 If Visual Studio can't find the file and you have checked that there are no spelling errors, take a look at these instructions.

    This exercise is about creating a program which allows the user to search for recipes based on their names, preparation times, or ingredients used. The program should read the recipes from a file submitted by the user.

    Each recipe consists of three or more lines. The first line has the name of the recipe, the second line contains an integer number representing the preparation time in minutes, and the remaining line or lines contain the ingredients used, one on each line. The recipe ends with an empty line, with the exception of the final recipe in the file which just ends with the end of the file. So, there can be more than one recipe in a single file, like in the example below.

    Pancakes\n15\nmilk\neggs\nflour\nsugar\nsalt\nbutter\n\nMeatballs\n45\nmince\neggs\nbreadcrumbs\n\nTofu rolls\n30\ntofu\nrice\nwater\ncarrot\ncucumber\navocado\nwasabi\n\nCake pops\n60\nmilk\nbicarbonate\neggs\nsalt\nsugar\ncardamom\nbutter

    Hint: it might be best to first read through all the lines in the file and pop them into a list, which is then easier to manipulate in the way described in the exercise.

    Search for recipes based on the name of the recipe

    Please write a function named search_by_name(filename: str, word: str), which takes a filename and a search string as its arguments. The function should go through the file and select all recipes whose name contains the given search string. The names of these recipes are then returned in a list.

    An example of the function in action:

    found_recipes = search_by_name(\"recipes1.txt\", \"cake\")\n\nfor recipe in found_recipes:\n    print(recipe)

    Pancakes\nCake pops

    As you can see in the example above, the case of the letters is irrelevant. The search term cake returns both Pancakes and Cake pops, even though the latter is capitalized.

    NB: If Visual Studio can't find the file and you have checked that there are no spelling errors, take a look at these instructions.

    Search for recipes based on the preparation time

    Please write a function named search_by_time(filename: str, prep_time: int), which takes a filename and an integer as its arguments. The function should go through the file and select all recipes whose preparation time is at most the number given.

    The names of these recipes are again returned in a list, but the preparation time should be appended to each name. Please have a look at the example below.

    found_recipes = search_by_time(\"recipes1.txt\", 20)\n\nfor recipe in found_recipes:\n    print(recipe)

    Pancakes, preparation time 15 min

    Search for recipes based on the ingredients

    A word of caution: this third part of the exercise is considerably more demanding than the previous two. If you feel like you aren't making headway, it may be worth your while to move on, complete the other exercises in this part of the material, and then come back to this exercise if you have time later. Remember, you can submit and receive points for the first two parts of this exercise even if you haven't completed the third part.

    Please write a function named search_by_ingredient(filename: str, ingredient: str), which takes a filename and a search string as its arguments. The function should go through the file and select all recipes whose ingredients contain the given search string.

    The names of these recipes are returned in a list just like in the second part, with the preparation time appended. Please have a look at the example below.

    found_recipes = search_by_ingredient(\"recipes1.txt\", \"eggs\")\n\nfor recipe in found_recipes:\n    print(recipe)

    Pancakes, preparation time 15 min\nMeatballs, preparation time 45 min\nCake pops, preparation time 60 min

    In this exercise we will write some functions for working on a file containing location data from the stations for city bikes in Helsinki.

    Each file will follow this format:

    Longitude;Latitude;FID;name;total_slot;operative;id\n24.950292890004903;60.155444793742276;1;Kaivopuisto;30;Yes;001\n24.956347471358754;60.160959093887129;2;Laivasillankatu;12;Yes;002\n24.944927399779715;60.158189199971673;3;Kapteeninpuistikko;16;Yes;003

    Each station has a single line in the file. The line contains the coordinates, name, and other identifying information for the station.

    Distance between stations

    First, write a function named get_station_data(filename: str). This function should read the names and locations of all the stations in the file, and return them in a dictionary with the following format:

    \n{\n  \"Kaivopuisto\": (24.950292890004903, 60.155444793742276),\n  \"Laivasillankatu\": (24.956347471358754, 60.160959093887129),\n  \"Kapteeninpuistikko\": (24.944927399779715, 60.158189199971673)\n}\n

    Dictionary keys are the names of the stations, and the value attached is a tuple containing the location coordinates of the station. The first element in the tuple is the Longitude field, and the second is the Latitude field.

    Next, write a function named distance(stations: dict, station1: str, station2: str), which returns the distance between the two stations given as arguments.

    The distance is calculated using the Pythagorean theorem. The multiplication factors below are approximate values for converting latitudes and longitudes to distances in kilometres in the Helsinki region.

    # we will need the function sqrt from the math module \nimport math\n\nx_km = (longitude1 - longitude2) * 55.26\ny_km = (latitude1 - latitude2) * 111.2\ndistance_km = math.sqrt(x_km**2 + y_km**2)

    Some examples of the function in action:

    stations = get_station_data('stations1.csv')\nd = distance(stations, \"Designmuseo\", \"Hietalahdentori\")\nprint(d)\nd = distance(stations, \"Viiskulma\", \"Kaivopuisto\")\nprint(d)

    0.9032737292463177\n0.7753594392019532

    NB: If Visual Studio can't find the file and you have checked that there are no spelling errors, take a look at these instructions.

    The greatest distance

    Please write a function named greatest_distance(stations: dict), which works out the two stations on the list with the greatest distance from each other. The function should return a tuple, where the first two elements are the names of the two stations, and the third element is the distance between the two.

    stations = get_station_data('stations1.csv')\nstation1, station2, greatest = greatest_distance(stations)\nprint(station1, station2, greatest)

    Laivasillankatu Hietalahdentori 1.478708873076181

    ","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-6/1-reading-files.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know how to read the contents of files with Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what a text file and a CSV file are"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to process the contents of a CSV file in your programs"}]},{"type":"text","value":"\n"}]}]},{"type":"comment","value":"the same text is in sections 3-1, 5-1 and 6-1, check them all if you're changing this"},{"type":"element","tagName":"text-box","properties":{"variant":"hint","name":"About the exercises on this course"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Becoming a proficient programmer requires a lot of practice, sometimes even quite mechanical practice. It also involves developing problem solving skills and applying intuition. This is why there are a lot of exercises of different kinds on this course. Some of them ask you to quite straightforwardly apply what you have learnt in the material, but some of them are intentionally more challenging and open-ended."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some of the exercises might at first seem overwhelming, but this is nothing to worry about. None of the exercises is strictly mandatory, and in fact "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"only 25 % of the points in each part is required to pass the course."}]},{"type":"text","value":" You can find more details about passing the course on the "},{"type":"element","tagName":"a","properties":{"href":"/grading-and-exams"},"children":[{"type":"text","value":"page on grading"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"The exercises are not in any specific order of difficulty."}]},{"type":"text","value":" Each section usually introduces some new programming concepts, and these are then practised with both simpler and more complicated exercises. "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"If you come across an exercise that feels too difficult, move on to the next one."}]},{"type":"text","value":" You can always come back to the more difficult exercises if you have time later."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the going inevitably gets tough, a word of consolation: a task that seems impossibly difficult this week will likely feel rather easy in about four weeks' time."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A very common use case for programming is handling data stored in files. Programs can read data from files and write the computed results to files. Even large amounts of data become easy to process automatically when files are used."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"On this course we only deal with "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"text files"}]},{"type":"text","value":". All the files used will consist of lines of text. As an example, the Visual Studio Code editor we use on this course works with text files. NB: even though word processors such as Microsoft Word are usually used with files that contain text, Word documents themselves aren't text files. They also contain formatting information, and are encoded in a way that makes handling them in a program more complicated."}]},{"type":"element","tagName":"h2","properties":{"id":"reading-data-from-a-file","style":"position:relative;"},"children":[{"type":"text","value":"Reading data from a file"},{"type":"element","tagName":"a","properties":{"href":"#reading-data-from-a-file","ariaLabel":"reading data from a file 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":"Let's first work with a file called "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"example.txt"}]},{"type":"text","value":", with the following contents:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hello there!\nThis example file contains three lines of text.\nThis is the last line."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A simple way to include files in a Python program is to use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" statement. The header line opens the file, and the block where the file can be accessed follows. After the block the file is automatically closed, and can no longer be accessed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, the following code opens the file, reads the contents, prints them out, and then closes the file:"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"example.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n contents "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"read"},{"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":"contents"},{"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":"Hello there!\nThis example file contains three lines of text.\nThis is the last line."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"new_file"}]},{"type":"text","value":" above is a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"file handle"}]},{"type":"text","value":". Through it the file can accessed while it is still open. Here we used the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"read"}]},{"type":"text","value":", which returns the contents of the file as a single string. So, in this case the string returned by "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"read"}]},{"type":"text","value":" would be"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"text"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-text"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"Hello there!\\nThis example file contains three lines of text.\\nThis is the last line.\""}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"going-through-the-contents-of-a-file","style":"position:relative;"},"children":[{"type":"text","value":"Going through the contents of a file"},{"type":"element","tagName":"a","properties":{"href":"#going-through-the-contents-of-a-file","ariaLabel":"going through the contents of a file 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 "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"read"}]},{"type":"text","value":" method is useful for printing out the contents of the entire file, but more often we will want to go through the file line by line."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Text files can be thought of as lists of strings, each string representing a single line in the file. We can go through the list with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following example reads our example file using a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop, removes line breaks from the end of each line, counts the number of lines, and prints each line with its line number. It also keeps track of the length of the lines:"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"example.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n count "},{"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":"text","value":"\n total_length "},{"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":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n line "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"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":"\"\\n\""}]},{"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n count "},{"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":"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":"\"Line\""}]},{"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":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n length "},{"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":"line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n total_length "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" length\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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Total length of lines:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" total_length"},{"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":"Line 1 Hello there!\nLine 2 This example file contains three lines of text.\nLine 3 This is the last line.\nTotal length of lines: 81"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There is a line break "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\\n"}]},{"type":"text","value":" at the end of each line in the file, but the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" function also adds a line break by default. There are no extra line breaks in the printout above, because the line breaks at the ends of the lines are removed with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"replace"}]},{"type":"text","value":" method. It replaces each line break character with an empty string. This way the lengths of the lines are also calculated correctly."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Largest number","tmcname":"part06-01_largest_number"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"numbers.txt"}]},{"type":"text","value":" contains integer numbers, one number per line. The contents could look like this:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"45"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"108"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","parameter","variable"]},"children":[{"type":"text","value":"-10"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1100"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":".."}]},{"type":"text","value":".etc"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":".."}]},{"type":"text","value":"."}]}]}]},{"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":"largest"}]},{"type":"text","value":", which reads the file and returns the largest number in the file."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice that the function does not take any arguments. The file you are working with is always named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"numbers.txt"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" If Visual Studio Code can't find the file and you have checked that there are no spelling errors, take a look at the instructions following this exercise."}]}]},{"type":"element","tagName":"h2","properties":{"id":"what-if-visual-studio-code-cannot-find-my-file","style":"position:relative;"},"children":[{"type":"text","value":"What if Visual Studio Code cannot find my file?"},{"type":"element","tagName":"a","properties":{"href":"#what-if-visual-studio-code-cannot-find-my-file","ariaLabel":"what if visual studio code cannot find my file 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":"When you execute your code, Visual Studio Code might complain about not finding the file, even though you have checked and double-checked the file name was written correctly, and the file exists. Changing the following setting may fix the problem:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Open the settings from the menu bar: "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"File"}]},{"type":"text","value":" -> "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Preferences"}]},{"type":"text","value":" -> "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Settings"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Find the relevant setting with the search term \"executeinfile\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Choose the tab "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Workspace"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Select the option under "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Python"}]},{"type":"text","value":" -> "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Terminal"}]},{"type":"text","value":" -> "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Execute In File Dir"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your settings window should now look somewhat like this:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/6e5d89f76dfd47af6f9ea67b097452e8/e405b/6_1_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: 16.521739130434785%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAADCAIAAAAcOLh5AAAACXBIWXMAAAsTAAALEwEAmpwYAAAAZElEQVQI11XOQQqAMAwEwP5DknRNkNIilaInvXn3/8+RKIoOOe4uCQAQHYDWWq01/xS/UnLO07rP+9HbEEU8DQT+SCmZmZqlB77GRdvGUCa687+yiDAzVH3i8hb9MyZQJ0xv/gSEjBk5LU20dgAAAABJRU5ErkJggg=='); 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":["/static/6e5d89f76dfd47af6f9ea67b097452e8/a0b58/6_1_1.webp 230w","/static/6e5d89f76dfd47af6f9ea67b097452e8/bc10c/6_1_1.webp 460w","/static/6e5d89f76dfd47af6f9ea67b097452e8/966d8/6_1_1.webp 920w","/static/6e5d89f76dfd47af6f9ea67b097452e8/445df/6_1_1.webp 1380w","/static/6e5d89f76dfd47af6f9ea67b097452e8/9b2c8/6_1_1.webp 1566w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/6e5d89f76dfd47af6f9ea67b097452e8/81c8e/6_1_1.png 230w","/static/6e5d89f76dfd47af6f9ea67b097452e8/08a84/6_1_1.png 460w","/static/6e5d89f76dfd47af6f9ea67b097452e8/c0255/6_1_1.png 920w","/static/6e5d89f76dfd47af6f9ea67b097452e8/b1001/6_1_1.png 1380w","/static/6e5d89f76dfd47af6f9ea67b097452e8/e405b/6_1_1.png 1566w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/6e5d89f76dfd47af6f9ea67b097452e8/c0255/6_1_1.png","alt":"6 1 1","title":"6 1 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":"p","properties":{},"children":[{"type":"text","value":"If this doesn't help, you can copy the file in the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"src"}]},{"type":"text","value":" directory"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/a57483e6084d39a596ee24c41a42de70/6297e/6_1_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: 22.17391304347826%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAECAIAAAABPYjBAAAACXBIWXMAABYlAAAWJQFJUiTwAAAApklEQVQI123N0QqCQBAFUH/CUVlnt3JnVtcSyx1XikKCHur/PyhQih463KcLl5s87rcQRcLoa98037TO1czsFtba6p/kFESm8+U6HyTuT4FcY11Dtd9WVH4gol7keQ4Aa4OIySBjfxwkyKuLc90P5ImYiHeWtdmYRVmWSqmiKLIsAwCllDFGa50EiW3Xj3F6tjKQr5YlE28tozbrISKmaQoAiPg7fgOYxyLFEKvBSAAAAABJRU5ErkJggg=='); 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":["/static/a57483e6084d39a596ee24c41a42de70/a0b58/6_1_2.webp 230w","/static/a57483e6084d39a596ee24c41a42de70/bc10c/6_1_2.webp 460w","/static/a57483e6084d39a596ee24c41a42de70/966d8/6_1_2.webp 920w","/static/a57483e6084d39a596ee24c41a42de70/445df/6_1_2.webp 1380w","/static/a57483e6084d39a596ee24c41a42de70/78de1/6_1_2.webp 1840w","/static/a57483e6084d39a596ee24c41a42de70/4b7c9/6_1_2.webp 2044w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/a57483e6084d39a596ee24c41a42de70/81c8e/6_1_2.png 230w","/static/a57483e6084d39a596ee24c41a42de70/08a84/6_1_2.png 460w","/static/a57483e6084d39a596ee24c41a42de70/c0255/6_1_2.png 920w","/static/a57483e6084d39a596ee24c41a42de70/b1001/6_1_2.png 1380w","/static/a57483e6084d39a596ee24c41a42de70/161ec/6_1_2.png 1840w","/static/a57483e6084d39a596ee24c41a42de70/6297e/6_1_2.png 2044w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/a57483e6084d39a596ee24c41a42de70/c0255/6_1_2.png","alt":"6 1 2","title":"6 1 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":"directly to the root of the exercise directory"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/bc83b27162a67ce98f825821dc304a7e/6297e/6_1_3.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: 26.956521739130434%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAIAAADKYVtkAAAACXBIWXMAABYlAAAWJQFJUiTwAAAAt0lEQVQY023P3Q6CMAwFYN6DjqjtNta1MxlDdILGxBvf/4UMGO/4cm5Pf5pa79N0m+eHSBJJMSqzMKv3vaqklPq+995ba51zdkNkPR2COzallJzL8/lalketc86FWWJUIsfMqtq2bdd1sDF/AOaE2AzDUIYyjmPOWUQRiWidjYgxRlU1xnQ7DBGtmyWdy2ViZqL1ul8QSSQyMwDsVI1BxOZ6md65fsYlyTms3/6iznnd7JYBIITwBcyRLmVWTxqEAAAAAElFTkSuQmCC'); 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":["/static/bc83b27162a67ce98f825821dc304a7e/a0b58/6_1_3.webp 230w","/static/bc83b27162a67ce98f825821dc304a7e/bc10c/6_1_3.webp 460w","/static/bc83b27162a67ce98f825821dc304a7e/966d8/6_1_3.webp 920w","/static/bc83b27162a67ce98f825821dc304a7e/445df/6_1_3.webp 1380w","/static/bc83b27162a67ce98f825821dc304a7e/78de1/6_1_3.webp 1840w","/static/bc83b27162a67ce98f825821dc304a7e/4b7c9/6_1_3.webp 2044w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/bc83b27162a67ce98f825821dc304a7e/81c8e/6_1_3.png 230w","/static/bc83b27162a67ce98f825821dc304a7e/08a84/6_1_3.png 460w","/static/bc83b27162a67ce98f825821dc304a7e/c0255/6_1_3.png 920w","/static/bc83b27162a67ce98f825821dc304a7e/b1001/6_1_3.png 1380w","/static/bc83b27162a67ce98f825821dc304a7e/161ec/6_1_3.png 1840w","/static/bc83b27162a67ce98f825821dc304a7e/6297e/6_1_3.png 2044w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/bc83b27162a67ce98f825821dc304a7e/c0255/6_1_3.png","alt":"6 1 3","title":"6 1 3","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":"h2","properties":{"id":"debugging-code-which-handles-files","style":"position:relative;"},"children":[{"type":"text","value":"Debugging code which handles files"},{"type":"element","tagName":"a","properties":{"href":"#debugging-code-which-handles-files","ariaLabel":"debugging code which handles files 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":"Using the Visual Studio Code "},{"type":"element","tagName":"a","properties":{"href":"/part-4/1-vscode#the-built-in-debugger"},"children":[{"type":"text","value":"debugger"}]},{"type":"text","value":" with programs which handle files will often result in a nasty looking error message:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/b0c7745f7a4cc9c485a52d483e0aa290/eff3b/6_1_4.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: 31.304347826086953%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAGCAIAAABM9SnKAAAACXBIWXMAABYlAAAWJQFJUiTwAAABMklEQVQY003PyU7CQACA4d7EBWQpLdDS6XSWzkw702VIIBESgyaYWBZRE60QUIzv/wguB0PyX7/Db2itx5PJcDiKYym4ICFXaSKkZFKGMhUyESKmlAEY+BD5EAGIMA5iThhjRpZlOhsooTjlmFAKgaBgPMSjHOS8kwtHx55itgqt4xIllEqNPM85F4xxSmhAyAziDabbiG8F28voBcGnvrNot+ZmY2E252azMBtFvSZ7XQADQ19dI4QJoX+Y3vXhgcbvcbYL5Z6pHYlKQEpAS0DWrd6yZi3r1uK0Hpm26wND6xxj8o9nrv8Zxl/p4CCSQ5R8cFV6aAPwm4eeLWfd7Dy2OqvzRtS2XQB+n4/x1IOvNHrw8dIBSwcUtlPYzsqF9+3e7dnltFKdVqo3JxfC7v7gb6ThPL16POKXAAAAAElFTkSuQmCC'); 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":["/static/b0c7745f7a4cc9c485a52d483e0aa290/a0b58/6_1_4.webp 230w","/static/b0c7745f7a4cc9c485a52d483e0aa290/bc10c/6_1_4.webp 460w","/static/b0c7745f7a4cc9c485a52d483e0aa290/966d8/6_1_4.webp 920w","/static/b0c7745f7a4cc9c485a52d483e0aa290/445df/6_1_4.webp 1380w","/static/b0c7745f7a4cc9c485a52d483e0aa290/78de1/6_1_4.webp 1840w","/static/b0c7745f7a4cc9c485a52d483e0aa290/5340a/6_1_4.webp 2072w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/b0c7745f7a4cc9c485a52d483e0aa290/81c8e/6_1_4.png 230w","/static/b0c7745f7a4cc9c485a52d483e0aa290/08a84/6_1_4.png 460w","/static/b0c7745f7a4cc9c485a52d483e0aa290/c0255/6_1_4.png 920w","/static/b0c7745f7a4cc9c485a52d483e0aa290/b1001/6_1_4.png 1380w","/static/b0c7745f7a4cc9c485a52d483e0aa290/161ec/6_1_4.png 1840w","/static/b0c7745f7a4cc9c485a52d483e0aa290/eff3b/6_1_4.png 2072w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/b0c7745f7a4cc9c485a52d483e0aa290/c0255/6_1_4.png","alt":"6 1 4","title":"6 1 4","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 reason is that the debugger will always look for files in the root of the exercise directory. The "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Execute In File Dir"}]},{"type":"text","value":" setting mentioned above will have no effect on this. The easiest solution is to just copy the file to the root of the directory."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may need to restart Visual Studio Code after copying all the necessary files."}]},{"type":"element","tagName":"h2","properties":{"id":"reading-csv-files","style":"position:relative;"},"children":[{"type":"text","value":"Reading CSV files"},{"type":"element","tagName":"a","properties":{"href":"#reading-csv-files","ariaLabel":"reading csv files 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":"A CSV file, short for "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"comma-separated Values"}]},{"type":"text","value":", is a text file which contains data separated by a predetermined character. The most common characters used for this purpose are the comma "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" and the semicolon "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":", but any character is, in principle, possible."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"CSV files are commonly used to store records of different kinds. Many database and spreadsheet programs, such as Excel, can import and export data in CSV format, which makes data exchange between different systems easy."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We already learnt we can go through the lines in a file with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop, but how can we separate the different fields on a single line? Python has a string method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":" for just this purpose. The method takes the separator character(s) as a string argument, and returns the contents of the target string as a list of strings, separated at the separator."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how the method works:"}]},{"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":"text "},{"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":"\"monkey,banana,harpsichord\""}]},{"type":"text","value":"\nwords "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" text"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" words"},{"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":"word"},{"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":"monkey\nbanana\nharpsichord"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's assume we have a file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"grades.csv"}]},{"type":"text","value":", which contains names of students and the grades they received on some courses. Each line has the data of a single student, and the data is separated by a semicolon."}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Paul;5;4;5;3;4;5;5;4;2;4\nBeth;3;4;2;4;4;2;3;1;3;3\nRuth;4;5;5;4;5;5;4;5;4;4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program goes through the file line by line, splits each line into its separate parts, and prints out the name and grades of each student."}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"grades.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n line "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"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":"\"\\n\""}]},{"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" parts"},{"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":"\n grades "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" parts"},{"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":"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":"\"Name:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"\"Grades:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" grades"},{"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":"Name: Paul\nGrades: ['5', '4', '5', '3', '4', '5', '5', '4', '2', '4']\nName: Beth\nGrades: ['3', '4', '2', '4', '4', '2', '3', '1', '3', '3']\nName: Ruth\nGrades: ['4', '5', '5', '4', '5', '5', '4', '5', '4', '4']"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Fruit market","tmcname":"part06-02_fruit_market"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fruits.csv"}]},{"type":"text","value":" contains names of fruits, and their prices, in the format specified in this example:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"text","value":"banana"},{"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.50"}]},{"type":"text","value":"\napple"},{"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.95"}]},{"type":"text","value":"\norange"},{"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.0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":".."}]},{"type":"text","value":".etc"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":".."}]},{"type":"text","value":"."}]}]}]},{"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":"read_fruits"}]},{"type":"text","value":", which reads the file and returns a dictionary based on the contents. In the dictionary, the name of the fruit should be the key, and the value should be its price. Prices should be of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the function does not take any arguments. The file you are working with is always named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fruits.csv"}]},{"type":"text","value":"."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Matrix","tmcname":"part06-03_matrix"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"matrix.txt"}]},{"type":"text","value":" contains a matrix in the format specified in the example below:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1,0"}]},{"type":"text","value":",2,8,2,1,3,2,5,2,2,2\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"9,2"}]},{"type":"text","value":",4,5,2,4,2,4,1,10,4,2\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":".."}]},{"type":"text","value":".etc"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":".."}]},{"type":"text","value":"."}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write two functions, named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"matrix_sum"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"matrix_max"}]},{"type":"text","value":". Both go through the matrix in the file, and then return the sum of the elements or the element with the greatest value, as the names of the functions imply."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please also write the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"row_sums"}]},{"type":"text","value":", which returns a list containing the sum of each row in the matrix. For example, calling "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"row_sums"}]},{"type":"text","value":" when the matrix in the file is defined as"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1,2"}]},{"type":"text","value":",3\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2,3"}]},{"type":"text","value":",4"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"the function should return the list "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[6, 9]"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hint: you can also include other helper functions in your program. It is very worthwhile to spend a moment considering which functionalities are shared by the three functions you are asked to write. Notice that the three functions named above do not take any arguments, but any helper functions you write may take arguments. The file you are working with is always named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"matrix.txt"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" If Visual Studio Code can't find the file and you have checked that there are no spelling errors, take a look at the instructions before this exercise."}]}]},{"type":"element","tagName":"h2","properties":{"id":"reading-the-same-file-multiple-times","style":"position:relative;"},"children":[{"type":"text","value":"Reading the same file multiple times"},{"type":"element","tagName":"a","properties":{"href":"#reading-the-same-file-multiple-times","ariaLabel":"reading the same file multiple times 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":"Sometimes it is necessary to process the contents of a file more than once in a single program. Let's have a look at a program which works with some personal data stored in a CSV file:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"text","value":"\nPeter;40;Helsinki\nEmily;34;Espoo\nEric;42;London\nAdam;100;Amsterdam\nAlice;58;Paris\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":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"people.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"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":"# print out the names"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 "},{"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":"\"Name:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" parts"},{"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\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# find the oldest"}]},{"type":"text","value":"\n age_of_oldest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" parts"},{"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":"\n age "},{"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":"text","value":"parts"},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" age_of_oldest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n age_of_oldest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" age\n oldest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\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 oldest is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" oldest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Running this will result in a somewhat cryptic error message:"}]},{"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":"Traceback "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"most recent call 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":"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 oldest is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" oldest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nUnboundLocalError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" local variable "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'oldest'"}]},{"type":"text","value":" referenced before assignment"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The reason this happens is that the latter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop is not executed at all, beacuse the file can only be processed once. Once the last line is read, the file handle rests at the end of the file, and the data in the file can no longer be accessed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we want to access the contents in the second "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop, we will have to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"open"}]},{"type":"text","value":" the file a second time:"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"people.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"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":"# print out the names"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 "},{"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":"\"Name:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" parts"},{"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"people.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"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":"# find the oldest"}]},{"type":"text","value":"\n age_of_oldest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" parts"},{"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":"\n age "},{"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":"text","value":"parts"},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" age_of_oldest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n age_of_oldest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" age\n oldest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\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 oldest is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" oldest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"While the above code would work, it contains unnecessary repetition. It is usually best to read the file just once, and store its contents in an appropriate format for further processing:"}]},{"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":"people "},{"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":"# read the contents of the file and store it in a list"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"people.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 people"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"parts"},{"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":" "},{"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":"text","value":"parts"},{"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":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" parts"},{"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":"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":"# print out the names"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" people"},{"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":"\"Name:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person"},{"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# find the oldest"}]},{"type":"text","value":"\nage_of_oldest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" people"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" person"},{"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":"\n age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" person"},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" age_of_oldest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n age_of_oldest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" age\n oldest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\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 oldest is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" oldest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"more-csv-file-processing","style":"position:relative;"},"children":[{"type":"text","value":"More CSV file processing"},{"type":"element","tagName":"a","properties":{"href":"#more-csv-file-processing","ariaLabel":"more csv file processing 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":"Let's continue with the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"grades.csv"}]},{"type":"text","value":", which has the following contents:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Paul;5;4;5;3;4;5;5;4;2;4\nBeth;3;4;2;4;4;2;3;1;3;3\nRuth;4;5;5;4;5;5;4;5;4;4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This following program creates a dictionary "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"grades"}]},{"type":"text","value":" based on the contents of the file. The keys are the names of the students, and the value attached to each key is the list of grades attained by the student. The program converts the grades to integer values, so they can be processed easier."}]},{"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":"grades "},{"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","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"grades.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n line "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"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":"\"\\n\""}]},{"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" parts"},{"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":"\n grades"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"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","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":"for"}]},{"type":"text","value":" grade "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" parts"},{"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":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n grades"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"name"},{"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":"append"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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":"text","value":"grade"},{"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":"print"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"grades"},{"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': [5, 4, 5, 3, 4, 5, 5, 4, 2, 4], 'Beth': [3, 4, 2, 4, 4, 2, 3, 1, 3, 3], 'Ruth': [4, 5, 5, 4, 5, 5, 4, 5, 4, 4]}"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now we can print out some statistics on each student based on the contents of the dictionary "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"grades"}]},{"type":"text","value":":"}]},{"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":"for"}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" grade_list "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" grades"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n best "},{"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":"max"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"grade_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n average "},{"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":"grade_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","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"grade_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","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":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":": best grade "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"best"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", average "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"average"},{"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":"Paul: best grade 5, average 4.10\nBeth: best grade 4, average 2.90\nRuth: best grade 5, average 4.50"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please take a careful look at the program in the example above. It may seem a bit complicated at first, but the technique can be used on files containing many different types of data."}]},{"type":"element","tagName":"h2","properties":{"id":"removing-unnecessary-lines-spaces-and-line-breaks","style":"position:relative;"},"children":[{"type":"text","value":"Removing unnecessary lines, spaces and line breaks"},{"type":"element","tagName":"a","properties":{"href":"#removing-unnecessary-lines-spaces-and-line-breaks","ariaLabel":"removing unnecessary lines spaces and line breaks 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":"Let's assume we have a CSV file containing some names, which has been exported from Excel:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"text","value":"first"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" last\nPaul"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" Python\nJean"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" Java\nHarry"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":" Haskell"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Excel is notorious for adding extra whitespace. Here we have an extra space character between the items, after each semicolon."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We would like to print out the last names of each person on the list. The first line contains the headers for the data, and it can be safely ignored:"}]},{"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":"last_names "},{"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","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"people.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# ignore the header line"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" parts"},{"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":"\"first\""}]},{"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":"continue"}]},{"type":"text","value":"\n last_names"},{"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":"parts"},{"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":"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":"last_names"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exectuing this would print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[' Python\\n', ' Java\\n', ' Haskell']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The first two items have a line break character at the end, and all three have an extra leading space character."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We have already used the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"replace"}]},{"type":"text","value":" method to remove extra whitespace, but a more efficient solution is to use the Python string method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"strip"}]},{"type":"text","value":", which removes whitespace from the beginning and end of a string. It removes all spaces, line breaks, tabs and other characters which would not normally be printed out."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can try it out in the Python console:"}]},{"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","operator"]},"children":[{"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":"\" tryout \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strip"},{"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","string"]},"children":[{"type":"text","value":"'tryout'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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":"\"\\n\\ntest\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strip"},{"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","string"]},"children":[{"type":"text","value":"'test'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">>"}]},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Stripping the string requires only a small change to the program:"}]},{"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":"last_names "},{"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","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"people.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" parts"},{"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":"\"first\""}]},{"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":"continue"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# this was the header line, so it is ignored"}]},{"type":"text","value":"\n last_names"},{"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":"parts"},{"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":"text","value":"strip"},{"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"},{"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":"last_names"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now we have the desired tidy printout:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['Python', 'Java', 'Haskell']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are also the related string methods "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lstrip"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rstrip"}]},{"type":"text","value":". They remove only the leading or trailing unprintable characters, l for the left edge of the string and r for the right:"}]},{"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","operator"]},"children":[{"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":"\" teststring \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rstrip"},{"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","string"]},"children":[{"type":"text","value":"' teststring'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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":"\" teststring \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lstrip"},{"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","string"]},"children":[{"type":"text","value":"'teststring '"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"combining-data-from-different-files","style":"position:relative;"},"children":[{"type":"text","value":"Combining data from different files"},{"type":"element","tagName":"a","properties":{"href":"#combining-data-from-different-files","ariaLabel":"combining data from different files 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":"It is very common for the data processed by a program to be scattered in multiple files. Lets have a look at a situation where the personal details of the personnel of a company are stored in a file called "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"employees.csv"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"pic;name;address;city"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"080488-123X;Pekka Mikkola;Vilppulantie 7;00700 Helsinki"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"290274-044S;Liisa Marttinen;Mannerheimintie 100 A 10;00100 Helsinki"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"010479-007Z;Arto Vihavainen;Pihapolku 4;01010 Kerava"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"010499-345K;Leevi Hellas;Tapiolantie 11 B;02000 Espoo"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The salaries are stored in a separate file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"salaries.csv"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"pic;salary;bonus"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"080488-123X;3300;0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"290274-044S;4150;200"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"010479-007Z;1300;1200"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each data line in both files contains the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"personal identity code"}]},{"type":"text","value":", which identifies whose data we are dealing with. Using the personal identity code as a common factor, it is easy to connect the names and salaries of each employee. We can, for example, print out the following list of monthly incomes:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"incomes:\nPekka Mikkola 3300 euros\nLiisa Marttinen 4350 euros\nArto Vihavainen 2500 euros\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program uses two dictionaries as helper data structures: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"names"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"salaries"}]},{"type":"text","value":". Both use the PIC as key:"}]},{"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":"names "},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"employees.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" parts"},{"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":"\"pic\""}]},{"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":"continue"}]},{"type":"text","value":"\n names"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"parts"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" parts"},{"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":"text","value":"\n\nsalaries "},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"salaries.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" new_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" new_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" parts"},{"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":"\"pic\""}]},{"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":"continue"}]},{"type":"text","value":"\n salaries"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"parts"},{"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":" "},{"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":"text","value":"parts"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"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":"text","value":"parts"},{"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":"\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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"incomes:\""}]},{"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":" pic"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" names"},{"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":"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":"if"}]},{"type":"text","value":" pic "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" salaries"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n salary "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" salaries"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"pic"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"name"},{"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":"16"}]},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"salary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euros\""}]}]},{"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":"else"}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"name"},{"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":"16"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" 0 euros\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First the program produces the dictionaries "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"names"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"salaries"}]},{"type":"text","value":". They have the following contents:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'080488-123X'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Pekka Mikkola'"}]},{"type":"text","value":",\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'290274-044S'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Liisa Marttinen'"}]},{"type":"text","value":",\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'010479-007Z'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Arto Vihavainen'"}]},{"type":"text","value":",\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'010499-345K'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'Leevi Hellas'"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'080488-123X'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3300"}]},{"type":"text","value":",\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'290274-044S'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"4350"}]},{"type":"text","value":",\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'010479-007Z'"}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2500"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop at the end of the program combines the names of the employees with their respective salaries."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program also takes care of situations where the employee's pic is not present in the salary file."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Remember, the order in which items are stored in a dictionary does not matter, as the keys are processed based on hash values."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Course grading, part 1","tmcname":"part06-04_course_grading_part_1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This program works with two CSV files. One of them contains information about some students on a course:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"id;first;last"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345678;peter;pythons"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345687;jean;javanese"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345699;alice;adder"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The other contains the number of exercises each student has completed each week:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"id;e1;e2;e3;e4;e5;e6;e7"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345678;4;1;1;4;5;2;4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345687;3;5;3;1;5;4;6"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345699;10;2;2;7;10;2;2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, both CSV files also have a header row, which tells you what each column contains."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for the names of these two files, reads the files, and then prints out the total number of exercises completed by each student. If the files have the contents in the examples above, the program should print out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Student information: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"students1.csv"}]},{"type":"text","value":"\nExercises completed: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"exercises1.csv"}]},{"type":"text","value":"\npekka peloton 21\njaana javanainen 27\nliisa virtanen 35"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hint: while testing your program, you may quickly run out of patience if you always have to type in the file names at the prompt. You might want to hard-code the user input, like so:"}]},{"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":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"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":"# this is never executed"}]},{"type":"text","value":"\n student_info "},{"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":"\"Student information: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n exercise_data "},{"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":"\"Exercises completed: \""}]},{"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":"else"}]},{"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":"# hard-coded input"}]},{"type":"text","value":"\n student_info "},{"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":"\"students1.csv\""}]},{"type":"text","value":"\n exercise_data "},{"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":"\"exercises1.csv\""}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The actual functionality of the program is now \"hidden\" in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" branch of an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" statement. It will never be executed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now, if you want to quickly verify the program works correctly also with user input, you can just replace "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":":"}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n student_info "},{"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":"\"Student information: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n exercise_data "},{"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":"\"Exercises completed: \""}]},{"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":"else"}]},{"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":"# now this is the False branch, and is never executed"}]},{"type":"text","value":"\n student_info "},{"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":"\"students1.csv\""}]},{"type":"text","value":"\n exercise_data "},{"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":"\"exercises1.csv\""}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When you have verified your program works correctly, you can remove the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" structure, keeping the commands asking for input."}]},{"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 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."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB2:"}]},{"type":"text","value":" If Visual Studio can't find the file and you have checked that there are no spelling errors, take a look at "},{"type":"element","tagName":"a","properties":{"href":"/part-6/1-reading-files#what-if-visual-studio-code-cannot-find-my-file"},"children":[{"type":"text","value":"these instructions"}]},{"type":"text","value":"."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Course grading, part 2","tmcname":"part06-05_course_grading_part_2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's expand the program created in the previous exercise. Now also the exam points awarded to each student are contained in a CSV file. The contents of the file follow this format:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"id;e1;e2;e3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345678;4;1;4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345687;3;5;3"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"12345699;10;2;2"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the above example the student whose student number is 12345678 was awarded 4+1+4 points in the exam, which equals a total of 9 points."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should again ask the user for the names of the files. Then the program should process the files and print out a grade for each student."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Student information: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"students1.csv"}]},{"type":"text","value":"\nExercises completed: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"exercises1.csv"}]},{"type":"text","value":"\nExam points: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"exam_points1.csv"}]},{"type":"text","value":"\npekka peloton 0\njaana javanainen 1\nliisa virtanen 3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each completed exercise is counted towards "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"exercise points"}]},{"type":"text","value":", so that completing at least 10 % of the total exercices awards 1 point, completing at least 20 % awards 2 points, etc. Completing all 40 exercises awards 10 points. The number of points awarded is always an integer number."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The final grade for the course is determined based on the sum of exam and exercise points according to 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 (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-"}]},{"type":"element","tagName":"td","properties":{"align":"center"},"children":[{"type":"text","value":"5"}]}]}]}]},{"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 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."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Course grading, part 3","tmcname":"part06-06_course_grading_part_3"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This exercise will continue from the previous one. Now we shall print out some statistics based on the CSV files."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Student information: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"students1.csv"}]},{"type":"text","value":"\nExercises completed: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"exercises1.csv"}]},{"type":"text","value":"\nExam points: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"exam_points1.csv"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"name exec_nbr exec_pts. exm_pts. tot_pts. grade\npekka peloton 21 5 9 14 0\njaana javanainen 27 6 11 17 1\nliisa virtanen 35 8 14 22 3\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each row contains the information for a single student. The number of exercises completed, the number of exercise points awarded, the number of exam points awarded, the total number of points awarded, and the grade are all displayed in tidy columns. The width of the column for the name should be 30 characters, while the other columns should be 10 characters wide."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You might find the f-strings covered in "},{"type":"element","tagName":"a","properties":{"href":"/part-4/5-print-statement-formatting"},"children":[{"type":"text","value":"part 4"}]},{"type":"text","value":" useful here."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"F-strings differentiate between strings and numbers when it comes to justifying columns:"}]},{"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":"word "},{"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\""}]},{"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":"word"},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"continues\""}]}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"word"},{"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":">10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"continues\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"python continues\n pythoncontinues\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, by default strings are justified to the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"left"}]},{"type":"text","value":" edge of the area specified for them. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" symbol can be used to justify to the right edge."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With number values the logic is reversed:"}]},{"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":"42"}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"continues\""}]}]},{"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\""}]},{"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":"<10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"continues\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":" 42continues\n42 continues\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With numbers the default behaviour is to justify to the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"right"}]},{"type":"text","value":" edge. The symbol "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" can be used to justify to the left edge."}]},{"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 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."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Spell checker","tmcname":"part06-07_spellchecker"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user to type in some text. Your program should then perform a spell check, and print out feedback to the user, so that all misspelled words have stars around them. Please see the two examples below:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Write text: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"We use ptython to make a spell checker"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"We use *ptython* to make a spell checker\n"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Write text: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"This is acually a good and usefull program"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"This is *acually* good and *usefull* program\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The case of the letters should be irrelevant to the functioning of your program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template includes the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"wordlist.txt"}]},{"type":"text","value":", which contains all the words the spell checker should accept as correct."}]},{"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 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."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB2"}]},{"type":"text","value":" If Visual Studio can't find the file and you have checked that there are no spelling errors, take a look at "},{"type":"element","tagName":"a","properties":{"href":"/part-6/1-reading-files#what-if-visual-studio-code-cannot-find-my-file"},"children":[{"type":"text","value":"these instructions"}]},{"type":"text","value":"."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Recipe search","tmcname":"part06-08_recipe_search"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This exercise is about creating a program which allows the user to search for recipes based on their names, preparation times, or ingredients used. The program should read the recipes from a file submitted by the user."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each recipe consists of three or more lines. The first line has the name of the recipe, the second line contains an integer number representing the preparation time in minutes, and the remaining line or lines contain the ingredients used, one on each line. The recipe ends with an empty line, with the exception of the final recipe in the file which just ends with the end of the file. So, there can be more than one recipe in a single file, like in the example below."}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"text","value":"Pancakes\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"text","value":"\nmilk\neggs\nflour\nsugar\nsalt\nbutter\n\nMeatballs\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"45"}]},{"type":"text","value":"\nmince\neggs\nbreadcrumbs\n\nTofu rolls\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"type":"text","value":"\ntofu\nrice\nwater\ncarrot\ncucumber\navocado\nwasabi\n\nCake pops\n"},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"60"}]},{"type":"text","value":"\nmilk\nbicarbonate\neggs\nsalt\nsugar\ncardamom\nbutter"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint:"}]},{"type":"text","value":" it might be best to first read through all the lines in the file and pop them into a list, which is then easier to manipulate in the way described in the exercise."}]},{"type":"element","tagName":"h4","properties":{"id":"search-for-recipes-based-on-the-name-of-the-recipe","style":"position:relative;"},"children":[{"type":"text","value":"Search for recipes based on the name of the recipe"},{"type":"element","tagName":"a","properties":{"href":"#search-for-recipes-based-on-the-name-of-the-recipe","ariaLabel":"search for recipes based on the name of the recipe 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"search_by_name(filename: str, word: str)"}]},{"type":"text","value":", which takes a filename and a search string as its arguments. The function should go through the file and select all recipes whose "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" contains the given search string. The names of these recipes are then returned in a list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"found_recipes "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" search_by_name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"recipes1.txt\""}]},{"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":"\"cake\""}]},{"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":" recipe "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" found_recipes"},{"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":"recipe"},{"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":"Pancakes\nCake pops"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see in the example above, the case of the letters is irrelevant. The search term "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"cake"}]},{"type":"text","value":" returns both "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Pancakes"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Cake pops"}]},{"type":"text","value":", even though the latter is capitalized."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" If Visual Studio can't find the file and you have checked that there are no spelling errors, take a look at "},{"type":"element","tagName":"a","properties":{"href":"/part-6/1-reading-files#what-if-visual-studio-code-cannot-find-my-file"},"children":[{"type":"text","value":"these instructions"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h4","properties":{"id":"search-for-recipes-based-on-the-preparation-time","style":"position:relative;"},"children":[{"type":"text","value":"Search for recipes based on the preparation time"},{"type":"element","tagName":"a","properties":{"href":"#search-for-recipes-based-on-the-preparation-time","ariaLabel":"search for recipes based on the preparation time 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"search_by_time(filename: str, prep_time: int)"}]},{"type":"text","value":", which takes a filename and an integer as its arguments. The function should go through the file and select all recipes whose preparation time is at most the number given."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The names of these recipes are again returned in a list, but the preparation time should be appended to each name. Please have a look at the example below."}]},{"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":"found_recipes "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" search_by_time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"recipes1.txt\""}]},{"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":"20"}]},{"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":" recipe "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" found_recipes"},{"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":"recipe"},{"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":"Pancakes, preparation time 15 min"}]}]},{"type":"element","tagName":"h4","properties":{"id":"search-for-recipes-based-on-the-ingredients","style":"position:relative;"},"children":[{"type":"text","value":"Search for recipes based on the ingredients"},{"type":"element","tagName":"a","properties":{"href":"#search-for-recipes-based-on-the-ingredients","ariaLabel":"search for recipes based on the ingredients 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":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"A word of caution:"}]},{"type":"text","value":" this third part of the exercise is considerably more demanding than the previous two. If you feel like you aren't making headway, it may be worth your while to move on, complete the other exercises in this part of the material, and then come back to this exercise if you have time later. Remember, you can submit and receive points for the first two parts of this exercise even if you haven't completed the third part."}]},{"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":"search_by_ingredient(filename: str, ingredient: str)"}]},{"type":"text","value":", which takes a filename and a search string as its arguments. The function should go through the file and select all recipes whose "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"ingredients"}]},{"type":"text","value":" contain the given search string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The names of these recipes are returned in a list just like in the second part, with the preparation time appended. Please have a look at the example below."}]},{"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":"found_recipes "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" search_by_ingredient"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"recipes1.txt\""}]},{"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":"\"eggs\""}]},{"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":" recipe "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" found_recipes"},{"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":"recipe"},{"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":"Pancakes, preparation time 15 min\nMeatballs, preparation time 45 min\nCake pops, preparation time 60 min"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"City bikes","tmcname":"part06-09_city_bikes"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise we will write some functions for working on a file containing location data from the stations for "},{"type":"element","tagName":"a","properties":{"href":"https://www.hsl.fi/en/citybikes","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"city bikes in Helsinki"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each file will follow this format:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Longitude;Latitude;FID;name;total_slot;operative;id"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"24.950292890004903;60.155444793742276;1;Kaivopuisto;30;Yes;001"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"24.956347471358754;60.160959093887129;2;Laivasillankatu;12;Yes;002"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"24.944927399779715;60.158189199971673;3;Kapteeninpuistikko;16;Yes;003"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each station has a single line in the file. The line contains the coordinates, name, and other identifying information for the station."}]},{"type":"element","tagName":"h4","properties":{"id":"distance-between-stations","style":"position:relative;"},"children":[{"type":"text","value":"Distance between stations"},{"type":"element","tagName":"a","properties":{"href":"#distance-between-stations","ariaLabel":"distance between stations 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":"First, write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"get_station_data(filename: str)"}]},{"type":"text","value":". This function should read the names and locations of all the stations in the file, and return them in a dictionary with the following format:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"{\n \"Kaivopuisto\": (24.950292890004903, 60.155444793742276),\n \"Laivasillankatu\": (24.956347471358754, 60.160959093887129),\n \"Kapteeninpuistikko\": (24.944927399779715, 60.158189199971673)\n}\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Dictionary keys are the names of the stations, and the value attached is a tuple containing the location coordinates of the station. The first element in the tuple is the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Longitude"}]},{"type":"text","value":" field, and the second is the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Latitude"}]},{"type":"text","value":" field."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Next, write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"distance(stations: dict, station1: str, station2: str)"}]},{"type":"text","value":", which returns the distance between the two stations given as arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The distance is calculated using the Pythagorean theorem. The multiplication factors below are approximate values for converting latitudes and longitudes to distances in kilometres in the Helsinki region."}]},{"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":"# we will need the function sqrt from the math module "}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" math\n\nx_km "},{"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":"text","value":"longitude1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" longitude2"},{"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":"55.26"}]},{"type":"text","value":"\ny_km "},{"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":"text","value":"latitude1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" latitude2"},{"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":"111.2"}]},{"type":"text","value":"\ndistance_km "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x_km"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"**"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" y_km"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"p","properties":{},"children":[{"type":"text","value":"Some examples of the function in action:"}]},{"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":"stations "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" get_station_data"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'stations1.csv'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" distance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"stations"},{"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":"\"Designmuseo\""}]},{"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":"\"Hietalahdentori\""}]},{"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":"d"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" distance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"stations"},{"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":"\"Viiskulma\""}]},{"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":"\"Kaivopuisto\""}]},{"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":"d"},{"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.9032737292463177\n0.7753594392019532"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" If Visual Studio can't find the file and you have checked that there are no spelling errors, take a look at "},{"type":"element","tagName":"a","properties":{"href":"/part-6/1-reading-files#what-if-visual-studio-code-cannot-find-my-file"},"children":[{"type":"text","value":"these instructions"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h4","properties":{"id":"the-greatest-distance","style":"position:relative;"},"children":[{"type":"text","value":"The greatest distance"},{"type":"element","tagName":"a","properties":{"href":"#the-greatest-distance","ariaLabel":"the greatest distance 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":"Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"greatest_distance(stations: dict)"}]},{"type":"text","value":", which works out the two stations on the list with the greatest distance from each other. The function should return a tuple, where the first two elements are the names of the two stations, and the third element is the distance between the two."}]},{"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":"stations "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" get_station_data"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'stations1.csv'"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstation1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" station2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" greatest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" greatest_distance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"stations"},{"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":"station1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" station2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" greatest"},{"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":"Laivasillankatu Hietalahdentori 1.478708873076181"}]}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will know how to read the contents of files with Python
    • \n
    • You will know what a text file and a CSV file are
    • \n
    • You will be able to process the contents of a CSV file in your programs
    • \n

    Becoming a proficient programmer requires a lot of practice, sometimes even quite mechanical practice. It also involves developing problem solving skills and applying intuition. This is why there are a lot of exercises of different kinds on this course. Some of them ask you to quite straightforwardly apply what you have learnt in the material, but some of them are intentionally more challenging and open-ended.

    Some of the exercises might at first seem overwhelming, but this is nothing to worry about. None of the exercises is strictly mandatory, and in fact only 25 % of the points in each part is required to pass the course. You can find more details about passing the course on the page on grading.

    The exercises are not in any specific order of difficulty. Each section usually introduces some new programming concepts, and these are then practised with both simpler and more complicated exercises. If you come across an exercise that feels too difficult, move on to the next one. You can always come back to the more difficult exercises if you have time later.

    When the going inevitably gets tough, a word of consolation: a task that seems impossibly difficult this week will likely feel rather easy in about four weeks' time.

    A very common use case for programming is handling data stored in files. Programs can read data from files and write the computed results to files. Even large amounts of data become easy to process automatically when files are used.

    On this course we only deal with text files. All the files used will consist of lines of text. As an example, the Visual Studio Code editor we use on this course works with text files. NB: even though word processors such as Microsoft Word are usually used with files that contain text, Word documents themselves aren't text files. They also contain formatting information, and are encoded in a way that makes handling them in a program more complicated.

    Reading data from a file

    Let's first work with a file called example.txt, with the following contents:

    Hello there!\nThis example file contains three lines of text.\nThis is the last line.

    A simple way to include files in a Python program is to use the with statement. The header line opens the file, and the block where the file can be accessed follows. After the block the file is automatically closed, and can no longer be accessed.

    So, the following code opens the file, reads the contents, prints them out, and then closes the file:

    with open(\"example.txt\") as new_file:\n    contents = new_file.read()\n    print(contents)

    Hello there!\nThis example file contains three lines of text.\nThis is the last line.

    The variable new_file above is a file handle. Through it the file can accessed while it is still open. Here we used the method read, which returns the contents of the file as a single string. So, in this case the string returned by read would be

    \"Hello there!\\nThis example file contains three lines of text.\\nThis is the last line.\"

    Going through the contents of a file

    The read method is useful for printing out the contents of the entire file, but more often we will want to go through the file line by line.

    Text files can be thought of as lists of strings, each string representing a single line in the file. We can go through the list with a for loop.

    The following example reads our example file using a for loop, removes line breaks from the end of each line, counts the number of lines, and prints each line with its line number. It also keeps track of the length of the lines:

    with open(\"example.txt\") as new_file:\n    count = 0\n    total_length = 0\n\n    for line in new_file:\n        line = line.replace(\"\\n\", \"\")\n        count += 1\n        print(\"Line\", count, line)\n        length = len(line)\n        total_length += length\n\nprint(\"Total length of lines:\", total_length)

    Line 1 Hello there!\nLine 2 This example file contains three lines of text.\nLine 3 This is the last line.\nTotal length of lines: 81

    There is a line break \\n at the end of each line in the file, but the print function also adds a line break by default. There are no extra line breaks in the printout above, because the line breaks at the ends of the lines are removed with the replace method. It replaces each line break character with an empty string. This way the lengths of the lines are also calculated correctly.

    The file numbers.txt contains integer numbers, one number per line. The contents could look like this:

    2\n45\n108\n3\n-10\n1100\n...etc...

    Please write a function named largest, which reads the file and returns the largest number in the file.

    Notice that the function does not take any arguments. The file you are working with is always named numbers.txt.

    NB: If Visual Studio Code can't find the file and you have checked that there are no spelling errors, take a look at the instructions following this exercise.

    What if Visual Studio Code cannot find my file?

    When you execute your code, Visual Studio Code might complain about not finding the file, even though you have checked and double-checked the file name was written correctly, and the file exists. Changing the following setting may fix the problem:

      \n
    • Open the settings from the menu bar: File -> Preferences -> Settings
    • \n
    • Find the relevant setting with the search term \"executeinfile\"
    • \n
    • Choose the tab Workspace
    • \n
    • Select the option under Python -> Terminal -> Execute In File Dir
    • \n

    Your settings window should now look somewhat like this:

    \n \n \n \n \n \n \"6\n \n \n

    If this doesn't help, you can copy the file in the src directory

    \n \n \n \n \n \n \"6\n \n \n

    directly to the root of the exercise directory

    \n \n \n \n \n \n \"6\n \n \n

    Debugging code which handles files

    Using the Visual Studio Code debugger with programs which handle files will often result in a nasty looking error message:

    \n \n \n \n \n \n \"6\n \n \n

    The reason is that the debugger will always look for files in the root of the exercise directory. The Execute In File Dir setting mentioned above will have no effect on this. The easiest solution is to just copy the file to the root of the directory.

    You may need to restart Visual Studio Code after copying all the necessary files.

    Reading CSV files

    A CSV file, short for comma-separated Values, is a text file which contains data separated by a predetermined character. The most common characters used for this purpose are the comma , and the semicolon ;, but any character is, in principle, possible.

    CSV files are commonly used to store records of different kinds. Many database and spreadsheet programs, such as Excel, can import and export data in CSV format, which makes data exchange between different systems easy.

    We already learnt we can go through the lines in a file with a for loop, but how can we separate the different fields on a single line? Python has a string method split for just this purpose. The method takes the separator character(s) as a string argument, and returns the contents of the target string as a list of strings, separated at the separator.

    An example of how the method works:

    text = \"monkey,banana,harpsichord\"\nwords = text.split(\",\")\nfor word in words:\n    print(word)

    monkey\nbanana\nharpsichord

    Let's assume we have a file grades.csv, which contains names of students and the grades they received on some courses. Each line has the data of a single student, and the data is separated by a semicolon.

    Paul;5;4;5;3;4;5;5;4;2;4\nBeth;3;4;2;4;4;2;3;1;3;3\nRuth;4;5;5;4;5;5;4;5;4;4

    The following program goes through the file line by line, splits each line into its separate parts, and prints out the name and grades of each student.

    with open(\"grades.csv\") as new_file:\n    for line in new_file:\n        line = line.replace(\"\\n\", \"\")\n        parts = line.split(\";\")\n        name = parts[0]\n        grades = parts[1:]\n        print(\"Name:\", name)\n        print(\"Grades:\", grades)

    Name: Paul\nGrades: ['5', '4', '5', '3', '4', '5', '5', '4', '2', '4']\nName: Beth\nGrades: ['3', '4', '2', '4', '4', '2', '3', '1', '3', '3']\nName: Ruth\nGrades: ['4', '5', '5', '4', '5', '5', '4', '5', '4', '4']

    The file fruits.csv contains names of fruits, and their prices, in the format specified in this example:

    banana;6.50\napple;4.95\norange;8.0\n...etc...

    Please write a function named read_fruits, which reads the file and returns a dictionary based on the contents. In the dictionary, the name of the fruit should be the key, and the value should be its price. Prices should be of type float.

    NB: the function does not take any arguments. The file you are working with is always named fruits.csv.

    The file matrix.txt contains a matrix in the format specified in the example below:

    1,0,2,8,2,1,3,2,5,2,2,2\n9,2,4,5,2,4,2,4,1,10,4,2\n...etc...

    Please write two functions, named matrix_sum and matrix_max. Both go through the matrix in the file, and then return the sum of the elements or the element with the greatest value, as the names of the functions imply.

    Please also write the function row_sums, which returns a list containing the sum of each row in the matrix. For example, calling row_sums when the matrix in the file is defined as

    1,2,3\n2,3,4

    the function should return the list [6, 9].

    Hint: you can also include other helper functions in your program. It is very worthwhile to spend a moment considering which functionalities are shared by the three functions you are asked to write. Notice that the three functions named above do not take any arguments, but any helper functions you write may take arguments. The file you are working with is always named matrix.txt.

    NB: If Visual Studio Code can't find the file and you have checked that there are no spelling errors, take a look at the instructions before this exercise.

    Reading the same file multiple times

    Sometimes it is necessary to process the contents of a file more than once in a single program. Let's have a look at a program which works with some personal data stored in a CSV file:

    \nPeter;40;Helsinki\nEmily;34;Espoo\nEric;42;London\nAdam;100;Amsterdam\nAlice;58;Paris\n
    with open(\"people.csv\") as new_file:\n    # print out the names\n    for line in new_file:\n        parts = line.split(\";\")\n        print(\"Name:\", parts[0])\n\n    # find the oldest\n    age_of_oldest = -1\n    for line in new_file:\n        parts = line.split(\";\")\n        name = parts[0]\n        age = int(parts[1])\n        if age > age_of_oldest:\n            age_of_oldest = age\n            oldest = name\n    print(\"the oldest is\", oldest)

    Running this will result in a somewhat cryptic error message:

    Traceback (most recent call last):\n    print(\"the oldest is\"; oldest)\nUnboundLocalError: local variable 'oldest' referenced before assignment

    The reason this happens is that the latter for loop is not executed at all, beacuse the file can only be processed once. Once the last line is read, the file handle rests at the end of the file, and the data in the file can no longer be accessed.

    If we want to access the contents in the second for loop, we will have to open the file a second time:

    with open(\"people.csv\") as new_file:\n    # print out the names\n    for line in new_file:\n        parts = line.split(\";\")\n        print(\"Name:\", parts[0])\n\nwith open(\"people.csv\") as new_file:\n    # find the oldest\n    age_of_oldest = -1\n    for line in new_file:\n        parts = line.split(\";\")\n        name = parts[0]\n        age = int(parts[1])\n        if age > age_of_oldest:\n            age_of_oldest = age\n            oldest = name\n    print(\"the oldest is\", oldest)

    While the above code would work, it contains unnecessary repetition. It is usually best to read the file just once, and store its contents in an appropriate format for further processing:

    people = []\n# read the contents of the file and store it in a list\nwith open(\"people.csv\") as new_file:\n    for line in new_file:\n        parts = line.split(\";\")\n        people.append((parts[0], int(parts[1]), parts[2]))\n\n# print out the names\nfor person in people:\n    print(\"Name:\", person[0])\n\n# find the oldest\nage_of_oldest = -1\nfor person in people:\n    name = person[0]\n    age = person[1]\n    if age > age_of_oldest:\n        age_of_oldest = age\n        oldest = name\nprint(\"the oldest is\", oldest)

    More CSV file processing

    Let's continue with the file grades.csv, which has the following contents:

    Paul;5;4;5;3;4;5;5;4;2;4\nBeth;3;4;2;4;4;2;3;1;3;3\nRuth;4;5;5;4;5;5;4;5;4;4

    This following program creates a dictionary grades based on the contents of the file. The keys are the names of the students, and the value attached to each key is the list of grades attained by the student. The program converts the grades to integer values, so they can be processed easier.

    grades = {}\nwith open(\"grades.csv\") as new_file:\n    for line in new_file:\n        line = line.replace(\"\\n\", \"\")\n        parts = line.split(\";\")\n        name = parts[0]\n        grades[name] = []\n        for grade in parts[1:]:\n            grades[name].append(int(grade))\n\nprint(grades)

    {'Paul': [5, 4, 5, 3, 4, 5, 5, 4, 2, 4], 'Beth': [3, 4, 2, 4, 4, 2, 3, 1, 3, 3], 'Ruth': [4, 5, 5, 4, 5, 5, 4, 5, 4, 4]}

    Now we can print out some statistics on each student based on the contents of the dictionary grades:

    for name, grade_list in grades.items():\n    best = max(grade_list)\n    average = sum(grade_list) / len(grade_list)\n    print(f\"{name}: best grade {best}, average {average:.2f}\")

    Paul: best grade 5, average 4.10\nBeth: best grade 4, average 2.90\nRuth: best grade 5, average 4.50

    Please take a careful look at the program in the example above. It may seem a bit complicated at first, but the technique can be used on files containing many different types of data.

    Removing unnecessary lines, spaces and line breaks

    Let's assume we have a CSV file containing some names, which has been exported from Excel:

    first; last\nPaul; Python\nJean; Java\nHarry; Haskell

    Excel is notorious for adding extra whitespace. Here we have an extra space character between the items, after each semicolon.

    We would like to print out the last names of each person on the list. The first line contains the headers for the data, and it can be safely ignored:

    last_names = []\nwith open(\"people.csv\") as new_file:\n    for line in new_file:\n        parts = line.split(\";\")\n        # ignore the header line\n        if parts[0] == \"first\":\n            continue\n        last_names.append(parts[1])\n\nprint(last_names)

    Exectuing this would print out

    [' Python\\n', ' Java\\n', ' Haskell']

    The first two items have a line break character at the end, and all three have an extra leading space character.

    We have already used the replace method to remove extra whitespace, but a more efficient solution is to use the Python string method strip, which removes whitespace from the beginning and end of a string. It removes all spaces, line breaks, tabs and other characters which would not normally be printed out.

    You can try it out in the Python console:

    >>> \" tryout \".strip()\n'tryout'\n>>> \"\\n\\ntest\\n\".strip()\n'test'\n>>>

    Stripping the string requires only a small change to the program:

    last_names = []\nwith open(\"people.csv\") as new_file:\n    for line in new_file:\n        parts = line.split(';')\n        if parts[0] == \"first\":\n            continue # this was the header line, so it is ignored\n        last_names.append(parts[1].strip())\nprint(last_names)

    Now we have the desired tidy printout:

    ['Python', 'Java', 'Haskell']

    There are also the related string methods lstrip and rstrip. They remove only the leading or trailing unprintable characters, l for the left edge of the string and r for the right:

    >>> \" teststring  \".rstrip()\n' teststring'\n>>> \" teststring  \".lstrip()\n'teststring  '

    Combining data from different files

    It is very common for the data processed by a program to be scattered in multiple files. Lets have a look at a situation where the personal details of the personnel of a company are stored in a file called employees.csv:

    pic;name;address;city\n080488-123X;Pekka Mikkola;Vilppulantie 7;00700 Helsinki\n290274-044S;Liisa Marttinen;Mannerheimintie 100 A 10;00100 Helsinki\n010479-007Z;Arto Vihavainen;Pihapolku 4;01010 Kerava\n010499-345K;Leevi Hellas;Tapiolantie 11 B;02000 Espoo

    The salaries are stored in a separate file salaries.csv:

    pic;salary;bonus\n080488-123X;3300;0\n290274-044S;4150;200\n010479-007Z;1300;1200

    Each data line in both files contains the personal identity code, which identifies whose data we are dealing with. Using the personal identity code as a common factor, it is easy to connect the names and salaries of each employee. We can, for example, print out the following list of monthly incomes:

    \nincomes:\nPekka Mikkola    3300 euros\nLiisa Marttinen  4350 euros\nArto Vihavainen  2500 euros\n

    This program uses two dictionaries as helper data structures: names and salaries. Both use the PIC as key:

    names = {}\n\nwith open(\"employees.csv\") as new_file:\n    for line in new_file:\n        parts = line.split(';')\n        if parts[0] == \"pic\":\n            continue\n        names[parts[0]] = parts[1]\n\nsalaries = {}\n\nwith open(\"salaries.csv\") as new_file:\n    for line in new_file:\n        parts = line.split(';')\n        if parts[0] == \"pic\":\n            continue\n        salaries[parts[0]] = int(parts[1]) +int(parts[2])\n\nprint(\"incomes:\")\n\nfor pic, name in names.items():\n    if pic in salaries:\n        salary = salaries[pic]\n        print(f\"{name:16} {salary} euros\")\n    else:\n        print(f\"{name:16} 0 euros\")

    First the program produces the dictionaries names and salaries. They have the following contents:

    {\n    '080488-123X': 'Pekka Mikkola',\n    '290274-044S': 'Liisa Marttinen',\n    '010479-007Z': 'Arto Vihavainen',\n    '010499-345K': 'Leevi Hellas'\n}\n\n{\n    '080488-123X': 3300,\n    '290274-044S': 4350,\n    '010479-007Z': 2500\n}

    The for loop at the end of the program combines the names of the employees with their respective salaries.

    The program also takes care of situations where the employee's pic is not present in the salary file.

    Remember, the order in which items are stored in a dictionary does not matter, as the keys are processed based on hash values.

    This program works with two CSV files. One of them contains information about some students on a course:

    id;first;last\n12345678;peter;pythons\n12345687;jean;javanese\n12345699;alice;adder

    The other contains the number of exercises each student has completed each week:

    id;e1;e2;e3;e4;e5;e6;e7\n12345678;4;1;1;4;5;2;4\n12345687;3;5;3;1;5;4;6\n12345699;10;2;2;7;10;2;2

    As you can see above, both CSV files also have a header row, which tells you what each column contains.

    Please write a program which asks the user for the names of these two files, reads the files, and then prints out the total number of exercises completed by each student. If the files have the contents in the examples above, the program should print out the following:

    Student information: students1.csv\nExercises completed: exercises1.csv\npekka peloton 21\njaana javanainen 27\nliisa virtanen 35

    Hint: while testing your program, you may quickly run out of patience if you always have to type in the file names at the prompt. You might want to hard-code the user input, like so:

    if False:\n    # this is never executed\n    student_info = input(\"Student information: \")\n    exercise_data = input(\"Exercises completed: \")\nelse:\n    # hard-coded input\n    student_info = \"students1.csv\"\n    exercise_data = \"exercises1.csv\"

    The actual functionality of the program is now \"hidden\" in the False branch of an if statement. It will never be executed.

    Now, if you want to quickly verify the program works correctly also with user input, you can just replace False with True:

    \nif True:\n    student_info = input(\"Student information: \")\n    exercise_data = input(\"Exercises completed: \")\nelse:\n    # now this is the False branch, and is never executed\n    student_info = \"students1.csv\"\n    exercise_data = \"exercises1.csv\"

    When you have verified your program works correctly, you can remove the if structure, keeping the commands asking for input.

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    NB2: If Visual Studio can't find the file and you have checked that there are no spelling errors, take a look at these instructions.

    Let's expand the program created in the previous exercise. Now also the exam points awarded to each student are contained in a CSV file. The contents of the file follow this format:

    id;e1;e2;e3\n12345678;4;1;4\n12345687;3;5;3\n12345699;10;2;2

    In the above example the student whose student number is 12345678 was awarded 4+1+4 points in the exam, which equals a total of 9 points.

    The program should again ask the user for the names of the files. Then the program should process the files and print out a grade for each student.

    Student information: students1.csv\nExercises completed: exercises1.csv\nExam points: exam_points1.csv\npekka peloton 0\njaana javanainen 1\nliisa virtanen 3

    Each completed exercise is counted towards exercise points, so that completing at least 10 % of the total exercices awards 1 point, completing at least 20 % awards 2 points, etc. Completing all 40 exercises awards 10 points. The number of points awarded is always an integer number.

    The final grade for the course is determined based on the sum of exam and exercise points according to the following table:

    \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
    exam points + exercise pointsgrade
    0-140 (fail)
    15-171
    18-202
    21-233
    24-274
    28-5

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    This exercise will continue from the previous one. Now we shall print out some statistics based on the CSV files.

    Student information: students1.csv\nExercises completed: exercises1.csv\nExam points: exam_points1.csv

    \nname                          exec_nbr  exec_pts. exm_pts.  tot_pts.  grade\npekka peloton                 21        5         9         14        0\njaana javanainen              27        6         11        17        1\nliisa virtanen                35        8         14        22        3\n

    Each row contains the information for a single student. The number of exercises completed, the number of exercise points awarded, the number of exam points awarded, the total number of points awarded, and the grade are all displayed in tidy columns. The width of the column for the name should be 30 characters, while the other columns should be 10 characters wide.

    You might find the f-strings covered in part 4 useful here.

    F-strings differentiate between strings and numbers when it comes to justifying columns:

    word = \"python\"\nprint(f\"{word:10}continues\")\nprint(f\"{word:>10}continues\")
    \npython    continues\n    pythoncontinues\n

    As you can see above, by default strings are justified to the left edge of the area specified for them. The > symbol can be used to justify to the right edge.

    With number values the logic is reversed:

    number = 42\nprint(f\"{number:10}continues\")\nprint(f\"{number:<10}continues\")
    \n        42continues\n42        continues\n

    With numbers the default behaviour is to justify to the right edge. The symbol < can be used to justify to the left edge.

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    Please write a program which asks the user to type in some text. Your program should then perform a spell check, and print out feedback to the user, so that all misspelled words have stars around them. Please see the two examples below:

    Write text: We use ptython to make a spell checker

    \nWe use *ptython* to make a spell checker\n

    Write text: This is acually a good and usefull program

    \nThis is *acually* good and *usefull* program\n

    The case of the letters should be irrelevant to the functioning of your program.

    The exercise template includes the file wordlist.txt, which contains all the words the spell checker should accept as correct.

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    NB2 If Visual Studio can't find the file and you have checked that there are no spelling errors, take a look at these instructions.

    This exercise is about creating a program which allows the user to search for recipes based on their names, preparation times, or ingredients used. The program should read the recipes from a file submitted by the user.

    Each recipe consists of three or more lines. The first line has the name of the recipe, the second line contains an integer number representing the preparation time in minutes, and the remaining line or lines contain the ingredients used, one on each line. The recipe ends with an empty line, with the exception of the final recipe in the file which just ends with the end of the file. So, there can be more than one recipe in a single file, like in the example below.

    Pancakes\n15\nmilk\neggs\nflour\nsugar\nsalt\nbutter\n\nMeatballs\n45\nmince\neggs\nbreadcrumbs\n\nTofu rolls\n30\ntofu\nrice\nwater\ncarrot\ncucumber\navocado\nwasabi\n\nCake pops\n60\nmilk\nbicarbonate\neggs\nsalt\nsugar\ncardamom\nbutter

    Hint: it might be best to first read through all the lines in the file and pop them into a list, which is then easier to manipulate in the way described in the exercise.

    Search for recipes based on the name of the recipe

    Please write a function named search_by_name(filename: str, word: str), which takes a filename and a search string as its arguments. The function should go through the file and select all recipes whose name contains the given search string. The names of these recipes are then returned in a list.

    An example of the function in action:

    found_recipes = search_by_name(\"recipes1.txt\", \"cake\")\n\nfor recipe in found_recipes:\n    print(recipe)

    Pancakes\nCake pops

    As you can see in the example above, the case of the letters is irrelevant. The search term cake returns both Pancakes and Cake pops, even though the latter is capitalized.

    NB: If Visual Studio can't find the file and you have checked that there are no spelling errors, take a look at these instructions.

    Search for recipes based on the preparation time

    Please write a function named search_by_time(filename: str, prep_time: int), which takes a filename and an integer as its arguments. The function should go through the file and select all recipes whose preparation time is at most the number given.

    The names of these recipes are again returned in a list, but the preparation time should be appended to each name. Please have a look at the example below.

    found_recipes = search_by_time(\"recipes1.txt\", 20)\n\nfor recipe in found_recipes:\n    print(recipe)

    Pancakes, preparation time 15 min

    Search for recipes based on the ingredients

    A word of caution: this third part of the exercise is considerably more demanding than the previous two. If you feel like you aren't making headway, it may be worth your while to move on, complete the other exercises in this part of the material, and then come back to this exercise if you have time later. Remember, you can submit and receive points for the first two parts of this exercise even if you haven't completed the third part.

    Please write a function named search_by_ingredient(filename: str, ingredient: str), which takes a filename and a search string as its arguments. The function should go through the file and select all recipes whose ingredients contain the given search string.

    The names of these recipes are returned in a list just like in the second part, with the preparation time appended. Please have a look at the example below.

    found_recipes = search_by_ingredient(\"recipes1.txt\", \"eggs\")\n\nfor recipe in found_recipes:\n    print(recipe)

    Pancakes, preparation time 15 min\nMeatballs, preparation time 45 min\nCake pops, preparation time 60 min

    In this exercise we will write some functions for working on a file containing location data from the stations for city bikes in Helsinki.

    Each file will follow this format:

    Longitude;Latitude;FID;name;total_slot;operative;id\n24.950292890004903;60.155444793742276;1;Kaivopuisto;30;Yes;001\n24.956347471358754;60.160959093887129;2;Laivasillankatu;12;Yes;002\n24.944927399779715;60.158189199971673;3;Kapteeninpuistikko;16;Yes;003

    Each station has a single line in the file. The line contains the coordinates, name, and other identifying information for the station.

    Distance between stations

    First, write a function named get_station_data(filename: str). This function should read the names and locations of all the stations in the file, and return them in a dictionary with the following format:

    \n{\n  \"Kaivopuisto\": (24.950292890004903, 60.155444793742276),\n  \"Laivasillankatu\": (24.956347471358754, 60.160959093887129),\n  \"Kapteeninpuistikko\": (24.944927399779715, 60.158189199971673)\n}\n

    Dictionary keys are the names of the stations, and the value attached is a tuple containing the location coordinates of the station. The first element in the tuple is the Longitude field, and the second is the Latitude field.

    Next, write a function named distance(stations: dict, station1: str, station2: str), which returns the distance between the two stations given as arguments.

    The distance is calculated using the Pythagorean theorem. The multiplication factors below are approximate values for converting latitudes and longitudes to distances in kilometres in the Helsinki region.

    # we will need the function sqrt from the math module \nimport math\n\nx_km = (longitude1 - longitude2) * 55.26\ny_km = (latitude1 - latitude2) * 111.2\ndistance_km = math.sqrt(x_km**2 + y_km**2)

    Some examples of the function in action:

    stations = get_station_data('stations1.csv')\nd = distance(stations, \"Designmuseo\", \"Hietalahdentori\")\nprint(d)\nd = distance(stations, \"Viiskulma\", \"Kaivopuisto\")\nprint(d)

    0.9032737292463177\n0.7753594392019532

    NB: If Visual Studio can't find the file and you have checked that there are no spelling errors, take a look at these instructions.

    The greatest distance

    Please write a function named greatest_distance(stations: dict), which works out the two stations on the list with the greatest distance from each other. The function should return a tuple, where the first two elements are the names of the two stations, and the third element is the distance between the two.

    stations = get_station_data('stations1.csv')\nstation1, station2, greatest = greatest_distance(stations)\nprint(station1, station2, greatest)

    Laivasillankatu Hietalahdentori 1.478708873076181

    ","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-6/1-reading-files.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-6/2-writing-files/page-data.json b/page-data/part-6/2-writing-files/page-data.json index 73de520c0..8c51b7c5a 100644 --- a/page-data/part-6/2-writing-files/page-data.json +++ b/page-data/part-6/2-writing-files/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-6/2-writing-files", - "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 know how to create files with Python code"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to write text based data to a file"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to create a CSV file"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So far we have read data from files, but it is naturally also possible to write data to files. Typically a program processes data and stores the results in a file, so they can be used later or processed further by some other program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We can create a new file every time we want to write data to a file, but we can also append new data to the end of an existing file. In both cases we use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"open"}]},{"type":"text","value":" function from the previous section. For writing files the function requires a second argument."}]},{"type":"element","tagName":"h2","properties":{"id":"creating-a-new-file","style":"position:relative;"},"children":[{"type":"text","value":"Creating a new file"},{"type":"element","tagName":"a","properties":{"href":"#creating-a-new-file","ariaLabel":"creating a new file 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":"If you want to create a new file, you would call the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"open"}]},{"type":"text","value":" function with the additional argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"w"}]},{"type":"text","value":", to signify that the file should be opened in write mode. So, the function call 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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"new_file.txt\""}]},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"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":"# code to write something to the file"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB: if the file already exists, all the contents will be overwritten"}]},{"type":"text","value":". It pays to be very careful when creating new files."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With the file open you can write data to it. You can use the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"write"}]},{"type":"text","value":", which takes the string that is to be written as its argument."}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"new_file.txt\""}]},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hello there!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When you execute the program, a new file named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"new_file.txt"}]},{"type":"text","value":" appears in the directory. The contents would look like this:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hello there!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you want line breaks in the file, you will have to add them by hand - the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"write"}]},{"type":"text","value":" function doesn't work exactly like the more familiar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" function, though they are similar. So, the following program"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"new_file.txt\""}]},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hello there!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This is the second line\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This is the last line\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"would result in a file with these contents:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hello there!This is the second lineThis is the last line"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Line breaks are achieved by adding new line characters "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\\n"}]},{"type":"text","value":" to the argument 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":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"new_file.txt\""}]},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hello there!\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This is the second line\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This is the last line\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now the contents of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"new_file.txt"}]},{"type":"text","value":" would look like this:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hello there!\nThis is the second line\nThis is the last line"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Inscription","tmcname":"part06-10_inscription"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for the name of the user and then creates an \"inscription\" in a file specified by the user. Please see the example below."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Whom should I sign this to: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Ada"}]},{"type":"text","value":"\nWhere shall I save it: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"inscribed.txt"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The contents of the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"inscribed.txt"}]},{"type":"text","value":" would be"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hi Ada, we hope you enjoy learning Python with us! Best, Mooc.fi Team"}]}]},{"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 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."}]}]},{"type":"element","tagName":"h2","properties":{"id":"appending-data-to-an-existing-file","style":"position:relative;"},"children":[{"type":"text","value":"Appending data to an existing file"},{"type":"element","tagName":"a","properties":{"href":"#appending-data-to-an-existing-file","ariaLabel":"appending data to an existing file 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":"If you want to append data to the end of a file, instead of overwriting the entire file, you should open the file in append mode with the argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the file doesn't yet exist, append mode works exatly like write mode."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program opens the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"new_file.txt"}]},{"type":"text","value":" and appends a couple of lines of text to the end:"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"new_file.txt\""}]},{"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":"\"a\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This is the 4th line\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"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 yet another line.\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"After this program is executed the contents of the file would look like this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hello there!\nThis is the second line\nThis is the last line\nThis is the 4th line\nAnd yet another line."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In programming practice, appending data to files is not a very common task."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"More often a file is read, processed and overwritten in its entirety. For example, when the contents should change in the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"middle"}]},{"type":"text","value":" of the file, it is usually easiest to overwrite the entire file."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Diary","tmcname":"part06-11_diary"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which works as a simply diary. The diary entries should be saved in the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"diary.txt"}]},{"type":"text","value":". When the program is executed, it should first read any entries already in the file."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the automatic tests for this exercise will change the contents of the file. If you want to keep its contents, first make a copy of the file under a different name."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should work as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 - add an entry, 2 - read entries, 0 - quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nDiary entry: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Today I ate porridge"}]},{"type":"text","value":"\nDiary saved"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 - add an entry, 2 - read entries, 0 - quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nEntries:\nToday I ate porridge\n1 - add an entry, 2 - read entries, 0 - quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nDiary entry: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"I went to the sauna in the evening"}]},{"type":"text","value":"\nDiary saved"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 - add an entry, 2 - read entries, 0 - quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nEntries:\nToday I ate porridge\nI went to the sauna in the evening\n1 - add an entry, 2 - read entries, 0 - quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nBye now!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the program is executed for the second time, this should happen:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 - add an entry, 2 - read entries, 0 - quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nEntries:\nToday I ate porridge\nI went to the sauna in the evening\n1 - add an entry, 2 - read entries, 0 - quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nBye now!"}]}]},{"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 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."}]}]},{"type":"element","tagName":"h2","properties":{"id":"writing-csv-files","style":"position:relative;"},"children":[{"type":"text","value":"Writing CSV files"},{"type":"element","tagName":"a","properties":{"href":"#writing-csv-files","ariaLabel":"writing csv files 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":"CSV files can be written line by line with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"write"}]},{"type":"text","value":" method just like any other file. The following example creates the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"coders.csv"}]},{"type":"text","value":", with each line containing the name, working environment, favourite language and years of experience of a single programmer. The fields are separated by a semicolon."}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"coders.csv\""}]},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric;Windows;Pascal;10\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Matt;Linux;PHP;2\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Alan;Linux;Java;17\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily;Mac;Cobol;9\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Executing this program would result in the following file:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eric;Windows;Pascal;10\nMatt;Linux;PHP;2\nAlan;Linux;Java;17\nEmily;Mac;Cobol;9"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What if the data to be written is stored in computer memory in 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":"coders "},{"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":"\ncoders"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"\"Windows\""}]},{"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":"\"Pascal\""}]},{"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":"10"}]},{"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":"\ncoders"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Matt\""}]},{"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":"\"Linux\""}]},{"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":"\"PHP\""}]},{"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":"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":"\ncoders"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Alan\""}]},{"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":"\"Linux\""}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"17"}]},{"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":"\ncoders"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"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":"\"Mac\""}]},{"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":"\"Cobol\""}]},{"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":"9"}]},{"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":"We can build the string we want to write as an f-string, and write the ready line to the file like so:"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"coders.csv\""}]},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"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":" coder "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" coders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n line "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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":"coder"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"coder"},{"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":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"coder"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"coder"},{"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","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"line"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If each list of coder data was very long, with many more items, building the string by hand would be quite cumbersome. We can use a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop to build the string instead:"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"coders.csv\""}]},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"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":" coder "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" coders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n line "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" value "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" coder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n line "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"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":"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 line "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"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":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"line"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"clearing-file-contents-and-deleting-files","style":"position:relative;"},"children":[{"type":"text","value":"Clearing file contents and deleting files"},{"type":"element","tagName":"a","properties":{"href":"#clearing-file-contents-and-deleting-files","ariaLabel":"clearing file contents and deleting files 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":"Sometimes it is necessary to clear the contents of an existing file. Opening the file in write mode and closing the file immediately will achieve just 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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"file_to_be_cleared.txt\""}]},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"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":"pass"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" block only contains the command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":", which doesn't actually do anything. Python does not allow empty blocks, so the command is necessary here."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is possible to also bypass the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" block by using the following oneliner:"}]},{"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","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'file_to_be_cleared.txt'"}]},{"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":"'w'"}]},{"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":"close"},{"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":"text-box","properties":{"variant":"hint","name":"Deleting files"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can also delete a file entirely. We will have to ask for help from the operating system to achieve 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","comment"]},"children":[{"type":"text","value":"# the command to delete files is in the os module"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" os\n\nos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"unnecessary_file.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: this will not work when running the automatic tests on the course servers due to technical limitations in the testing environment. If you are asked to clear the contents of a file, use the methods described above."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Filtering the contents of a file","tmcname":"part06-12_filtering_file_contents"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"solutions.csv"}]},{"type":"text","value":" contains some solutions to mathematics problems:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Arto;2+5;7"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Pekka;3-2;1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Erkki;9+3;11"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Arto;8-3;4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Pekka;5+5;10"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"...jne..."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, on each line the format is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name_of_student;problem;result"}]},{"type":"text","value":". All the operations are either addition or subtraction, and each has exactly two operands."}]},{"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":"filter_solutions()"}]},{"type":"text","value":" which"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Reads the contents of the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"solutions.csv"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"writes those lines which have a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"correct"}]},{"type":"text","value":" result into the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"correct.csv"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"writes those lines which have an "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"incorrect"}]},{"type":"text","value":" result into the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"incorrect.csv"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Using the example above, the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"correct.csv"}]},{"type":"text","value":" would contain the lines"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"text","value":"Arto"},{"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":"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":"7"}]},{"type":"text","value":"\nPekka"},{"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":"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":"1"}]},{"type":"text","value":"\nPekka"},{"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":"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":"10"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The other two would be in the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"incorrect.csv"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write the lines in the same order as they appear in the original file. Do not change the original file."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the function should have the exact same result, no matter how many times it is called. That is, it shouldn't matter if the function is called once"}]},{"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":"filter_solutions"},{"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":"or multiple times in a row"}]},{"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":"filter_solutions"},{"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":"\nfilter_solutions"},{"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":"\nfilter_solutions"},{"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":"\nfilter_solutions"},{"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":"After the execution, the contents of the files "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"correct.csv"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"incorrect.csv"}]},{"type":"text","value":" should be exactly the same in either case."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Store personal data","tmcname":"part06-13_store_personal_data"},"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":"store_personal_data(person: tuple)"}]},{"type":"text","value":", which takes a tuple containing some identifying information as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The tuple contains the following elements:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Name (string)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Age (integer)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Height (float)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This should be processed and written into the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"people.csv"}]},{"type":"text","value":". The file may already contain some data; the new entry goes to the end of the file. The data should be written in the format"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"name;age;height"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each entry should be on a separate line. If we call the function with the argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(\"Paul Paulson\", 37, 175.5)"}]},{"type":"text","value":", the function should write this line to the end of the file:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Paul Paulson;37;175.5"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"handling-data-in-a-csv-format","style":"position:relative;"},"children":[{"type":"text","value":"Handling data in a CSV format"},{"type":"element","tagName":"a","properties":{"href":"#handling-data-in-a-csv-format","ariaLabel":"handling data in a csv format 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":"Let's write a program which assesses students' performance on a course. The program reads a CSV file, which contains weekly exercise points received by the students. The program then calculates the points total and determines the grade attained by each student. Finally, the program creates a CSV file containing the points total and grade for each student."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The CSV file given as input to the program looks like this:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter;4;2;3;5;4;0;0\nPaula;7;2;8;3;5;4;5\nSusan;3;4;3;5;3;4;4\nEmily;6;6;5;5;0;4;8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program logic is divided into three functions: reading the file and processing the contents into an accessible format, determining the grade, and writing the file."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The file is read following the principles covered in the previous section. The data is stored in a dictionary, where the key is the student's name, and the value is a list of the points received by the student, in integer format:"}]},{"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":"read_weekly_points"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"filename"},{"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 weekly_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","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"filename"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 point_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","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":" points "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" parts"},{"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":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n point_list"},{"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":"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":"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 weekly_points"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"parts"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" point_list\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" weekly_points"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The second function is for determining the grade based on the points received. This function is in turn used by the third function, which writes the results to the file."}]},{"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":"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","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" 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":"20"}]},{"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","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" 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":"25"}]},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" 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":"30"}]},{"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","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" 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":"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","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" 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":"40"}]},{"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","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"save_results"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"filename"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" weekly_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","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"filename"},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"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":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" point_list "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" weekly_points"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n point_sum "},{"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":"point_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"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":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"point_sum"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"grade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"point_sum"},{"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":"\\n\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This structure lets us write a very simple main function. Notice how the filenames for the files whch are read and written are given as arguments in 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":"text","value":"weekly_points "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" read_weekly_points"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"weekly_points.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsave_results"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"results.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" weekly_points"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the main function is executed, the contents of the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"results.csv"}]},{"type":"text","value":" created as a result looks like this:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter;18;0\nPaula;34;3\nSusan;26;2\nEmily;41;5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice how each function defined above is relatively simple, and they all have a single responsibility. This is a common and advisable approach when programming larger wholes. The single reponsibility principle makes verifying functionality easier. It also makes it easier to make changes to the program later, and to add new features."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Say we wanted to add a function for printing out the grade for a single student. We already have a function which determines the student's grade, so we can use this in our new 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"get_grade"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"student_name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" weekly_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","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" point_list "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" weekly_points"},{"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":"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":"if"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" student_name"},{"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":" grade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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":"point_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\n\nweekly_points "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" read_weekly_points"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"weekly_points.csv\""}]},{"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":"get_grade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Paula\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" weekly_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":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we determine a certain functionality in the program needs fixing, in a well designed program the change will affect only some select sections of code, and it will be easier to determine where the changes should be made. For example, if we wanted to change the grade boundaries, we'd only need to implement the change in the function for determining the grade, and it would work also in all the other functions utilizing this function. If the code for this single functionality was implemented in multiple places, there would be a definite risk that we would not remember to change all the instances when changing the functionality."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Course grading, part 4","tmcname":"part06-14_course_grading_part_4"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's revisit the course grading project from the previous section."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As we left if last time, the program read and processed files containing student information, completed exercises and exam results. We'll add a file containing information about the course. An example of the format of the file:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"\nname: Introduction to Programming\nstudy credits: 5\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should then create two files. There should be a file called "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"results.txt"}]},{"type":"text","value":" with the following contents:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Introduction to Programming, 5 credits\n======================================\nname exec_nbr exec_pts. exm_pts. tot_pts. grade\npekka peloton 21 5 9 14 0\njaana javanainen 27 6 11 17 1\nliisa virtanen 35 8 14 22 3\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The statistics section is identical to the results printed out in part 3 of the project. The only addition here is the header section."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Additionally, there should be a file called "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"results.csv"}]},{"type":"text","value":" with the following format:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"12345678;pekka peloton;0\n12345687;jaana javanainen;1\n12345699;liisa virtanen;3\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the program is executed, it should look like this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Student information: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"students1.csv"}]},{"type":"text","value":"\nExercises completed: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"exercises1.csv"}]},{"type":"text","value":"\nExam points: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"exam_points1.csv"}]},{"type":"text","value":"\nCourse information: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"course1.txt"}]},{"type":"text","value":"\nResults written to files results.txt and results.csv"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"That is, the program only asks for the names of the input files. All output should be written to the files. The user will only see a message confirming this."}]},{"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 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."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Word search","tmcname":"part06-15_word_search"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template includes the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"words.txt"}]},{"type":"text","value":", which contains words in English."}]},{"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":"find_words(search_term: str)"}]},{"type":"text","value":". It should return a list containing all the words in the file which match the search term."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The search term may include lowercase letters and the following wildcard characters:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"A dot "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" means that any single character is acceptable in its place. For example, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ca."}]},{"type":"text","value":" would yield words like "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"cat"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"car"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"p.ng"}]},{"type":"text","value":" would yield words like "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"ping"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"pong"}]},{"type":"text","value":", and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":".a.e"}]},{"type":"text","value":" would yield words like "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"sane"}]},{"type":"text","value":", "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"care"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"late"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"An asterisk "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" at the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"end"}]},{"type":"text","value":" of the search term means that any word which "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"begins"}]},{"type":"text","value":" with the search term is acceptable. An asterisk at the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"beginning"}]},{"type":"text","value":" of the search term means that any word which "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"ends"}]},{"type":"text","value":" with the search term is acceptable. For example, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ca*"}]},{"type":"text","value":" would yield words like "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"california"}]},{"type":"text","value":", "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"cat"}]},{"type":"text","value":", "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"caring"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"catapult"}]},{"type":"text","value":", while "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*ane"}]},{"type":"text","value":" would yield words like "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"crane"}]},{"type":"text","value":", "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"insane"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"aeroplane"}]},{"type":"text","value":". There can only ever be a single asterisk in the search term."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If there are no wildcard characters in the search term, only words which match the search term exactly are returned."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume both wildcards are never used in the same search term."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The words in the file are all written in lowercase. You may also assume the argument to the function will be in lowercase entirely."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If no matching words are found, the function should return an empty list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hint: the Pythons string methods "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"startswith()"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"endswith()"}]},{"type":"text","value":" may be useful here. You can search for more information about them online."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"text","value":"find_words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"*vokes\""}]},{"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":"['convokes', 'equivokes', 'evokes', 'invokes', 'provokes', 'reinvokes', 'revokes']"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Dictionary stored in a file","tmcname":"part06-16_dictionary_file"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which functions as a dictionary. The user can type in new entries or look for existing entries."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should work as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 - Add word, 2 - Search, 3 - Quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nThe word in Finnish: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"auto"}]},{"type":"text","value":"\nThe word in English: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"car"}]},{"type":"text","value":"\nDictionary entry added\n1 - Add word, 2 - Search, 3 - Quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nThe word in Finnish: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"roska"}]},{"type":"text","value":"\nThe word in English: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"garbage"}]},{"type":"text","value":"\nDictionary entry added\n1 - Add word, 2 - Search, 3 - Quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nThe word in Finnish: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"laukku"}]},{"type":"text","value":"\nThe word in English: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"bag"}]},{"type":"text","value":"\nDictionary entry added\n1 - Add word, 2 - Search, 3 - Quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nSearch term: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"bag"}]},{"type":"text","value":"\nroska - garbage\nlaukku - bag\n1 - Add word, 2 - Search, 3 - Quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nSearch term: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"car"}]},{"type":"text","value":"\nauto - car\n1 - Add word, 2 - Search, 3 - Quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nSearch term: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"laukku"}]},{"type":"text","value":"\nlaukku - bag\n1 - Add word, 2 - Search, 3 - Quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nBye!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The dictionary entries should be written to a file called "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dictionary.txt"}]},{"type":"text","value":". The program should first read the contents of the file. New entries are written to the end of the file whenever they are added to the dictionary."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The format of the data stored in the dictionary is up to you."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" the automatic tests for this exercise may change the contents of the file. If you want to keep its contents, first make a copy of the file under a different name."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB2:"}]},{"type":"text","value":" this exercise doesn't ask you to write any 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."}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will know how to create files with Python code
    • \n
    • You will be able to write text based data to a file
    • \n
    • You will know how to create a CSV file
    • \n

    So far we have read data from files, but it is naturally also possible to write data to files. Typically a program processes data and stores the results in a file, so they can be used later or processed further by some other program.

    We can create a new file every time we want to write data to a file, but we can also append new data to the end of an existing file. In both cases we use the open function from the previous section. For writing files the function requires a second argument.

    Creating a new file

    If you want to create a new file, you would call the open function with the additional argument w, to signify that the file should be opened in write mode. So, the function call could look like this:

    with open(\"new_file.txt\", \"w\") as my_file:\n    # code to write something to the file

    NB: if the file already exists, all the contents will be overwritten. It pays to be very careful when creating new files.

    With the file open you can write data to it. You can use the method write, which takes the string that is to be written as its argument.

    with open(\"new_file.txt\", \"w\") as my_file:\n    my_file.write(\"Hello there!\")

    When you execute the program, a new file named new_file.txt appears in the directory. The contents would look like this:

    Hello there!

    If you want line breaks in the file, you will have to add them by hand - the write function doesn't work exactly like the more familiar print function, though they are similar. So, the following program

    with open(\"new_file.txt\", \"w\") as my_file:\n    my_file.write(\"Hello there!\")\n    my_file.write(\"This is the second line\")\n    my_file.write(\"This is the last line\")

    would result in a file with these contents:

    Hello there!This is the second lineThis is the last line

    Line breaks are achieved by adding new line characters \\n to the argument strings:

    with open(\"new_file.txt\", \"w\") as my_file:\n    my_file.write(\"Hello there!\\n\")\n    my_file.write(\"This is the second line\\n\")\n    my_file.write(\"This is the last line\\n\")

    Now the contents of new_file.txt would look like this:

    Hello there!\nThis is the second line\nThis is the last line

    Please write a program which asks for the name of the user and then creates an \"inscription\" in a file specified by the user. Please see the example below.

    Whom should I sign this to: Ada\nWhere shall I save it: inscribed.txt

    The contents of the file inscribed.txt would be

    Hi Ada, we hope you enjoy learning Python with us! Best, Mooc.fi Team

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    Appending data to an existing file

    If you want to append data to the end of a file, instead of overwriting the entire file, you should open the file in append mode with the argument a.

    If the file doesn't yet exist, append mode works exatly like write mode.

    The following program opens the file new_file.txt and appends a couple of lines of text to the end:

    with open(\"new_file.txt\", \"a\") as my_file:\n    my_file.write(\"This is the 4th line\\n\")\n    my_file.write(\"And yet another line.\\n\")

    After this program is executed the contents of the file would look like this:

    Hello there!\nThis is the second line\nThis is the last line\nThis is the 4th line\nAnd yet another line.

    In programming practice, appending data to files is not a very common task.

    More often a file is read, processed and overwritten in its entirety. For example, when the contents should change in the middle of the file, it is usually easiest to overwrite the entire file.

    Please write a program which works as a simply diary. The diary entries should be saved in the file diary.txt. When the program is executed, it should first read any entries already in the file.

    NB: the automatic tests for this exercise will change the contents of the file. If you want to keep its contents, first make a copy of the file under a different name.

    The program should work as follows:

    1 - add an entry, 2 - read entries, 0 - quit\nFunction: 1\nDiary entry: Today I ate porridge\nDiary saved

    1 - add an entry, 2 - read entries, 0 - quit\nFunction: 2\nEntries:\nToday I ate porridge\n1 - add an entry, 2 - read entries, 0 - quit\nFunction: 1\nDiary entry: I went to the sauna in the evening\nDiary saved

    1 - add an entry, 2 - read entries, 0 - quit\nFunction: 2\nEntries:\nToday I ate porridge\nI went to the sauna in the evening\n1 - add an entry, 2 - read entries, 0 - quit\nFunction: 0\nBye now!

    When the program is executed for the second time, this should happen:

    1 - add an entry, 2 - read entries, 0 - quit\nFunction: 2\nEntries:\nToday I ate porridge\nI went to the sauna in the evening\n1 - add an entry, 2 - read entries, 0 - quit\nFunction: 0\nBye now!

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    Writing CSV files

    CSV files can be written line by line with the write method just like any other file. The following example creates the file coders.csv, with each line containing the name, working environment, favourite language and years of experience of a single programmer. The fields are separated by a semicolon.

    with open(\"coders.csv\", \"w\") as my_file:\n    my_file.write(\"Eric;Windows;Pascal;10\\n\")\n    my_file.write(\"Matt;Linux;PHP;2\\n\")\n    my_file.write(\"Alan;Linux;Java;17\\n\")\n    my_file.write(\"Emily;Mac;Cobol;9\\n\")

    Executing this program would result in the following file:

    Eric;Windows;Pascal;10\nMatt;Linux;PHP;2\nAlan;Linux;Java;17\nEmily;Mac;Cobol;9

    What if the data to be written is stored in computer memory in a list?

    coders = []\ncoders.append([\"Eric\", \"Windows\", \"Pascal\", 10])\ncoders.append([\"Matt\", \"Linux\", \"PHP\", 2])\ncoders.append([\"Alan\", \"Linux\", \"Java\", 17])\ncoders.append([\"Emily\", \"Mac\", \"Cobol\", 9])

    We can build the string we want to write as an f-string, and write the ready line to the file like so:

    with open(\"coders.csv\", \"w\") as my_file:\n    for coder in coders:\n        line = f\"{coder[0]};{coder[1]};{coder[2]};{coder[3]}\"\n        my_file.write(line+\"\\n\")

    If each list of coder data was very long, with many more items, building the string by hand would be quite cumbersome. We can use a for loop to build the string instead:

    with open(\"coders.csv\", \"w\") as my_file:\n    for coder in coders:\n        line = \"\"\n        for value in coder:\n            line += f\"{value};\"\n        line = line[:-1]\n        my_file.write(line+\"\\n\")

    Clearing file contents and deleting files

    Sometimes it is necessary to clear the contents of an existing file. Opening the file in write mode and closing the file immediately will achieve just this:

    with open(\"file_to_be_cleared.txt\", \"w\") as my_file:\n    pass

    Now the with block only contains the command pass, which doesn't actually do anything. Python does not allow empty blocks, so the command is necessary here.

    It is possible to also bypass the with block by using the following oneliner:

    open('file_to_be_cleared.txt', 'w').close()

    You can also delete a file entirely. We will have to ask for help from the operating system to achieve this:

    # the command to delete files is in the os module\nimport os\n\nos.remove(\"unnecessary_file.csv\")

    NB: this will not work when running the automatic tests on the course servers due to technical limitations in the testing environment. If you are asked to clear the contents of a file, use the methods described above.

    The file solutions.csv contains some solutions to mathematics problems:

    Arto;2+5;7\nPekka;3-2;1\nErkki;9+3;11\nArto;8-3;4\nPekka;5+5;10\n...jne...

    As you can see above, on each line the format is name_of_student;problem;result. All the operations are either addition or subtraction, and each has exactly two operands.

    Please write a function named filter_solutions() which

      \n
    • Reads the contents of the file solutions.csv
    • \n
    • writes those lines which have a correct result into the file correct.csv
    • \n
    • writes those lines which have an incorrect result into the file incorrect.csv
    • \n

    Using the example above, the file correct.csv would contain the lines

    Arto;2+5;7\nPekka;3-2;1\nPekka;5+5;10

    The other two would be in the file incorrect.csv.

    Please write the lines in the same order as they appear in the original file. Do not change the original file.

    NB: the function should have the exact same result, no matter how many times it is called. That is, it shouldn't matter if the function is called once

    filter_solutions()

    or multiple times in a row

    filter_solutions()\nfilter_solutions()\nfilter_solutions()\nfilter_solutions()

    After the execution, the contents of the files correct.csv and incorrect.csv should be exactly the same in either case.

    Please write a function named store_personal_data(person: tuple), which takes a tuple containing some identifying information as its argument.

    The tuple contains the following elements:

      \n
    • Name (string)
    • \n
    • Age (integer)
    • \n
    • Height (float)
    • \n

    This should be processed and written into the file people.csv. The file may already contain some data; the new entry goes to the end of the file. The data should be written in the format

    name;age;height

    Each entry should be on a separate line. If we call the function with the argument (\"Paul Paulson\", 37, 175.5), the function should write this line to the end of the file:

    Paul Paulson;37;175.5

    Handling data in a CSV format

    Let's write a program which assesses students' performance on a course. The program reads a CSV file, which contains weekly exercise points received by the students. The program then calculates the points total and determines the grade attained by each student. Finally, the program creates a CSV file containing the points total and grade for each student.

    The CSV file given as input to the program looks like this:

    Peter;4;2;3;5;4;0;0\nPaula;7;2;8;3;5;4;5\nSusan;3;4;3;5;3;4;4\nEmily;6;6;5;5;0;4;8

    The program logic is divided into three functions: reading the file and processing the contents into an accessible format, determining the grade, and writing the file.

    The file is read following the principles covered in the previous section. The data is stored in a dictionary, where the key is the student's name, and the value is a list of the points received by the student, in integer format:

    def read_weekly_points(filename):\n    weekly_points = {}\n    with open(filename) as my_file:\n        for line in my_file:\n            parts = line.split(\";\")\n            point_list = []\n            for points in parts[1:]:\n                point_list.append(int(points))\n            weekly_points[parts[0]] = point_list\n\n    return weekly_points

    The second function is for determining the grade based on the points received. This function is in turn used by the third function, which writes the results to the file.

    def grade(points):\n    if points < 20:\n        return 0\n    elif points < 25:\n        return 1\n    elif points < 30:\n        return 2\n    elif points < 35:\n        return 3\n    elif points < 40:\n        return 4\n    else:\n        return 5\n\ndef save_results(filename, weekly_points):\n    with open(filename, \"w\") as my_file:\n        for name, point_list in weekly_points.items():\n            point_sum = sum(point_list)\n            my_file.write(f\"{name};{point_sum};{grade(point_sum)}\\n\")

    This structure lets us write a very simple main function. Notice how the filenames for the files whch are read and written are given as arguments in the main function:

    weekly_points = read_weekly_points(\"weekly_points.csv\")\nsave_results(\"results.csv\", weekly_points)

    When the main function is executed, the contents of the file results.csv created as a result looks like this:

    Peter;18;0\nPaula;34;3\nSusan;26;2\nEmily;41;5

    Notice how each function defined above is relatively simple, and they all have a single responsibility. This is a common and advisable approach when programming larger wholes. The single reponsibility principle makes verifying functionality easier. It also makes it easier to make changes to the program later, and to add new features.

    Say we wanted to add a function for printing out the grade for a single student. We already have a function which determines the student's grade, so we can use this in our new function:

    def get_grade(student_name, weekly_points):\n    for name, point_list in weekly_points.items():\n        if name == student_name:\n            return grade(sum(point_list))\n\n\nweekly_points = read_weekly_points(\"weekly_points.csv\")\nprint(get_grade(\"Paula\", weekly_points))\n

    3

    If we determine a certain functionality in the program needs fixing, in a well designed program the change will affect only some select sections of code, and it will be easier to determine where the changes should be made. For example, if we wanted to change the grade boundaries, we'd only need to implement the change in the function for determining the grade, and it would work also in all the other functions utilizing this function. If the code for this single functionality was implemented in multiple places, there would be a definite risk that we would not remember to change all the instances when changing the functionality.

    Let's revisit the course grading project from the previous section.

    As we left if last time, the program read and processed files containing student information, completed exercises and exam results. We'll add a file containing information about the course. An example of the format of the file:

    \n\nname: Introduction to Programming\nstudy credits: 5\n

    The program should then create two files. There should be a file called results.txt with the following contents:

    \nIntroduction to Programming, 5 credits\n======================================\nname                          exec_nbr  exec_pts. exm_pts.  tot_pts.  grade\npekka peloton                 21        5         9         14        0\njaana javanainen              27        6         11        17        1\nliisa virtanen                35        8         14        22        3\n

    The statistics section is identical to the results printed out in part 3 of the project. The only addition here is the header section.

    Additionally, there should be a file called results.csv with the following format:

    \n12345678;pekka peloton;0\n12345687;jaana javanainen;1\n12345699;liisa virtanen;3\n

    When the program is executed, it should look like this:

    Student information: students1.csv\nExercises completed: exercises1.csv\nExam points: exam_points1.csv\nCourse information: course1.txt\nResults written to files results.txt and results.csv

    That is, the program only asks for the names of the input files. All output should be written to the files. The user will only see a message confirming this.

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    The exercise template includes the file words.txt, which contains words in English.

    Please write a function named find_words(search_term: str). It should return a list containing all the words in the file which match the search term.

    The search term may include lowercase letters and the following wildcard characters:

      \n
    • A dot . means that any single character is acceptable in its place. For example, ca. would yield words like cat and car, p.ng would yield words like ping and pong, and .a.e would yield words like sane, care and late.
    • \n
    • An asterisk * at the end of the search term means that any word which begins with the search term is acceptable. An asterisk at the beginning of the search term means that any word which ends with the search term is acceptable. For example, ca* would yield words like california, cat, caring and catapult, while *ane would yield words like crane, insane and aeroplane. There can only ever be a single asterisk in the search term.
    • \n
    • If there are no wildcard characters in the search term, only words which match the search term exactly are returned.
    • \n

    You may assume both wildcards are never used in the same search term.

    The words in the file are all written in lowercase. You may also assume the argument to the function will be in lowercase entirely.

    If no matching words are found, the function should return an empty list.

    Hint: the Pythons string methods startswith() and endswith() may be useful here. You can search for more information about them online.

    An example of the function in action:

    print(find_words(\"*vokes\"))

    ['convokes', 'equivokes', 'evokes', 'invokes', 'provokes', 'reinvokes', 'revokes']

    Please write a program which functions as a dictionary. The user can type in new entries or look for existing entries.

    The program should work as follows:

    1 - Add word, 2 - Search, 3 - Quit\nFunction: 1\nThe word in Finnish: auto\nThe word in English: car\nDictionary entry added\n1 - Add word, 2 - Search, 3 - Quit\nFunction: 1\nThe word in Finnish: roska\nThe word in English: garbage\nDictionary entry added\n1 - Add word, 2 - Search, 3 - Quit\nFunction: 1\nThe word in Finnish: laukku\nThe word in English: bag\nDictionary entry added\n1 - Add word, 2 - Search, 3 - Quit\nFunction: 2\nSearch term: bag\nroska - garbage\nlaukku - bag\n1 - Add word, 2 - Search, 3 - Quit\nFunction: 2\nSearch term: car\nauto - car\n1 - Add word, 2 - Search, 3 - Quit\nFunction: 2\nSearch term: laukku\nlaukku - bag\n1 - Add word, 2 - Search, 3 - Quit\nFunction: 3\nBye!

    The dictionary entries should be written to a file called dictionary.txt. The program should first read the contents of the file. New entries are written to the end of the file whenever they are added to the dictionary.

    The format of the data stored in the dictionary is up to you.

    NB: the automatic tests for this exercise may change the contents of the file. If you want to keep its contents, first make a copy of the file under a different name.

    NB2: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    ","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-6/2-writing-files.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know how to create files with Python code"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to write text based data to a file"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to create a CSV file"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So far we have read data from files, but it is naturally also possible to write data to files. Typically a program processes data and stores the results in a file, so they can be used later or processed further by some other program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We can create a new file every time we want to write data to a file, but we can also append new data to the end of an existing file. In both cases we use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"open"}]},{"type":"text","value":" function from the previous section. For writing files the function requires a second argument."}]},{"type":"element","tagName":"h2","properties":{"id":"creating-a-new-file","style":"position:relative;"},"children":[{"type":"text","value":"Creating a new file"},{"type":"element","tagName":"a","properties":{"href":"#creating-a-new-file","ariaLabel":"creating a new file 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":"If you want to create a new file, you would call the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"open"}]},{"type":"text","value":" function with the additional argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"w"}]},{"type":"text","value":", to signify that the file should be opened in write mode. So, the function call 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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"new_file.txt\""}]},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"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":"# code to write something to the file"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB: if the file already exists, all the contents will be overwritten"}]},{"type":"text","value":". It pays to be very careful when creating new files."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With the file open you can write data to it. You can use the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"write"}]},{"type":"text","value":", which takes the string that is to be written as its argument."}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"new_file.txt\""}]},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hello there!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When you execute the program, a new file named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"new_file.txt"}]},{"type":"text","value":" appears in the directory. The contents would look like this:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hello there!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you want line breaks in the file, you will have to add them by hand - the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"write"}]},{"type":"text","value":" function doesn't work exactly like the more familiar "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" function, though they are similar. So, the following program"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"new_file.txt\""}]},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hello there!\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This is the second line\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This is the last line\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"would result in a file with these contents:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hello there!This is the second lineThis is the last line"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Line breaks are achieved by adding new line characters "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\\n"}]},{"type":"text","value":" to the argument 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":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"new_file.txt\""}]},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hello there!\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This is the second line\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This is the last line\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now the contents of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"new_file.txt"}]},{"type":"text","value":" would look like this:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hello there!\nThis is the second line\nThis is the last line"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Inscription","tmcname":"part06-10_inscription"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks for the name of the user and then creates an \"inscription\" in a file specified by the user. Please see the example below."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Whom should I sign this to: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Ada"}]},{"type":"text","value":"\nWhere shall I save it: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"inscribed.txt"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The contents of the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"inscribed.txt"}]},{"type":"text","value":" would be"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hi Ada, we hope you enjoy learning Python with us! Best, Mooc.fi Team"}]}]},{"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 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."}]}]},{"type":"element","tagName":"h2","properties":{"id":"appending-data-to-an-existing-file","style":"position:relative;"},"children":[{"type":"text","value":"Appending data to an existing file"},{"type":"element","tagName":"a","properties":{"href":"#appending-data-to-an-existing-file","ariaLabel":"appending data to an existing file 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":"If you want to append data to the end of a file, instead of overwriting the entire file, you should open the file in append mode with the argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the file doesn't yet exist, append mode works exatly like write mode."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program opens the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"new_file.txt"}]},{"type":"text","value":" and appends a couple of lines of text to the end:"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"new_file.txt\""}]},{"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":"\"a\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This is the 4th line\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"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 yet another line.\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"After this program is executed the contents of the file would look like this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hello there!\nThis is the second line\nThis is the last line\nThis is the 4th line\nAnd yet another line."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In programming practice, appending data to files is not a very common task."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"More often a file is read, processed and overwritten in its entirety. For example, when the contents should change in the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"middle"}]},{"type":"text","value":" of the file, it is usually easiest to overwrite the entire file."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Diary","tmcname":"part06-11_diary"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which works as a simply diary. The diary entries should be saved in the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"diary.txt"}]},{"type":"text","value":". When the program is executed, it should first read any entries already in the file."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the automatic tests for this exercise will change the contents of the file. If you want to keep its contents, first make a copy of the file under a different name."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should work as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 - add an entry, 2 - read entries, 0 - quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nDiary entry: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Today I ate porridge"}]},{"type":"text","value":"\nDiary saved"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 - add an entry, 2 - read entries, 0 - quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nEntries:\nToday I ate porridge\n1 - add an entry, 2 - read entries, 0 - quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nDiary entry: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"I went to the sauna in the evening"}]},{"type":"text","value":"\nDiary saved"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 - add an entry, 2 - read entries, 0 - quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nEntries:\nToday I ate porridge\nI went to the sauna in the evening\n1 - add an entry, 2 - read entries, 0 - quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nBye now!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the program is executed for the second time, this should happen:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 - add an entry, 2 - read entries, 0 - quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nEntries:\nToday I ate porridge\nI went to the sauna in the evening\n1 - add an entry, 2 - read entries, 0 - quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\nBye now!"}]}]},{"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 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."}]}]},{"type":"element","tagName":"h2","properties":{"id":"writing-csv-files","style":"position:relative;"},"children":[{"type":"text","value":"Writing CSV files"},{"type":"element","tagName":"a","properties":{"href":"#writing-csv-files","ariaLabel":"writing csv files 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":"CSV files can be written line by line with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"write"}]},{"type":"text","value":" method just like any other file. The following example creates the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"coders.csv"}]},{"type":"text","value":", with each line containing the name, working environment, favourite language and years of experience of a single programmer. The fields are separated by a semicolon."}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"coders.csv\""}]},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric;Windows;Pascal;10\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Matt;Linux;PHP;2\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Alan;Linux;Java;17\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily;Mac;Cobol;9\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Executing this program would result in the following file:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eric;Windows;Pascal;10\nMatt;Linux;PHP;2\nAlan;Linux;Java;17\nEmily;Mac;Cobol;9"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What if the data to be written is stored in computer memory in 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":"coders "},{"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":"\ncoders"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"\"Windows\""}]},{"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":"\"Pascal\""}]},{"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":"10"}]},{"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":"\ncoders"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Matt\""}]},{"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":"\"Linux\""}]},{"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":"\"PHP\""}]},{"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":"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":"\ncoders"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Alan\""}]},{"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":"\"Linux\""}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"17"}]},{"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":"\ncoders"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"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":"\"Mac\""}]},{"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":"\"Cobol\""}]},{"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":"9"}]},{"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":"We can build the string we want to write as an f-string, and write the ready line to the file like so:"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"coders.csv\""}]},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"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":" coder "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" coders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n line "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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":"coder"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"coder"},{"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":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"coder"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":";"}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"coder"},{"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","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"line"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If each list of coder data was very long, with many more items, building the string by hand would be quite cumbersome. We can use a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop to build the string instead:"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"coders.csv\""}]},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"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":" coder "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" coders"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n line "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" value "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" coder"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n line "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"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":"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 line "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"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":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"line"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"\\n\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"clearing-file-contents-and-deleting-files","style":"position:relative;"},"children":[{"type":"text","value":"Clearing file contents and deleting files"},{"type":"element","tagName":"a","properties":{"href":"#clearing-file-contents-and-deleting-files","ariaLabel":"clearing file contents and deleting files 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":"Sometimes it is necessary to clear the contents of an existing file. Opening the file in write mode and closing the file immediately will achieve just 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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"file_to_be_cleared.txt\""}]},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"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":"pass"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" block only contains the command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":", which doesn't actually do anything. Python does not allow empty blocks, so the command is necessary here."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is possible to also bypass the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" block by using the following oneliner:"}]},{"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","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"'file_to_be_cleared.txt'"}]},{"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":"'w'"}]},{"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":"close"},{"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":"text-box","properties":{"variant":"hint","name":"Deleting files"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can also delete a file entirely. We will have to ask for help from the operating system to achieve 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","comment"]},"children":[{"type":"text","value":"# the command to delete files is in the os module"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" os\n\nos"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"unnecessary_file.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: this will not work when running the automatic tests on the course servers due to technical limitations in the testing environment. If you are asked to clear the contents of a file, use the methods described above."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Filtering the contents of a file","tmcname":"part06-12_filtering_file_contents"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"solutions.csv"}]},{"type":"text","value":" contains some solutions to mathematics problems:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Arto;2+5;7"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Pekka;3-2;1"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Erkki;9+3;11"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Arto;8-3;4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"Pekka;5+5;10"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"...jne..."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, on each line the format is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name_of_student;problem;result"}]},{"type":"text","value":". All the operations are either addition or subtraction, and each has exactly two operands."}]},{"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":"filter_solutions()"}]},{"type":"text","value":" which"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Reads the contents of the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"solutions.csv"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"writes those lines which have a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"correct"}]},{"type":"text","value":" result into the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"correct.csv"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"writes those lines which have an "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"incorrect"}]},{"type":"text","value":" result into the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"incorrect.csv"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Using the example above, the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"correct.csv"}]},{"type":"text","value":" would contain the lines"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"text","value":"Arto"},{"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":"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":"7"}]},{"type":"text","value":"\nPekka"},{"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":"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":"1"}]},{"type":"text","value":"\nPekka"},{"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":"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":"10"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The other two would be in the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"incorrect.csv"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write the lines in the same order as they appear in the original file. Do not change the original file."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the function should have the exact same result, no matter how many times it is called. That is, it shouldn't matter if the function is called once"}]},{"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":"filter_solutions"},{"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":"or multiple times in a row"}]},{"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":"filter_solutions"},{"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":"\nfilter_solutions"},{"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":"\nfilter_solutions"},{"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":"\nfilter_solutions"},{"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":"After the execution, the contents of the files "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"correct.csv"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"incorrect.csv"}]},{"type":"text","value":" should be exactly the same in either case."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Store personal data","tmcname":"part06-13_store_personal_data"},"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":"store_personal_data(person: tuple)"}]},{"type":"text","value":", which takes a tuple containing some identifying information as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The tuple contains the following elements:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Name (string)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Age (integer)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Height (float)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This should be processed and written into the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"people.csv"}]},{"type":"text","value":". The file may already contain some data; the new entry goes to the end of the file. The data should be written in the format"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"name;age;height"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each entry should be on a separate line. If we call the function with the argument "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"(\"Paul Paulson\", 37, 175.5)"}]},{"type":"text","value":", the function should write this line to the end of the file:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Paul Paulson;37;175.5"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"handling-data-in-a-csv-format","style":"position:relative;"},"children":[{"type":"text","value":"Handling data in a CSV format"},{"type":"element","tagName":"a","properties":{"href":"#handling-data-in-a-csv-format","ariaLabel":"handling data in a csv format 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":"Let's write a program which assesses students' performance on a course. The program reads a CSV file, which contains weekly exercise points received by the students. The program then calculates the points total and determines the grade attained by each student. Finally, the program creates a CSV file containing the points total and grade for each student."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The CSV file given as input to the program looks like this:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter;4;2;3;5;4;0;0\nPaula;7;2;8;3;5;4;5\nSusan;3;4;3;5;3;4;4\nEmily;6;6;5;5;0;4;8"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program logic is divided into three functions: reading the file and processing the contents into an accessible format, determining the grade, and writing the file."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The file is read following the principles covered in the previous section. The data is stored in a dictionary, where the key is the student's name, and the value is a list of the points received by the student, in integer format:"}]},{"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":"read_weekly_points"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"filename"},{"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 weekly_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","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"filename"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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 point_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","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":" points "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" parts"},{"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":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n point_list"},{"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":"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":"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 weekly_points"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"parts"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" point_list\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" weekly_points"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The second function is for determining the grade based on the points received. This function is in turn used by the third function, which writes the results to the file."}]},{"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":"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","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" 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":"20"}]},{"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","number"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" 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":"25"}]},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" 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":"30"}]},{"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","number"]},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" 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":"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","number"]},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"elif"}]},{"type":"text","value":" 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":"40"}]},{"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","number"]},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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","number"]},"children":[{"type":"text","value":"5"}]},{"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":"save_results"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"filename"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" weekly_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","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"filename"},{"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":"\"w\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"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":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" point_list "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" weekly_points"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n point_sum "},{"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":"point_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"write"},{"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":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"point_sum"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"grade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"point_sum"},{"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":"\\n\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This structure lets us write a very simple main function. Notice how the filenames for the files whch are read and written are given as arguments in 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":"text","value":"weekly_points "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" read_weekly_points"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"weekly_points.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsave_results"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"results.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" weekly_points"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the main function is executed, the contents of the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"results.csv"}]},{"type":"text","value":" created as a result looks like this:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter;18;0\nPaula;34;3\nSusan;26;2\nEmily;41;5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice how each function defined above is relatively simple, and they all have a single responsibility. This is a common and advisable approach when programming larger wholes. The single reponsibility principle makes verifying functionality easier. It also makes it easier to make changes to the program later, and to add new features."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Say we wanted to add a function for printing out the grade for a single student. We already have a function which determines the student's grade, so we can use this in our new 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"get_grade"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"student_name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" weekly_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","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" point_list "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" weekly_points"},{"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":"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":"if"}]},{"type":"text","value":" name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" student_name"},{"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":" grade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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":"point_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\n\nweekly_points "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" read_weekly_points"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"weekly_points.csv\""}]},{"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":"get_grade"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Paula\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" weekly_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":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we determine a certain functionality in the program needs fixing, in a well designed program the change will affect only some select sections of code, and it will be easier to determine where the changes should be made. For example, if we wanted to change the grade boundaries, we'd only need to implement the change in the function for determining the grade, and it would work also in all the other functions utilizing this function. If the code for this single functionality was implemented in multiple places, there would be a definite risk that we would not remember to change all the instances when changing the functionality."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Course grading, part 4","tmcname":"part06-14_course_grading_part_4"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's revisit the course grading project from the previous section."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As we left if last time, the program read and processed files containing student information, completed exercises and exam results. We'll add a file containing information about the course. An example of the format of the file:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"\nname: Introduction to Programming\nstudy credits: 5\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should then create two files. There should be a file called "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"results.txt"}]},{"type":"text","value":" with the following contents:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"Introduction to Programming, 5 credits\n======================================\nname exec_nbr exec_pts. exm_pts. tot_pts. grade\npekka peloton 21 5 9 14 0\njaana javanainen 27 6 11 17 1\nliisa virtanen 35 8 14 22 3\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The statistics section is identical to the results printed out in part 3 of the project. The only addition here is the header section."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Additionally, there should be a file called "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"results.csv"}]},{"type":"text","value":" with the following format:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"12345678;pekka peloton;0\n12345687;jaana javanainen;1\n12345699;liisa virtanen;3\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the program is executed, it should look like this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Student information: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"students1.csv"}]},{"type":"text","value":"\nExercises completed: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"exercises1.csv"}]},{"type":"text","value":"\nExam points: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"exam_points1.csv"}]},{"type":"text","value":"\nCourse information: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"course1.txt"}]},{"type":"text","value":"\nResults written to files results.txt and results.csv"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"That is, the program only asks for the names of the input files. All output should be written to the files. The user will only see a message confirming this."}]},{"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 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."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Word search","tmcname":"part06-15_word_search"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template includes the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"words.txt"}]},{"type":"text","value":", which contains words in English."}]},{"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":"find_words(search_term: str)"}]},{"type":"text","value":". It should return a list containing all the words in the file which match the search term."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The search term may include lowercase letters and the following wildcard characters:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"A dot "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":" means that any single character is acceptable in its place. For example, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ca."}]},{"type":"text","value":" would yield words like "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"cat"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"car"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"p.ng"}]},{"type":"text","value":" would yield words like "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"ping"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"pong"}]},{"type":"text","value":", and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":".a.e"}]},{"type":"text","value":" would yield words like "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"sane"}]},{"type":"text","value":", "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"care"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"late"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"An asterisk "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" at the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"end"}]},{"type":"text","value":" of the search term means that any word which "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"begins"}]},{"type":"text","value":" with the search term is acceptable. An asterisk at the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"beginning"}]},{"type":"text","value":" of the search term means that any word which "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"ends"}]},{"type":"text","value":" with the search term is acceptable. For example, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ca*"}]},{"type":"text","value":" would yield words like "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"california"}]},{"type":"text","value":", "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"cat"}]},{"type":"text","value":", "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"caring"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"catapult"}]},{"type":"text","value":", while "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"*ane"}]},{"type":"text","value":" would yield words like "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"crane"}]},{"type":"text","value":", "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"insane"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"aeroplane"}]},{"type":"text","value":". There can only ever be a single asterisk in the search term."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If there are no wildcard characters in the search term, only words which match the search term exactly are returned."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume both wildcards are never used in the same search term."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The words in the file are all written in lowercase. You may also assume the argument to the function will be in lowercase entirely."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If no matching words are found, the function should return an empty list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hint: the Pythons string methods "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"startswith()"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"endswith()"}]},{"type":"text","value":" may be useful here. You can search for more information about them online."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"text","value":"find_words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"*vokes\""}]},{"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":"['convokes', 'equivokes', 'evokes', 'invokes', 'provokes', 'reinvokes', 'revokes']"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Dictionary stored in a file","tmcname":"part06-16_dictionary_file"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which functions as a dictionary. The user can type in new entries or look for existing entries."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should work as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1 - Add word, 2 - Search, 3 - Quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nThe word in Finnish: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"auto"}]},{"type":"text","value":"\nThe word in English: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"car"}]},{"type":"text","value":"\nDictionary entry added\n1 - Add word, 2 - Search, 3 - Quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nThe word in Finnish: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"roska"}]},{"type":"text","value":"\nThe word in English: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"garbage"}]},{"type":"text","value":"\nDictionary entry added\n1 - Add word, 2 - Search, 3 - Quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1"}]},{"type":"text","value":"\nThe word in Finnish: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"laukku"}]},{"type":"text","value":"\nThe word in English: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"bag"}]},{"type":"text","value":"\nDictionary entry added\n1 - Add word, 2 - Search, 3 - Quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nSearch term: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"bag"}]},{"type":"text","value":"\nroska - garbage\nlaukku - bag\n1 - Add word, 2 - Search, 3 - Quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nSearch term: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"car"}]},{"type":"text","value":"\nauto - car\n1 - Add word, 2 - Search, 3 - Quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\nSearch term: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"laukku"}]},{"type":"text","value":"\nlaukku - bag\n1 - Add word, 2 - Search, 3 - Quit\nFunction: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nBye!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The dictionary entries should be written to a file called "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dictionary.txt"}]},{"type":"text","value":". The program should first read the contents of the file. New entries are written to the end of the file whenever they are added to the dictionary."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The format of the data stored in the dictionary is up to you."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" the automatic tests for this exercise may change the contents of the file. If you want to keep its contents, first make a copy of the file under a different name."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB2:"}]},{"type":"text","value":" this exercise doesn't ask you to write any 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."}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will know how to create files with Python code
    • \n
    • You will be able to write text based data to a file
    • \n
    • You will know how to create a CSV file
    • \n

    So far we have read data from files, but it is naturally also possible to write data to files. Typically a program processes data and stores the results in a file, so they can be used later or processed further by some other program.

    We can create a new file every time we want to write data to a file, but we can also append new data to the end of an existing file. In both cases we use the open function from the previous section. For writing files the function requires a second argument.

    Creating a new file

    If you want to create a new file, you would call the open function with the additional argument w, to signify that the file should be opened in write mode. So, the function call could look like this:

    with open(\"new_file.txt\", \"w\") as my_file:\n    # code to write something to the file

    NB: if the file already exists, all the contents will be overwritten. It pays to be very careful when creating new files.

    With the file open you can write data to it. You can use the method write, which takes the string that is to be written as its argument.

    with open(\"new_file.txt\", \"w\") as my_file:\n    my_file.write(\"Hello there!\")

    When you execute the program, a new file named new_file.txt appears in the directory. The contents would look like this:

    Hello there!

    If you want line breaks in the file, you will have to add them by hand - the write function doesn't work exactly like the more familiar print function, though they are similar. So, the following program

    with open(\"new_file.txt\", \"w\") as my_file:\n    my_file.write(\"Hello there!\")\n    my_file.write(\"This is the second line\")\n    my_file.write(\"This is the last line\")

    would result in a file with these contents:

    Hello there!This is the second lineThis is the last line

    Line breaks are achieved by adding new line characters \\n to the argument strings:

    with open(\"new_file.txt\", \"w\") as my_file:\n    my_file.write(\"Hello there!\\n\")\n    my_file.write(\"This is the second line\\n\")\n    my_file.write(\"This is the last line\\n\")

    Now the contents of new_file.txt would look like this:

    Hello there!\nThis is the second line\nThis is the last line

    Please write a program which asks for the name of the user and then creates an \"inscription\" in a file specified by the user. Please see the example below.

    Whom should I sign this to: Ada\nWhere shall I save it: inscribed.txt

    The contents of the file inscribed.txt would be

    Hi Ada, we hope you enjoy learning Python with us! Best, Mooc.fi Team

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    Appending data to an existing file

    If you want to append data to the end of a file, instead of overwriting the entire file, you should open the file in append mode with the argument a.

    If the file doesn't yet exist, append mode works exatly like write mode.

    The following program opens the file new_file.txt and appends a couple of lines of text to the end:

    with open(\"new_file.txt\", \"a\") as my_file:\n    my_file.write(\"This is the 4th line\\n\")\n    my_file.write(\"And yet another line.\\n\")

    After this program is executed the contents of the file would look like this:

    Hello there!\nThis is the second line\nThis is the last line\nThis is the 4th line\nAnd yet another line.

    In programming practice, appending data to files is not a very common task.

    More often a file is read, processed and overwritten in its entirety. For example, when the contents should change in the middle of the file, it is usually easiest to overwrite the entire file.

    Please write a program which works as a simply diary. The diary entries should be saved in the file diary.txt. When the program is executed, it should first read any entries already in the file.

    NB: the automatic tests for this exercise will change the contents of the file. If you want to keep its contents, first make a copy of the file under a different name.

    The program should work as follows:

    1 - add an entry, 2 - read entries, 0 - quit\nFunction: 1\nDiary entry: Today I ate porridge\nDiary saved

    1 - add an entry, 2 - read entries, 0 - quit\nFunction: 2\nEntries:\nToday I ate porridge\n1 - add an entry, 2 - read entries, 0 - quit\nFunction: 1\nDiary entry: I went to the sauna in the evening\nDiary saved

    1 - add an entry, 2 - read entries, 0 - quit\nFunction: 2\nEntries:\nToday I ate porridge\nI went to the sauna in the evening\n1 - add an entry, 2 - read entries, 0 - quit\nFunction: 0\nBye now!

    When the program is executed for the second time, this should happen:

    1 - add an entry, 2 - read entries, 0 - quit\nFunction: 2\nEntries:\nToday I ate porridge\nI went to the sauna in the evening\n1 - add an entry, 2 - read entries, 0 - quit\nFunction: 0\nBye now!

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    Writing CSV files

    CSV files can be written line by line with the write method just like any other file. The following example creates the file coders.csv, with each line containing the name, working environment, favourite language and years of experience of a single programmer. The fields are separated by a semicolon.

    with open(\"coders.csv\", \"w\") as my_file:\n    my_file.write(\"Eric;Windows;Pascal;10\\n\")\n    my_file.write(\"Matt;Linux;PHP;2\\n\")\n    my_file.write(\"Alan;Linux;Java;17\\n\")\n    my_file.write(\"Emily;Mac;Cobol;9\\n\")

    Executing this program would result in the following file:

    Eric;Windows;Pascal;10\nMatt;Linux;PHP;2\nAlan;Linux;Java;17\nEmily;Mac;Cobol;9

    What if the data to be written is stored in computer memory in a list?

    coders = []\ncoders.append([\"Eric\", \"Windows\", \"Pascal\", 10])\ncoders.append([\"Matt\", \"Linux\", \"PHP\", 2])\ncoders.append([\"Alan\", \"Linux\", \"Java\", 17])\ncoders.append([\"Emily\", \"Mac\", \"Cobol\", 9])

    We can build the string we want to write as an f-string, and write the ready line to the file like so:

    with open(\"coders.csv\", \"w\") as my_file:\n    for coder in coders:\n        line = f\"{coder[0]};{coder[1]};{coder[2]};{coder[3]}\"\n        my_file.write(line+\"\\n\")

    If each list of coder data was very long, with many more items, building the string by hand would be quite cumbersome. We can use a for loop to build the string instead:

    with open(\"coders.csv\", \"w\") as my_file:\n    for coder in coders:\n        line = \"\"\n        for value in coder:\n            line += f\"{value};\"\n        line = line[:-1]\n        my_file.write(line+\"\\n\")

    Clearing file contents and deleting files

    Sometimes it is necessary to clear the contents of an existing file. Opening the file in write mode and closing the file immediately will achieve just this:

    with open(\"file_to_be_cleared.txt\", \"w\") as my_file:\n    pass

    Now the with block only contains the command pass, which doesn't actually do anything. Python does not allow empty blocks, so the command is necessary here.

    It is possible to also bypass the with block by using the following oneliner:

    open('file_to_be_cleared.txt', 'w').close()

    You can also delete a file entirely. We will have to ask for help from the operating system to achieve this:

    # the command to delete files is in the os module\nimport os\n\nos.remove(\"unnecessary_file.csv\")

    NB: this will not work when running the automatic tests on the course servers due to technical limitations in the testing environment. If you are asked to clear the contents of a file, use the methods described above.

    The file solutions.csv contains some solutions to mathematics problems:

    Arto;2+5;7\nPekka;3-2;1\nErkki;9+3;11\nArto;8-3;4\nPekka;5+5;10\n...jne...

    As you can see above, on each line the format is name_of_student;problem;result. All the operations are either addition or subtraction, and each has exactly two operands.

    Please write a function named filter_solutions() which

      \n
    • Reads the contents of the file solutions.csv
    • \n
    • writes those lines which have a correct result into the file correct.csv
    • \n
    • writes those lines which have an incorrect result into the file incorrect.csv
    • \n

    Using the example above, the file correct.csv would contain the lines

    Arto;2+5;7\nPekka;3-2;1\nPekka;5+5;10

    The other two would be in the file incorrect.csv.

    Please write the lines in the same order as they appear in the original file. Do not change the original file.

    NB: the function should have the exact same result, no matter how many times it is called. That is, it shouldn't matter if the function is called once

    filter_solutions()

    or multiple times in a row

    filter_solutions()\nfilter_solutions()\nfilter_solutions()\nfilter_solutions()

    After the execution, the contents of the files correct.csv and incorrect.csv should be exactly the same in either case.

    Please write a function named store_personal_data(person: tuple), which takes a tuple containing some identifying information as its argument.

    The tuple contains the following elements:

      \n
    • Name (string)
    • \n
    • Age (integer)
    • \n
    • Height (float)
    • \n

    This should be processed and written into the file people.csv. The file may already contain some data; the new entry goes to the end of the file. The data should be written in the format

    name;age;height

    Each entry should be on a separate line. If we call the function with the argument (\"Paul Paulson\", 37, 175.5), the function should write this line to the end of the file:

    Paul Paulson;37;175.5

    Handling data in a CSV format

    Let's write a program which assesses students' performance on a course. The program reads a CSV file, which contains weekly exercise points received by the students. The program then calculates the points total and determines the grade attained by each student. Finally, the program creates a CSV file containing the points total and grade for each student.

    The CSV file given as input to the program looks like this:

    Peter;4;2;3;5;4;0;0\nPaula;7;2;8;3;5;4;5\nSusan;3;4;3;5;3;4;4\nEmily;6;6;5;5;0;4;8

    The program logic is divided into three functions: reading the file and processing the contents into an accessible format, determining the grade, and writing the file.

    The file is read following the principles covered in the previous section. The data is stored in a dictionary, where the key is the student's name, and the value is a list of the points received by the student, in integer format:

    def read_weekly_points(filename):\n    weekly_points = {}\n    with open(filename) as my_file:\n        for line in my_file:\n            parts = line.split(\";\")\n            point_list = []\n            for points in parts[1:]:\n                point_list.append(int(points))\n            weekly_points[parts[0]] = point_list\n\n    return weekly_points

    The second function is for determining the grade based on the points received. This function is in turn used by the third function, which writes the results to the file.

    def grade(points):\n    if points < 20:\n        return 0\n    elif points < 25:\n        return 1\n    elif points < 30:\n        return 2\n    elif points < 35:\n        return 3\n    elif points < 40:\n        return 4\n    else:\n        return 5\n\ndef save_results(filename, weekly_points):\n    with open(filename, \"w\") as my_file:\n        for name, point_list in weekly_points.items():\n            point_sum = sum(point_list)\n            my_file.write(f\"{name};{point_sum};{grade(point_sum)}\\n\")

    This structure lets us write a very simple main function. Notice how the filenames for the files whch are read and written are given as arguments in the main function:

    weekly_points = read_weekly_points(\"weekly_points.csv\")\nsave_results(\"results.csv\", weekly_points)

    When the main function is executed, the contents of the file results.csv created as a result looks like this:

    Peter;18;0\nPaula;34;3\nSusan;26;2\nEmily;41;5

    Notice how each function defined above is relatively simple, and they all have a single responsibility. This is a common and advisable approach when programming larger wholes. The single reponsibility principle makes verifying functionality easier. It also makes it easier to make changes to the program later, and to add new features.

    Say we wanted to add a function for printing out the grade for a single student. We already have a function which determines the student's grade, so we can use this in our new function:

    def get_grade(student_name, weekly_points):\n    for name, point_list in weekly_points.items():\n        if name == student_name:\n            return grade(sum(point_list))\n\n\nweekly_points = read_weekly_points(\"weekly_points.csv\")\nprint(get_grade(\"Paula\", weekly_points))\n

    3

    If we determine a certain functionality in the program needs fixing, in a well designed program the change will affect only some select sections of code, and it will be easier to determine where the changes should be made. For example, if we wanted to change the grade boundaries, we'd only need to implement the change in the function for determining the grade, and it would work also in all the other functions utilizing this function. If the code for this single functionality was implemented in multiple places, there would be a definite risk that we would not remember to change all the instances when changing the functionality.

    Let's revisit the course grading project from the previous section.

    As we left if last time, the program read and processed files containing student information, completed exercises and exam results. We'll add a file containing information about the course. An example of the format of the file:

    \n\nname: Introduction to Programming\nstudy credits: 5\n

    The program should then create two files. There should be a file called results.txt with the following contents:

    \nIntroduction to Programming, 5 credits\n======================================\nname                          exec_nbr  exec_pts. exm_pts.  tot_pts.  grade\npekka peloton                 21        5         9         14        0\njaana javanainen              27        6         11        17        1\nliisa virtanen                35        8         14        22        3\n

    The statistics section is identical to the results printed out in part 3 of the project. The only addition here is the header section.

    Additionally, there should be a file called results.csv with the following format:

    \n12345678;pekka peloton;0\n12345687;jaana javanainen;1\n12345699;liisa virtanen;3\n

    When the program is executed, it should look like this:

    Student information: students1.csv\nExercises completed: exercises1.csv\nExam points: exam_points1.csv\nCourse information: course1.txt\nResults written to files results.txt and results.csv

    That is, the program only asks for the names of the input files. All output should be written to the files. The user will only see a message confirming this.

    NB: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    The exercise template includes the file words.txt, which contains words in English.

    Please write a function named find_words(search_term: str). It should return a list containing all the words in the file which match the search term.

    The search term may include lowercase letters and the following wildcard characters:

      \n
    • A dot . means that any single character is acceptable in its place. For example, ca. would yield words like cat and car, p.ng would yield words like ping and pong, and .a.e would yield words like sane, care and late.
    • \n
    • An asterisk * at the end of the search term means that any word which begins with the search term is acceptable. An asterisk at the beginning of the search term means that any word which ends with the search term is acceptable. For example, ca* would yield words like california, cat, caring and catapult, while *ane would yield words like crane, insane and aeroplane. There can only ever be a single asterisk in the search term.
    • \n
    • If there are no wildcard characters in the search term, only words which match the search term exactly are returned.
    • \n

    You may assume both wildcards are never used in the same search term.

    The words in the file are all written in lowercase. You may also assume the argument to the function will be in lowercase entirely.

    If no matching words are found, the function should return an empty list.

    Hint: the Pythons string methods startswith() and endswith() may be useful here. You can search for more information about them online.

    An example of the function in action:

    print(find_words(\"*vokes\"))

    ['convokes', 'equivokes', 'evokes', 'invokes', 'provokes', 'reinvokes', 'revokes']

    Please write a program which functions as a dictionary. The user can type in new entries or look for existing entries.

    The program should work as follows:

    1 - Add word, 2 - Search, 3 - Quit\nFunction: 1\nThe word in Finnish: auto\nThe word in English: car\nDictionary entry added\n1 - Add word, 2 - Search, 3 - Quit\nFunction: 1\nThe word in Finnish: roska\nThe word in English: garbage\nDictionary entry added\n1 - Add word, 2 - Search, 3 - Quit\nFunction: 1\nThe word in Finnish: laukku\nThe word in English: bag\nDictionary entry added\n1 - Add word, 2 - Search, 3 - Quit\nFunction: 2\nSearch term: bag\nroska - garbage\nlaukku - bag\n1 - Add word, 2 - Search, 3 - Quit\nFunction: 2\nSearch term: car\nauto - car\n1 - Add word, 2 - Search, 3 - Quit\nFunction: 2\nSearch term: laukku\nlaukku - bag\n1 - Add word, 2 - Search, 3 - Quit\nFunction: 3\nBye!

    The dictionary entries should be written to a file called dictionary.txt. The program should first read the contents of the file. New entries are written to the end of the file whenever they are added to the dictionary.

    The format of the data stored in the dictionary is up to you.

    NB: the automatic tests for this exercise may change the contents of the file. If you want to keep its contents, first make a copy of the file under a different name.

    NB2: this exercise doesn't ask you to write any functions, so you should not place any code within an if __name__ == \"__main__\" block.

    ","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-6/2-writing-files.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-6/3-errors/page-data.json b/page-data/part-6/3-errors/page-data.json index 9d6ab3651..f55836e4b 100644 --- a/page-data/part-6/3-errors/page-data.json +++ b/page-data/part-6/3-errors/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-6/3-errors", - "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 know how to handle invalid input"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will understand what are exceptions in programming"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be familiar with the most common exception types in Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to handle exceptions in your own programs"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The are two basic categories of errors that come up in programming contexts:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Syntax errors, which prevent the execution of the program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Runtime errors, which halt the execution"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Errors in category 1 are usually easy to fix, as the Python interpreter flags the error location when attempting to execute the program. Common syntax errors include a missing colon at the end of a header line, or a missing quotation mark at the end of a string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Errors in category 2 can be harder to spot, as it may happen that they only occur at a certain point in the execution of a program, and only in certain circumstances. The program may work just fine in most situations, but halt due to an error in a specific marginal case. We will now concentrate on handling these types of errors."}]},{"type":"element","tagName":"h2","properties":{"id":"input-validation","style":"position:relative;"},"children":[{"type":"text","value":"Input validation"},{"type":"element","tagName":"a","properties":{"href":"#input-validation","ariaLabel":"input validation 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":"Many errors that come up during the execution of a program have to do with invalid input. Some examples include:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"missing or empty input values in mandatory fields, such as empty strings when the length of the string is critical"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"negative values where only positive values are accepted, such as -15 as the amount of an ingredient in a recipe"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"missing files or typos in filenames"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"values that are too small or too large, for example when working with dates and times"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"invalid indexes, such as trying to access index 3 in the string \"hey\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"values of a wrong type, such as strings when integers are expected"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fortunately, we as programmers can prepare for most errors. Let's have a look at a program which asks the user for their age, and makes sure it is an acceptable number (between 0 and 150, in this case):"}]},{"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":"age "},{"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"]},"children":[{"type":"text","value":"\"Please type in your age: \""}]},{"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":"if"}]},{"type":"text","value":" age "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" age "},{"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":"150"}]},{"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":"\"That is a fine age\""}]},{"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":"else"}]},{"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":"\"This is not a valid age\""}]},{"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 your age: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"25"}]},{"type":"text","value":"\nThat is a fine age"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in your age: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-3"}]},{"type":"text","value":"\nThis is not a valid age"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As long as the user types in an integer value, our input validation seems to work fine. But what if they type in a string?"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in your age: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"twenty-three"}]},{"type":"text","value":"\nValueError: invalid literal for int() with base 10: 'twenty-three'"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":" function is unable to parse the input string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"twenty-three"}]},{"type":"text","value":" as a valid integer value. The execution halts and the above error message is printed."}]},{"type":"element","tagName":"h2","properties":{"id":"exceptions","style":"position:relative;"},"children":[{"type":"text","value":"Exceptions"},{"type":"element","tagName":"a","properties":{"href":"#exceptions","ariaLabel":"exceptions 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":"Errors that occur while the program is already running are called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"exceptions"}]},{"type":"text","value":". It is possible to prepare for exceptions, and handle them so that the execution continues despite them occurring."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exception handling in Python is accomplished with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" statements. The idea is that if something within a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":" block causes an exception, Python checks if there is a corresponding "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" block. If such a block exists, it is executed and the program themn continues as if nothing happened."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's change the above example so that the program is prepared for the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" exception:"}]},{"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":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n age "},{"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"]},"children":[{"type":"text","value":"\"Please type in your age: \""}]},{"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":"except"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" age "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" age "},{"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":"150"}]},{"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":"\"That is a fine age\""}]},{"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":"else"}]},{"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":"\"This is not a valid age\""}]},{"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 your age: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"twenty-three"}]},{"type":"text","value":"\nThis is not a valid age"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We can use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":" block to flag that the code within the block may cause an error. In the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" statement directly after the block the relevant error is mentioned. In the above example we covered only a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" exception. If the exception had some other cause, the execution would still have halted, despite the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" blocks."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the above example, if the error is caught, the value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"age"}]},{"type":"text","value":" is set to -1. This is an invalid input value which we have already programmed behaviour for, as the program excpects the age of the user to be greater than 0."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example we have a function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"read_integer"}]},{"type":"text","value":", which asks the user to type in an integer value, but the function is also prepared for invalid input. The function keeps asking for integers until the user types in a valid input value."}]},{"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":"read_integer"}]},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n input_str "},{"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 an integer: \""}]},{"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","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"input_str"},{"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":"except"}]},{"type":"text","value":" ValueError"},{"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":"\"This input is invalid\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nnumber "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" read_integer"},{"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":"\"Thank you!\""}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"to the power of three is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" number"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in an integer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"three"}]},{"type":"text","value":"\nThis input is invalid\nPlease type in an integer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"aybabtu"}]},{"type":"text","value":"\nThis input is invalid\nPlease type in an integer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nThank you!\n5 to the power of three is 125"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sometimes it is enough to catch exceptions with a try-except structure, without doing anything about them. That is, we can just ignore the situation in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" block."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we were to change the above example so that we only accepted integers smaller than 100, the results 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":"read_small_integer"}]},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n input_str "},{"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 an integer: \""}]},{"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":"text","value":"input_str"},{"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":"if"}]},{"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":"100"}]},{"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":" number\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" ValueError"},{"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":"pass"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# this command doesn't actually do anything"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This input is invalid\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nnumber "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" read_small_integer"},{"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":"number"},{"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":"\"to the power of three is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" number"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in an integer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"three"}]},{"type":"text","value":"\nThis input is invalid\nPlease type in an integer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1000"}]},{"type":"text","value":"\nThis input is invalid\nPlease type in an integer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nThank you!\n5 to the power of three is 125"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" block only contains the command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":", which doesn't do anything. Python does not allow empty blocks, so the command is necessary."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Reading input","tmcname":"part06-17_read_input"},"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":"read_input"}]},{"type":"text","value":", which asks the user for input until the user types in an integer which falls within the bounds given as arguments to the function. The function should return the final valid integer value typed in by the user."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":" read_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 number: \""}]},{"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":"5"}]},{"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":"10"}]},{"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":"\"You typed in:\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"seven"}]},{"type":"text","value":"\nYou must type in an integer between 5 and 10\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-3"}]},{"type":"text","value":"\nYou must type in an integer between 5 and 10\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\nYou typed in: 8"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"typical-errors","style":"position:relative;"},"children":[{"type":"text","value":"Typical errors"},{"type":"element","tagName":"a","properties":{"href":"#typical-errors","ariaLabel":"typical errors 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":"Here is a selection of typical errors you will likely come across, along with some situations where they may occur."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ValueError"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This error is often thrown when the argument passed to a function is somehow invalid. For example, the function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float(\"1,23\")"}]},{"type":"text","value":"causes an error, because decimals are always separated by a point in Python, and here we have a comma."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"TypeError"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This error occurs when a value is of the wrong type. For example, the function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len(10)"}]},{"type":"text","value":" causes a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TypeError"}]},{"type":"text","value":", because the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" requires a value whose length can be calculated, such as a string or a list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"IndexError"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This common error occurs when trying to refer to an index which doesn't exist. For example, the expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"abc\"[5]"}]},{"type":"text","value":" causes an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"IndexError"}]},{"type":"text","value":", because the string in question has no index 5."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ZeroDivisionError"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As the name implies, this error is thrown when trying to divide by zero, which we know from mathematics to always be a bad idea. For example, if we try to determine the arithmetic mean of values in a list with the formula "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum(my_list) / len(my_list)"}]},{"type":"text","value":", but our list has length zero, this error will occur."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Exceptions in file handling"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some common errors when working with files are "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"FileNotFoundError"}]},{"type":"text","value":" (when trying to access a file which doesn't exist), "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"io.UnsupportedOperation"}]},{"type":"text","value":" (when trying to perform an operation on a file which is not supported by the mode in which the file is opened) or "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"PermissionError"}]},{"type":"text","value":" (the program lacks necessary permissions to access the file)."}]},{"type":"element","tagName":"h2","properties":{"id":"handling-multiple-exceptions-at-once","style":"position:relative;"},"children":[{"type":"text","value":"Handling multiple exceptions at once"},{"type":"element","tagName":"a","properties":{"href":"#handling-multiple-exceptions-at-once","ariaLabel":"handling multiple exceptions at once 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":"There may be more than one "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" block attached to each "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":" block. For example, the following program can handle both a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"FileNotFoundException"}]},{"type":"text","value":" and a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PermissionError"}]},{"type":"text","value":":"}]},{"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":"try"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"example.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_file"},{"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":"line"},{"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":"except"}]},{"type":"text","value":" FileNotFoundError"},{"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 file example.txt was not found\""}]},{"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":"except"}]},{"type":"text","value":" PermissionError"},{"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":"\"No permission to access the file example.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sometimes it is not necessary to specify the error the program prepares for. Especially when dealing with files, it is often enough to know that an error has occurred, and safely exit the program. It is not always necessary to know "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"why"}]},{"type":"text","value":" the error occurred. If we need to cover for all possible exceptions, we can use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" block without specifying the error:"}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"example.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_file"},{"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":"line"},{"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":"except"}]},{"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":"\"There was an error when reading the file.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" statement here covers all possible errors, even those caused by the programming mistakes. Only syntax errors will not be caught by this, as they prevent the code from being executed in the first place."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, the following program will always throw an error, because the variable name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"my_file"}]},{"type":"text","value":" is written as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"myfile"}]},{"type":"text","value":" on the third line."}]},{"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":"try"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"example.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" myfile"},{"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":"line"},{"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":"except"}]},{"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":"\"There was an error when reading the file.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" block can hide the actual error: the problem here was not caused by file handling as such, but by the variable name which was misspelled. Without the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" block the error thrown would be shown, and the cause could be found more easily. Therefore it is usually a good idea to use only "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" blocks specifically declared for certain error types."}]},{"type":"element","tagName":"h2","properties":{"id":"passing-exceptions","style":"position:relative;"},"children":[{"type":"text","value":"Passing exceptions"},{"type":"element","tagName":"a","properties":{"href":"#passing-exceptions","ariaLabel":"passing exceptions 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":"If executing a function causes an exception, and this exception is not handled, it is passed on to the section of code which called the function, and so forth up the call chain, until it reaches the main function level. If it is not handled there, either, the execution of the program halts, and the exception is usually printed out for the user to see."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example we have the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"testing"}]},{"type":"text","value":". If it causes an exception, this is not handled within the function itself, but in 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"testing"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"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","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"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":"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":"try"}]},{"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":"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 number: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n testing"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"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":"\"Something went wrong\""}]},{"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 number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"three"}]},{"type":"text","value":"\nSomething went wrong"}]}]},{"type":"element","tagName":"h2","properties":{"id":"raising-exceptions","style":"position:relative;"},"children":[{"type":"text","value":"Raising exceptions"},{"type":"element","tagName":"a","properties":{"href":"#raising-exceptions","ariaLabel":"raising exceptions 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":"You can also raise exceptions, with the command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":". It may seem like an odd idea to purposefully cause errors in your programs, but it can, in fact, be a very useful mechanism."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For instance, it can sometimes be a good idea to raise an error when detecting invalid parameters. So far we have usually printed out messages when validating input, but if we are writing a function which is executed from elsewhere, just printing something out can go unnoticed when the function is called. Raising an error can make debugging easier."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example we have a function which calculates factorials (for example, the factorial of the number 5 is 1 * 2 * 3 * 4 * 5). If the argument passed to the function is negative, the function raises an error:"}]},{"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":"factorial"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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":"if"}]},{"type":"text","value":" n "},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 input was negative: \""}]},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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 k "},{"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":"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":"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","operator"]},"children":[{"type":"text","value":"+"}]},{"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":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" i\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" k\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":"factorial"},{"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","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":"factorial"},{"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","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":"factorial"},{"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":"div","properties":{},"children":[{"type":"text","value":"6\n720\nTraceback (most recent call last):\nFile \"test.py\", line 11, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nprint(factorial(-1))\nFile \"test.py\", line 3, in factorial\nraise ValueError(\"The input was negative: \" + str(n))\nValueError: The input was negative: -1"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Parameter validation","tmcname":"part06-18_parameter_validation"},"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":"new_person(name: str, age: int)"}]},{"type":"text","value":", which creates and returns a tuple containing the data in the arguments. The first element should be the name and the second the age."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the values stored in the parameter variables are not valid, the function should throw a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" exception."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Invalid parameters in this case include:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"name is an empty string"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"name contains less than two words"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"name is longer than 40 characters"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"age is a negative number"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"age is greater than 150"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Incorrect lottery numbers","tmcname":"part06-19_incorrect_lottery_numbers"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lottery_numbers.csv"}]},{"type":"text","value":" containts winning lottery numbers in the following format:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"week 1;5,7,11,13,23,24,30\nweek 2;9,13,14,24,34,35,37\n...etc..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each line should contain a header "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"week x"}]},{"type":"text","value":", followed by seven integer numbers which are all between 1 and 39 inclusive."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The file has been corrupted. Lines in the file may contain the following kinds of errors (these exact lines may not be present in the file, but errors in a similar format will be):"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The week number is incorrect:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"week zzc;1,5,13,22,24,25,26"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"One or more numbers are not correct:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"week 22;1,**,5,6,13,2b,34"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Too few numbers:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"week 13;4,6,17,19,24,33"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The numbers are too small or large:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"week 39;5,9,15,35,39,41,105"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The same number appears twice:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"week 41;5,12,3,35,12,14,36"}]}]},{"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":"filter_incorrect()"}]},{"type":"text","value":", which creates a file called "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"correct_numbers.csv"}]},{"type":"text","value":". The file should contain only those lines from the original file which are in the correct format."}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will know how to handle invalid input
    • \n
    • You will understand what are exceptions in programming
    • \n
    • You will be familiar with the most common exception types in Python
    • \n
    • You will be able to handle exceptions in your own programs
    • \n

    The are two basic categories of errors that come up in programming contexts:

      \n
    1. Syntax errors, which prevent the execution of the program
    2. \n
    3. Runtime errors, which halt the execution
    4. \n

    Errors in category 1 are usually easy to fix, as the Python interpreter flags the error location when attempting to execute the program. Common syntax errors include a missing colon at the end of a header line, or a missing quotation mark at the end of a string.

    Errors in category 2 can be harder to spot, as it may happen that they only occur at a certain point in the execution of a program, and only in certain circumstances. The program may work just fine in most situations, but halt due to an error in a specific marginal case. We will now concentrate on handling these types of errors.

    Input validation

    Many errors that come up during the execution of a program have to do with invalid input. Some examples include:

      \n
    • missing or empty input values in mandatory fields, such as empty strings when the length of the string is critical
    • \n
    • negative values where only positive values are accepted, such as -15 as the amount of an ingredient in a recipe
    • \n
    • missing files or typos in filenames
    • \n
    • values that are too small or too large, for example when working with dates and times
    • \n
    • invalid indexes, such as trying to access index 3 in the string \"hey\"
    • \n
    • values of a wrong type, such as strings when integers are expected
    • \n

    Fortunately, we as programmers can prepare for most errors. Let's have a look at a program which asks the user for their age, and makes sure it is an acceptable number (between 0 and 150, in this case):

    age = int(input(\"Please type in your age: \"))\nif age >= 0 and age <= 150:\n    print(\"That is a fine age\")\nelse:\n    print(\"This is not a valid age\")

    Please type in your age: 25\nThat is a fine age

    Please type in your age: -3\nThis is not a valid age

    As long as the user types in an integer value, our input validation seems to work fine. But what if they type in a string?

    Please type in your age: twenty-three\nValueError: invalid literal for int() with base 10: 'twenty-three'

    The int function is unable to parse the input string twenty-three as a valid integer value. The execution halts and the above error message is printed.

    Exceptions

    Errors that occur while the program is already running are called exceptions. It is possible to prepare for exceptions, and handle them so that the execution continues despite them occurring.

    Exception handling in Python is accomplished with try and except statements. The idea is that if something within a try block causes an exception, Python checks if there is a corresponding except block. If such a block exists, it is executed and the program themn continues as if nothing happened.

    Let's change the above example so that the program is prepared for the ValueError exception:

    try:\n    age = int(input(\"Please type in your age: \"))\nexcept ValueError:\n    age = -1\n\nif age >= 0 and age <= 150:\n    print(\"That is a fine age\")\nelse:\n    print(\"This is not a valid age\")

    Please type in your age: twenty-three\nThis is not a valid age

    We can use the try block to flag that the code within the block may cause an error. In the except statement directly after the block the relevant error is mentioned. In the above example we covered only a ValueError exception. If the exception had some other cause, the execution would still have halted, despite the try and except blocks.

    In the above example, if the error is caught, the value of age is set to -1. This is an invalid input value which we have already programmed behaviour for, as the program excpects the age of the user to be greater than 0.

    In the following example we have a function read_integer, which asks the user to type in an integer value, but the function is also prepared for invalid input. The function keeps asking for integers until the user types in a valid input value.

    def read_integer():\n    while True:\n        try:\n            input_str = input(\"Please type in an integer: \")\n            return int(input_str)\n        except ValueError:\n            print(\"This input is invalid\")\n\nnumber = read_integer()\nprint(\"Thank you!\")\nprint(number, \"to the power of three is\", number**3)

    Please type in an integer: three\nThis input is invalid\nPlease type in an integer: aybabtu\nThis input is invalid\nPlease type in an integer: 5\nThank you!\n5 to the power of three is 125

    Sometimes it is enough to catch exceptions with a try-except structure, without doing anything about them. That is, we can just ignore the situation in the except block.

    If we were to change the above example so that we only accepted integers smaller than 100, the results could look like this:

    def read_small_integer():\n    while True:\n        try:\n            input_str = input(\"Please type in an integer: \")\n            number = int(input_str)\n            if number < 100:\n                return number\n        except ValueError:\n            pass # this command doesn't actually do anything\n\n        print(\"This input is invalid\")\n\nnumber = read_small_integer()\nprint(number, \"to the power of three is\", number**3)

    Please type in an integer: three\nThis input is invalid\nPlease type in an integer: 1000\nThis input is invalid\nPlease type in an integer: 5\nThank you!\n5 to the power of three is 125

    Now the except block only contains the command pass, which doesn't do anything. Python does not allow empty blocks, so the command is necessary.

    Please write a function named read_input, which asks the user for input until the user types in an integer which falls within the bounds given as arguments to the function. The function should return the final valid integer value typed in by the user.

    An example of the function in action:

    number = read_input(\"Please type in a number: \", 5, 10)\nprint(\"You typed in:\", number)

    Please type in a number: seven\nYou must type in an integer between 5 and 10\nPlease type in a number: -3\nYou must type in an integer between 5 and 10\nPlease type in a number: 8\nYou typed in: 8

    Typical errors

    Here is a selection of typical errors you will likely come across, along with some situations where they may occur.

    ValueError

    This error is often thrown when the argument passed to a function is somehow invalid. For example, the function call float(\"1,23\")causes an error, because decimals are always separated by a point in Python, and here we have a comma.

    TypeError

    This error occurs when a value is of the wrong type. For example, the function call len(10) causes a TypeError, because the function len requires a value whose length can be calculated, such as a string or a list.

    IndexError

    This common error occurs when trying to refer to an index which doesn't exist. For example, the expression \"abc\"[5] causes an IndexError, because the string in question has no index 5.

    ZeroDivisionError

    As the name implies, this error is thrown when trying to divide by zero, which we know from mathematics to always be a bad idea. For example, if we try to determine the arithmetic mean of values in a list with the formula sum(my_list) / len(my_list), but our list has length zero, this error will occur.

    Exceptions in file handling

    Some common errors when working with files are FileNotFoundError (when trying to access a file which doesn't exist), io.UnsupportedOperation (when trying to perform an operation on a file which is not supported by the mode in which the file is opened) or PermissionError (the program lacks necessary permissions to access the file).

    Handling multiple exceptions at once

    There may be more than one except block attached to each try block. For example, the following program can handle both a FileNotFoundException and a PermissionError:

    try:\n    with open(\"example.txt\") as my_file:\n        for line in my_file:\n            print(line)\nexcept FileNotFoundError:\n    print(\"The file example.txt was not found\")\nexcept PermissionError:\n    print(\"No permission to access the file example.txt\")

    Sometimes it is not necessary to specify the error the program prepares for. Especially when dealing with files, it is often enough to know that an error has occurred, and safely exit the program. It is not always necessary to know why the error occurred. If we need to cover for all possible exceptions, we can use the except block without specifying the error:

    \ntry:\n    with open(\"example.txt\") as my_file:\n        for line in my_file:\n            print(line)\nexcept:\n    print(\"There was an error when reading the file.\")\n

    NB: the except statement here covers all possible errors, even those caused by the programming mistakes. Only syntax errors will not be caught by this, as they prevent the code from being executed in the first place.

    For example, the following program will always throw an error, because the variable name my_file is written as myfile on the third line.

    try:\n    with open(\"example.txt\") as my_file:\n        for line in myfile:\n            print(line)\nexcept:\n    print(\"There was an error when reading the file.\")

    An except block can hide the actual error: the problem here was not caused by file handling as such, but by the variable name which was misspelled. Without the except block the error thrown would be shown, and the cause could be found more easily. Therefore it is usually a good idea to use only except blocks specifically declared for certain error types.

    Passing exceptions

    If executing a function causes an exception, and this exception is not handled, it is passed on to the section of code which called the function, and so forth up the call chain, until it reaches the main function level. If it is not handled there, either, the execution of the program halts, and the exception is usually printed out for the user to see.

    In the following example we have the function testing. If it causes an exception, this is not handled within the function itself, but in the main function:

    def testing(x):\n    print(int(x) + 1)\n\ntry:\n    number = input(\"Please type in a number: \")\n    testing(number)\nexcept:\n    print(\"Something went wrong\")

    Please type in a number: three\nSomething went wrong

    Raising exceptions

    You can also raise exceptions, with the command raise. It may seem like an odd idea to purposefully cause errors in your programs, but it can, in fact, be a very useful mechanism.

    For instance, it can sometimes be a good idea to raise an error when detecting invalid parameters. So far we have usually printed out messages when validating input, but if we are writing a function which is executed from elsewhere, just printing something out can go unnoticed when the function is called. Raising an error can make debugging easier.

    In the following example we have a function which calculates factorials (for example, the factorial of the number 5 is 1 * 2 * 3 * 4 * 5). If the argument passed to the function is negative, the function raises an error:

    def factorial(n):\n    if n < 0:\n        raise ValueError(\"The input was negative: \" + str(n))\n    k = 1\n    for i in range(2, n + 1):\n        k *= i\n    return k\n\nprint(factorial(3))\nprint(factorial(6))\nprint(factorial(-1))
    6\n720\nTraceback (most recent call last):\nFile \"test.py\", line 11, in \nprint(factorial(-1))\nFile \"test.py\", line 3, in factorial\nraise ValueError(\"The input was negative: \" + str(n))\nValueError: The input was negative: -1

    Please write a function named new_person(name: str, age: int), which creates and returns a tuple containing the data in the arguments. The first element should be the name and the second the age.

    If the values stored in the parameter variables are not valid, the function should throw a ValueError exception.

    Invalid parameters in this case include:

      \n
    • name is an empty string
    • \n
    • name contains less than two words
    • \n
    • name is longer than 40 characters
    • \n
    • age is a negative number
    • \n
    • age is greater than 150
    • \n

    The file lottery_numbers.csv containts winning lottery numbers in the following format:

    week 1;5,7,11,13,23,24,30\nweek 2;9,13,14,24,34,35,37\n...etc...

    Each line should contain a header week x, followed by seven integer numbers which are all between 1 and 39 inclusive.

    The file has been corrupted. Lines in the file may contain the following kinds of errors (these exact lines may not be present in the file, but errors in a similar format will be):

    The week number is incorrect:

    week zzc;1,5,13,22,24,25,26

    One or more numbers are not correct:

    week 22;1,**,5,6,13,2b,34

    Too few numbers:

    week 13;4,6,17,19,24,33

    The numbers are too small or large:

    week 39;5,9,15,35,39,41,105

    The same number appears twice:

    week 41;5,12,3,35,12,14,36

    Please write a function named filter_incorrect(), which creates a file called correct_numbers.csv. The file should contain only those lines from the original file which are in the correct format.

    ","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-6/3-errors.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know how to handle invalid input"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will understand what are exceptions in programming"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be familiar with the most common exception types in Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to handle exceptions in your own programs"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The are two basic categories of errors that come up in programming contexts:"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Syntax errors, which prevent the execution of the program"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Runtime errors, which halt the execution"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Errors in category 1 are usually easy to fix, as the Python interpreter flags the error location when attempting to execute the program. Common syntax errors include a missing colon at the end of a header line, or a missing quotation mark at the end of a string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Errors in category 2 can be harder to spot, as it may happen that they only occur at a certain point in the execution of a program, and only in certain circumstances. The program may work just fine in most situations, but halt due to an error in a specific marginal case. We will now concentrate on handling these types of errors."}]},{"type":"element","tagName":"h2","properties":{"id":"input-validation","style":"position:relative;"},"children":[{"type":"text","value":"Input validation"},{"type":"element","tagName":"a","properties":{"href":"#input-validation","ariaLabel":"input validation 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":"Many errors that come up during the execution of a program have to do with invalid input. Some examples include:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"missing or empty input values in mandatory fields, such as empty strings when the length of the string is critical"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"negative values where only positive values are accepted, such as -15 as the amount of an ingredient in a recipe"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"missing files or typos in filenames"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"values that are too small or too large, for example when working with dates and times"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"invalid indexes, such as trying to access index 3 in the string \"hey\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"values of a wrong type, such as strings when integers are expected"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Fortunately, we as programmers can prepare for most errors. Let's have a look at a program which asks the user for their age, and makes sure it is an acceptable number (between 0 and 150, in this case):"}]},{"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":"age "},{"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"]},"children":[{"type":"text","value":"\"Please type in your age: \""}]},{"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":"if"}]},{"type":"text","value":" age "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" age "},{"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":"150"}]},{"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":"\"That is a fine age\""}]},{"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":"else"}]},{"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":"\"This is not a valid age\""}]},{"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 your age: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"25"}]},{"type":"text","value":"\nThat is a fine age"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in your age: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-3"}]},{"type":"text","value":"\nThis is not a valid age"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As long as the user types in an integer value, our input validation seems to work fine. But what if they type in a string?"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in your age: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"twenty-three"}]},{"type":"text","value":"\nValueError: invalid literal for int() with base 10: 'twenty-three'"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"int"}]},{"type":"text","value":" function is unable to parse the input string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"twenty-three"}]},{"type":"text","value":" as a valid integer value. The execution halts and the above error message is printed."}]},{"type":"element","tagName":"h2","properties":{"id":"exceptions","style":"position:relative;"},"children":[{"type":"text","value":"Exceptions"},{"type":"element","tagName":"a","properties":{"href":"#exceptions","ariaLabel":"exceptions 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":"Errors that occur while the program is already running are called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"exceptions"}]},{"type":"text","value":". It is possible to prepare for exceptions, and handle them so that the execution continues despite them occurring."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Exception handling in Python is accomplished with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" statements. The idea is that if something within a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":" block causes an exception, Python checks if there is a corresponding "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" block. If such a block exists, it is executed and the program themn continues as if nothing happened."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's change the above example so that the program is prepared for the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" exception:"}]},{"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":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n age "},{"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"]},"children":[{"type":"text","value":"\"Please type in your age: \""}]},{"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":"except"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n age "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" age "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" age "},{"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":"150"}]},{"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":"\"That is a fine age\""}]},{"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":"else"}]},{"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":"\"This is not a valid age\""}]},{"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 your age: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"twenty-three"}]},{"type":"text","value":"\nThis is not a valid age"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We can use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":" block to flag that the code within the block may cause an error. In the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" statement directly after the block the relevant error is mentioned. In the above example we covered only a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" exception. If the exception had some other cause, the execution would still have halted, despite the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" blocks."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the above example, if the error is caught, the value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"age"}]},{"type":"text","value":" is set to -1. This is an invalid input value which we have already programmed behaviour for, as the program excpects the age of the user to be greater than 0."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example we have a function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"read_integer"}]},{"type":"text","value":", which asks the user to type in an integer value, but the function is also prepared for invalid input. The function keeps asking for integers until the user types in a valid input value."}]},{"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":"read_integer"}]},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n input_str "},{"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 an integer: \""}]},{"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","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"input_str"},{"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":"except"}]},{"type":"text","value":" ValueError"},{"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":"\"This input is invalid\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nnumber "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" read_integer"},{"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":"\"Thank you!\""}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"to the power of three is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" number"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in an integer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"three"}]},{"type":"text","value":"\nThis input is invalid\nPlease type in an integer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"aybabtu"}]},{"type":"text","value":"\nThis input is invalid\nPlease type in an integer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nThank you!\n5 to the power of three is 125"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sometimes it is enough to catch exceptions with a try-except structure, without doing anything about them. That is, we can just ignore the situation in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" block."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we were to change the above example so that we only accepted integers smaller than 100, the results 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":"read_small_integer"}]},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"while"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n input_str "},{"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 an integer: \""}]},{"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":"text","value":"input_str"},{"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":"if"}]},{"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":"100"}]},{"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":" number\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" ValueError"},{"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":"pass"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# this command doesn't actually do anything"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This input is invalid\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nnumber "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" read_small_integer"},{"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":"number"},{"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":"\"to the power of three is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" number"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in an integer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"three"}]},{"type":"text","value":"\nThis input is invalid\nPlease type in an integer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1000"}]},{"type":"text","value":"\nThis input is invalid\nPlease type in an integer: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nThank you!\n5 to the power of three is 125"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" block only contains the command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":", which doesn't do anything. Python does not allow empty blocks, so the command is necessary."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Reading input","tmcname":"part06-17_read_input"},"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":"read_input"}]},{"type":"text","value":", which asks the user for input until the user types in an integer which falls within the bounds given as arguments to the function. The function should return the final valid integer value typed in by the user."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":" read_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 number: \""}]},{"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":"5"}]},{"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":"10"}]},{"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":"\"You typed in:\""}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"seven"}]},{"type":"text","value":"\nYou must type in an integer between 5 and 10\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-3"}]},{"type":"text","value":"\nYou must type in an integer between 5 and 10\nPlease type in a number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"8"}]},{"type":"text","value":"\nYou typed in: 8"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"typical-errors","style":"position:relative;"},"children":[{"type":"text","value":"Typical errors"},{"type":"element","tagName":"a","properties":{"href":"#typical-errors","ariaLabel":"typical errors 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":"Here is a selection of typical errors you will likely come across, along with some situations where they may occur."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ValueError"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This error is often thrown when the argument passed to a function is somehow invalid. For example, the function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"float(\"1,23\")"}]},{"type":"text","value":"causes an error, because decimals are always separated by a point in Python, and here we have a comma."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"TypeError"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This error occurs when a value is of the wrong type. For example, the function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len(10)"}]},{"type":"text","value":" causes a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TypeError"}]},{"type":"text","value":", because the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" requires a value whose length can be calculated, such as a string or a list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"IndexError"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This common error occurs when trying to refer to an index which doesn't exist. For example, the expression "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"abc\"[5]"}]},{"type":"text","value":" causes an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"IndexError"}]},{"type":"text","value":", because the string in question has no index 5."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"ZeroDivisionError"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As the name implies, this error is thrown when trying to divide by zero, which we know from mathematics to always be a bad idea. For example, if we try to determine the arithmetic mean of values in a list with the formula "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum(my_list) / len(my_list)"}]},{"type":"text","value":", but our list has length zero, this error will occur."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Exceptions in file handling"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some common errors when working with files are "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"FileNotFoundError"}]},{"type":"text","value":" (when trying to access a file which doesn't exist), "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"io.UnsupportedOperation"}]},{"type":"text","value":" (when trying to perform an operation on a file which is not supported by the mode in which the file is opened) or "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"PermissionError"}]},{"type":"text","value":" (the program lacks necessary permissions to access the file)."}]},{"type":"element","tagName":"h2","properties":{"id":"handling-multiple-exceptions-at-once","style":"position:relative;"},"children":[{"type":"text","value":"Handling multiple exceptions at once"},{"type":"element","tagName":"a","properties":{"href":"#handling-multiple-exceptions-at-once","ariaLabel":"handling multiple exceptions at once 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":"There may be more than one "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" block attached to each "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"try"}]},{"type":"text","value":" block. For example, the following program can handle both a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"FileNotFoundException"}]},{"type":"text","value":" and a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PermissionError"}]},{"type":"text","value":":"}]},{"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":"try"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"example.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_file"},{"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":"line"},{"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":"except"}]},{"type":"text","value":" FileNotFoundError"},{"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 file example.txt was not found\""}]},{"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":"except"}]},{"type":"text","value":" PermissionError"},{"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":"\"No permission to access the file example.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sometimes it is not necessary to specify the error the program prepares for. Especially when dealing with files, it is often enough to know that an error has occurred, and safely exit the program. It is not always necessary to know "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"why"}]},{"type":"text","value":" the error occurred. If we need to cover for all possible exceptions, we can use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" block without specifying the error:"}]},{"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":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"try"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"example.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" my_file"},{"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":"line"},{"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":"except"}]},{"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":"\"There was an error when reading the file.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" statement here covers all possible errors, even those caused by the programming mistakes. Only syntax errors will not be caught by this, as they prevent the code from being executed in the first place."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, the following program will always throw an error, because the variable name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"my_file"}]},{"type":"text","value":" is written as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"myfile"}]},{"type":"text","value":" on the third line."}]},{"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":"try"}]},{"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":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"example.txt\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" myfile"},{"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":"line"},{"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":"except"}]},{"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":"\"There was an error when reading the file.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" block can hide the actual error: the problem here was not caused by file handling as such, but by the variable name which was misspelled. Without the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" block the error thrown would be shown, and the cause could be found more easily. Therefore it is usually a good idea to use only "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"except"}]},{"type":"text","value":" blocks specifically declared for certain error types."}]},{"type":"element","tagName":"h2","properties":{"id":"passing-exceptions","style":"position:relative;"},"children":[{"type":"text","value":"Passing exceptions"},{"type":"element","tagName":"a","properties":{"href":"#passing-exceptions","ariaLabel":"passing exceptions 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":"If executing a function causes an exception, and this exception is not handled, it is passed on to the section of code which called the function, and so forth up the call chain, until it reaches the main function level. If it is not handled there, either, the execution of the program halts, and the exception is usually printed out for the user to see."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example we have the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"testing"}]},{"type":"text","value":". If it causes an exception, this is not handled within the function itself, but in 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","keyword"]},"children":[{"type":"text","value":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"testing"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"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","builtin"]},"children":[{"type":"text","value":"int"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"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":"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":"try"}]},{"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":"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 number: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n testing"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"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":"\"Something went wrong\""}]},{"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 number: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"three"}]},{"type":"text","value":"\nSomething went wrong"}]}]},{"type":"element","tagName":"h2","properties":{"id":"raising-exceptions","style":"position:relative;"},"children":[{"type":"text","value":"Raising exceptions"},{"type":"element","tagName":"a","properties":{"href":"#raising-exceptions","ariaLabel":"raising exceptions 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":"You can also raise exceptions, with the command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":". It may seem like an odd idea to purposefully cause errors in your programs, but it can, in fact, be a very useful mechanism."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For instance, it can sometimes be a good idea to raise an error when detecting invalid parameters. So far we have usually printed out messages when validating input, but if we are writing a function which is executed from elsewhere, just printing something out can go unnoticed when the function is called. Raising an error can make debugging easier."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example we have a function which calculates factorials (for example, the factorial of the number 5 is 1 * 2 * 3 * 4 * 5). If the argument passed to the function is negative, the function raises an error:"}]},{"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":"factorial"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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":"if"}]},{"type":"text","value":" n "},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 input was negative: \""}]},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"n"},{"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 k "},{"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":"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":"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","operator"]},"children":[{"type":"text","value":"+"}]},{"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":"text","value":"\n k "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*="}]},{"type":"text","value":" i\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" k\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":"factorial"},{"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","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":"factorial"},{"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","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":"factorial"},{"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":"div","properties":{},"children":[{"type":"text","value":"6\n720\nTraceback (most recent call last):\nFile \"test.py\", line 11, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nprint(factorial(-1))\nFile \"test.py\", line 3, in factorial\nraise ValueError(\"The input was negative: \" + str(n))\nValueError: The input was negative: -1"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Parameter validation","tmcname":"part06-18_parameter_validation"},"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":"new_person(name: str, age: int)"}]},{"type":"text","value":", which creates and returns a tuple containing the data in the arguments. The first element should be the name and the second the age."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the values stored in the parameter variables are not valid, the function should throw a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" exception."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Invalid parameters in this case include:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"name is an empty string"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"name contains less than two words"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"name is longer than 40 characters"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"age is a negative number"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"age is greater than 150"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Incorrect lottery numbers","tmcname":"part06-19_incorrect_lottery_numbers"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lottery_numbers.csv"}]},{"type":"text","value":" containts winning lottery numbers in the following format:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"week 1;5,7,11,13,23,24,30\nweek 2;9,13,14,24,34,35,37\n...etc..."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each line should contain a header "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"week x"}]},{"type":"text","value":", followed by seven integer numbers which are all between 1 and 39 inclusive."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The file has been corrupted. Lines in the file may contain the following kinds of errors (these exact lines may not be present in the file, but errors in a similar format will be):"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The week number is incorrect:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"week zzc;1,5,13,22,24,25,26"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"One or more numbers are not correct:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"week 22;1,**,5,6,13,2b,34"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Too few numbers:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"week 13;4,6,17,19,24,33"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The numbers are too small or large:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"week 39;5,9,15,35,39,41,105"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The same number appears twice:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"week 41;5,12,3,35,12,14,36"}]}]},{"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":"filter_incorrect()"}]},{"type":"text","value":", which creates a file called "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"correct_numbers.csv"}]},{"type":"text","value":". The file should contain only those lines from the original file which are in the correct format."}]}]},{"type":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"}]},"html":"

    After this section

      \n
    • You will know how to handle invalid input
    • \n
    • You will understand what are exceptions in programming
    • \n
    • You will be familiar with the most common exception types in Python
    • \n
    • You will be able to handle exceptions in your own programs
    • \n

    The are two basic categories of errors that come up in programming contexts:

      \n
    1. Syntax errors, which prevent the execution of the program
    2. \n
    3. Runtime errors, which halt the execution
    4. \n

    Errors in category 1 are usually easy to fix, as the Python interpreter flags the error location when attempting to execute the program. Common syntax errors include a missing colon at the end of a header line, or a missing quotation mark at the end of a string.

    Errors in category 2 can be harder to spot, as it may happen that they only occur at a certain point in the execution of a program, and only in certain circumstances. The program may work just fine in most situations, but halt due to an error in a specific marginal case. We will now concentrate on handling these types of errors.

    Input validation

    Many errors that come up during the execution of a program have to do with invalid input. Some examples include:

      \n
    • missing or empty input values in mandatory fields, such as empty strings when the length of the string is critical
    • \n
    • negative values where only positive values are accepted, such as -15 as the amount of an ingredient in a recipe
    • \n
    • missing files or typos in filenames
    • \n
    • values that are too small or too large, for example when working with dates and times
    • \n
    • invalid indexes, such as trying to access index 3 in the string \"hey\"
    • \n
    • values of a wrong type, such as strings when integers are expected
    • \n

    Fortunately, we as programmers can prepare for most errors. Let's have a look at a program which asks the user for their age, and makes sure it is an acceptable number (between 0 and 150, in this case):

    age = int(input(\"Please type in your age: \"))\nif age >= 0 and age <= 150:\n    print(\"That is a fine age\")\nelse:\n    print(\"This is not a valid age\")

    Please type in your age: 25\nThat is a fine age

    Please type in your age: -3\nThis is not a valid age

    As long as the user types in an integer value, our input validation seems to work fine. But what if they type in a string?

    Please type in your age: twenty-three\nValueError: invalid literal for int() with base 10: 'twenty-three'

    The int function is unable to parse the input string twenty-three as a valid integer value. The execution halts and the above error message is printed.

    Exceptions

    Errors that occur while the program is already running are called exceptions. It is possible to prepare for exceptions, and handle them so that the execution continues despite them occurring.

    Exception handling in Python is accomplished with try and except statements. The idea is that if something within a try block causes an exception, Python checks if there is a corresponding except block. If such a block exists, it is executed and the program themn continues as if nothing happened.

    Let's change the above example so that the program is prepared for the ValueError exception:

    try:\n    age = int(input(\"Please type in your age: \"))\nexcept ValueError:\n    age = -1\n\nif age >= 0 and age <= 150:\n    print(\"That is a fine age\")\nelse:\n    print(\"This is not a valid age\")

    Please type in your age: twenty-three\nThis is not a valid age

    We can use the try block to flag that the code within the block may cause an error. In the except statement directly after the block the relevant error is mentioned. In the above example we covered only a ValueError exception. If the exception had some other cause, the execution would still have halted, despite the try and except blocks.

    In the above example, if the error is caught, the value of age is set to -1. This is an invalid input value which we have already programmed behaviour for, as the program excpects the age of the user to be greater than 0.

    In the following example we have a function read_integer, which asks the user to type in an integer value, but the function is also prepared for invalid input. The function keeps asking for integers until the user types in a valid input value.

    def read_integer():\n    while True:\n        try:\n            input_str = input(\"Please type in an integer: \")\n            return int(input_str)\n        except ValueError:\n            print(\"This input is invalid\")\n\nnumber = read_integer()\nprint(\"Thank you!\")\nprint(number, \"to the power of three is\", number**3)

    Please type in an integer: three\nThis input is invalid\nPlease type in an integer: aybabtu\nThis input is invalid\nPlease type in an integer: 5\nThank you!\n5 to the power of three is 125

    Sometimes it is enough to catch exceptions with a try-except structure, without doing anything about them. That is, we can just ignore the situation in the except block.

    If we were to change the above example so that we only accepted integers smaller than 100, the results could look like this:

    def read_small_integer():\n    while True:\n        try:\n            input_str = input(\"Please type in an integer: \")\n            number = int(input_str)\n            if number < 100:\n                return number\n        except ValueError:\n            pass # this command doesn't actually do anything\n\n        print(\"This input is invalid\")\n\nnumber = read_small_integer()\nprint(number, \"to the power of three is\", number**3)

    Please type in an integer: three\nThis input is invalid\nPlease type in an integer: 1000\nThis input is invalid\nPlease type in an integer: 5\nThank you!\n5 to the power of three is 125

    Now the except block only contains the command pass, which doesn't do anything. Python does not allow empty blocks, so the command is necessary.

    Please write a function named read_input, which asks the user for input until the user types in an integer which falls within the bounds given as arguments to the function. The function should return the final valid integer value typed in by the user.

    An example of the function in action:

    number = read_input(\"Please type in a number: \", 5, 10)\nprint(\"You typed in:\", number)

    Please type in a number: seven\nYou must type in an integer between 5 and 10\nPlease type in a number: -3\nYou must type in an integer between 5 and 10\nPlease type in a number: 8\nYou typed in: 8

    Typical errors

    Here is a selection of typical errors you will likely come across, along with some situations where they may occur.

    ValueError

    This error is often thrown when the argument passed to a function is somehow invalid. For example, the function call float(\"1,23\")causes an error, because decimals are always separated by a point in Python, and here we have a comma.

    TypeError

    This error occurs when a value is of the wrong type. For example, the function call len(10) causes a TypeError, because the function len requires a value whose length can be calculated, such as a string or a list.

    IndexError

    This common error occurs when trying to refer to an index which doesn't exist. For example, the expression \"abc\"[5] causes an IndexError, because the string in question has no index 5.

    ZeroDivisionError

    As the name implies, this error is thrown when trying to divide by zero, which we know from mathematics to always be a bad idea. For example, if we try to determine the arithmetic mean of values in a list with the formula sum(my_list) / len(my_list), but our list has length zero, this error will occur.

    Exceptions in file handling

    Some common errors when working with files are FileNotFoundError (when trying to access a file which doesn't exist), io.UnsupportedOperation (when trying to perform an operation on a file which is not supported by the mode in which the file is opened) or PermissionError (the program lacks necessary permissions to access the file).

    Handling multiple exceptions at once

    There may be more than one except block attached to each try block. For example, the following program can handle both a FileNotFoundException and a PermissionError:

    try:\n    with open(\"example.txt\") as my_file:\n        for line in my_file:\n            print(line)\nexcept FileNotFoundError:\n    print(\"The file example.txt was not found\")\nexcept PermissionError:\n    print(\"No permission to access the file example.txt\")

    Sometimes it is not necessary to specify the error the program prepares for. Especially when dealing with files, it is often enough to know that an error has occurred, and safely exit the program. It is not always necessary to know why the error occurred. If we need to cover for all possible exceptions, we can use the except block without specifying the error:

    \ntry:\n    with open(\"example.txt\") as my_file:\n        for line in my_file:\n            print(line)\nexcept:\n    print(\"There was an error when reading the file.\")\n

    NB: the except statement here covers all possible errors, even those caused by the programming mistakes. Only syntax errors will not be caught by this, as they prevent the code from being executed in the first place.

    For example, the following program will always throw an error, because the variable name my_file is written as myfile on the third line.

    try:\n    with open(\"example.txt\") as my_file:\n        for line in myfile:\n            print(line)\nexcept:\n    print(\"There was an error when reading the file.\")

    An except block can hide the actual error: the problem here was not caused by file handling as such, but by the variable name which was misspelled. Without the except block the error thrown would be shown, and the cause could be found more easily. Therefore it is usually a good idea to use only except blocks specifically declared for certain error types.

    Passing exceptions

    If executing a function causes an exception, and this exception is not handled, it is passed on to the section of code which called the function, and so forth up the call chain, until it reaches the main function level. If it is not handled there, either, the execution of the program halts, and the exception is usually printed out for the user to see.

    In the following example we have the function testing. If it causes an exception, this is not handled within the function itself, but in the main function:

    def testing(x):\n    print(int(x) + 1)\n\ntry:\n    number = input(\"Please type in a number: \")\n    testing(number)\nexcept:\n    print(\"Something went wrong\")

    Please type in a number: three\nSomething went wrong

    Raising exceptions

    You can also raise exceptions, with the command raise. It may seem like an odd idea to purposefully cause errors in your programs, but it can, in fact, be a very useful mechanism.

    For instance, it can sometimes be a good idea to raise an error when detecting invalid parameters. So far we have usually printed out messages when validating input, but if we are writing a function which is executed from elsewhere, just printing something out can go unnoticed when the function is called. Raising an error can make debugging easier.

    In the following example we have a function which calculates factorials (for example, the factorial of the number 5 is 1 * 2 * 3 * 4 * 5). If the argument passed to the function is negative, the function raises an error:

    def factorial(n):\n    if n < 0:\n        raise ValueError(\"The input was negative: \" + str(n))\n    k = 1\n    for i in range(2, n + 1):\n        k *= i\n    return k\n\nprint(factorial(3))\nprint(factorial(6))\nprint(factorial(-1))
    6\n720\nTraceback (most recent call last):\nFile \"test.py\", line 11, in \nprint(factorial(-1))\nFile \"test.py\", line 3, in factorial\nraise ValueError(\"The input was negative: \" + str(n))\nValueError: The input was negative: -1

    Please write a function named new_person(name: str, age: int), which creates and returns a tuple containing the data in the arguments. The first element should be the name and the second the age.

    If the values stored in the parameter variables are not valid, the function should throw a ValueError exception.

    Invalid parameters in this case include:

      \n
    • name is an empty string
    • \n
    • name contains less than two words
    • \n
    • name is longer than 40 characters
    • \n
    • age is a negative number
    • \n
    • age is greater than 150
    • \n

    The file lottery_numbers.csv containts winning lottery numbers in the following format:

    week 1;5,7,11,13,23,24,30\nweek 2;9,13,14,24,34,35,37\n...etc...

    Each line should contain a header week x, followed by seven integer numbers which are all between 1 and 39 inclusive.

    The file has been corrupted. Lines in the file may contain the following kinds of errors (these exact lines may not be present in the file, but errors in a similar format will be):

    The week number is incorrect:

    week zzc;1,5,13,22,24,25,26

    One or more numbers are not correct:

    week 22;1,**,5,6,13,2b,34

    Too few numbers:

    week 13;4,6,17,19,24,33

    The numbers are too small or large:

    week 39;5,9,15,35,39,41,105

    The same number appears twice:

    week 41;5,12,3,35,12,14,36

    Please write a function named filter_incorrect(), which creates a file called correct_numbers.csv. The file should contain only those lines from the original file which are in the correct format.

    ","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-6/3-errors.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-6/4-scope-of-variables/page-data.json b/page-data/part-6/4-scope-of-variables/page-data.json index 82bf58f2f..594dd10d9 100644 --- a/page-data/part-6/4-scope-of-variables/page-data.json +++ b/page-data/part-6/4-scope-of-variables/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-6/4-scope-of-variables", - "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 know what is meant by a local variable"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how the scope of a variable affects how it is used"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what the Python keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"global"}]},{"type":"text","value":"means"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use local and global variables in the correct contexts"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"scope"}]},{"type":"text","value":" of a variable refers to the sections of a program where a variable is accessible. A "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"local"}]},{"type":"text","value":" variable is only accessible in a defined section of the program, while a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"global"}]},{"type":"text","value":" variable is available for use in any section of the program."}]},{"type":"element","tagName":"h2","properties":{"id":"local-variables","style":"position:relative;"},"children":[{"type":"text","value":"Local variables"},{"type":"element","tagName":"a","properties":{"href":"#local-variables","ariaLabel":"local variables 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":"Variables defined within a Python function are local variables, only available within the function. This applies to both function parameters, and other variables defined within the function definition. A variable which is local to a function "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"does not exist outside the function"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example we are trying to access the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" in the main function, but this causes an error:"}]},{"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":"testing"}]},{"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 x "},{"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":"5"}]},{"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":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntesting"},{"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":"x"},{"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\nNameError: name 'x' is not defined"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" only exists while the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"testing"}]},{"type":"text","value":" is being executed. Other functions or the main function cannot access the variable."}]},{"type":"element","tagName":"h2","properties":{"id":"global-variables","style":"position:relative;"},"children":[{"type":"text","value":"Global variables"},{"type":"element","tagName":"a","properties":{"href":"#global-variables","ariaLabel":"global variables 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":"Variables defined within the main function are global variables. We previously defined the main function as those sections of code in a Python program which do not fall within any other function. The value stored in a global variable can be accessed from any other function in the program, so the following does not cause any errors:"}]},{"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":"testing"}]},{"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 "},{"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":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nx "},{"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":"\ntesting"},{"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":"3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A global variable cannot be changed directly from within another function. The following function "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"has no effect"}]},{"type":"text","value":" on the value stored in the global 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":"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":"testing"}]},{"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 x "},{"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":"5"}]},{"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":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nx "},{"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":"\ntesting"},{"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":"x"},{"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":"Here the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"testing"}]},{"type":"text","value":" creates a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"new, local"}]},{"type":"text","value":" variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":", which \"masks\" the global variable while the function is being executed. This variable has the value 5, but it is a different variable than the global "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" which is defined in the main function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"But what would the following code do?"}]},{"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":"testing"}]},{"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 "},{"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":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n x "},{"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":"5"}]},{"type":"text","value":"\n\nx "},{"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":"\ntesting"},{"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":"x"},{"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":"UnboundLocalError: local variable 'x' referenced before assignment"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"testing"}]},{"type":"text","value":" assigns a value to the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":", so Python interprets "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" to be a local variable instead of the global variable of the same name. The function attempts to access the variable before it is defined, so there is an error."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we wish to specify that we mean to change the global variable within a function, we will need the Python keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"global"}]},{"type":"text","value":":"}]},{"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":"testing"}]},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"global"}]},{"type":"text","value":" x\n x "},{"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":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nx "},{"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":"5"}]},{"type":"text","value":"\ntesting"},{"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":"x"},{"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":"3\n3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now the assignment "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x = 3"}]},{"type":"text","value":" within the function also affects the main function. All sections of the program are using the same global variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"when-should-you-use-global-variables","style":"position:relative;"},"children":[{"type":"text","value":"When should you use global variables?"},{"type":"element","tagName":"a","properties":{"href":"#when-should-you-use-global-variables","ariaLabel":"when should you use global variables 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":"Global variables are not a way to bypass function parameters or return values, and they should not be used as such. That is, it is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"possible"}]},{"type":"text","value":" to write a function which stores its results directly in a global 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":"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":"calculate_sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":"global"}]},{"type":"text","value":" result\n result "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\n\ncalculate_sum"},{"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":" "},{"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":"\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":"p","properties":{},"children":[{"type":"text","value":"It is better to write a function with a return value, as we are used to do by now:"}]},{"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":"calculate_sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" calculate_sum"},{"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":" "},{"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":"\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":"p","properties":{},"children":[{"type":"text","value":"The advantage of the latter approach is that the function is an "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"independent"}]},{"type":"text","value":" whole. It has certain, defined parameters, and it returns a result. It has no side effects, so it can be tested and changed independently of the other sections of the program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Global variables are useful in situations where we need to have some common, \"higher level\" information available to all functions in the program. The following is an example of just such a situation:"}]},{"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":"calculate_sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":"global"}]},{"type":"text","value":" count\n count "},{"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":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\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":"calculate_difference"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":"global"}]},{"type":"text","value":" count\n count "},{"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":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" b\n\n\ncount "},{"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":"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":"calculate_sum"},{"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":" "},{"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","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":"calculate_sum"},{"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":" "},{"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","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":"calculate_difference"},{"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":" "},{"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":"\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":"calculate_sum"},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"There were\""}]},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"function calls\""}]},{"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\n10\n3\n1\nThere were 4 function calls"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this case we want to keep track of how many times either of the functions were called during the execution of the program. The global variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"count"}]},{"type":"text","value":" is useful here, because we can increment it from the functions themselves as they are executed, but still access the final value in the main function."}]},{"type":"element","tagName":"h2","properties":{"id":"passing-data-from-one-function-to-another-revisited","style":"position:relative;"},"children":[{"type":"text","value":"Passing data from one function to another, revisited"},{"type":"element","tagName":"a","properties":{"href":"#passing-data-from-one-function-to-another-revisited","ariaLabel":"passing data from one function to another revisited 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 4-6, some significant overlap"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If a program consists of multiple functions, the question of passing data from one function to another often comes up."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When we touched upon this topic "},{"type":"element","tagName":"a","properties":{"href":"/part-4/6-strings-and-lists#passing-data-from-one-function-to-another"},"children":[{"type":"text","value":"previously"}]},{"type":"text","value":", we had a program which asks the user for some integer values, prints them out, and performs some statistical analysis on the numbers. The program was 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":"An example of the program's execution:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in 5 numbers:\nNumber 1: 10\nNumber 2: 34\nNumber 3: -32\nNumber 4: 99\nNumber 5: -53\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 basic principle here is that the main function \"stores\" the data processed by the program. Here, this means the numbers typed in by the user, which are stored 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 numbers are needed in some function, the variable is passed as an argument, as seen above when 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":" are called. If the function produces a result that is relevant elsewhere in the program, the function returns this with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" statement, as seen 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":"analyse"}]},{"type":"text","value":" above."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As always in programming, there are many ways to arrive at the same functionality. It would be possible to use the keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"global"}]},{"type":"text","value":" and have the functions directly access the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"inputs"}]},{"type":"text","value":" variable defined in the main function. There are good reasons why "},{"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":"this is not a smart move"}]},{"type":"text","value":", however. If many different functions can access and potentially change a variable directly, it quickly becomes impossible to reliably track the state of the program, and the program risks becoming unpredictable. This is especially the case as the number of function involved grows large, as it is wont to do in large software projects."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In conclusion, 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, explicit "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"main"}]},{"type":"text","value":" 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":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this week's materials."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"9662ad5e-7c21-52be-b107-44a017a27144"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will know what is meant by a local variable
    • \n
    • You will know how the scope of a variable affects how it is used
    • \n
    • You will know what the Python keyword globalmeans
    • \n
    • You will be able to use local and global variables in the correct contexts
    • \n

    The scope of a variable refers to the sections of a program where a variable is accessible. A local variable is only accessible in a defined section of the program, while a global variable is available for use in any section of the program.

    Local variables

    Variables defined within a Python function are local variables, only available within the function. This applies to both function parameters, and other variables defined within the function definition. A variable which is local to a function does not exist outside the function.

    In the following example we are trying to access the variable x in the main function, but this causes an error:

    def testing():\n    x = 5\n    print(x)\n\ntesting()\nprint(x)

    5\nNameError: name 'x' is not defined

    The variable x only exists while the function testing is being executed. Other functions or the main function cannot access the variable.

    Global variables

    Variables defined within the main function are global variables. We previously defined the main function as those sections of code in a Python program which do not fall within any other function. The value stored in a global variable can be accessed from any other function in the program, so the following does not cause any errors:

    def testing():\n    print(x)\n\nx = 3\ntesting()

    3

    A global variable cannot be changed directly from within another function. The following function has no effect on the value stored in the global variable:

    def testing():\n    x = 5\n    print(x)\n\nx = 3\ntesting()\nprint(x)

    5\n3

    Here the function testing creates a new, local variable x, which \"masks\" the global variable while the function is being executed. This variable has the value 5, but it is a different variable than the global x which is defined in the main function.

    But what would the following code do?

    def testing():\n    print(x)\n    x = 5\n\nx = 3\ntesting()\nprint(x)

    UnboundLocalError: local variable 'x' referenced before assignment

    The function testing assigns a value to the variable x, so Python interprets x to be a local variable instead of the global variable of the same name. The function attempts to access the variable before it is defined, so there is an error.

    If we wish to specify that we mean to change the global variable within a function, we will need the Python keyword global:

    def testing():\n    global x\n    x = 3\n    print(x)\n\nx = 5\ntesting()\nprint(x)

    3\n3

    Now the assignment x = 3 within the function also affects the main function. All sections of the program are using the same global variable x.

    When should you use global variables?

    Global variables are not a way to bypass function parameters or return values, and they should not be used as such. That is, it is possible to write a function which stores its results directly in a global variable:

    def calculate_sum(a, b):\n    global result\n    result = a + b\n\ncalculate_sum(2, 3)\nprint(result)

    It is better to write a function with a return value, as we are used to do by now:

    def calculate_sum(a, b):\n    return a + b\n\nresult = calculate_sum(2, 3)\nprint(result)

    The advantage of the latter approach is that the function is an independent whole. It has certain, defined parameters, and it returns a result. It has no side effects, so it can be tested and changed independently of the other sections of the program.

    Global variables are useful in situations where we need to have some common, \"higher level\" information available to all functions in the program. The following is an example of just such a situation:

    def calculate_sum(a, b):\n    global count\n    count += 1\n    return a + b\n\ndef calculate_difference(a, b):\n    global count\n    count += 1\n    return a - b\n\n\ncount = 0\nprint(calculate_sum(2, 3))\nprint(calculate_sum(5, 5))\nprint(calculate_difference(5, 2))\nprint(calculate_sum(1, 0))\nprint(\"There were\", count, \"function calls\")

    5\n10\n3\n1\nThere were 4 function calls

    In this case we want to keep track of how many times either of the functions were called during the execution of the program. The global variable count is useful here, because we can increment it from the functions themselves as they are executed, but still access the final value in the main function.

    Passing data from one function to another, revisited

    If a program consists of multiple functions, the question of passing data from one function to another often comes up.

    When we touched upon this topic previously, we had a program which asks the user for some integer values, prints them out, and performs some statistical analysis on the numbers. The program was divided into three separate functions:

    def input_from_user(how_many: int):\n    print(f\"Please type in {how_many} numbers:\")\n    numbers = []\n\n    for i in range(how_many):\n        number = int(input(f\"Number {i+1}: \"))\n        numbers.append(number)\n\n    return numbers\n\ndef print_result(numbers: list):\n    print(\"The numbers are: \")\n    for number in numbers:\n        print(number)\n\ndef analyze(numbers: list):\n    mean = sum(numbers) / len(numbers)\n    return f\"There are altogether {len(numbers)} numbers, the mean is {mean}, the smallest is {min(numbers)} and the greatest is {max(numbers)}\"\n\n# the main function using these functions\ninputs = input_from_user(5)\nprint_result(inputs)\nanalysis_result = analyze(inputs)\nprint(analysis_result)

    An example of the program's execution:

    Please type in 5 numbers:\nNumber 1: 10\nNumber 2: 34\nNumber 3: -32\nNumber 4: 99\nNumber 5: -53\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

    The basic principle here is that the main function \"stores\" the data processed by the program. Here, this means the numbers typed in by the user, which are stored in the variable inputs.

    If the numbers are needed in some function, the variable is passed as an argument, as seen above when the functions print_result and analyze are called. If the function produces a result that is relevant elsewhere in the program, the function returns this with a return statement, as seen with the functions input_from_user and analyse above.

    As always in programming, there are many ways to arrive at the same functionality. It would be possible to use the keyword global and have the functions directly access the inputs variable defined in the main function. There are good reasons why this is not a smart move, however. If many different functions can access and potentially change a variable directly, it quickly becomes impossible to reliably track the state of the program, and the program risks becoming unpredictable. This is especially the case as the number of function involved grows large, as it is wont to do in large software projects.

    In conclusion, passing data into and out of functions is best handled by arguments and return values.

    You could also separate the implicit main function in the example above into its own, explicit main function. Then the variable inputs would no longer be a global variable, but instead a local variable within the main function:

    # your main function goes here\ndef main():\n    inputs = input_from_user(5)\n    print_result(inputs)\n    analysis_result = analyze(inputs)\n\n    print(analysis_result)\n\n# run the main function\nmain()

    Please respond to a quick questionnaire on this week's materials.

    ","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-6/4-scope-of-variables.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know what is meant by a local variable"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how the scope of a variable affects how it is used"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what the Python keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"global"}]},{"type":"text","value":"means"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use local and global variables in the correct contexts"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"scope"}]},{"type":"text","value":" of a variable refers to the sections of a program where a variable is accessible. A "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"local"}]},{"type":"text","value":" variable is only accessible in a defined section of the program, while a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"global"}]},{"type":"text","value":" variable is available for use in any section of the program."}]},{"type":"element","tagName":"h2","properties":{"id":"local-variables","style":"position:relative;"},"children":[{"type":"text","value":"Local variables"},{"type":"element","tagName":"a","properties":{"href":"#local-variables","ariaLabel":"local variables 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":"Variables defined within a Python function are local variables, only available within the function. This applies to both function parameters, and other variables defined within the function definition. A variable which is local to a function "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"does not exist outside the function"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example we are trying to access the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" in the main function, but this causes an error:"}]},{"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":"testing"}]},{"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 x "},{"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":"5"}]},{"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":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ntesting"},{"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":"x"},{"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\nNameError: name 'x' is not defined"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" only exists while the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"testing"}]},{"type":"text","value":" is being executed. Other functions or the main function cannot access the variable."}]},{"type":"element","tagName":"h2","properties":{"id":"global-variables","style":"position:relative;"},"children":[{"type":"text","value":"Global variables"},{"type":"element","tagName":"a","properties":{"href":"#global-variables","ariaLabel":"global variables 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":"Variables defined within the main function are global variables. We previously defined the main function as those sections of code in a Python program which do not fall within any other function. The value stored in a global variable can be accessed from any other function in the program, so the following does not cause any errors:"}]},{"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":"testing"}]},{"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 "},{"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":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nx "},{"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":"\ntesting"},{"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":"3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A global variable cannot be changed directly from within another function. The following function "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"has no effect"}]},{"type":"text","value":" on the value stored in the global 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":"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":"testing"}]},{"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 x "},{"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":"5"}]},{"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":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nx "},{"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":"\ntesting"},{"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":"x"},{"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":"Here the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"testing"}]},{"type":"text","value":" creates a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"new, local"}]},{"type":"text","value":" variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":", which \"masks\" the global variable while the function is being executed. This variable has the value 5, but it is a different variable than the global "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" which is defined in the main function."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"But what would the following code do?"}]},{"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":"testing"}]},{"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 "},{"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":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n x "},{"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":"5"}]},{"type":"text","value":"\n\nx "},{"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":"\ntesting"},{"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":"x"},{"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":"UnboundLocalError: local variable 'x' referenced before assignment"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"testing"}]},{"type":"text","value":" assigns a value to the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":", so Python interprets "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" to be a local variable instead of the global variable of the same name. The function attempts to access the variable before it is defined, so there is an error."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we wish to specify that we mean to change the global variable within a function, we will need the Python keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"global"}]},{"type":"text","value":":"}]},{"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":"testing"}]},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"global"}]},{"type":"text","value":" x\n x "},{"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":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nx "},{"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":"5"}]},{"type":"text","value":"\ntesting"},{"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":"x"},{"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":"3\n3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now the assignment "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x = 3"}]},{"type":"text","value":" within the function also affects the main function. All sections of the program are using the same global variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"when-should-you-use-global-variables","style":"position:relative;"},"children":[{"type":"text","value":"When should you use global variables?"},{"type":"element","tagName":"a","properties":{"href":"#when-should-you-use-global-variables","ariaLabel":"when should you use global variables 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":"Global variables are not a way to bypass function parameters or return values, and they should not be used as such. That is, it is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"possible"}]},{"type":"text","value":" to write a function which stores its results directly in a global 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":"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":"calculate_sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":"global"}]},{"type":"text","value":" result\n result "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\n\ncalculate_sum"},{"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":" "},{"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":"\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":"p","properties":{},"children":[{"type":"text","value":"It is better to write a function with a return value, as we are used to do by now:"}]},{"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":"calculate_sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" calculate_sum"},{"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":" "},{"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":"\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":"p","properties":{},"children":[{"type":"text","value":"The advantage of the latter approach is that the function is an "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"independent"}]},{"type":"text","value":" whole. It has certain, defined parameters, and it returns a result. It has no side effects, so it can be tested and changed independently of the other sections of the program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Global variables are useful in situations where we need to have some common, \"higher level\" information available to all functions in the program. The following is an example of just such a situation:"}]},{"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":"calculate_sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":"global"}]},{"type":"text","value":" count\n count "},{"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":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" b\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":"calculate_difference"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"a"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" b"},{"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":"global"}]},{"type":"text","value":" count\n count "},{"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":"return"}]},{"type":"text","value":" a "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" b\n\n\ncount "},{"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":"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":"calculate_sum"},{"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":" "},{"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","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":"calculate_sum"},{"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":" "},{"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","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":"calculate_difference"},{"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":" "},{"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":"\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":"calculate_sum"},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"There were\""}]},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"function calls\""}]},{"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\n10\n3\n1\nThere were 4 function calls"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this case we want to keep track of how many times either of the functions were called during the execution of the program. The global variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"count"}]},{"type":"text","value":" is useful here, because we can increment it from the functions themselves as they are executed, but still access the final value in the main function."}]},{"type":"element","tagName":"h2","properties":{"id":"passing-data-from-one-function-to-another-revisited","style":"position:relative;"},"children":[{"type":"text","value":"Passing data from one function to another, revisited"},{"type":"element","tagName":"a","properties":{"href":"#passing-data-from-one-function-to-another-revisited","ariaLabel":"passing data from one function to another revisited 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 4-6, some significant overlap"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If a program consists of multiple functions, the question of passing data from one function to another often comes up."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When we touched upon this topic "},{"type":"element","tagName":"a","properties":{"href":"/part-4/6-strings-and-lists#passing-data-from-one-function-to-another"},"children":[{"type":"text","value":"previously"}]},{"type":"text","value":", we had a program which asks the user for some integer values, prints them out, and performs some statistical analysis on the numbers. The program was 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":"An example of the program's execution:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in 5 numbers:\nNumber 1: 10\nNumber 2: 34\nNumber 3: -32\nNumber 4: 99\nNumber 5: -53\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 basic principle here is that the main function \"stores\" the data processed by the program. Here, this means the numbers typed in by the user, which are stored 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 numbers are needed in some function, the variable is passed as an argument, as seen above when 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":" are called. If the function produces a result that is relevant elsewhere in the program, the function returns this with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" statement, as seen 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":"analyse"}]},{"type":"text","value":" above."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As always in programming, there are many ways to arrive at the same functionality. It would be possible to use the keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"global"}]},{"type":"text","value":" and have the functions directly access the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"inputs"}]},{"type":"text","value":" variable defined in the main function. There are good reasons why "},{"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":"this is not a smart move"}]},{"type":"text","value":", however. If many different functions can access and potentially change a variable directly, it quickly becomes impossible to reliably track the state of the program, and the program risks becoming unpredictable. This is especially the case as the number of function involved grows large, as it is wont to do in large software projects."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In conclusion, 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, explicit "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"main"}]},{"type":"text","value":" 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":"comment","value":"-\nA quiz to review the contents of this section:\n\n\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this week's materials."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"9662ad5e-7c21-52be-b107-44a017a27144"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will know what is meant by a local variable
    • \n
    • You will know how the scope of a variable affects how it is used
    • \n
    • You will know what the Python keyword globalmeans
    • \n
    • You will be able to use local and global variables in the correct contexts
    • \n

    The scope of a variable refers to the sections of a program where a variable is accessible. A local variable is only accessible in a defined section of the program, while a global variable is available for use in any section of the program.

    Local variables

    Variables defined within a Python function are local variables, only available within the function. This applies to both function parameters, and other variables defined within the function definition. A variable which is local to a function does not exist outside the function.

    In the following example we are trying to access the variable x in the main function, but this causes an error:

    def testing():\n    x = 5\n    print(x)\n\ntesting()\nprint(x)

    5\nNameError: name 'x' is not defined

    The variable x only exists while the function testing is being executed. Other functions or the main function cannot access the variable.

    Global variables

    Variables defined within the main function are global variables. We previously defined the main function as those sections of code in a Python program which do not fall within any other function. The value stored in a global variable can be accessed from any other function in the program, so the following does not cause any errors:

    def testing():\n    print(x)\n\nx = 3\ntesting()

    3

    A global variable cannot be changed directly from within another function. The following function has no effect on the value stored in the global variable:

    def testing():\n    x = 5\n    print(x)\n\nx = 3\ntesting()\nprint(x)

    5\n3

    Here the function testing creates a new, local variable x, which \"masks\" the global variable while the function is being executed. This variable has the value 5, but it is a different variable than the global x which is defined in the main function.

    But what would the following code do?

    def testing():\n    print(x)\n    x = 5\n\nx = 3\ntesting()\nprint(x)

    UnboundLocalError: local variable 'x' referenced before assignment

    The function testing assigns a value to the variable x, so Python interprets x to be a local variable instead of the global variable of the same name. The function attempts to access the variable before it is defined, so there is an error.

    If we wish to specify that we mean to change the global variable within a function, we will need the Python keyword global:

    def testing():\n    global x\n    x = 3\n    print(x)\n\nx = 5\ntesting()\nprint(x)

    3\n3

    Now the assignment x = 3 within the function also affects the main function. All sections of the program are using the same global variable x.

    When should you use global variables?

    Global variables are not a way to bypass function parameters or return values, and they should not be used as such. That is, it is possible to write a function which stores its results directly in a global variable:

    def calculate_sum(a, b):\n    global result\n    result = a + b\n\ncalculate_sum(2, 3)\nprint(result)

    It is better to write a function with a return value, as we are used to do by now:

    def calculate_sum(a, b):\n    return a + b\n\nresult = calculate_sum(2, 3)\nprint(result)

    The advantage of the latter approach is that the function is an independent whole. It has certain, defined parameters, and it returns a result. It has no side effects, so it can be tested and changed independently of the other sections of the program.

    Global variables are useful in situations where we need to have some common, \"higher level\" information available to all functions in the program. The following is an example of just such a situation:

    def calculate_sum(a, b):\n    global count\n    count += 1\n    return a + b\n\ndef calculate_difference(a, b):\n    global count\n    count += 1\n    return a - b\n\n\ncount = 0\nprint(calculate_sum(2, 3))\nprint(calculate_sum(5, 5))\nprint(calculate_difference(5, 2))\nprint(calculate_sum(1, 0))\nprint(\"There were\", count, \"function calls\")

    5\n10\n3\n1\nThere were 4 function calls

    In this case we want to keep track of how many times either of the functions were called during the execution of the program. The global variable count is useful here, because we can increment it from the functions themselves as they are executed, but still access the final value in the main function.

    Passing data from one function to another, revisited

    If a program consists of multiple functions, the question of passing data from one function to another often comes up.

    When we touched upon this topic previously, we had a program which asks the user for some integer values, prints them out, and performs some statistical analysis on the numbers. The program was divided into three separate functions:

    def input_from_user(how_many: int):\n    print(f\"Please type in {how_many} numbers:\")\n    numbers = []\n\n    for i in range(how_many):\n        number = int(input(f\"Number {i+1}: \"))\n        numbers.append(number)\n\n    return numbers\n\ndef print_result(numbers: list):\n    print(\"The numbers are: \")\n    for number in numbers:\n        print(number)\n\ndef analyze(numbers: list):\n    mean = sum(numbers) / len(numbers)\n    return f\"There are altogether {len(numbers)} numbers, the mean is {mean}, the smallest is {min(numbers)} and the greatest is {max(numbers)}\"\n\n# the main function using these functions\ninputs = input_from_user(5)\nprint_result(inputs)\nanalysis_result = analyze(inputs)\nprint(analysis_result)

    An example of the program's execution:

    Please type in 5 numbers:\nNumber 1: 10\nNumber 2: 34\nNumber 3: -32\nNumber 4: 99\nNumber 5: -53\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

    The basic principle here is that the main function \"stores\" the data processed by the program. Here, this means the numbers typed in by the user, which are stored in the variable inputs.

    If the numbers are needed in some function, the variable is passed as an argument, as seen above when the functions print_result and analyze are called. If the function produces a result that is relevant elsewhere in the program, the function returns this with a return statement, as seen with the functions input_from_user and analyse above.

    As always in programming, there are many ways to arrive at the same functionality. It would be possible to use the keyword global and have the functions directly access the inputs variable defined in the main function. There are good reasons why this is not a smart move, however. If many different functions can access and potentially change a variable directly, it quickly becomes impossible to reliably track the state of the program, and the program risks becoming unpredictable. This is especially the case as the number of function involved grows large, as it is wont to do in large software projects.

    In conclusion, passing data into and out of functions is best handled by arguments and return values.

    You could also separate the implicit main function in the example above into its own, explicit main function. Then the variable inputs would no longer be a global variable, but instead a local variable within the main function:

    # your main function goes here\ndef main():\n    inputs = input_from_user(5)\n    print_result(inputs)\n    analysis_result = analyze(inputs)\n\n    print(analysis_result)\n\n# run the main function\nmain()

    Please respond to a quick questionnaire on this week's materials.

    ","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-6/4-scope-of-variables.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-6/page-data.json b/page-data/part-6/page-data.json index 8d075847c..3fa83e1dc 100644 --- a/page-data/part-6/page-data.json +++ b/page-data/part-6/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/part-6", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-6","title":"Part 6"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-6/index.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-6","title":"Part 6"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-6/index.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-7/1-modules/page-data.json b/page-data/part-7/1-modules/page-data.json index 939e686f1..6a064ac14 100644 --- a/page-data/part-7/1-modules/page-data.json +++ b/page-data/part-7/1-modules/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-7/1-modules", - "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 know what a Python module is"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to include a module in your program with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to look for more information on the contents of modules"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"debugging-revisited","style":"position:relative;"},"children":[{"type":"text","value":"Debugging revisited"},{"type":"element","tagName":"a","properties":{"href":"#debugging-revisited","ariaLabel":"debugging revisited 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 have already come across quite a few debugging methods on this course. The "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":" should now be familiar to you, and "},{"type":"element","tagName":"a","properties":{"href":"/part-2/1-programming-terminology#debugging"},"children":[{"type":"text","value":"debugging print outs"}]},{"type":"text","value":" are a good old staple by now. You have possibly also tried the Visual Studio Code "},{"type":"element","tagName":"a","properties":{"href":"/part-4/1-vscode#the-built-in-debugger"},"children":[{"type":"text","value":"built-in debugger"}]},{"type":"text","value":". If you come across problems with the debugger not finding your files, there were some tips for handling these situations in the "},{"type":"element","tagName":"a","properties":{"href":"/part-6/1-reading-files#debugging-code-which-handles-files"},"children":[{"type":"text","value":"previous part"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In Python version 3.7 brought yet another easy and useful tool for debugging programs: the "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/functions.html?highlight=breakpoint#breakpoint","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"breakpoint()"}]},{"type":"text","value":" command."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can add this command to any point in your code (within normal syntactic rules, of course). When the program is run, the execution halts at the point where you inserted the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"breakpoint"}]},{"type":"text","value":" command. Here is an example of debugging efforts when completing one of the exercises from the previous part (please ignore the Finnish variable names in the images, and concentrate on the functionality):"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/f9eb8b0c4a4f642d316973fc108cae86/ec5f6/7_1_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: 62.173913043478265%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAACXBIWXMAABYlAAAWJQFJUiTwAAABeklEQVQoz43R247bIBQFUH/HhKu5GIMNPr40mQwYT+yM2qn6/79TxcnDqGqrLB0J8bBB2qdofQCAHvq+g7ZxlbUhdHYnhKCUsr/i3GheSCEQQozxvoMGAgx9bQzGmBCC/w0hpJUulFJaKoAAXees894zxu/h/0AIhbYppJQl56fTcd221geEEHkCRqi27hZWSo7TeDyefAhSSkopeU7BOWeM8pKzsiyFkEIS8nTY1rWzzlSmCyF4b4xxe88PZSkfZ+mcizEOwwAA1lpjTDEdv71fLjGllPNbiinPaZ7nJcc53a9vKQ7jOE3TsJt23vu2bYvleP55uX5eth/LusV5PadrzNeUP+blY16uKZ9hHKEHgKG//dnvvPdN0xS/XvMacyurinCFmcJ0H6bJbRRhNb7NCzqgL+7bLj7TkrfVQ6drw6TgSspKk5IfMDoQ/EIwJcRRgSmhXzwK+96/jr4Luu6MrblwQjVS11xUlFeUa8odLRsq0P7KH23/Bv61bj0zKY2KAAAAAElFTkSuQmCC'); 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":["/static/f9eb8b0c4a4f642d316973fc108cae86/a0b58/7_1_1.webp 230w","/static/f9eb8b0c4a4f642d316973fc108cae86/bc10c/7_1_1.webp 460w","/static/f9eb8b0c4a4f642d316973fc108cae86/966d8/7_1_1.webp 920w","/static/f9eb8b0c4a4f642d316973fc108cae86/445df/7_1_1.webp 1380w","/static/f9eb8b0c4a4f642d316973fc108cae86/78de1/7_1_1.webp 1840w","/static/f9eb8b0c4a4f642d316973fc108cae86/a02b9/7_1_1.webp 1852w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/f9eb8b0c4a4f642d316973fc108cae86/81c8e/7_1_1.png 230w","/static/f9eb8b0c4a4f642d316973fc108cae86/08a84/7_1_1.png 460w","/static/f9eb8b0c4a4f642d316973fc108cae86/c0255/7_1_1.png 920w","/static/f9eb8b0c4a4f642d316973fc108cae86/b1001/7_1_1.png 1380w","/static/f9eb8b0c4a4f642d316973fc108cae86/161ec/7_1_1.png 1840w","/static/f9eb8b0c4a4f642d316973fc108cae86/ec5f6/7_1_1.png 1852w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/f9eb8b0c4a4f642d316973fc108cae86/c0255/7_1_1.png","alt":"7 1 1","title":"7 1 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":"p","properties":{},"children":[{"type":"text","value":"When the execution halts at the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"breakpoint"}]},{"type":"text","value":" command, an interactive console window is opened. Here you can write any code just as you would in a normal Python console, and see how the code works at exactly that point in the program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"breakpoint"}]},{"type":"text","value":" command is especially useful when you know that some line of code causes an error, but you are not quite sure why that is. Add a breakpoint just before the problematic line of code and run your program. Now you can try out different options in the interactive console window, and figure out the correct commands to include in your program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is also possible to continue execution from where it halted. The command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":", or the shorthand "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"c"}]},{"type":"text","value":", typed into the debugging console will resume execution until the next breakpoint is reached. The following picture depicts a situation where the loop has already been executed a few times:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/0ec6d7f1917313cc4582ecd977846154/c7bb6/7_1_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: 44.78260869565218%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA/ElEQVQoz42R22rDMAyG8xi7iGMpPqSuHZ8SzErrOlfd5WDQ93+VkXjLOhij34XQjyX9Qm5Yz0ajvbdqQwhBCOmeo0HEcTQ5X1JKgNB1HX2Cn2ajjyFGY91BKQDo/nImhLRtSzaqXJsB4IW2HVCOPWMMEevgfXwFEesrAFBKa2y0MXeV3qSfD5r1TAixmn+z12mt53k2xiilOOdCCERscs63S5l8GIN3zllr/cY0TSGEYRiklMMG5/xRCiGavJSP07IM9ojbWr+p54EHdrmufTqf7+X2/lpKSLPz1rnqHGP03u9WSqk9VqSUTbmWmGZ+GGiPgFANAb6S///5E+mjTgE+v0lMAAAAAElFTkSuQmCC'); 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":["/static/0ec6d7f1917313cc4582ecd977846154/a0b58/7_1_2.webp 230w","/static/0ec6d7f1917313cc4582ecd977846154/bc10c/7_1_2.webp 460w","/static/0ec6d7f1917313cc4582ecd977846154/966d8/7_1_2.webp 920w","/static/0ec6d7f1917313cc4582ecd977846154/445df/7_1_2.webp 1380w","/static/0ec6d7f1917313cc4582ecd977846154/78de1/7_1_2.webp 1840w","/static/0ec6d7f1917313cc4582ecd977846154/ca269/7_1_2.webp 1986w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/0ec6d7f1917313cc4582ecd977846154/81c8e/7_1_2.png 230w","/static/0ec6d7f1917313cc4582ecd977846154/08a84/7_1_2.png 460w","/static/0ec6d7f1917313cc4582ecd977846154/c0255/7_1_2.png 920w","/static/0ec6d7f1917313cc4582ecd977846154/b1001/7_1_2.png 1380w","/static/0ec6d7f1917313cc4582ecd977846154/161ec/7_1_2.png 1840w","/static/0ec6d7f1917313cc4582ecd977846154/c7bb6/7_1_2.png 1986w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/0ec6d7f1917313cc4582ecd977846154/c0255/7_1_2.png","alt":"7 1 2","title":"7 1 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":"There are also some other commands available in the debugging console. You may find them "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/pdb.html#debugger-commands","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"here"}]},{"type":"text","value":", or else you can type in "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"help"}]},{"type":"text","value":" in the debugging console:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/19fd7ec3fff52d6018519fac3d868e78/ca98b/7_1_3.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: 42.608695652173914%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA5klEQVQoz4WPy07EMAxF+yF9pHnHTds4cVxpkNixYc3/fwuiURGgmeGsbEvH9u0WAAeg12XD5IIHAKnUdA9mJqJ1XUMIWmtrbXe83N7L7cMSCWuGqR+G8WI4+W7FSVvUJl2p9TXXt8S07nlPAUIT+r6fpmme50e7vuSM6BfYqRDzlvaUUjjx3m/bBgB/hF8yIjJzrRURY4zNPI4DEUspWutnMhGllBDRe99eHcexBZNSCiHGx3S11pxzjFEpNV807Wfg+zIz55yttVJKY4w+McYopZxz/1xupnMOAJxz/qLVQognmT8B8I9MXlB9in4AAAAASUVORK5CYII='); 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":["/static/19fd7ec3fff52d6018519fac3d868e78/a0b58/7_1_3.webp 230w","/static/19fd7ec3fff52d6018519fac3d868e78/bc10c/7_1_3.webp 460w","/static/19fd7ec3fff52d6018519fac3d868e78/966d8/7_1_3.webp 920w","/static/19fd7ec3fff52d6018519fac3d868e78/445df/7_1_3.webp 1380w","/static/19fd7ec3fff52d6018519fac3d868e78/78de1/7_1_3.webp 1840w","/static/19fd7ec3fff52d6018519fac3d868e78/28038/7_1_3.webp 1968w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/19fd7ec3fff52d6018519fac3d868e78/81c8e/7_1_3.png 230w","/static/19fd7ec3fff52d6018519fac3d868e78/08a84/7_1_3.png 460w","/static/19fd7ec3fff52d6018519fac3d868e78/c0255/7_1_3.png 920w","/static/19fd7ec3fff52d6018519fac3d868e78/b1001/7_1_3.png 1380w","/static/19fd7ec3fff52d6018519fac3d868e78/161ec/7_1_3.png 1840w","/static/19fd7ec3fff52d6018519fac3d868e78/ca98b/7_1_3.png 1968w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/19fd7ec3fff52d6018519fac3d868e78/c0255/7_1_3.png","alt":"7 1 3","title":"7 1 3","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 command "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"exit"}]},{"type":"text","value":" finishes the execution of the program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When you are done debugging, remember to remove the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"breakpoint"}]},{"type":"text","value":" commands from your code!"}]},{"type":"element","tagName":"h2","properties":{"id":"using-modules","style":"position:relative;"},"children":[{"type":"text","value":"Using modules"},{"type":"element","tagName":"a","properties":{"href":"#using-modules","ariaLabel":"using modules 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 Python language definition already contains some useful functions, such as the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" function which returns the length of a string or a list, or the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" function which returns the sum of items in a data structure, but they will only get a programmer so far. The Python "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"standard library"}]},{"type":"text","value":" is a collection of standardised functions and objects, which can be used to expand the expressive power of Python in many ways. We have already used some functions defined in the standard library in previous exercises, for example when calculating square roots."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The standard library is comprised of "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"modules"}]},{"type":"text","value":", which contain functions and classes grouped around different themes and functionalities. In this part of the course we will familiarize ourselves with some useful Python modules. We will also learn to write our own modules."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" makes the contents of the given module accessible in the current program. Let's have a closer look at working with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" module. It contains the definitions of some mathematical functions, such as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":" for square root and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"log"}]},{"type":"text","value":" for logarithm."}]},{"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":"import"}]},{"type":"text","value":" math\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# The square root of the number 5"}]},{"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":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sqrt"},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the base 2 logarithm of the number 8"}]},{"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":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"log"},{"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":" "},{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2.23606797749979\n3.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The functions are defined in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" module, so they must be referred to as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math.sqrt"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math.log"}]},{"type":"text","value":" in the program code."}]},{"type":"element","tagName":"h2","properties":{"id":"selecting-distinct-sections-from-a-module","style":"position:relative;"},"children":[{"type":"text","value":"Selecting distinct sections from a module"},{"type":"element","tagName":"a","properties":{"href":"#selecting-distinct-sections-from-a-module","ariaLabel":"selecting distinct sections from a module 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":"Another way to use modules is to select a distinct entity from the module with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" command. In case we want to use just the functions "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"log"}]},{"type":"text","value":" from the module "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":", we can do the following:"}]},{"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":"from"}]},{"type":"text","value":" math "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" log\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":"sqrt"},{"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","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":"log"},{"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":"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, we do not need the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" prefix when using the functions imported in this manner."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sometimes a handy shortcut is to import "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"all"}]},{"type":"text","value":" the contents of a module with the star notation:"}]},{"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":"from"}]},{"type":"text","value":" math "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"sqrt"},{"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","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":"log"},{"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":"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Importing modules with the star notation can be useful when testing and in some smaller projects, but it can pose some new problems, too. We will come across these later."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Hypotenuse","tmcname":"part07-01_hypotenuse"},"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":"hypotenuse(leg1: float, leg2: float)"}]},{"type":"text","value":", which takes the lengths of the two sides adjacent to the right angle of an orthogonal triangle. The function should return the length of the hypotenuse, or the side opposite to the right angle."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can use the "},{"type":"element","tagName":"a","properties":{"href":"https://en.wikipedia.org/wiki/Pythagorean_theorem","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Pythagorean theorem"}]},{"type":"text","value":" to calculate the result. You will need the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":" function from the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" module."}]},{"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":"text","value":"hypotenuse"},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 5.0"}]},{"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":"hypotenuse"},{"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":"12"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 13.0"}]},{"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":"hypotenuse"},{"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":"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 1.4142135623730951"}]}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-contents-of-a-module","style":"position:relative;"},"children":[{"type":"text","value":"The contents of a module"},{"type":"element","tagName":"a","properties":{"href":"#the-contents-of-a-module","ariaLabel":"the contents of a module 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 Python documentation has extensive resources on each module in the Python standard library. The documentation contains information on the functions and methods defined in the module, and how the module can be used. For example, here is the link to the documentation for the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" module:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/math.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://docs.python.org/3/library/math.html"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We can also have a look at the contents of the module with the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dir"}]},{"type":"text","value":":"}]},{"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":"import"}]},{"type":"text","value":" math\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":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"dir"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"math"},{"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":"The function returns a list of names defined by the module. These may be, for example, names of classes, constant values or functions:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Special characters","tmcname":"part07-02_special_characters"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The Python module "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/string.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"string"}]},{"type":"text","value":" contains some string constants, which define certain groups of characters. These include for example lowercase letters and punctuation characters. Please familiarize yourself with these constants, and then write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"separate_characters(my_string: str)"}]},{"type":"text","value":". The function takes a string as its argument, and it should separate the characters in the string into three other strings, and return these in a tuple:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The first string should contain the lowercase and uppercase ASCII letters (string constant "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ascii_letters"}]},{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The second string should contain all punctuation characters defined by the string constant "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"punctuation"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The third string should contain all the other characters (including whitespace)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The characters should appear in the three strings in the same order as they appeared in the original string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" separate_characters"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olé!!! Hey, are ümläüts wörking?\""}]},{"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":"parts"},{"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":"parts"},{"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":"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":"parts"},{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OlHeyaremltswrking\n!!!,?\né üäü ö"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Fractions","tmcname":"part07-03_fractions"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please familiarize yourself with the Python module "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fractions"}]},{"type":"text","value":". Use it to write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fractionate(amount: int)"}]},{"type":"text","value":", which takes the number of parts as its argument. The function should divide the number 1 into as many equal sized fractions as is specified by the argument, and return these in a list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"for"}]},{"type":"text","value":" p "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fractionate"},{"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","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":"p"},{"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":"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":"fractionate"},{"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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1/3\n1/3\n1/3"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[Fraction(1, 5), Fraction(1, 5), Fraction(1, 5), Fraction(1, 5), Fraction(1, 5)]"}]}]}]},{"type":"comment","value":"-\n\n"}]},"html":"

    After this section

      \n
    • You will know what a Python module is
    • \n
    • You will be able to include a module in your program with the import statement
    • \n
    • You will know how to look for more information on the contents of modules
    • \n

    Debugging revisited

    We have already come across quite a few debugging methods on this course. The visualisation tool should now be familiar to you, and debugging print outs are a good old staple by now. You have possibly also tried the Visual Studio Code built-in debugger. If you come across problems with the debugger not finding your files, there were some tips for handling these situations in the previous part.

    In Python version 3.7 brought yet another easy and useful tool for debugging programs: the breakpoint() command.

    You can add this command to any point in your code (within normal syntactic rules, of course). When the program is run, the execution halts at the point where you inserted the breakpoint command. Here is an example of debugging efforts when completing one of the exercises from the previous part (please ignore the Finnish variable names in the images, and concentrate on the functionality):

    \n \n \n \n \n \n \"7\n \n \n

    When the execution halts at the breakpoint command, an interactive console window is opened. Here you can write any code just as you would in a normal Python console, and see how the code works at exactly that point in the program.

    The breakpoint command is especially useful when you know that some line of code causes an error, but you are not quite sure why that is. Add a breakpoint just before the problematic line of code and run your program. Now you can try out different options in the interactive console window, and figure out the correct commands to include in your program.

    It is also possible to continue execution from where it halted. The command continue, or the shorthand c, typed into the debugging console will resume execution until the next breakpoint is reached. The following picture depicts a situation where the loop has already been executed a few times:

    \n \n \n \n \n \n \"7\n \n \n

    There are also some other commands available in the debugging console. You may find them here, or else you can type in help in the debugging console:

    \n \n math module. It contains the definitions of some mathematical functions, such as sqrt for square root and log for logarithm.

    import math\n\n# The square root of the number 5\nprint(math.sqrt(5))\n# the base 2 logarithm of the number 8\nprint(math.log(8, 2))

    2.23606797749979\n3.0

    The functions are defined in the math module, so they must be referred to as math.sqrt and math.log in the program code.

    Selecting distinct sections from a module

    Another way to use modules is to select a distinct entity from the module with the from command. In case we want to use just the functions sqrt and log from the module math, we can do the following:

    from math import sqrt, log\n\nprint(sqrt(5))\nprint(log(5,2))

    As you can see above, we do not need the math prefix when using the functions imported in this manner.

    Sometimes a handy shortcut is to import all the contents of a module with the star notation:

    from math import *\n\nprint(sqrt(5))\nprint(log(5,2))

    Importing modules with the star notation can be useful when testing and in some smaller projects, but it can pose some new problems, too. We will come across these later.

    Please write a function named hypotenuse(leg1: float, leg2: float), which takes the lengths of the two sides adjacent to the right angle of an orthogonal triangle. The function should return the length of the hypotenuse, or the side opposite to the right angle.

    You can use the Pythagorean theorem to calculate the result. You will need the sqrt function from the math module.

    Some examples:

    print(hypotenuse(3,4)) # 5.0\nprint(hypotenuse(5,12)) # 13.0\nprint(hypotenuse(1,1)) # 1.4142135623730951

    The contents of a module

    The Python documentation has extensive resources on each module in the Python standard library. The documentation contains information on the functions and methods defined in the module, and how the module can be used. For example, here is the link to the documentation for the math module:

    We can also have a look at the contents of the module with the function dir:

    import math\n\nprint(dir(math))

    The function returns a list of names defined by the module. These may be, for example, names of classes, constant values or functions:

    ['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

    The Python module string contains some string constants, which define certain groups of characters. These include for example lowercase letters and punctuation characters. Please familiarize yourself with these constants, and then write a function named separate_characters(my_string: str). The function takes a string as its argument, and it should separate the characters in the string into three other strings, and return these in a tuple:

      \n
    • The first string should contain the lowercase and uppercase ASCII letters (string constant ascii_letters)
    • \n
    • The second string should contain all punctuation characters defined by the string constant punctuation
    • \n
    • The third string should contain all the other characters (including whitespace)
    • \n

    The characters should appear in the three strings in the same order as they appeared in the original string.

    An example of the function in action:

    parts = separate_characters(\"Olé!!! Hey, are ümläüts wörking?\")\nprint(parts[0])\nprint(parts[1])\nprint(parts[2])

    OlHeyaremltswrking\n!!!,?\né üäü ö

    Please familiarize yourself with the Python module fractions. Use it to write a function named fractionate(amount: int), which takes the number of parts as its argument. The function should divide the number 1 into as many equal sized fractions as is specified by the argument, and return these in a list.

    An example of the function in action:

    for p in fractionate(3):\n    print(p)\n\nprint()\n\nprint(fractionate(5))

    1/3\n1/3\n1/3

    [Fraction(1, 5), Fraction(1, 5), Fraction(1, 5), Fraction(1, 5), Fraction(1, 5)]

    ","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-7/1-modules.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know what a Python module is"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to include a module in your program with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to look for more information on the contents of modules"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"debugging-revisited","style":"position:relative;"},"children":[{"type":"text","value":"Debugging revisited"},{"type":"element","tagName":"a","properties":{"href":"#debugging-revisited","ariaLabel":"debugging revisited 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 have already come across quite a few debugging methods on this course. The "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":" should now be familiar to you, and "},{"type":"element","tagName":"a","properties":{"href":"/part-2/1-programming-terminology#debugging"},"children":[{"type":"text","value":"debugging print outs"}]},{"type":"text","value":" are a good old staple by now. You have possibly also tried the Visual Studio Code "},{"type":"element","tagName":"a","properties":{"href":"/part-4/1-vscode#the-built-in-debugger"},"children":[{"type":"text","value":"built-in debugger"}]},{"type":"text","value":". If you come across problems with the debugger not finding your files, there were some tips for handling these situations in the "},{"type":"element","tagName":"a","properties":{"href":"/part-6/1-reading-files#debugging-code-which-handles-files"},"children":[{"type":"text","value":"previous part"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In Python version 3.7 brought yet another easy and useful tool for debugging programs: the "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/functions.html?highlight=breakpoint#breakpoint","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"breakpoint()"}]},{"type":"text","value":" command."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can add this command to any point in your code (within normal syntactic rules, of course). When the program is run, the execution halts at the point where you inserted the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"breakpoint"}]},{"type":"text","value":" command. Here is an example of debugging efforts when completing one of the exercises from the previous part (please ignore the Finnish variable names in the images, and concentrate on the functionality):"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/f9eb8b0c4a4f642d316973fc108cae86/ec5f6/7_1_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: 62.173913043478265%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAIAAADtbgqsAAAACXBIWXMAABYlAAAWJQFJUiTwAAABeklEQVQoz43R247bIBQFUH/HhKu5GIMNPr40mQwYT+yM2qn6/79TxcnDqGqrLB0J8bBB2qdofQCAHvq+g7ZxlbUhdHYnhKCUsr/i3GheSCEQQozxvoMGAgx9bQzGmBCC/w0hpJUulFJaKoAAXees894zxu/h/0AIhbYppJQl56fTcd221geEEHkCRqi27hZWSo7TeDyefAhSSkopeU7BOWeM8pKzsiyFkEIS8nTY1rWzzlSmCyF4b4xxe88PZSkfZ+mcizEOwwAA1lpjTDEdv71fLjGllPNbiinPaZ7nJcc53a9vKQ7jOE3TsJt23vu2bYvleP55uX5eth/LusV5PadrzNeUP+blY16uKZ9hHKEHgKG//dnvvPdN0xS/XvMacyurinCFmcJ0H6bJbRRhNb7NCzqgL+7bLj7TkrfVQ6drw6TgSspKk5IfMDoQ/EIwJcRRgSmhXzwK+96/jr4Luu6MrblwQjVS11xUlFeUa8odLRsq0P7KH23/Bv61bj0zKY2KAAAAAElFTkSuQmCC'); 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":["/static/f9eb8b0c4a4f642d316973fc108cae86/a0b58/7_1_1.webp 230w","/static/f9eb8b0c4a4f642d316973fc108cae86/bc10c/7_1_1.webp 460w","/static/f9eb8b0c4a4f642d316973fc108cae86/966d8/7_1_1.webp 920w","/static/f9eb8b0c4a4f642d316973fc108cae86/445df/7_1_1.webp 1380w","/static/f9eb8b0c4a4f642d316973fc108cae86/78de1/7_1_1.webp 1840w","/static/f9eb8b0c4a4f642d316973fc108cae86/a02b9/7_1_1.webp 1852w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/f9eb8b0c4a4f642d316973fc108cae86/81c8e/7_1_1.png 230w","/static/f9eb8b0c4a4f642d316973fc108cae86/08a84/7_1_1.png 460w","/static/f9eb8b0c4a4f642d316973fc108cae86/c0255/7_1_1.png 920w","/static/f9eb8b0c4a4f642d316973fc108cae86/b1001/7_1_1.png 1380w","/static/f9eb8b0c4a4f642d316973fc108cae86/161ec/7_1_1.png 1840w","/static/f9eb8b0c4a4f642d316973fc108cae86/ec5f6/7_1_1.png 1852w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/f9eb8b0c4a4f642d316973fc108cae86/c0255/7_1_1.png","alt":"7 1 1","title":"7 1 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":"p","properties":{},"children":[{"type":"text","value":"When the execution halts at the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"breakpoint"}]},{"type":"text","value":" command, an interactive console window is opened. Here you can write any code just as you would in a normal Python console, and see how the code works at exactly that point in the program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"breakpoint"}]},{"type":"text","value":" command is especially useful when you know that some line of code causes an error, but you are not quite sure why that is. Add a breakpoint just before the problematic line of code and run your program. Now you can try out different options in the interactive console window, and figure out the correct commands to include in your program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is also possible to continue execution from where it halted. The command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"continue"}]},{"type":"text","value":", or the shorthand "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"c"}]},{"type":"text","value":", typed into the debugging console will resume execution until the next breakpoint is reached. The following picture depicts a situation where the loop has already been executed a few times:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/0ec6d7f1917313cc4582ecd977846154/c7bb6/7_1_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: 44.78260869565218%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA/ElEQVQoz42R22rDMAyG8xi7iGMpPqSuHZ8SzErrOlfd5WDQ93+VkXjLOhij34XQjyX9Qm5Yz0ajvbdqQwhBCOmeo0HEcTQ5X1JKgNB1HX2Cn2ajjyFGY91BKQDo/nImhLRtSzaqXJsB4IW2HVCOPWMMEevgfXwFEesrAFBKa2y0MXeV3qSfD5r1TAixmn+z12mt53k2xiilOOdCCERscs63S5l8GIN3zllr/cY0TSGEYRiklMMG5/xRCiGavJSP07IM9ojbWr+p54EHdrmufTqf7+X2/lpKSLPz1rnqHGP03u9WSqk9VqSUTbmWmGZ+GGiPgFANAb6S///5E+mjTgE+v0lMAAAAAElFTkSuQmCC'); 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":["/static/0ec6d7f1917313cc4582ecd977846154/a0b58/7_1_2.webp 230w","/static/0ec6d7f1917313cc4582ecd977846154/bc10c/7_1_2.webp 460w","/static/0ec6d7f1917313cc4582ecd977846154/966d8/7_1_2.webp 920w","/static/0ec6d7f1917313cc4582ecd977846154/445df/7_1_2.webp 1380w","/static/0ec6d7f1917313cc4582ecd977846154/78de1/7_1_2.webp 1840w","/static/0ec6d7f1917313cc4582ecd977846154/ca269/7_1_2.webp 1986w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/0ec6d7f1917313cc4582ecd977846154/81c8e/7_1_2.png 230w","/static/0ec6d7f1917313cc4582ecd977846154/08a84/7_1_2.png 460w","/static/0ec6d7f1917313cc4582ecd977846154/c0255/7_1_2.png 920w","/static/0ec6d7f1917313cc4582ecd977846154/b1001/7_1_2.png 1380w","/static/0ec6d7f1917313cc4582ecd977846154/161ec/7_1_2.png 1840w","/static/0ec6d7f1917313cc4582ecd977846154/c7bb6/7_1_2.png 1986w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/0ec6d7f1917313cc4582ecd977846154/c0255/7_1_2.png","alt":"7 1 2","title":"7 1 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":"There are also some other commands available in the debugging console. You may find them "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/pdb.html#debugger-commands","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"here"}]},{"type":"text","value":", or else you can type in "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"help"}]},{"type":"text","value":" in the debugging console:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/19fd7ec3fff52d6018519fac3d868e78/ca98b/7_1_3.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: 42.608695652173914%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAIAAAC9o5sfAAAACXBIWXMAABYlAAAWJQFJUiTwAAAA5klEQVQoz4WPy07EMAxF+yF9pHnHTds4cVxpkNixYc3/fwuiURGgmeGsbEvH9u0WAAeg12XD5IIHAKnUdA9mJqJ1XUMIWmtrbXe83N7L7cMSCWuGqR+G8WI4+W7FSVvUJl2p9TXXt8S07nlPAUIT+r6fpmme50e7vuSM6BfYqRDzlvaUUjjx3m/bBgB/hF8yIjJzrRURY4zNPI4DEUspWutnMhGllBDRe99eHcexBZNSCiHGx3S11pxzjFEpNV807Wfg+zIz55yttVJKY4w+McYopZxz/1xupnMOAJxz/qLVQognmT8B8I9MXlB9in4AAAAASUVORK5CYII='); 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":["/static/19fd7ec3fff52d6018519fac3d868e78/a0b58/7_1_3.webp 230w","/static/19fd7ec3fff52d6018519fac3d868e78/bc10c/7_1_3.webp 460w","/static/19fd7ec3fff52d6018519fac3d868e78/966d8/7_1_3.webp 920w","/static/19fd7ec3fff52d6018519fac3d868e78/445df/7_1_3.webp 1380w","/static/19fd7ec3fff52d6018519fac3d868e78/78de1/7_1_3.webp 1840w","/static/19fd7ec3fff52d6018519fac3d868e78/28038/7_1_3.webp 1968w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/19fd7ec3fff52d6018519fac3d868e78/81c8e/7_1_3.png 230w","/static/19fd7ec3fff52d6018519fac3d868e78/08a84/7_1_3.png 460w","/static/19fd7ec3fff52d6018519fac3d868e78/c0255/7_1_3.png 920w","/static/19fd7ec3fff52d6018519fac3d868e78/b1001/7_1_3.png 1380w","/static/19fd7ec3fff52d6018519fac3d868e78/161ec/7_1_3.png 1840w","/static/19fd7ec3fff52d6018519fac3d868e78/ca98b/7_1_3.png 1968w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/19fd7ec3fff52d6018519fac3d868e78/c0255/7_1_3.png","alt":"7 1 3","title":"7 1 3","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 command "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"exit"}]},{"type":"text","value":" finishes the execution of the program."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When you are done debugging, remember to remove the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"breakpoint"}]},{"type":"text","value":" commands from your code!"}]},{"type":"element","tagName":"h2","properties":{"id":"using-modules","style":"position:relative;"},"children":[{"type":"text","value":"Using modules"},{"type":"element","tagName":"a","properties":{"href":"#using-modules","ariaLabel":"using modules 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 Python language definition already contains some useful functions, such as the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"len"}]},{"type":"text","value":" function which returns the length of a string or a list, or the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sum"}]},{"type":"text","value":" function which returns the sum of items in a data structure, but they will only get a programmer so far. The Python "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"standard library"}]},{"type":"text","value":" is a collection of standardised functions and objects, which can be used to expand the expressive power of Python in many ways. We have already used some functions defined in the standard library in previous exercises, for example when calculating square roots."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The standard library is comprised of "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"modules"}]},{"type":"text","value":", which contain functions and classes grouped around different themes and functionalities. In this part of the course we will familiarize ourselves with some useful Python modules. We will also learn to write our own modules."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" makes the contents of the given module accessible in the current program. Let's have a closer look at working with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" module. It contains the definitions of some mathematical functions, such as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":" for square root and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"log"}]},{"type":"text","value":" for logarithm."}]},{"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":"import"}]},{"type":"text","value":" math\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# The square root of the number 5"}]},{"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":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sqrt"},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the base 2 logarithm of the number 8"}]},{"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":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"log"},{"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":" "},{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2.23606797749979\n3.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The functions are defined in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" module, so they must be referred to as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math.sqrt"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math.log"}]},{"type":"text","value":" in the program code."}]},{"type":"element","tagName":"h2","properties":{"id":"selecting-distinct-sections-from-a-module","style":"position:relative;"},"children":[{"type":"text","value":"Selecting distinct sections from a module"},{"type":"element","tagName":"a","properties":{"href":"#selecting-distinct-sections-from-a-module","ariaLabel":"selecting distinct sections from a module 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":"Another way to use modules is to select a distinct entity from the module with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" command. In case we want to use just the functions "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"log"}]},{"type":"text","value":" from the module "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":", we can do the following:"}]},{"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":"from"}]},{"type":"text","value":" math "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" log\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":"sqrt"},{"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","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":"log"},{"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":"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, we do not need the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" prefix when using the functions imported in this manner."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Sometimes a handy shortcut is to import "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"all"}]},{"type":"text","value":" the contents of a module with the star notation:"}]},{"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":"from"}]},{"type":"text","value":" math "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"sqrt"},{"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","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":"log"},{"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":"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Importing modules with the star notation can be useful when testing and in some smaller projects, but it can pose some new problems, too. We will come across these later."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Hypotenuse","tmcname":"part07-01_hypotenuse"},"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":"hypotenuse(leg1: float, leg2: float)"}]},{"type":"text","value":", which takes the lengths of the two sides adjacent to the right angle of an orthogonal triangle. The function should return the length of the hypotenuse, or the side opposite to the right angle."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can use the "},{"type":"element","tagName":"a","properties":{"href":"https://en.wikipedia.org/wiki/Pythagorean_theorem","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Pythagorean theorem"}]},{"type":"text","value":" to calculate the result. You will need the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"sqrt"}]},{"type":"text","value":" function from the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" module."}]},{"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":"text","value":"hypotenuse"},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 5.0"}]},{"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":"hypotenuse"},{"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":"12"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 13.0"}]},{"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":"hypotenuse"},{"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":"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 1.4142135623730951"}]}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-contents-of-a-module","style":"position:relative;"},"children":[{"type":"text","value":"The contents of a module"},{"type":"element","tagName":"a","properties":{"href":"#the-contents-of-a-module","ariaLabel":"the contents of a module 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 Python documentation has extensive resources on each module in the Python standard library. The documentation contains information on the functions and methods defined in the module, and how the module can be used. For example, here is the link to the documentation for the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" module:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/math.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://docs.python.org/3/library/math.html"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We can also have a look at the contents of the module with the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dir"}]},{"type":"text","value":":"}]},{"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":"import"}]},{"type":"text","value":" math\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":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"dir"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"math"},{"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":"The function returns a list of names defined by the module. These may be, for example, names of classes, constant values or functions:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Special characters","tmcname":"part07-02_special_characters"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The Python module "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/string.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"string"}]},{"type":"text","value":" contains some string constants, which define certain groups of characters. These include for example lowercase letters and punctuation characters. Please familiarize yourself with these constants, and then write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"separate_characters(my_string: str)"}]},{"type":"text","value":". The function takes a string as its argument, and it should separate the characters in the string into three other strings, and return these in a tuple:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The first string should contain the lowercase and uppercase ASCII letters (string constant "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ascii_letters"}]},{"type":"text","value":")"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The second string should contain all punctuation characters defined by the string constant "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"punctuation"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The third string should contain all the other characters (including whitespace)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The characters should appear in the three strings in the same order as they appeared in the original string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"parts "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" separate_characters"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Olé!!! Hey, are ümläüts wörking?\""}]},{"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":"parts"},{"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":"parts"},{"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":"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":"parts"},{"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"OlHeyaremltswrking\n!!!,?\né üäü ö"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Fractions","tmcname":"part07-03_fractions"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please familiarize yourself with the Python module "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fractions"}]},{"type":"text","value":". Use it to write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fractionate(amount: int)"}]},{"type":"text","value":", which takes the number of parts as its argument. The function should divide the number 1 into as many equal sized fractions as is specified by the argument, and return these in a list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"for"}]},{"type":"text","value":" p "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" fractionate"},{"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","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":"p"},{"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":"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":"fractionate"},{"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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1/3\n1/3\n1/3"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[Fraction(1, 5), Fraction(1, 5), Fraction(1, 5), Fraction(1, 5), Fraction(1, 5)]"}]}]}]},{"type":"comment","value":"-\n\n"}]},"html":"

    After this section

      \n
    • You will know what a Python module is
    • \n
    • You will be able to include a module in your program with the import statement
    • \n
    • You will know how to look for more information on the contents of modules
    • \n

    Debugging revisited

    We have already come across quite a few debugging methods on this course. The visualisation tool should now be familiar to you, and debugging print outs are a good old staple by now. You have possibly also tried the Visual Studio Code built-in debugger. If you come across problems with the debugger not finding your files, there were some tips for handling these situations in the previous part.

    In Python version 3.7 brought yet another easy and useful tool for debugging programs: the breakpoint() command.

    You can add this command to any point in your code (within normal syntactic rules, of course). When the program is run, the execution halts at the point where you inserted the breakpoint command. Here is an example of debugging efforts when completing one of the exercises from the previous part (please ignore the Finnish variable names in the images, and concentrate on the functionality):

    \n \n \n \n \n \n \"7\n \n \n

    When the execution halts at the breakpoint command, an interactive console window is opened. Here you can write any code just as you would in a normal Python console, and see how the code works at exactly that point in the program.

    The breakpoint command is especially useful when you know that some line of code causes an error, but you are not quite sure why that is. Add a breakpoint just before the problematic line of code and run your program. Now you can try out different options in the interactive console window, and figure out the correct commands to include in your program.

    It is also possible to continue execution from where it halted. The command continue, or the shorthand c, typed into the debugging console will resume execution until the next breakpoint is reached. The following picture depicts a situation where the loop has already been executed a few times:

    \n \n \n \n \n \n \"7\n \n \n

    There are also some other commands available in the debugging console. You may find them here, or else you can type in help in the debugging console:

    \n \n math module. It contains the definitions of some mathematical functions, such as sqrt for square root and log for logarithm.

    import math\n\n# The square root of the number 5\nprint(math.sqrt(5))\n# the base 2 logarithm of the number 8\nprint(math.log(8, 2))

    2.23606797749979\n3.0

    The functions are defined in the math module, so they must be referred to as math.sqrt and math.log in the program code.

    Selecting distinct sections from a module

    Another way to use modules is to select a distinct entity from the module with the from command. In case we want to use just the functions sqrt and log from the module math, we can do the following:

    from math import sqrt, log\n\nprint(sqrt(5))\nprint(log(5,2))

    As you can see above, we do not need the math prefix when using the functions imported in this manner.

    Sometimes a handy shortcut is to import all the contents of a module with the star notation:

    from math import *\n\nprint(sqrt(5))\nprint(log(5,2))

    Importing modules with the star notation can be useful when testing and in some smaller projects, but it can pose some new problems, too. We will come across these later.

    Please write a function named hypotenuse(leg1: float, leg2: float), which takes the lengths of the two sides adjacent to the right angle of an orthogonal triangle. The function should return the length of the hypotenuse, or the side opposite to the right angle.

    You can use the Pythagorean theorem to calculate the result. You will need the sqrt function from the math module.

    Some examples:

    print(hypotenuse(3,4)) # 5.0\nprint(hypotenuse(5,12)) # 13.0\nprint(hypotenuse(1,1)) # 1.4142135623730951

    The contents of a module

    The Python documentation has extensive resources on each module in the Python standard library. The documentation contains information on the functions and methods defined in the module, and how the module can be used. For example, here is the link to the documentation for the math module:

    We can also have a look at the contents of the module with the function dir:

    import math\n\nprint(dir(math))

    The function returns a list of names defined by the module. These may be, for example, names of classes, constant values or functions:

    ['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

    The Python module string contains some string constants, which define certain groups of characters. These include for example lowercase letters and punctuation characters. Please familiarize yourself with these constants, and then write a function named separate_characters(my_string: str). The function takes a string as its argument, and it should separate the characters in the string into three other strings, and return these in a tuple:

      \n
    • The first string should contain the lowercase and uppercase ASCII letters (string constant ascii_letters)
    • \n
    • The second string should contain all punctuation characters defined by the string constant punctuation
    • \n
    • The third string should contain all the other characters (including whitespace)
    • \n

    The characters should appear in the three strings in the same order as they appeared in the original string.

    An example of the function in action:

    parts = separate_characters(\"Olé!!! Hey, are ümläüts wörking?\")\nprint(parts[0])\nprint(parts[1])\nprint(parts[2])

    OlHeyaremltswrking\n!!!,?\né üäü ö

    Please familiarize yourself with the Python module fractions. Use it to write a function named fractionate(amount: int), which takes the number of parts as its argument. The function should divide the number 1 into as many equal sized fractions as is specified by the argument, and return these in a list.

    An example of the function in action:

    for p in fractionate(3):\n    print(p)\n\nprint()\n\nprint(fractionate(5))

    1/3\n1/3\n1/3

    [Fraction(1, 5), Fraction(1, 5), Fraction(1, 5), Fraction(1, 5), Fraction(1, 5)]

    ","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-7/1-modules.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-7/2-randomness/page-data.json b/page-data/part-7/2-randomness/page-data.json index d5f5e5c00..4f3d18125 100644 --- a/page-data/part-7/2-randomness/page-data.json +++ b/page-data/part-7/2-randomness/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-7/2-randomness", - "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 some of the functions in the module "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"random"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to make use of random numbers in your programs"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This section concentrates on the module "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html?highlight=random#module-random","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"random"}]},{"type":"text","value":" from the Python standard library. It contains tools for generating random numbers and other randomized functionality."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The sections in this part of the material contain many links to the "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"documentation"}]},{"type":"text","value":" of the Python standard library. We recommend following the links to familiarize yourself with how the documentation works."}]},{"type":"element","tagName":"h2","properties":{"id":"generating-a-random-number","style":"position:relative;"},"children":[{"type":"text","value":"Generating a random number"},{"type":"element","tagName":"a","properties":{"href":"#generating-a-random-number","ariaLabel":"generating a random number 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 function "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html?highlight=random#random.randint","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"randint(a, b)"}]},{"type":"text","value":" returns a random integer value between "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":", inclusive. For example, the following program works like a generic die:"}]},{"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":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint\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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The result of the throw:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" randint"},{"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":"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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Executing this could print out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The result of the throw: 4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program throws the die ten times:"}]},{"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":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint\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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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 result of the throw:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" randint"},{"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":"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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Running the above could print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The result of the throw: 5\nThe result of the throw: 4\nThe result of the throw: 3\nThe result of the throw: 2\nThe result of the throw: 3\nThe result of the throw: 4\nThe result of the throw: 6\nThe result of the throw: 4\nThe result of the throw: 4\nThe result of the throw: 3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: it is worth remembering that the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"randint"}]},{"type":"text","value":" works a bit differently when compared to, for example, slices, or the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":", which we've come across previously. The function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"randint(1, 6)"}]},{"type":"text","value":" results in a number between 1 and 6 inclusive, but the function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range(1, 6)"}]},{"type":"text","value":" results in a range of numbers from 1 to 5."}]},{"type":"element","tagName":"h2","properties":{"id":"more-randomizing-functions","style":"position:relative;"},"children":[{"type":"text","value":"More randomizing functions"},{"type":"element","tagName":"a","properties":{"href":"#more-randomizing-functions","ariaLabel":"more randomizing 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":"The function "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html?highlight=random#random.shuffle","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"shuffle"}]},{"type":"text","value":" will shuffle any data structure passed as an argument, in place. For example, the following program shuffles a list of words:"}]},{"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":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" shuffle\n\nwords "},{"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":"\"atlas\""}]},{"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":"\"banana\""}]},{"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":"\"carrot\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nshuffle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"words"},{"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":"words"},{"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":"['banana', 'atlas', 'carrot']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"choice"}]},{"type":"text","value":" returns a randomly picked item from a data structure:"}]},{"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":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" choice\n\nwords "},{"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":"\"atlas\""}]},{"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":"\"banana\""}]},{"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":"\"carrot\""}]},{"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":"choice"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"words"},{"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":"'carrot'"}]}]},{"type":"element","tagName":"h2","properties":{"id":"lottery-numbers","style":"position:relative;"},"children":[{"type":"text","value":"Lottery numbers"},{"type":"element","tagName":"a","properties":{"href":"#lottery-numbers","ariaLabel":"lottery numbers 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":"A common example for studying randomness is the case of lottery numbers. Let's try and draw some lottery numbers. In Finland the national lottery consists of a pool of 40 numbers, 7 of which are chosen for each week's draw."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A first attempt at drawing a set of numbers 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":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint\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":"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":"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":"randint"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"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":"This would not work in the long run, however, as the same number may appear twice in a single weekly draw of seven numbers. We need a way to make sure the numbers drawn are all unique."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"One possibility is to store the drawn numbers in a list, and only add a number if it is not already on the list. This can be repeated until the length of the list is seven:"}]},{"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":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint\n\nweekly_draw "},{"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","keyword"]},"children":[{"type":"text","value":"while"}]},{"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":"weekly_draw"},{"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":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n new_rnd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" randint"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"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":"if"}]},{"type":"text","value":" new_rnd "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" weekly_draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n weekly_draw"},{"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":"new_rnd"},{"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":"weekly_draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A more compact approach would be to use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"shuffle"}]},{"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","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" shuffle\n\nnumber_pool "},{"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":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"41"}]},{"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":"\nshuffle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"number_pool"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nweekly_draw "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" number_pool"},{"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":"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":"weekly_draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here the idea is that we first create a list containing the available numbers 1 to 40, rather like the balls in a lottery machine. The pool of numbers is then shuffled, and the first seven numbers chosen for the weekly draw. This saves us the trouble of writing a loop."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In fact, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"random"}]},{"type":"text","value":" module contains an even easier way to select lottery numbers: the "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html?highlight=random#random.sample","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"sample"}]},{"type":"text","value":" function. It returns a random selection of a specified size from a given data structure:"}]},{"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":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" sample\n\nnumber_pool "},{"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":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"41"}]},{"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":"\nweekly_draw "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sample"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"number_pool"},{"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":"7"}]},{"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":"weekly_draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lottery numbers","tmcname":"part07-04_lottery_numbers"},"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":"lottery_numbers(amount: int, lower: int, upper: int)"}]},{"type":"text","value":", which generates as many random numbers as specified by the first argument. All numbers should fall within the bounds "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lower"}]},{"type":"text","value":" to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"upper"}]},{"type":"text","value":". The numbers should be stored in a list and returned. The numbers should be in ascending order in the returned list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As these are lottery numbers, no number should appear twice in the list."}]},{"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":"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":" lottery_numbers"},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"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":"number"},{"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\n7\n11\n16\n22\n29\n38"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"where-do-these-random-numbers-come-from","style":"position:relative;"},"children":[{"type":"text","value":"Where do these random numbers come from?"},{"type":"element","tagName":"a","properties":{"href":"#where-do-these-random-numbers-come-from","ariaLabel":"where do these random numbers come from 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 features of the module "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"random"}]},{"type":"text","value":" are based on an algorithm which produces random numbers based on a specific initialization value and some arithmetic operations. The initialization value is often called a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"seed value"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The seed value can be supplied by the user with the "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html?highlight=random#random.seed","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"seed"}]},{"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","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" seed\n\nseed"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1337"}]},{"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":"# this will always produce the same \"random\" number"}]},{"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":"randint"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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":"If we have functions which rely on randomization, and we set seed value, the function will produce the same result each time it is executed. The result may be different with different Python versions, but in essence randomness is lost by setting a seed value. This can be a useful feature when testing a program, for example."}]},{"type":"element","tagName":"text-box","properties":{"variant":"info","name":"True randomness"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To be specific, the numbers provided by the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"random"}]},{"type":"text","value":" module are not truly random. Instead, they are "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"pseudorandom"}]},{"type":"text","value":". Computers are, in essence, deterministic machines. In an ideal situation, it should be possible to foretell the way they function down to the last bit. Therefore it is very difficult to create truly random numbers with a computer. For many applications, however, pseudorandom numbers are good enough. When true random numbers are required, the seed value is usually generated by some source outside the computer, for example background radiation, noise levels, or "},{"type":"element","tagName":"a","properties":{"href":"https://blog.cloudflare.com/randomness-101-lavarand-in-production/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"lava lamps"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"For more information about randomness, please see "},{"type":"element","tagName":"a","properties":{"href":"https://www.random.org/randomness/"},"children":[{"type":"text","value":"random.org"}]},{"type":"text","value":"."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Password generator, part 1","tmcname":"part07-05_password_generator_part_1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a function which creates passwords of a desired length, consisting of lowercase characters a to z."}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"generate_password"},{"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":"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":"lttehepy\nolsxttjl\ncbjncrzo\ndwxqjdgu\ngpfdcecs\njabyvgar\nxnbbonbl\nktmsjyww\nejhprmel\nrjkoacib"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Password generator, part 2","tmcname":"part07-06_password_generator_part_2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write an improved version of your password generator. The function now takes three arguments:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If the second argument is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":", the generated password should also contain one or more numbers."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If the third argument is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":", the generated password should also contain one or more of these special characters: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!?=+-()#"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Despite these two additional arguments, the password should always contain at least one lowercase alphabet. You may assume the function will only be called with combinations of arguments that are possible to formulate into passwords following these rules. That is, the arguments will not specify e.g. a password of length 2 which contains both a number and a special characters, for then there would not be space for the mandatory lowercase letter."}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"generate_strong_password"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"2?0n+u31\nu=m4nl94\nn#=i6r#(\nda9?zvm?\n7h)!)g?!\na=59x2n5\n(jr6n3b5\n9n(4i+2!\n32+qba#=\nn?b0a7ey"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Dice roller","tmcname":"part07-07_dice_roller"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will write some functions which can be used in games that involve dice."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Instead of normal dice this exercise specifies "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"non-transitive dice"}]},{"type":"text","value":". You can read up on these "},{"type":"element","tagName":"a","properties":{"href":"https://singingbanana.com/dice/article.htm","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"here"}]},{"type":"text","value":" or "},{"type":"element","tagName":"a","properties":{"href":"https://www.youtube.com/watch?v=LrIp6CKUlH8","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"watch this video"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You will use three dice:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Die A has the sides 3, 3, 3, 3, 3, 6"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Die B has the sides 2, 2, 2, 5, 5, 5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Die C has the sides 1, 4, 4, 4, 4, 4"}]},{"type":"text","value":"\n"}]},{"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":"roll(die: str)"}]},{"type":"text","value":", which rolls the die specified by the argument. An example of how this 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":"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"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":"roll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"A\""}]},{"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":" "},{"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":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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"},{"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"},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"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":"roll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"B\""}]},{"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":" "},{"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":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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"},{"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"},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"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":"roll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"C\""}]},{"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":" "},{"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":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 6 3 6 3\n2 2 5 2 2 5 5 2 2 5 2 5 5 5 2 5 2 2 2 2\n4 4 4 4 4 1 1 4 4 4 1 4 4 4 4 4 4 4 4 4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Also write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"play(die1: str, die2: str, times: int)"}]},{"type":"text","value":", which throws both dice as many times as specified by the third argument. The function should return a tuple. The first item should be the number of times die 1 won, the second the number of times die 2 won, and the third item should be the number of ties."}]},{"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":"result "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" play"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"A\""}]},{"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":"\"C\""}]},{"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":"1000"}]},{"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":"text","value":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" play"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"B\""}]},{"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":"\"B\""}]},{"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":"1000"}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(292, 708, 0)\n(249, 273, 478)"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Random words","tmcname":"part07-08_random_words"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"words.txt"}]},{"type":"text","value":", which contains some English language words, one on each line."}]},{"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":"words(n: int, beginning: str)"}]},{"type":"text","value":", which returns a list containing "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" random words from the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"words.txt"}]},{"type":"text","value":" file. All words should begin with the string specified by the second argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The same word should not appear twice in the list. If there are not enough words beginning with the specified string, the function should raise a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" exception."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"word_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" words"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ca\""}]},{"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":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" word_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":"word"},{"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":"cat\ncar\ncarbon"}]}]}]},{"type":"comment","value":"-\n\n"}]},"html":"

    After this section

      \n
    • You will be familiar with some of the functions in the module random
    • \n
    • You will be able to make use of random numbers in your programs
    • \n

    This section concentrates on the module random from the Python standard library. It contains tools for generating random numbers and other randomized functionality.

    The sections in this part of the material contain many links to the documentation of the Python standard library. We recommend following the links to familiarize yourself with how the documentation works.

    Generating a random number

    The function randint(a, b) returns a random integer value between a and b, inclusive. For example, the following program works like a generic die:

    from random import randint\n\nprint(\"The result of the throw:\", randint(1, 6))

    Executing this could print out:

    The result of the throw: 4

    The following program throws the die ten times:

    from random import randint\n\nfor i in range(10):\n    print(\"The result of the throw:\", randint(1, 6))

    Running the above could print out

    The result of the throw: 5\nThe result of the throw: 4\nThe result of the throw: 3\nThe result of the throw: 2\nThe result of the throw: 3\nThe result of the throw: 4\nThe result of the throw: 6\nThe result of the throw: 4\nThe result of the throw: 4\nThe result of the throw: 3

    NB: it is worth remembering that the function randint works a bit differently when compared to, for example, slices, or the function range, which we've come across previously. The function call randint(1, 6) results in a number between 1 and 6 inclusive, but the function call range(1, 6) results in a range of numbers from 1 to 5.

    More randomizing functions

    The function shuffle will shuffle any data structure passed as an argument, in place. For example, the following program shuffles a list of words:

    from random import shuffle\n\nwords = [\"atlas\", \"banana\", \"carrot\"]\nshuffle(words)\nprint(words)

    ['banana', 'atlas', 'carrot']

    The function choice returns a randomly picked item from a data structure:

    from random import choice\n\nwords = [\"atlas\", \"banana\", \"carrot\"]\nprint(choice(words))

    'carrot'

    Lottery numbers

    A common example for studying randomness is the case of lottery numbers. Let's try and draw some lottery numbers. In Finland the national lottery consists of a pool of 40 numbers, 7 of which are chosen for each week's draw.

    A first attempt at drawing a set of numbers could look like this:

    from random import randint\n\nfor i in range(7):\n    print(randint(1, 40))

    This would not work in the long run, however, as the same number may appear twice in a single weekly draw of seven numbers. We need a way to make sure the numbers drawn are all unique.

    One possibility is to store the drawn numbers in a list, and only add a number if it is not already on the list. This can be repeated until the length of the list is seven:

    from random import randint\n\nweekly_draw = []\nwhile len(weekly_draw) < 7:\n    new_rnd = randint(1, 40)\n    if new_rnd not in weekly_draw:\n        weekly_draw.append(new_rnd)\n\nprint(weekly_draw)

    A more compact approach would be to use the shuffle function:

    from random import shuffle\n\nnumber_pool = list(range(1, 41))\nshuffle(number_pool)\nweekly_draw = number_pool[0:7]\nprint(weekly_draw)

    Here the idea is that we first create a list containing the available numbers 1 to 40, rather like the balls in a lottery machine. The pool of numbers is then shuffled, and the first seven numbers chosen for the weekly draw. This saves us the trouble of writing a loop.

    In fact, the random module contains an even easier way to select lottery numbers: the sample function. It returns a random selection of a specified size from a given data structure:

    from random import sample\n\nnumber_pool = list(range(1, 41))\nweekly_draw = sample(number_pool, 7)\nprint(weekly_draw)

    Please write a function named lottery_numbers(amount: int, lower: int, upper: int), which generates as many random numbers as specified by the first argument. All numbers should fall within the bounds lower to upper. The numbers should be stored in a list and returned. The numbers should be in ascending order in the returned list.

    As these are lottery numbers, no number should appear twice in the list.

    An example of how the function should work:

    for number in lottery_numbers(7, 1, 40):\n    print(number)

    4\n7\n11\n16\n22\n29\n38

    Where do these random numbers come from?

    The features of the module random are based on an algorithm which produces random numbers based on a specific initialization value and some arithmetic operations. The initialization value is often called a seed value.

    The seed value can be supplied by the user with the seed function:

    from random import randint, seed\n\nseed(1337)\n# this will always produce the same \"random\" number\nprint(randint(1, 100))

    If we have functions which rely on randomization, and we set seed value, the function will produce the same result each time it is executed. The result may be different with different Python versions, but in essence randomness is lost by setting a seed value. This can be a useful feature when testing a program, for example.

    To be specific, the numbers provided by the random module are not truly random. Instead, they are pseudorandom. Computers are, in essence, deterministic machines. In an ideal situation, it should be possible to foretell the way they function down to the last bit. Therefore it is very difficult to create truly random numbers with a computer. For many applications, however, pseudorandom numbers are good enough. When true random numbers are required, the seed value is usually generated by some source outside the computer, for example background radiation, noise levels, or lava lamps.

    For more information about randomness, please see random.org.

    Please write a function which creates passwords of a desired length, consisting of lowercase characters a to z.

    An example of how the function should work:

    for i in range(10):\n    print(generate_password(8))

    lttehepy\nolsxttjl\ncbjncrzo\ndwxqjdgu\ngpfdcecs\njabyvgar\nxnbbonbl\nktmsjyww\nejhprmel\nrjkoacib

    Please write an improved version of your password generator. The function now takes three arguments:

      \n
    • If the second argument is True, the generated password should also contain one or more numbers.
    • \n
    • If the third argument is True, the generated password should also contain one or more of these special characters: !?=+-()#.
    • \n

    Despite these two additional arguments, the password should always contain at least one lowercase alphabet. You may assume the function will only be called with combinations of arguments that are possible to formulate into passwords following these rules. That is, the arguments will not specify e.g. a password of length 2 which contains both a number and a special characters, for then there would not be space for the mandatory lowercase letter.

    An example of how the function should work:

    for i in range(10):\n    print(generate_strong_password(8, True, True))

    2?0n+u31\nu=m4nl94\nn#=i6r#(\nda9?zvm?\n7h)!)g?!\na=59x2n5\n(jr6n3b5\n9n(4i+2!\n32+qba#=\nn?b0a7ey

    In this exercise you will write some functions which can be used in games that involve dice.

    Instead of normal dice this exercise specifies non-transitive dice. You can read up on these here or watch this video.

    You will use three dice:

      \n
    • Die A has the sides 3, 3, 3, 3, 3, 6
    • \n
    • Die B has the sides 2, 2, 2, 5, 5, 5
    • \n
    • Die C has the sides 1, 4, 4, 4, 4, 4
    • \n

    Please write a function named roll(die: str), which rolls the die specified by the argument. An example of how this should work:

    for i in range(20):\n    print(roll(\"A\"), \" \", end=\"\")\nprint()\nfor i in range(20):\n    print(roll(\"B\"), \" \", end=\"\")\nprint()\nfor i in range(20):\n    print(roll(\"C\"), \" \", end=\"\")

    3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 6 3 6 3\n2 2 5 2 2 5 5 2 2 5 2 5 5 5 2 5 2 2 2 2\n4 4 4 4 4 1 1 4 4 4 1 4 4 4 4 4 4 4 4 4

    Also write a function named play(die1: str, die2: str, times: int), which throws both dice as many times as specified by the third argument. The function should return a tuple. The first item should be the number of times die 1 won, the second the number of times die 2 won, and the third item should be the number of ties.

    result = play(\"A\", \"C\", 1000)\nprint(result)\nresult = play(\"B\", \"B\", 1000)\nprint(result)

    (292, 708, 0)\n(249, 273, 478)

    The exercise template contains the file words.txt, which contains some English language words, one on each line.

    Please write a function named words(n: int, beginning: str), which returns a list containing n random words from the words.txt file. All words should begin with the string specified by the second argument.

    The same word should not appear twice in the list. If there are not enough words beginning with the specified string, the function should raise a ValueError exception.

    An example of the function in action:

    word_list = words(3, \"ca\")\nfor word in word_list:\n    print(word)

    cat\ncar\ncarbon

    ","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-7/2-randomness.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 some of the functions in the module "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"random"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to make use of random numbers in your programs"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This section concentrates on the module "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html?highlight=random#module-random","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"random"}]},{"type":"text","value":" from the Python standard library. It contains tools for generating random numbers and other randomized functionality."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The sections in this part of the material contain many links to the "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"documentation"}]},{"type":"text","value":" of the Python standard library. We recommend following the links to familiarize yourself with how the documentation works."}]},{"type":"element","tagName":"h2","properties":{"id":"generating-a-random-number","style":"position:relative;"},"children":[{"type":"text","value":"Generating a random number"},{"type":"element","tagName":"a","properties":{"href":"#generating-a-random-number","ariaLabel":"generating a random number 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 function "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html?highlight=random#random.randint","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"randint(a, b)"}]},{"type":"text","value":" returns a random integer value between "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":", inclusive. For example, the following program works like a generic die:"}]},{"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":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint\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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The result of the throw:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" randint"},{"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":"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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Executing this could print out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The result of the throw: 4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program throws the die ten times:"}]},{"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":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint\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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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 result of the throw:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" randint"},{"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":"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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Running the above could print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The result of the throw: 5\nThe result of the throw: 4\nThe result of the throw: 3\nThe result of the throw: 2\nThe result of the throw: 3\nThe result of the throw: 4\nThe result of the throw: 6\nThe result of the throw: 4\nThe result of the throw: 4\nThe result of the throw: 3"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: it is worth remembering that the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"randint"}]},{"type":"text","value":" works a bit differently when compared to, for example, slices, or the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range"}]},{"type":"text","value":", which we've come across previously. The function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"randint(1, 6)"}]},{"type":"text","value":" results in a number between 1 and 6 inclusive, but the function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"range(1, 6)"}]},{"type":"text","value":" results in a range of numbers from 1 to 5."}]},{"type":"element","tagName":"h2","properties":{"id":"more-randomizing-functions","style":"position:relative;"},"children":[{"type":"text","value":"More randomizing functions"},{"type":"element","tagName":"a","properties":{"href":"#more-randomizing-functions","ariaLabel":"more randomizing 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":"The function "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html?highlight=random#random.shuffle","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"shuffle"}]},{"type":"text","value":" will shuffle any data structure passed as an argument, in place. For example, the following program shuffles a list of words:"}]},{"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":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" shuffle\n\nwords "},{"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":"\"atlas\""}]},{"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":"\"banana\""}]},{"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":"\"carrot\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nshuffle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"words"},{"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":"words"},{"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":"['banana', 'atlas', 'carrot']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"choice"}]},{"type":"text","value":" returns a randomly picked item from a data structure:"}]},{"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":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" choice\n\nwords "},{"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":"\"atlas\""}]},{"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":"\"banana\""}]},{"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":"\"carrot\""}]},{"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":"choice"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"words"},{"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":"'carrot'"}]}]},{"type":"element","tagName":"h2","properties":{"id":"lottery-numbers","style":"position:relative;"},"children":[{"type":"text","value":"Lottery numbers"},{"type":"element","tagName":"a","properties":{"href":"#lottery-numbers","ariaLabel":"lottery numbers 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":"A common example for studying randomness is the case of lottery numbers. Let's try and draw some lottery numbers. In Finland the national lottery consists of a pool of 40 numbers, 7 of which are chosen for each week's draw."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A first attempt at drawing a set of numbers 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":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint\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":"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":"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":"randint"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"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":"This would not work in the long run, however, as the same number may appear twice in a single weekly draw of seven numbers. We need a way to make sure the numbers drawn are all unique."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"One possibility is to store the drawn numbers in a list, and only add a number if it is not already on the list. This can be repeated until the length of the list is seven:"}]},{"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":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint\n\nweekly_draw "},{"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","keyword"]},"children":[{"type":"text","value":"while"}]},{"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":"weekly_draw"},{"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":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n new_rnd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" randint"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"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":"if"}]},{"type":"text","value":" new_rnd "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" weekly_draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n weekly_draw"},{"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":"new_rnd"},{"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":"weekly_draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A more compact approach would be to use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"shuffle"}]},{"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","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" shuffle\n\nnumber_pool "},{"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":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"41"}]},{"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":"\nshuffle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"number_pool"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nweekly_draw "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" number_pool"},{"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":"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":"weekly_draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here the idea is that we first create a list containing the available numbers 1 to 40, rather like the balls in a lottery machine. The pool of numbers is then shuffled, and the first seven numbers chosen for the weekly draw. This saves us the trouble of writing a loop."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In fact, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"random"}]},{"type":"text","value":" module contains an even easier way to select lottery numbers: the "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html?highlight=random#random.sample","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"sample"}]},{"type":"text","value":" function. It returns a random selection of a specified size from a given data structure:"}]},{"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":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" sample\n\nnumber_pool "},{"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":"list"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"41"}]},{"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":"\nweekly_draw "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" sample"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"number_pool"},{"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":"7"}]},{"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":"weekly_draw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Lottery numbers","tmcname":"part07-04_lottery_numbers"},"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":"lottery_numbers(amount: int, lower: int, upper: int)"}]},{"type":"text","value":", which generates as many random numbers as specified by the first argument. All numbers should fall within the bounds "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"lower"}]},{"type":"text","value":" to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"upper"}]},{"type":"text","value":". The numbers should be stored in a list and returned. The numbers should be in ascending order in the returned list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As these are lottery numbers, no number should appear twice in the list."}]},{"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":"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":" lottery_numbers"},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"40"}]},{"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":"number"},{"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\n7\n11\n16\n22\n29\n38"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"where-do-these-random-numbers-come-from","style":"position:relative;"},"children":[{"type":"text","value":"Where do these random numbers come from?"},{"type":"element","tagName":"a","properties":{"href":"#where-do-these-random-numbers-come-from","ariaLabel":"where do these random numbers come from 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 features of the module "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"random"}]},{"type":"text","value":" are based on an algorithm which produces random numbers based on a specific initialization value and some arithmetic operations. The initialization value is often called a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"seed value"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The seed value can be supplied by the user with the "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/random.html?highlight=random#random.seed","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"seed"}]},{"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","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" seed\n\nseed"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1337"}]},{"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":"# this will always produce the same \"random\" number"}]},{"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":"randint"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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":"If we have functions which rely on randomization, and we set seed value, the function will produce the same result each time it is executed. The result may be different with different Python versions, but in essence randomness is lost by setting a seed value. This can be a useful feature when testing a program, for example."}]},{"type":"element","tagName":"text-box","properties":{"variant":"info","name":"True randomness"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To be specific, the numbers provided by the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"random"}]},{"type":"text","value":" module are not truly random. Instead, they are "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"pseudorandom"}]},{"type":"text","value":". Computers are, in essence, deterministic machines. In an ideal situation, it should be possible to foretell the way they function down to the last bit. Therefore it is very difficult to create truly random numbers with a computer. For many applications, however, pseudorandom numbers are good enough. When true random numbers are required, the seed value is usually generated by some source outside the computer, for example background radiation, noise levels, or "},{"type":"element","tagName":"a","properties":{"href":"https://blog.cloudflare.com/randomness-101-lavarand-in-production/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"lava lamps"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"For more information about randomness, please see "},{"type":"element","tagName":"a","properties":{"href":"https://www.random.org/randomness/"},"children":[{"type":"text","value":"random.org"}]},{"type":"text","value":"."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Password generator, part 1","tmcname":"part07-05_password_generator_part_1"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a function which creates passwords of a desired length, consisting of lowercase characters a to z."}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"generate_password"},{"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":"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":"lttehepy\nolsxttjl\ncbjncrzo\ndwxqjdgu\ngpfdcecs\njabyvgar\nxnbbonbl\nktmsjyww\nejhprmel\nrjkoacib"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Password generator, part 2","tmcname":"part07-06_password_generator_part_2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write an improved version of your password generator. The function now takes three arguments:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If the second argument is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":", the generated password should also contain one or more numbers."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If the third argument is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":", the generated password should also contain one or more of these special characters: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!?=+-()#"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Despite these two additional arguments, the password should always contain at least one lowercase alphabet. You may assume the function will only be called with combinations of arguments that are possible to formulate into passwords following these rules. That is, the arguments will not specify e.g. a password of length 2 which contains both a number and a special characters, for then there would not be space for the mandatory lowercase letter."}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"generate_strong_password"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"2?0n+u31\nu=m4nl94\nn#=i6r#(\nda9?zvm?\n7h)!)g?!\na=59x2n5\n(jr6n3b5\n9n(4i+2!\n32+qba#=\nn?b0a7ey"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Dice roller","tmcname":"part07-07_dice_roller"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will write some functions which can be used in games that involve dice."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Instead of normal dice this exercise specifies "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"non-transitive dice"}]},{"type":"text","value":". You can read up on these "},{"type":"element","tagName":"a","properties":{"href":"https://singingbanana.com/dice/article.htm","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"here"}]},{"type":"text","value":" or "},{"type":"element","tagName":"a","properties":{"href":"https://www.youtube.com/watch?v=LrIp6CKUlH8","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"watch this video"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You will use three dice:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Die A has the sides 3, 3, 3, 3, 3, 6"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Die B has the sides 2, 2, 2, 5, 5, 5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Die C has the sides 1, 4, 4, 4, 4, 4"}]},{"type":"text","value":"\n"}]},{"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":"roll(die: str)"}]},{"type":"text","value":", which rolls the die specified by the argument. An example of how this 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":"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"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":"roll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"A\""}]},{"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":" "},{"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":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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"},{"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"},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"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":"roll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"B\""}]},{"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":" "},{"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":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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"},{"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"},{"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"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":"roll"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"C\""}]},{"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":" "},{"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":" end"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 6 3 6 3\n2 2 5 2 2 5 5 2 2 5 2 5 5 5 2 5 2 2 2 2\n4 4 4 4 4 1 1 4 4 4 1 4 4 4 4 4 4 4 4 4"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Also write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"play(die1: str, die2: str, times: int)"}]},{"type":"text","value":", which throws both dice as many times as specified by the third argument. The function should return a tuple. The first item should be the number of times die 1 won, the second the number of times die 2 won, and the third item should be the number of ties."}]},{"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":"result "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" play"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"A\""}]},{"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":"\"C\""}]},{"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":"1000"}]},{"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":"text","value":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" play"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"B\""}]},{"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":"\"B\""}]},{"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":"1000"}]},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"(292, 708, 0)\n(249, 273, 478)"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Random words","tmcname":"part07-08_random_words"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"words.txt"}]},{"type":"text","value":", which contains some English language words, one on each line."}]},{"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":"words(n: int, beginning: str)"}]},{"type":"text","value":", which returns a list containing "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"n"}]},{"type":"text","value":" random words from the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"words.txt"}]},{"type":"text","value":" file. All words should begin with the string specified by the second argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The same word should not appear twice in the list. If there are not enough words beginning with the specified string, the function should raise a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" exception."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"word_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" words"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ca\""}]},{"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":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" word_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":"word"},{"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":"cat\ncar\ncarbon"}]}]}]},{"type":"comment","value":"-\n\n"}]},"html":"

    After this section

      \n
    • You will be familiar with some of the functions in the module random
    • \n
    • You will be able to make use of random numbers in your programs
    • \n

    This section concentrates on the module random from the Python standard library. It contains tools for generating random numbers and other randomized functionality.

    The sections in this part of the material contain many links to the documentation of the Python standard library. We recommend following the links to familiarize yourself with how the documentation works.

    Generating a random number

    The function randint(a, b) returns a random integer value between a and b, inclusive. For example, the following program works like a generic die:

    from random import randint\n\nprint(\"The result of the throw:\", randint(1, 6))

    Executing this could print out:

    The result of the throw: 4

    The following program throws the die ten times:

    from random import randint\n\nfor i in range(10):\n    print(\"The result of the throw:\", randint(1, 6))

    Running the above could print out

    The result of the throw: 5\nThe result of the throw: 4\nThe result of the throw: 3\nThe result of the throw: 2\nThe result of the throw: 3\nThe result of the throw: 4\nThe result of the throw: 6\nThe result of the throw: 4\nThe result of the throw: 4\nThe result of the throw: 3

    NB: it is worth remembering that the function randint works a bit differently when compared to, for example, slices, or the function range, which we've come across previously. The function call randint(1, 6) results in a number between 1 and 6 inclusive, but the function call range(1, 6) results in a range of numbers from 1 to 5.

    More randomizing functions

    The function shuffle will shuffle any data structure passed as an argument, in place. For example, the following program shuffles a list of words:

    from random import shuffle\n\nwords = [\"atlas\", \"banana\", \"carrot\"]\nshuffle(words)\nprint(words)

    ['banana', 'atlas', 'carrot']

    The function choice returns a randomly picked item from a data structure:

    from random import choice\n\nwords = [\"atlas\", \"banana\", \"carrot\"]\nprint(choice(words))

    'carrot'

    Lottery numbers

    A common example for studying randomness is the case of lottery numbers. Let's try and draw some lottery numbers. In Finland the national lottery consists of a pool of 40 numbers, 7 of which are chosen for each week's draw.

    A first attempt at drawing a set of numbers could look like this:

    from random import randint\n\nfor i in range(7):\n    print(randint(1, 40))

    This would not work in the long run, however, as the same number may appear twice in a single weekly draw of seven numbers. We need a way to make sure the numbers drawn are all unique.

    One possibility is to store the drawn numbers in a list, and only add a number if it is not already on the list. This can be repeated until the length of the list is seven:

    from random import randint\n\nweekly_draw = []\nwhile len(weekly_draw) < 7:\n    new_rnd = randint(1, 40)\n    if new_rnd not in weekly_draw:\n        weekly_draw.append(new_rnd)\n\nprint(weekly_draw)

    A more compact approach would be to use the shuffle function:

    from random import shuffle\n\nnumber_pool = list(range(1, 41))\nshuffle(number_pool)\nweekly_draw = number_pool[0:7]\nprint(weekly_draw)

    Here the idea is that we first create a list containing the available numbers 1 to 40, rather like the balls in a lottery machine. The pool of numbers is then shuffled, and the first seven numbers chosen for the weekly draw. This saves us the trouble of writing a loop.

    In fact, the random module contains an even easier way to select lottery numbers: the sample function. It returns a random selection of a specified size from a given data structure:

    from random import sample\n\nnumber_pool = list(range(1, 41))\nweekly_draw = sample(number_pool, 7)\nprint(weekly_draw)

    Please write a function named lottery_numbers(amount: int, lower: int, upper: int), which generates as many random numbers as specified by the first argument. All numbers should fall within the bounds lower to upper. The numbers should be stored in a list and returned. The numbers should be in ascending order in the returned list.

    As these are lottery numbers, no number should appear twice in the list.

    An example of how the function should work:

    for number in lottery_numbers(7, 1, 40):\n    print(number)

    4\n7\n11\n16\n22\n29\n38

    Where do these random numbers come from?

    The features of the module random are based on an algorithm which produces random numbers based on a specific initialization value and some arithmetic operations. The initialization value is often called a seed value.

    The seed value can be supplied by the user with the seed function:

    from random import randint, seed\n\nseed(1337)\n# this will always produce the same \"random\" number\nprint(randint(1, 100))

    If we have functions which rely on randomization, and we set seed value, the function will produce the same result each time it is executed. The result may be different with different Python versions, but in essence randomness is lost by setting a seed value. This can be a useful feature when testing a program, for example.

    To be specific, the numbers provided by the random module are not truly random. Instead, they are pseudorandom. Computers are, in essence, deterministic machines. In an ideal situation, it should be possible to foretell the way they function down to the last bit. Therefore it is very difficult to create truly random numbers with a computer. For many applications, however, pseudorandom numbers are good enough. When true random numbers are required, the seed value is usually generated by some source outside the computer, for example background radiation, noise levels, or lava lamps.

    For more information about randomness, please see random.org.

    Please write a function which creates passwords of a desired length, consisting of lowercase characters a to z.

    An example of how the function should work:

    for i in range(10):\n    print(generate_password(8))

    lttehepy\nolsxttjl\ncbjncrzo\ndwxqjdgu\ngpfdcecs\njabyvgar\nxnbbonbl\nktmsjyww\nejhprmel\nrjkoacib

    Please write an improved version of your password generator. The function now takes three arguments:

      \n
    • If the second argument is True, the generated password should also contain one or more numbers.
    • \n
    • If the third argument is True, the generated password should also contain one or more of these special characters: !?=+-()#.
    • \n

    Despite these two additional arguments, the password should always contain at least one lowercase alphabet. You may assume the function will only be called with combinations of arguments that are possible to formulate into passwords following these rules. That is, the arguments will not specify e.g. a password of length 2 which contains both a number and a special characters, for then there would not be space for the mandatory lowercase letter.

    An example of how the function should work:

    for i in range(10):\n    print(generate_strong_password(8, True, True))

    2?0n+u31\nu=m4nl94\nn#=i6r#(\nda9?zvm?\n7h)!)g?!\na=59x2n5\n(jr6n3b5\n9n(4i+2!\n32+qba#=\nn?b0a7ey

    In this exercise you will write some functions which can be used in games that involve dice.

    Instead of normal dice this exercise specifies non-transitive dice. You can read up on these here or watch this video.

    You will use three dice:

      \n
    • Die A has the sides 3, 3, 3, 3, 3, 6
    • \n
    • Die B has the sides 2, 2, 2, 5, 5, 5
    • \n
    • Die C has the sides 1, 4, 4, 4, 4, 4
    • \n

    Please write a function named roll(die: str), which rolls the die specified by the argument. An example of how this should work:

    for i in range(20):\n    print(roll(\"A\"), \" \", end=\"\")\nprint()\nfor i in range(20):\n    print(roll(\"B\"), \" \", end=\"\")\nprint()\nfor i in range(20):\n    print(roll(\"C\"), \" \", end=\"\")

    3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 6 3 6 3\n2 2 5 2 2 5 5 2 2 5 2 5 5 5 2 5 2 2 2 2\n4 4 4 4 4 1 1 4 4 4 1 4 4 4 4 4 4 4 4 4

    Also write a function named play(die1: str, die2: str, times: int), which throws both dice as many times as specified by the third argument. The function should return a tuple. The first item should be the number of times die 1 won, the second the number of times die 2 won, and the third item should be the number of ties.

    result = play(\"A\", \"C\", 1000)\nprint(result)\nresult = play(\"B\", \"B\", 1000)\nprint(result)

    (292, 708, 0)\n(249, 273, 478)

    The exercise template contains the file words.txt, which contains some English language words, one on each line.

    Please write a function named words(n: int, beginning: str), which returns a list containing n random words from the words.txt file. All words should begin with the string specified by the second argument.

    The same word should not appear twice in the list. If there are not enough words beginning with the specified string, the function should raise a ValueError exception.

    An example of the function in action:

    word_list = words(3, \"ca\")\nfor word in word_list:\n    print(word)

    cat\ncar\ncarbon

    ","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-7/2-randomness.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-7/3-times-and-dates/page-data.json b/page-data/part-7/3-times-and-dates/page-data.json index cd6e10317..af2709397 100644 --- a/page-data/part-7/3-times-and-dates/page-data.json +++ b/page-data/part-7/3-times-and-dates/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-7/3-times-and-dates", - "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 know how to handle dates and times in Python code"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to create and use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":" objects"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to compare and calculate differences between two dates or times"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-datetime-object","style":"position:relative;"},"children":[{"type":"text","value":"The datetime object"},{"type":"element","tagName":"a","properties":{"href":"#the-datetime-object","ariaLabel":"the datetime object 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 Python "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html?highlight=datetime#datetime.datetime","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":" module includes the function "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html?highlight=datetime#datetime.datetime.now","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"now"}]},{"type":"text","value":", which returns a datetime object containing the current date and time. The default printout of a datetime object looks 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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\nmy_time "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"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":"my_time"},{"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":"2021-10-19 08:46:49.311393"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can also define the object yourself:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\nmy_time "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1952"}]},{"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":"12"}]},{"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":"24"}]},{"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_time"},{"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":"1952-12-24 00:00:00"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"By default, the time is set to midnight, as we did not give a time of day in the example above."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Different elements of the datetime object can be accessed in the following manner:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\nmy_time "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1952"}]},{"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":"12"}]},{"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":"24"}]},{"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":"\"Day:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" my_time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"day"},{"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":"\"Month:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" my_time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month"},{"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":"\"Year:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" my_time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year"},{"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":"Day: 24\nMonth: 12\nYear: 1952"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A time of day can also be specified. The precision can vary, as you can see below:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\npv1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2021"}]},{"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":"6"}]},{"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":"30"}]},{"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":"13"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 30.6.2021 at 1PM"}]},{"type":"text","value":"\npv2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2021"}]},{"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":"6"}]},{"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":"30"}]},{"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":"18"}]},{"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":"45"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 30.6.2021 at 6.45PM"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"compare-times-and-calculate-differences-between-them","style":"position:relative;"},"children":[{"type":"text","value":"Compare times and calculate differences between them"},{"type":"element","tagName":"a","properties":{"href":"#compare-times-and-calculate-differences-between-them","ariaLabel":"compare times and calculate differences between them 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 familiar comparison operators work also on datetime objects:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\ntime_now "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"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":"\nmidsummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2021"}]},{"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":"6"}]},{"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":"26"}]},{"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":"if"}]},{"type":"text","value":" time_now "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" midsummer"},{"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":"\"It is not yet Midsummer\""}]},{"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":"elif"}]},{"type":"text","value":" time_now "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" midsummer"},{"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":"\"Happy Midsummer!\""}]},{"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":"elif"}]},{"type":"text","value":" time_now "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" midsummer"},{"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":"\"It is past Midsummer\""}]},{"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":"It is past Midsummer"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The difference between two datetime objects can be calculated simply with the subtraction operator:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\ntime_now "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"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":"\nmidsummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2021"}]},{"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":"6"}]},{"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":"26"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ndifference "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" midsummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" time_now\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":"\"Midsummer is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" difference"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"days"},{"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":"\"days away\""}]},{"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":"Midsummer is -116 days away"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the result of the datetime subtraction is a "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html?highlight=datetime#timedelta-objects","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"timedelta"}]},{"type":"text","value":" object. It is less versatile than the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":" object. For instance, you can access the number of days in a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timedelta"}]},{"type":"text","value":" object, but not the number of years, as the length of a year varies. A "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timedelta"}]},{"type":"text","value":" object contains the attributes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"days"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"seconds"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"microseconds"}]},{"type":"text","value":". Other measures can be passed as arguments, but they will be converted internally."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Similarly, addition is available between "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timedelta"}]},{"type":"text","value":" objects. The result will be the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":" produced when the specified number of days (or weeks, seconds, etc) is added to a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":" object:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" timedelta\nmidsummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2021"}]},{"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":"6"}]},{"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":"26"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\none_week "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" timedelta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"days"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"text","value":"\nweek_from_date "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" midsummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" one_week\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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"A week after Midsummer it will be\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" week_from_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nlong_time "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" timedelta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"weeks"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"32"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" days"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"32 weeks and 15 days after Midsummer it will be\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" midsummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" long_time"},{"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":"A week after Midsummer it will be 2021-07-03 00:00:00\n32 weeks and 15 days after Midsummer it will be 2022-02-20 00:00:00"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's see how a higher precision works:"}]},{"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":"time_now "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"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":"\nmidnight "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2021"}]},{"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":"6"}]},{"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":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndifference "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" midnight "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" time_now\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\"Midnight is still "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"difference"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"seconds"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" seconds away\""}]}]},{"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":"Midnight is still 8188 seconds away"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"How old","tmcname":"part07-09_how_old"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for their date of birth, and then prints out how old the user was on the eve of the new millennium. The program should ask for the day, month and year separately, and print out the age in days. Please have a look at the examples below:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Day: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nMonth: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9"}]},{"type":"text","value":"\nYear: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1979"}]},{"type":"text","value":"\nYou were 7417 days old on the eve of the new millennium."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Day: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"28"}]},{"type":"text","value":"\nMonth: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nYear: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2005"}]},{"type":"text","value":"\nYou weren't born yet on the eve of the new millennium."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume all day-month-year combinations given as an argument will be valid dates. That is, there will not be a date like February 31st."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Valid PIC?","tmcname":"part07-10_valid_pic"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will validate Finnish Personal Identity Codes (PIC)."}]},{"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":"is_it_valid(pic: str)"}]},{"type":"text","value":", which returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" based on whether the PIC given as an argument is valid or not. Finnish PICs follow the format "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ddmmyyXyyyz"}]},{"type":"text","value":", where "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ddmmyy"}]},{"type":"text","value":" contains the date of birth, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"X"}]},{"type":"text","value":" is the marker for century, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yyy"}]},{"type":"text","value":" is the personal identifier and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"z"}]},{"type":"text","value":" is a control character."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should check the validity by these three criteria:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The first half of the code is a valid, existing date in the format "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ddmmyy"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The century marker is either "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" (1800s), "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" (1900s) or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"A"}]},{"type":"text","value":" (2000s)."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The control character is valid."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The control character is calculated by taking the nine-digit number created by the date of birth and the personal identifier, dividing this by 31, and selecting the character at the index specified by the remainder from the string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0123456789ABCDEFHJKLMNPRSTUVWXY"}]},{"type":"text","value":". For example, if the remainder was 12, the control character would be "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"C"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"More examples and explanations of the uses of the PIC are available at the "},{"type":"element","tagName":"a","properties":{"href":"https://dvv.fi/en/personal-identity-code","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Digital and Population Data Services Agency"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB!"}]},{"type":"text","value":" Please make sure you do not share your own PIC, for example in the code you use for testing or through the course support channels."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here are some valid PICs you can use for testing:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"230827-906F"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"120488+246L"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"310823A9877"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"formatting-times-and-dates","style":"position:relative;"},"children":[{"type":"text","value":"Formatting times and dates"},{"type":"element","tagName":"a","properties":{"href":"#formatting-times-and-dates","ariaLabel":"formatting times and dates 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 "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":" module contains a handy method "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html?highlight=datetime#datetime.date.strftime","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"strftime"}]},{"type":"text","value":" for formatting the string representation of a datetime object. For example, the following code will print the current date in the format "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dd.mm.yyyy"}]},{"type":"text","value":", and then the date and time in a different format:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\nmy_time "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"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":"my_time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strftime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"%d.%m.%Y\""}]},{"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":"my_time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strftime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"%d/%m/%Y %H:%M\""}]},{"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":"19.10.2021\n19/10/2021 09:31"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Time formatting uses specific characters to signify specific formats. The following is a list of a few of them (please see the Python "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/time.html#time.strftime","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"documentation"}]},{"type":"text","value":" for a complete list):"}]},{"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":"left"},"children":[{"type":"text","value":"Notation"}]},{"type":"element","tagName":"th","properties":{"align":"left"},"children":[{"type":"text","value":"Significance"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%d"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"day (01–31)"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%m"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"month (01–12)"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%Y"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"year in 4 digit format"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%H"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"hours in 24 hour format"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%M"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"minutes (00–59)"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%S"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"seconds (00–59)"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can also specify the delimiter between the different elements, as seen in the examples above."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Datetime formatting works in the reverse direction as well, in case you need to parse a datetime object from a string given by the user. The method "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html?highlight=datetime#datetime.datetime.strptime","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"strptime"}]},{"type":"text","value":" will do just that:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\nbirthday "},{"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 your birthday in the format dd.mm.yyyy: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmy_time "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strptime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"birthday"},{"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":"\"%d.%m.%Y\""}]},{"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":"if"}]},{"type":"text","value":" my_time "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2000"}]},{"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":"1"}]},{"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":"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":"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":"\"You were born in the previous millennium\""}]},{"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":"else"}]},{"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":"\"You were born during this millennium\""}]},{"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 your birthday in the format dd.mm.yyyy: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5.11.1986"}]},{"type":"text","value":"\nYou were born in the previous millennium"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Screen time","tmcname":"part07-11_screen_time"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program for recording the amount of time the user has spent in front of a television, computer or mobile device screen over a specific period of time."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should work as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Filename: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"late_june.txt"}]},{"type":"text","value":"\nStarting date: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"24.6.2020"}]},{"type":"text","value":"\nHow many days: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nPlease type in screen time in minutes on each day (TV computer mobile):\nScreen time 24.06.2020: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"60 120 0"}]},{"type":"text","value":"\nScreen time 25.06.2020: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0 0 0"}]},{"type":"text","value":"\nScreen time 26.06.2020: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"180 0 0"}]},{"type":"text","value":"\nScreen time 27.06.2020: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"25 240 15"}]},{"type":"text","value":"\nScreen time 28.06.2020: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"45 90 5"}]},{"type":"text","value":"\nData stored in file late_june.txt"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The user will input each day on a separate line, and the entries will contain three numbers separated by spaces, representing minutes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With the above input, the program should store the data in a file named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"late_june.txt"}]},{"type":"text","value":". The contents should look like this:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Time period: 24.06.2020-28.06.2020\nTotal minutes: 780\nAverage minutes: 156.0\n24.06.2020: 60/120/0\n25.06.2020: 0/0/0\n26.06.2020: 180/0/0\n27.06.2020: 25/240/15\n28.06.2020: 45/90/5"}]}]}]},{"type":"comment","value":"-\n\n"}]},"html":"

    After this section

      \n
    • You will know how to handle dates and times in Python code
    • \n
    • You will be able to create and use datetime objects
    • \n
    • You will know how to compare and calculate differences between two dates or times
    • \n

    The datetime object

    The Python datetime module includes the function now, which returns a datetime object containing the current date and time. The default printout of a datetime object looks like this:

    from datetime import datetime\n\nmy_time = datetime.now()\nprint(my_time)

    2021-10-19 08:46:49.311393

    You can also define the object yourself:

    from datetime import datetime\n\nmy_time = datetime(1952, 12, 24)\nprint(my_time)

    1952-12-24 00:00:00

    By default, the time is set to midnight, as we did not give a time of day in the example above.

    Different elements of the datetime object can be accessed in the following manner:

    from datetime import datetime\n\nmy_time = datetime(1952, 12, 24)\nprint(\"Day:\", my_time.day)\nprint(\"Month:\", my_time.month)\nprint(\"Year:\", my_time.year)

    Day: 24\nMonth: 12\nYear: 1952

    A time of day can also be specified. The precision can vary, as you can see below:

    from datetime import datetime\n\npv1 = datetime(2021, 6, 30, 13)     # 30.6.2021 at 1PM\npv2 = datetime(2021, 6, 30, 18, 45) # 30.6.2021 at 6.45PM

    Compare times and calculate differences between them

    The familiar comparison operators work also on datetime objects:

    from datetime import datetime\n\ntime_now = datetime.now()\nmidsummer = datetime(2021, 6, 26)\n\nif time_now < midsummer:\n    print(\"It is not yet Midsummer\")\nelif time_now == midsummer:\n    print(\"Happy Midsummer!\")\nelif time_now > midsummer:\n    print(\"It is past Midsummer\")

    It is past Midsummer

    The difference between two datetime objects can be calculated simply with the subtraction operator:

    from datetime import datetime\n\ntime_now = datetime.now()\nmidsummer = datetime(2021, 6, 26)\n\ndifference = midsummer - time_now\nprint(\"Midsummer is\", difference.days, \"days away\")

    Midsummer is -116 days away

    NB: the result of the datetime subtraction is a timedelta object. It is less versatile than the datetime object. For instance, you can access the number of days in a timedelta object, but not the number of years, as the length of a year varies. A timedelta object contains the attributes days, seconds and microseconds. Other measures can be passed as arguments, but they will be converted internally.

    Similarly, addition is available between datetime and timedelta objects. The result will be the datetime produced when the specified number of days (or weeks, seconds, etc) is added to a datetime object:

    from datetime import datetime, timedelta\nmidsummer = datetime(2021, 6, 26)\n\none_week = timedelta(days=7)\nweek_from_date = midsummer + one_week\n\nprint(\"A week after Midsummer it will be\", week_from_date)\n\nlong_time = timedelta(weeks=32, days=15)\n\nprint(\"32 weeks and 15 days after Midsummer it will be\", midsummer + long_time)

    A week after Midsummer it will be 2021-07-03 00:00:00\n32 weeks and 15 days after Midsummer it will be 2022-02-20 00:00:00

    Let's see how a higher precision works:

    time_now = datetime.now()\nmidnight = datetime(2021, 6, 30)\ndifference = midnight - time_now\nprint(f\"Midnight is still {difference.seconds} seconds away\")

    Midnight is still 8188 seconds away

    Please write a program which asks the user for their date of birth, and then prints out how old the user was on the eve of the new millennium. The program should ask for the day, month and year separately, and print out the age in days. Please have a look at the examples below:

    Day: 10\nMonth: 9\nYear: 1979\nYou were 7417 days old on the eve of the new millennium.

    Day: 28\nMonth: 3\nYear: 2005\nYou weren't born yet on the eve of the new millennium.

    You may assume all day-month-year combinations given as an argument will be valid dates. That is, there will not be a date like February 31st.

    In this exercise you will validate Finnish Personal Identity Codes (PIC).

    Please write a function named is_it_valid(pic: str), which returns True or False based on whether the PIC given as an argument is valid or not. Finnish PICs follow the format ddmmyyXyyyz, where ddmmyy contains the date of birth, X is the marker for century, yyy is the personal identifier and z is a control character.

    The program should check the validity by these three criteria:

      \n
    • The first half of the code is a valid, existing date in the format ddmmyy.
    • \n
    • The century marker is either + (1800s), - (1900s) or A (2000s).
    • \n
    • The control character is valid.
    • \n

    The control character is calculated by taking the nine-digit number created by the date of birth and the personal identifier, dividing this by 31, and selecting the character at the index specified by the remainder from the string 0123456789ABCDEFHJKLMNPRSTUVWXY. For example, if the remainder was 12, the control character would be C.

    More examples and explanations of the uses of the PIC are available at the Digital and Population Data Services Agency.

    NB! Please make sure you do not share your own PIC, for example in the code you use for testing or through the course support channels.

    Here are some valid PICs you can use for testing:

      \n
    • 230827-906F
    • \n
    • 120488+246L
    • \n
    • 310823A9877
    • \n

    Formatting times and dates

    The datetime module contains a handy method strftime for formatting the string representation of a datetime object. For example, the following code will print the current date in the format dd.mm.yyyy, and then the date and time in a different format:

    from datetime import datetime\n\nmy_time = datetime.now()\nprint(my_time.strftime(\"%d.%m.%Y\"))\nprint(my_time.strftime(\"%d/%m/%Y %H:%M\"))

    19.10.2021\n19/10/2021 09:31

    Time formatting uses specific characters to signify specific formats. The following is a list of a few of them (please see the Python documentation for a complete list):

    \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
    NotationSignificance
    %dday (01–31)
    %mmonth (01–12)
    %Yyear in 4 digit format
    %Hhours in 24 hour format
    %Mminutes (00–59)
    %Sseconds (00–59)

    You can also specify the delimiter between the different elements, as seen in the examples above.

    Datetime formatting works in the reverse direction as well, in case you need to parse a datetime object from a string given by the user. The method strptime will do just that:

    from datetime import datetime\n\nbirthday = input(\"Please type in your birthday in the format dd.mm.yyyy: \")\nmy_time = datetime.strptime(birthday, \"%d.%m.%Y\")\n\nif my_time < datetime(2000, 1, 1):\n    print(\"You were born in the previous millennium\")\nelse:\n    print(\"You were born during this millennium\")

    Please type in your birthday in the format dd.mm.yyyy: 5.11.1986\nYou were born in the previous millennium

    Please write a program for recording the amount of time the user has spent in front of a television, computer or mobile device screen over a specific period of time.

    The program should work as follows:

    Filename: late_june.txt\nStarting date: 24.6.2020\nHow many days: 5\nPlease type in screen time in minutes on each day (TV computer mobile):\nScreen time 24.06.2020: 60 120 0\nScreen time 25.06.2020: 0 0 0\nScreen time 26.06.2020: 180 0 0\nScreen time 27.06.2020: 25 240 15\nScreen time 28.06.2020: 45 90 5\nData stored in file late_june.txt

    The user will input each day on a separate line, and the entries will contain three numbers separated by spaces, representing minutes.

    With the above input, the program should store the data in a file named late_june.txt. The contents should look like this:

    Time period: 24.06.2020-28.06.2020\nTotal minutes: 780\nAverage minutes: 156.0\n24.06.2020: 60/120/0\n25.06.2020: 0/0/0\n26.06.2020: 180/0/0\n27.06.2020: 25/240/15\n28.06.2020: 45/90/5

    ","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-7/3-times-and-dates.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know how to handle dates and times in Python code"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to create and use "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":" objects"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to compare and calculate differences between two dates or times"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-datetime-object","style":"position:relative;"},"children":[{"type":"text","value":"The datetime object"},{"type":"element","tagName":"a","properties":{"href":"#the-datetime-object","ariaLabel":"the datetime object 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 Python "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html?highlight=datetime#datetime.datetime","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":" module includes the function "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html?highlight=datetime#datetime.datetime.now","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"now"}]},{"type":"text","value":", which returns a datetime object containing the current date and time. The default printout of a datetime object looks 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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\nmy_time "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"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":"my_time"},{"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":"2021-10-19 08:46:49.311393"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can also define the object yourself:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\nmy_time "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1952"}]},{"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":"12"}]},{"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":"24"}]},{"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_time"},{"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":"1952-12-24 00:00:00"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"By default, the time is set to midnight, as we did not give a time of day in the example above."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Different elements of the datetime object can be accessed in the following manner:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\nmy_time "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1952"}]},{"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":"12"}]},{"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":"24"}]},{"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":"\"Day:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" my_time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"day"},{"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":"\"Month:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" my_time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month"},{"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":"\"Year:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" my_time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year"},{"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":"Day: 24\nMonth: 12\nYear: 1952"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A time of day can also be specified. The precision can vary, as you can see below:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\npv1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2021"}]},{"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":"6"}]},{"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":"30"}]},{"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":"13"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 30.6.2021 at 1PM"}]},{"type":"text","value":"\npv2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2021"}]},{"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":"6"}]},{"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":"30"}]},{"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":"18"}]},{"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":"45"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# 30.6.2021 at 6.45PM"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"compare-times-and-calculate-differences-between-them","style":"position:relative;"},"children":[{"type":"text","value":"Compare times and calculate differences between them"},{"type":"element","tagName":"a","properties":{"href":"#compare-times-and-calculate-differences-between-them","ariaLabel":"compare times and calculate differences between them 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 familiar comparison operators work also on datetime objects:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\ntime_now "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"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":"\nmidsummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2021"}]},{"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":"6"}]},{"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":"26"}]},{"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":"if"}]},{"type":"text","value":" time_now "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" midsummer"},{"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":"\"It is not yet Midsummer\""}]},{"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":"elif"}]},{"type":"text","value":" time_now "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" midsummer"},{"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":"\"Happy Midsummer!\""}]},{"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":"elif"}]},{"type":"text","value":" time_now "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" midsummer"},{"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":"\"It is past Midsummer\""}]},{"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":"It is past Midsummer"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The difference between two datetime objects can be calculated simply with the subtraction operator:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\ntime_now "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"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":"\nmidsummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2021"}]},{"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":"6"}]},{"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":"26"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ndifference "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" midsummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" time_now\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":"\"Midsummer is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" difference"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"days"},{"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":"\"days away\""}]},{"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":"Midsummer is -116 days away"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the result of the datetime subtraction is a "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html?highlight=datetime#timedelta-objects","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"timedelta"}]},{"type":"text","value":" object. It is less versatile than the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":" object. For instance, you can access the number of days in a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timedelta"}]},{"type":"text","value":" object, but not the number of years, as the length of a year varies. A "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timedelta"}]},{"type":"text","value":" object contains the attributes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"days"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"seconds"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"microseconds"}]},{"type":"text","value":". Other measures can be passed as arguments, but they will be converted internally."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Similarly, addition is available between "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"timedelta"}]},{"type":"text","value":" objects. The result will be the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":" produced when the specified number of days (or weeks, seconds, etc) is added to a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":" object:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" timedelta\nmidsummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2021"}]},{"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":"6"}]},{"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":"26"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\none_week "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" timedelta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"days"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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":"text","value":"\nweek_from_date "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" midsummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" one_week\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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"A week after Midsummer it will be\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" week_from_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nlong_time "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" timedelta"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"weeks"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"32"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" days"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"32 weeks and 15 days after Midsummer it will be\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" midsummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" long_time"},{"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":"A week after Midsummer it will be 2021-07-03 00:00:00\n32 weeks and 15 days after Midsummer it will be 2022-02-20 00:00:00"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's see how a higher precision works:"}]},{"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":"time_now "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"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":"\nmidnight "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2021"}]},{"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":"6"}]},{"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":"30"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndifference "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" midnight "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" time_now\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\"Midnight is still "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"difference"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"seconds"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" seconds away\""}]}]},{"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":"Midnight is still 8188 seconds away"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"How old","tmcname":"part07-09_how_old"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program which asks the user for their date of birth, and then prints out how old the user was on the eve of the new millennium. The program should ask for the day, month and year separately, and print out the age in days. Please have a look at the examples below:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Day: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"10"}]},{"type":"text","value":"\nMonth: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"9"}]},{"type":"text","value":"\nYear: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"1979"}]},{"type":"text","value":"\nYou were 7417 days old on the eve of the new millennium."}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Day: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"28"}]},{"type":"text","value":"\nMonth: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"3"}]},{"type":"text","value":"\nYear: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2005"}]},{"type":"text","value":"\nYou weren't born yet on the eve of the new millennium."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume all day-month-year combinations given as an argument will be valid dates. That is, there will not be a date like February 31st."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Valid PIC?","tmcname":"part07-10_valid_pic"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will validate Finnish Personal Identity Codes (PIC)."}]},{"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":"is_it_valid(pic: str)"}]},{"type":"text","value":", which returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" based on whether the PIC given as an argument is valid or not. Finnish PICs follow the format "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ddmmyyXyyyz"}]},{"type":"text","value":", where "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ddmmyy"}]},{"type":"text","value":" contains the date of birth, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"X"}]},{"type":"text","value":" is the marker for century, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"yyy"}]},{"type":"text","value":" is the personal identifier and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"z"}]},{"type":"text","value":" is a control character."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should check the validity by these three criteria:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The first half of the code is a valid, existing date in the format "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ddmmyy"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The century marker is either "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" (1800s), "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" (1900s) or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"A"}]},{"type":"text","value":" (2000s)."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"The control character is valid."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The control character is calculated by taking the nine-digit number created by the date of birth and the personal identifier, dividing this by 31, and selecting the character at the index specified by the remainder from the string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0123456789ABCDEFHJKLMNPRSTUVWXY"}]},{"type":"text","value":". For example, if the remainder was 12, the control character would be "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"C"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"More examples and explanations of the uses of the PIC are available at the "},{"type":"element","tagName":"a","properties":{"href":"https://dvv.fi/en/personal-identity-code","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Digital and Population Data Services Agency"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB!"}]},{"type":"text","value":" Please make sure you do not share your own PIC, for example in the code you use for testing or through the course support channels."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here are some valid PICs you can use for testing:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"230827-906F"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"120488+246L"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"310823A9877"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"formatting-times-and-dates","style":"position:relative;"},"children":[{"type":"text","value":"Formatting times and dates"},{"type":"element","tagName":"a","properties":{"href":"#formatting-times-and-dates","ariaLabel":"formatting times and dates 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 "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"datetime"}]},{"type":"text","value":" module contains a handy method "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html?highlight=datetime#datetime.date.strftime","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"strftime"}]},{"type":"text","value":" for formatting the string representation of a datetime object. For example, the following code will print the current date in the format "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dd.mm.yyyy"}]},{"type":"text","value":", and then the date and time in a different format:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\nmy_time "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"now"},{"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":"my_time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strftime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"%d.%m.%Y\""}]},{"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":"my_time"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strftime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"%d/%m/%Y %H:%M\""}]},{"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":"19.10.2021\n19/10/2021 09:31"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Time formatting uses specific characters to signify specific formats. The following is a list of a few of them (please see the Python "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/time.html#time.strftime","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"documentation"}]},{"type":"text","value":" for a complete list):"}]},{"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":"left"},"children":[{"type":"text","value":"Notation"}]},{"type":"element","tagName":"th","properties":{"align":"left"},"children":[{"type":"text","value":"Significance"}]}]}]},{"type":"element","tagName":"tbody","properties":{},"children":[{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%d"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"day (01–31)"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%m"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"month (01–12)"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%Y"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"year in 4 digit format"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%H"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"hours in 24 hour format"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%M"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"minutes (00–59)"}]}]},{"type":"element","tagName":"tr","properties":{},"children":[{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"%S"}]}]},{"type":"element","tagName":"td","properties":{"align":"left"},"children":[{"type":"text","value":"seconds (00–59)"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can also specify the delimiter between the different elements, as seen in the examples above."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Datetime formatting works in the reverse direction as well, in case you need to parse a datetime object from a string given by the user. The method "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html?highlight=datetime#datetime.datetime.strptime","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"strptime"}]},{"type":"text","value":" will do just that:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" datetime\n\nbirthday "},{"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 your birthday in the format dd.mm.yyyy: \""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmy_time "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"strptime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"birthday"},{"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":"\"%d.%m.%Y\""}]},{"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":"if"}]},{"type":"text","value":" my_time "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" datetime"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2000"}]},{"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":"1"}]},{"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":"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":"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":"\"You were born in the previous millennium\""}]},{"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":"else"}]},{"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":"\"You were born during this millennium\""}]},{"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 your birthday in the format dd.mm.yyyy: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5.11.1986"}]},{"type":"text","value":"\nYou were born in the previous millennium"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Screen time","tmcname":"part07-11_screen_time"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a program for recording the amount of time the user has spent in front of a television, computer or mobile device screen over a specific period of time."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program should work as follows:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Filename: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"late_june.txt"}]},{"type":"text","value":"\nStarting date: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"24.6.2020"}]},{"type":"text","value":"\nHow many days: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\nPlease type in screen time in minutes on each day (TV computer mobile):\nScreen time 24.06.2020: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"60 120 0"}]},{"type":"text","value":"\nScreen time 25.06.2020: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"0 0 0"}]},{"type":"text","value":"\nScreen time 26.06.2020: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"180 0 0"}]},{"type":"text","value":"\nScreen time 27.06.2020: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"25 240 15"}]},{"type":"text","value":"\nScreen time 28.06.2020: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"45 90 5"}]},{"type":"text","value":"\nData stored in file late_june.txt"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The user will input each day on a separate line, and the entries will contain three numbers separated by spaces, representing minutes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With the above input, the program should store the data in a file named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"late_june.txt"}]},{"type":"text","value":". The contents should look like this:"}]},{"type":"element","tagName":"sample-data","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Time period: 24.06.2020-28.06.2020\nTotal minutes: 780\nAverage minutes: 156.0\n24.06.2020: 60/120/0\n25.06.2020: 0/0/0\n26.06.2020: 180/0/0\n27.06.2020: 25/240/15\n28.06.2020: 45/90/5"}]}]}]},{"type":"comment","value":"-\n\n"}]},"html":"

    After this section

      \n
    • You will know how to handle dates and times in Python code
    • \n
    • You will be able to create and use datetime objects
    • \n
    • You will know how to compare and calculate differences between two dates or times
    • \n

    The datetime object

    The Python datetime module includes the function now, which returns a datetime object containing the current date and time. The default printout of a datetime object looks like this:

    from datetime import datetime\n\nmy_time = datetime.now()\nprint(my_time)

    2021-10-19 08:46:49.311393

    You can also define the object yourself:

    from datetime import datetime\n\nmy_time = datetime(1952, 12, 24)\nprint(my_time)

    1952-12-24 00:00:00

    By default, the time is set to midnight, as we did not give a time of day in the example above.

    Different elements of the datetime object can be accessed in the following manner:

    from datetime import datetime\n\nmy_time = datetime(1952, 12, 24)\nprint(\"Day:\", my_time.day)\nprint(\"Month:\", my_time.month)\nprint(\"Year:\", my_time.year)

    Day: 24\nMonth: 12\nYear: 1952

    A time of day can also be specified. The precision can vary, as you can see below:

    from datetime import datetime\n\npv1 = datetime(2021, 6, 30, 13)     # 30.6.2021 at 1PM\npv2 = datetime(2021, 6, 30, 18, 45) # 30.6.2021 at 6.45PM

    Compare times and calculate differences between them

    The familiar comparison operators work also on datetime objects:

    from datetime import datetime\n\ntime_now = datetime.now()\nmidsummer = datetime(2021, 6, 26)\n\nif time_now < midsummer:\n    print(\"It is not yet Midsummer\")\nelif time_now == midsummer:\n    print(\"Happy Midsummer!\")\nelif time_now > midsummer:\n    print(\"It is past Midsummer\")

    It is past Midsummer

    The difference between two datetime objects can be calculated simply with the subtraction operator:

    from datetime import datetime\n\ntime_now = datetime.now()\nmidsummer = datetime(2021, 6, 26)\n\ndifference = midsummer - time_now\nprint(\"Midsummer is\", difference.days, \"days away\")

    Midsummer is -116 days away

    NB: the result of the datetime subtraction is a timedelta object. It is less versatile than the datetime object. For instance, you can access the number of days in a timedelta object, but not the number of years, as the length of a year varies. A timedelta object contains the attributes days, seconds and microseconds. Other measures can be passed as arguments, but they will be converted internally.

    Similarly, addition is available between datetime and timedelta objects. The result will be the datetime produced when the specified number of days (or weeks, seconds, etc) is added to a datetime object:

    from datetime import datetime, timedelta\nmidsummer = datetime(2021, 6, 26)\n\none_week = timedelta(days=7)\nweek_from_date = midsummer + one_week\n\nprint(\"A week after Midsummer it will be\", week_from_date)\n\nlong_time = timedelta(weeks=32, days=15)\n\nprint(\"32 weeks and 15 days after Midsummer it will be\", midsummer + long_time)

    A week after Midsummer it will be 2021-07-03 00:00:00\n32 weeks and 15 days after Midsummer it will be 2022-02-20 00:00:00

    Let's see how a higher precision works:

    time_now = datetime.now()\nmidnight = datetime(2021, 6, 30)\ndifference = midnight - time_now\nprint(f\"Midnight is still {difference.seconds} seconds away\")

    Midnight is still 8188 seconds away

    Please write a program which asks the user for their date of birth, and then prints out how old the user was on the eve of the new millennium. The program should ask for the day, month and year separately, and print out the age in days. Please have a look at the examples below:

    Day: 10\nMonth: 9\nYear: 1979\nYou were 7417 days old on the eve of the new millennium.

    Day: 28\nMonth: 3\nYear: 2005\nYou weren't born yet on the eve of the new millennium.

    You may assume all day-month-year combinations given as an argument will be valid dates. That is, there will not be a date like February 31st.

    In this exercise you will validate Finnish Personal Identity Codes (PIC).

    Please write a function named is_it_valid(pic: str), which returns True or False based on whether the PIC given as an argument is valid or not. Finnish PICs follow the format ddmmyyXyyyz, where ddmmyy contains the date of birth, X is the marker for century, yyy is the personal identifier and z is a control character.

    The program should check the validity by these three criteria:

      \n
    • The first half of the code is a valid, existing date in the format ddmmyy.
    • \n
    • The century marker is either + (1800s), - (1900s) or A (2000s).
    • \n
    • The control character is valid.
    • \n

    The control character is calculated by taking the nine-digit number created by the date of birth and the personal identifier, dividing this by 31, and selecting the character at the index specified by the remainder from the string 0123456789ABCDEFHJKLMNPRSTUVWXY. For example, if the remainder was 12, the control character would be C.

    More examples and explanations of the uses of the PIC are available at the Digital and Population Data Services Agency.

    NB! Please make sure you do not share your own PIC, for example in the code you use for testing or through the course support channels.

    Here are some valid PICs you can use for testing:

      \n
    • 230827-906F
    • \n
    • 120488+246L
    • \n
    • 310823A9877
    • \n

    Formatting times and dates

    The datetime module contains a handy method strftime for formatting the string representation of a datetime object. For example, the following code will print the current date in the format dd.mm.yyyy, and then the date and time in a different format:

    from datetime import datetime\n\nmy_time = datetime.now()\nprint(my_time.strftime(\"%d.%m.%Y\"))\nprint(my_time.strftime(\"%d/%m/%Y %H:%M\"))

    19.10.2021\n19/10/2021 09:31

    Time formatting uses specific characters to signify specific formats. The following is a list of a few of them (please see the Python documentation for a complete list):

    \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
    NotationSignificance
    %dday (01–31)
    %mmonth (01–12)
    %Yyear in 4 digit format
    %Hhours in 24 hour format
    %Mminutes (00–59)
    %Sseconds (00–59)

    You can also specify the delimiter between the different elements, as seen in the examples above.

    Datetime formatting works in the reverse direction as well, in case you need to parse a datetime object from a string given by the user. The method strptime will do just that:

    from datetime import datetime\n\nbirthday = input(\"Please type in your birthday in the format dd.mm.yyyy: \")\nmy_time = datetime.strptime(birthday, \"%d.%m.%Y\")\n\nif my_time < datetime(2000, 1, 1):\n    print(\"You were born in the previous millennium\")\nelse:\n    print(\"You were born during this millennium\")

    Please type in your birthday in the format dd.mm.yyyy: 5.11.1986\nYou were born in the previous millennium

    Please write a program for recording the amount of time the user has spent in front of a television, computer or mobile device screen over a specific period of time.

    The program should work as follows:

    Filename: late_june.txt\nStarting date: 24.6.2020\nHow many days: 5\nPlease type in screen time in minutes on each day (TV computer mobile):\nScreen time 24.06.2020: 60 120 0\nScreen time 25.06.2020: 0 0 0\nScreen time 26.06.2020: 180 0 0\nScreen time 27.06.2020: 25 240 15\nScreen time 28.06.2020: 45 90 5\nData stored in file late_june.txt

    The user will input each day on a separate line, and the entries will contain three numbers separated by spaces, representing minutes.

    With the above input, the program should store the data in a file named late_june.txt. The contents should look like this:

    Time period: 24.06.2020-28.06.2020\nTotal minutes: 780\nAverage minutes: 156.0\n24.06.2020: 60/120/0\n25.06.2020: 0/0/0\n26.06.2020: 180/0/0\n27.06.2020: 25/240/15\n28.06.2020: 45/90/5

    ","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-7/3-times-and-dates.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-7/4-data-processing/page-data.json b/page-data/part-7/4-data-processing/page-data.json index d0c082732..b1c43c7d5 100644 --- a/page-data/part-7/4-data-processing/page-data.json +++ b/page-data/part-7/4-data-processing/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-7/4-data-processing", - "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 know how to use a module to process CSV files"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to use a module to process JSON files"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to retrieve and read files from the internet"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"reading-csv-files","style":"position:relative;"},"children":[{"type":"text","value":"Reading CSV files"},{"type":"element","tagName":"a","properties":{"href":"#reading-csv-files","ariaLabel":"reading csv files 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":"CSV is such a simple format that so far we have accessed the with hand-written code. There is, however, a ready-made module in the Python standard library for working with CSV files: "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/csv.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"csv"}]},{"type":"text","value":". It works 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":"import"}]},{"type":"text","value":" csv\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"test.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" csv"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"reader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" delimiter"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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 "},{"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":"line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The above code reads all lines in the CSV file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"test.csv"}]},{"type":"text","value":", separates the contents of each line into a list using the delimiter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":", and prints each list. So, assuming the contents of the line are as follows:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":"012121212;5\n012345678;2\n015151515;4"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The code would print out this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['012121212', '5']\n['012345678', '2']\n['015151515', '4']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Since the CSV format is so simple, what's the use of having a separate module when we can just as well use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":" function? Well, for one, the way the module is built, it will also work correctly if the values in the file are strings, which may also contain the delimiter character. If some line in the file looked like this"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":"\"aaa;bbb\";\"ccc;ddd\""}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"the above code would produce this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['aaa;bbb', 'ccc;ddd']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":" function would also split within the strings, which would likely break the data, and our program in the process."}]},{"type":"element","tagName":"h2","properties":{"id":"reading-json-files","style":"position:relative;"},"children":[{"type":"text","value":"Reading JSON files"},{"type":"element","tagName":"a","properties":{"href":"#reading-json-files","ariaLabel":"reading json files 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":"CSV is just one of many machine-readable data formats. "},{"type":"element","tagName":"a","properties":{"href":"https://www.json.org/json-en.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"JSON"}]},{"type":"text","value":" is another, and it is used often when data has to be transferred between applications."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"JSON files are text files with a strict format, which is perhaps a little less accessible to the human eye than the CSV format. The following example uses the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"courses.json"}]},{"type":"text","value":", which contains information about some courses:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":"[\n {\n \"name\": \"Introduction to Programming\",\n \"abbreviation\": \"ItP\",\n \"periods\": [1, 3]\n },\n {\n \"name\": \"Advanced Course in Programming\",\n \"abbreviation\": \"ACiP\",\n \"periods\": [2, 4]\n },\n {\n \"name\": \"Database Application\",\n \"abbreviation\": \"DbApp\",\n \"periods\": [1, 2, 3, 4]\n }\n]"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The structure of a JSON file might look quite familiar to you by know. The JSON file above looks exactly like a Python list, which contains three Python dictionaries."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The standard library has a module for working with JSON files: "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/json.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"json"}]},{"type":"text","value":". The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"loads"}]},{"type":"text","value":" takes any argument passed in a JSON format and transforms it into a Python data structure. So, processing the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"courses.json"}]},{"type":"text","value":" file with the code below"}]},{"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":"import"}]},{"type":"text","value":" json\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"courses.json\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n data "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"read"},{"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\ncourses "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" json"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"loads"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"data"},{"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":"courses"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"would print out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[{'name': 'Introduction to Programming', 'abbreviation': 'ItP', 'periods': [1, 3]}, {'name': 'Advanced Course in Programming', 'abbreviation': 'ACiP', 'periods': [2, 4]}, {'name': 'Database Application', 'abbreviation': 'DbApp', 'periods': [1, 2, 3, 4]}]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we also wanted to print out the name of each course, we could expand our program with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop:"}]},{"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":"for"}]},{"type":"text","value":" course "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" courses"},{"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":"course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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":"Introduction to Programming\nAdvanced Course in Programming\nDatabase Application"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Handling JSON files","tmcname":"part07-12_json_files"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at a JSON file, which contains some information about students in the following format:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"json"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"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":"\"Peter Pythons\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"age\""}]},{"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":"27"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"hobbies\""}]},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"coding\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"knitting\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"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","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"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":"\"Jean Javanese\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"age\""}]},{"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":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"hobbies\""}]},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"coding\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"rock climbing\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"reading\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"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":"print_persons(filename: str)"}]},{"type":"text","value":", which reads a JSON file in the above format, and prints the contents as shown below. The file may contain any number of entries."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter Pythons 27 years (coding, knitting)\nJean Javanese 24 years (coding, rock climbing, reading)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The hobbies should be listed in the same order as they appear in the JSON file."}]}]},{"type":"element","tagName":"h2","properties":{"id":"retrieving-a-file-from-the-internet","style":"position:relative;"},"children":[{"type":"text","value":"Retrieving a file from the internet"},{"type":"element","tagName":"a","properties":{"href":"#retrieving-a-file-from-the-internet","ariaLabel":"retrieving a file from the internet 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 Python standard library also contains modules for dealing with online content, and one useful function is "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/urllib.request.html#urllib.request.urlopen","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"urllib.request.urlopen"}]},{"type":"text","value":". You are encouraged to have a look at the entire module, but the following example should be enough for you to get to grips with the function. It can be used to retrieve content from the internet, so it can be processed in your programs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following code would print out the contents of the University of Helsinki front page:"}]},{"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":"import"}]},{"type":"text","value":" urllib"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"request\n\nmy_request "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" urllib"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"request"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"urlopen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"https://helsinki.fi\""}]},{"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_request"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"read"},{"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":"p","properties":{},"children":[{"type":"text","value":"Pages intended for human eyes do not usually look very pretty when their code is printed out. In the following examples, however, we will work with machine-readable "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"data"}]},{"type":"text","value":" from an online source. Much of the machine-readable data available online is in JSON format."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Course statistics","tmcname":"part07-13_course_statistics"},"children":[{"type":"element","tagName":"h4","properties":{"id":"retrieving-the-list-of-active-courses","style":"position:relative;"},"children":[{"type":"text","value":"Retrieving the list of active courses"},{"type":"element","tagName":"a","properties":{"href":"#retrieving-the-list-of-active-courses","ariaLabel":"retrieving the list of active courses 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":"At the address "},{"type":"element","tagName":"a","properties":{"href":"https://studies.cs.helsinki.fi/stats-mock/api/courses","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://studies.cs.helsinki.fi/stats-mock/api/courses"}]},{"type":"text","value":" you will find basic information about some of the courses offered by the University of Helsinki Department of Computer Science, in JSON format."}]},{"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":"retrieve_all()"}]},{"type":"text","value":", which retrieves the data of all the courses which are currently active (the field "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enabled"}]},{"type":"text","value":" has the value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"true"}]},{"type":"text","value":"). These should be returned as a list of tuples, in the following format:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"[\n ('Full Stack Open 2020', 'ofs2019', 2020, 201),\n ('DevOps with Docker 2019', 'docker2019', 2019, 36),\n ('DevOps with Docker 2020', 'docker2020', 2020, 36),\n ('Beta DevOps with Kubernetes', 'beta-dwk-20', 2020, 28)\n]\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each tuple contains the following fields from the original data:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the name of the course: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fullName"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"year"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the sum of the values listed in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exercises"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB"}]},{"type":"text","value":": It is essential that you retrieve the data with the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"urllib.request.urlopen"}]},{"type":"text","value":", or the automated tests may not work correctly."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB2:"}]},{"type":"text","value":" The tests are designed so that they slightly modify the data retrieved from the internet, to make sure you do not hard-code your return values."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB3:"}]},{"type":"text","value":" Some Mac users have come across the following issue:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"text","value":"File "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py\""}]},{"type":"text","value":", line "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1353"}]},{"type":"text","value":", "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" do_open\n raise URLError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"err"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nurllib.error.URLError: "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"urlopen error "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"SSL: CERTIFICATE_VERIFY_FAILED"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" certificate verify failed: unable to get "},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":"local"}]},{"type":"text","value":" issuer certificate "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"_ssl.c:1124"},{"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":"p","properties":{},"children":[{"type":"text","value":"The solution depends on how Python is installed on your machine. In some cases executing the following in a terminal helps:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":"cd"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"/Applications/Python 3.8/\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sudo"}]},{"type":"text","value":" \"./Install Certificates.command"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The path used in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cd"}]},{"type":"text","value":" command above depends on the version of Python you have installed. The path may also be, for example, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"/Applications/Python 3.9/\""}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://stackoverflow.com/questions/27835619/urllib-and-ssl-certificate-verify-failed-error","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Various solutions"}]},{"type":"text","value":" to the problem have been suggested."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"One trick some have found useful:"}]},{"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":"import"}]},{"type":"text","value":" urllib"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"request\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" json\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" ssl "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# add this library to your import section"}]},{"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":"retrieve_all"}]},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# add the following line to the beginning of all your functions"}]},{"type":"text","value":"\n context "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ssl"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_create_unverified_context"},{"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":"# the rest of your function"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Another potential workaround:"}]},{"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":"import"}]},{"type":"text","value":" urllib"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"request\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" certifi "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# add this library to your import section"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" json\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":"retrieve_all"}]},{"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 address "},{"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":"\"https://studies.cs.helsinki.fi/stats-mock/api/courses\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# add a second argument to the function call"}]},{"type":"text","value":"\n request "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" urllib"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"request"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"urlopen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"address"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" cafile"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"certifi"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"where"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the rest of your function"}]}]}]}]},{"type":"element","tagName":"h4","properties":{"id":"retrieving-the-data-for-a-single-course","style":"position:relative;"},"children":[{"type":"text","value":"Retrieving the data for a single course"},{"type":"element","tagName":"a","properties":{"href":"#retrieving-the-data-for-a-single-course","ariaLabel":"retrieving the data for a single course 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":"Each course also has its own URL, where more specific weekly data about the course is available. The URLs follow the format "},{"type":"element","tagName":"a","properties":{"href":"https://studies.cs.helsinki.fi/stats-mock/api/courses/****/stats","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://studies.cs.helsinki.fi/stats-mock/api/courses/****/stats"}]},{"type":"text","value":", where you would replace the stars with the contents of the field "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" for the course you want to access."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, the data for the course "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"docker2019"}]},{"type":"text","value":" is at the address "},{"type":"element","tagName":"a","properties":{"href":"https://studies.cs.helsinki.fi/stats-mock/api/courses/docker2019/stats","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://studies.cs.helsinki.fi/stats-mock/api/courses/docker2019/stats"}]},{"type":"text","value":"."}]},{"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":"retrieve_course(course_name: str)"}]},{"type":"text","value":", which returns statistics for the specified course, in dictionary format."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, the function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"retrieve_course(\"docker2019\")"}]},{"type":"text","value":" would return a dictionary with the following contents:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"{\n 'weeks': 4,\n 'students': 220,\n 'hours': 5966,\n 'hours_average': 27,\n 'exercises': 4988,\n 'exercises_average': 22\n}\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The values in the dictionary are determined as follows:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"weeks"}]},{"type":"text","value":": the number of JSON object literals retrieved"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"students"}]},{"type":"text","value":": the maximum number of students in all the weeks"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hours"}]},{"type":"text","value":": the sum of all "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hour_total"}]},{"type":"text","value":" values in the different weeks"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hours_average"}]},{"type":"text","value":": the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hours"}]},{"type":"text","value":" value divided by the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"students"}]},{"type":"text","value":" value (rounded down to the closest integer value)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exercises"}]},{"type":"text","value":": the sum of all "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exercise_total"}]},{"type":"text","value":" values in the different weeks"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exercises_average"}]},{"type":"text","value":": the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exercises"}]},{"type":"text","value":" value divided by the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"students"}]},{"type":"text","value":" value (rounded down to the closest integer value)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB"}]},{"type":"text","value":": See the notices in Part 1 of the exercise, as they apply here, too."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB2"}]},{"type":"text","value":": The Python "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/math.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" module has a useful function for rounding down integers."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Who cheated","tmcname":"part07-14_who_cheated"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"start_times.csv"}]},{"type":"text","value":" contains individual start times for a programming exam, in the format "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name;hh:mm"}]},{"type":"text","value":". An example:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"jarmo;09:00"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"timo;18:42"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"kalle;13:23"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Additionally, the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"submissions.csv"}]},{"type":"text","value":" contains points and handin times for individual exercises. The format here is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name;task;points;hh:mm"}]},{"type":"text","value":". An example:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"jarmo;1;8;16:05"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"timo;2;10;21:22"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"jarmo;2;10;19:15"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"jne..."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your task is to find the students who spent over 3 hours on the exam tasks. That is, any student whose "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"any"}]},{"type":"text","value":" task was handed in over 3 hours later than their exam start time is labelled a cheater. There may be more than one submission for the same task for each student. You may assume all times are within the same day."}]},{"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":"cheaters()"}]},{"type":"text","value":", which returns a list containing the names of the students who cheated"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Who cheated, version 2","tmcname":"part07-15_who_cheated_2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You have the CSV files from the previous exercise at your disposal again. Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"final_points()"}]},{"type":"text","value":", which returns the final exam points received by the students, in a dictionary format, following these criteria:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If there are multiple submissions for the same task, the submission with the highest number of points is taken into account."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If the submission was made over 3 hours after the start time, the submission is ignored."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The tasks are numbered 1 to 8, and each submission is graded with 0 to 6 points."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the dicionary returned the key should be the name of the student, and the value the total points received by the student."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hint: nested dictionaries might be a good approach when processing the tasks and submission times of each student."}]}]},{"type":"element","tagName":"h2","properties":{"id":"looking-for-modules","style":"position:relative;"},"children":[{"type":"text","value":"Looking for modules"},{"type":"element","tagName":"a","properties":{"href":"#looking-for-modules","ariaLabel":"looking for modules 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 official Python documentation contains information on all modules available in the standard library:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://docs.python.org/3/library/"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In addition to the standard library, the internet is full of freely available Python modules for different purposes. Some commonly used modules are listed here:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://wiki.python.org/moin/UsefulModules","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://wiki.python.org/moin/UsefulModules"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Spell checker, version 2","tmcname":"part07-16_spellchecker_2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will write an improved version of the Spell checker from the "},{"type":"element","tagName":"a","properties":{"href":"/part-6/1-reading-files"},"children":[{"type":"text","value":"previous part"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Just like in the previous version, the program should ask the user to type in a line of text. Your program should then perform a spell check, and print out feedback to the user, so that all misspelled words have stars around them. Additionally, "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"the program should print out a list of suggestions for the misspelled words"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please have a look at the following two examples."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"write text: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"We use ptython to make a spell checker"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"We use *ptython* to make a spell checker\nsuggestions:\nptython: python, pythons, typhon\n"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"write text: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"this is acually a good and usefull program"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"this is *acually* a good and *usefull* program\nsuggestions:\nacually: actually, tactually, factually\nusefull: usefully, useful, museful\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The suggestions should be determined with the function "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/difflib.html#difflib.get_close_matches","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"get_close_matches"}]},{"type":"text","value":" from the Python standard library module "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/difflib.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"difflib"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB"}]},{"type":"text","value":": For the automatic tests to work correctly, please use the function with the \"default settings\". That is, please pass only two arguments to the function: the misspelled word, and the word list."}]}]},{"type":"comment","value":"-\n\n"}]},"html":"

    After this section

      \n
    • You will know how to use a module to process CSV files
    • \n
    • You will know how to use a module to process JSON files
    • \n
    • You will be able to retrieve and read files from the internet
    • \n

    Reading CSV files

    CSV is such a simple format that so far we have accessed the with hand-written code. There is, however, a ready-made module in the Python standard library for working with CSV files: csv. It works like this:

    import csv\n\nwith open(\"test.csv\") as my_file:\n    for line in csv.reader(my_file, delimiter=\";\"):\n        print(line)

    The above code reads all lines in the CSV file test.csv, separates the contents of each line into a list using the delimiter ;, and prints each list. So, assuming the contents of the line are as follows:

    012121212;5\n012345678;2\n015151515;4

    The code would print out this:

    ['012121212', '5']\n['012345678', '2']\n['015151515', '4']

    Since the CSV format is so simple, what's the use of having a separate module when we can just as well use the split function? Well, for one, the way the module is built, it will also work correctly if the values in the file are strings, which may also contain the delimiter character. If some line in the file looked like this

    "aaa;bbb";"ccc;ddd"

    the above code would produce this:

    ['aaa;bbb', 'ccc;ddd']

    Using the split function would also split within the strings, which would likely break the data, and our program in the process.

    Reading JSON files

    CSV is just one of many machine-readable data formats. JSON is another, and it is used often when data has to be transferred between applications.

    JSON files are text files with a strict format, which is perhaps a little less accessible to the human eye than the CSV format. The following example uses the file courses.json, which contains information about some courses:

    [\n    {\n        "name": "Introduction to Programming",\n        "abbreviation": "ItP",\n        "periods": [1, 3]\n    },\n    {\n        "name": "Advanced Course in Programming",\n        "abbreviation": "ACiP",\n        "periods": [2, 4]\n    },\n    {\n        "name": "Database Application",\n        "abbreviation": "DbApp",\n        "periods": [1, 2, 3, 4]\n    }\n]

    The structure of a JSON file might look quite familiar to you by know. The JSON file above looks exactly like a Python list, which contains three Python dictionaries.

    The standard library has a module for working with JSON files: json. The function loads takes any argument passed in a JSON format and transforms it into a Python data structure. So, processing the courses.json file with the code below

    import json\n\nwith open(\"courses.json\") as my_file:\n    data = my_file.read()\n\ncourses = json.loads(data)\nprint(courses)

    would print out the following:

    [{'name': 'Introduction to Programming', 'abbreviation': 'ItP', 'periods': [1, 3]}, {'name': 'Advanced Course in Programming', 'abbreviation': 'ACiP', 'periods': [2, 4]}, {'name': 'Database Application', 'abbreviation': 'DbApp', 'periods': [1, 2, 3, 4]}]

    If we also wanted to print out the name of each course, we could expand our program with a for loop:

    for course in courses:\n    print(course[\"name\"])

    Introduction to Programming\nAdvanced Course in Programming\nDatabase Application

    Let's have a look at a JSON file, which contains some information about students in the following format:

    [\n    {\n        \"name\": \"Peter Pythons\",\n        \"age\": 27,\n        \"hobbies\": [\n            \"coding\",\n            \"knitting\"\n        ]\n    },\n    {\n        \"name\": \"Jean Javanese\",\n        \"age\": 24,\n        \"hobbies\": [\n            \"coding\",\n            \"rock climbing\",\n            \"reading\"\n        ]\n    }\n]

    Please write a function named print_persons(filename: str), which reads a JSON file in the above format, and prints the contents as shown below. The file may contain any number of entries.

    Peter Pythons 27 years (coding, knitting)\nJean Javanese 24 years (coding, rock climbing, reading)

    The hobbies should be listed in the same order as they appear in the JSON file.

    Retrieving a file from the internet

    The Python standard library also contains modules for dealing with online content, and one useful function is urllib.request.urlopen. You are encouraged to have a look at the entire module, but the following example should be enough for you to get to grips with the function. It can be used to retrieve content from the internet, so it can be processed in your programs.

    The following code would print out the contents of the University of Helsinki front page:

    import urllib.request\n\nmy_request = urllib.request.urlopen(\"https://helsinki.fi\")\nprint(my_request.read())

    Pages intended for human eyes do not usually look very pretty when their code is printed out. In the following examples, however, we will work with machine-readable data from an online source. Much of the machine-readable data available online is in JSON format.

    Retrieving the list of active courses

    At the address https://studies.cs.helsinki.fi/stats-mock/api/courses you will find basic information about some of the courses offered by the University of Helsinki Department of Computer Science, in JSON format.

    Please write a function named retrieve_all(), which retrieves the data of all the courses which are currently active (the field enabled has the value true). These should be returned as a list of tuples, in the following format:

    \n[\n    ('Full Stack Open 2020', 'ofs2019', 2020, 201),\n    ('DevOps with Docker 2019', 'docker2019', 2019, 36),\n    ('DevOps with Docker 2020', 'docker2020', 2020, 36),\n    ('Beta DevOps with Kubernetes', 'beta-dwk-20', 2020, 28)\n]\n

    Each tuple contains the following fields from the original data:

      \n
    • the name of the course: fullName
    • \n
    • name
    • \n
    • year
    • \n
    • the sum of the values listed in exercises
    • \n

    NB: It is essential that you retrieve the data with the function urllib.request.urlopen, or the automated tests may not work correctly.

    NB2: The tests are designed so that they slightly modify the data retrieved from the internet, to make sure you do not hard-code your return values.

    NB3: Some Mac users have come across the following issue:

    File \"/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py\", line 1353, in do_open\n    raise URLError(err)\nurllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1124)>

    The solution depends on how Python is installed on your machine. In some cases executing the following in a terminal helps:

    cd \"/Applications/Python 3.8/\"\nsudo \"./Install Certificates.command

    The path used in the cd command above depends on the version of Python you have installed. The path may also be, for example, \"/Applications/Python 3.9/\".

    Various solutions to the problem have been suggested.

    One trick some have found useful:

    import urllib.request\nimport json\nimport ssl # add this library to your import section\n\ndef retrieve_all():\n    # add the following line to the beginning of all your functions\n    context = ssl._create_unverified_context()\n    # the rest of your function

    Another potential workaround:

    import urllib.request\nimport certifi # add this library to your import section\nimport json\n\ndef retrieve_all():\n   address = \"https://studies.cs.helsinki.fi/stats-mock/api/courses\"\n   # add a second argument to the function call\n   request = urllib.request.urlopen(address, cafile=certifi.where())\n   # the rest of your function

    Retrieving the data for a single course

    Each course also has its own URL, where more specific weekly data about the course is available. The URLs follow the format https://studies.cs.helsinki.fi/stats-mock/api/courses/****/stats, where you would replace the stars with the contents of the field name for the course you want to access.

    For example, the data for the course docker2019 is at the address https://studies.cs.helsinki.fi/stats-mock/api/courses/docker2019/stats.

    Please write a function named retrieve_course(course_name: str), which returns statistics for the specified course, in dictionary format.

    For example, the function call retrieve_course(\"docker2019\") would return a dictionary with the following contents:

    \n{\n    'weeks': 4,\n    'students': 220,\n    'hours': 5966,\n    'hours_average': 27,\n    'exercises': 4988,\n    'exercises_average': 22\n}\n

    The values in the dictionary are determined as follows:

      \n
    • weeks: the number of JSON object literals retrieved
    • \n
    • students: the maximum number of students in all the weeks
    • \n
    • hours: the sum of all hour_total values in the different weeks
    • \n
    • hours_average: the hours value divided by the students value (rounded down to the closest integer value)
    • \n
    • exercises: the sum of all exercise_total values in the different weeks
    • \n
    • exercises_average: the exercises value divided by the students value (rounded down to the closest integer value)
    • \n

    NB: See the notices in Part 1 of the exercise, as they apply here, too.

    NB2: The Python math module has a useful function for rounding down integers.

    The file start_times.csv contains individual start times for a programming exam, in the format name;hh:mm. An example:

    jarmo;09:00\ntimo;18:42\nkalle;13:23

    Additionally, the file submissions.csv contains points and handin times for individual exercises. The format here is name;task;points;hh:mm. An example:

    jarmo;1;8;16:05\ntimo;2;10;21:22\njarmo;2;10;19:15\njne...

    Your task is to find the students who spent over 3 hours on the exam tasks. That is, any student whose any task was handed in over 3 hours later than their exam start time is labelled a cheater. There may be more than one submission for the same task for each student. You may assume all times are within the same day.

    Please write a function named cheaters(), which returns a list containing the names of the students who cheated

    You have the CSV files from the previous exercise at your disposal again. Please write a function named final_points(), which returns the final exam points received by the students, in a dictionary format, following these criteria:

      \n
    • If there are multiple submissions for the same task, the submission with the highest number of points is taken into account.
    • \n
    • If the submission was made over 3 hours after the start time, the submission is ignored.
    • \n

    The tasks are numbered 1 to 8, and each submission is graded with 0 to 6 points.

    In the dicionary returned the key should be the name of the student, and the value the total points received by the student.

    Hint: nested dictionaries might be a good approach when processing the tasks and submission times of each student.

    Looking for modules

    The official Python documentation contains information on all modules available in the standard library:

    In addition to the standard library, the internet is full of freely available Python modules for different purposes. Some commonly used modules are listed here:

    In this exercise you will write an improved version of the Spell checker from the previous part.

    Just like in the previous version, the program should ask the user to type in a line of text. Your program should then perform a spell check, and print out feedback to the user, so that all misspelled words have stars around them. Additionally, the program should print out a list of suggestions for the misspelled words.

    Please have a look at the following two examples.

    write text: We use ptython to make a spell checker

    \nWe use *ptython* to make a spell checker\nsuggestions:\nptython: python, pythons, typhon\n

    write text: this is acually a good and usefull program

    \nthis is *acually* a good and *usefull* program\nsuggestions:\nacually: actually, tactually, factually\nusefull: usefully, useful, museful\n

    The suggestions should be determined with the function get_close_matches from the Python standard library module difflib.

    NB: For the automatic tests to work correctly, please use the function with the \"default settings\". That is, please pass only two arguments to the function: the misspelled word, and the word list.

    ","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-7/4-data-processing.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know how to use a module to process CSV files"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to use a module to process JSON files"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to retrieve and read files from the internet"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"reading-csv-files","style":"position:relative;"},"children":[{"type":"text","value":"Reading CSV files"},{"type":"element","tagName":"a","properties":{"href":"#reading-csv-files","ariaLabel":"reading csv files 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":"CSV is such a simple format that so far we have accessed the with hand-written code. There is, however, a ready-made module in the Python standard library for working with CSV files: "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/csv.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"csv"}]},{"type":"text","value":". It works 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":"import"}]},{"type":"text","value":" csv\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"test.csv\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"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":" line "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" csv"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"reader"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" delimiter"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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 "},{"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":"line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The above code reads all lines in the CSV file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"test.csv"}]},{"type":"text","value":", separates the contents of each line into a list using the delimiter "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":";"}]},{"type":"text","value":", and prints each list. So, assuming the contents of the line are as follows:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":"012121212;5\n012345678;2\n015151515;4"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The code would print out this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['012121212', '5']\n['012345678', '2']\n['015151515', '4']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Since the CSV format is so simple, what's the use of having a separate module when we can just as well use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":" function? Well, for one, the way the module is built, it will also work correctly if the values in the file are strings, which may also contain the delimiter character. If some line in the file looked like this"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":"\"aaa;bbb\";\"ccc;ddd\""}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"the above code would produce this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"['aaa;bbb', 'ccc;ddd']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split"}]},{"type":"text","value":" function would also split within the strings, which would likely break the data, and our program in the process."}]},{"type":"element","tagName":"h2","properties":{"id":"reading-json-files","style":"position:relative;"},"children":[{"type":"text","value":"Reading JSON files"},{"type":"element","tagName":"a","properties":{"href":"#reading-json-files","ariaLabel":"reading json files 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":"CSV is just one of many machine-readable data formats. "},{"type":"element","tagName":"a","properties":{"href":"https://www.json.org/json-en.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"JSON"}]},{"type":"text","value":" is another, and it is used often when data has to be transferred between applications."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"JSON files are text files with a strict format, which is perhaps a little less accessible to the human eye than the CSV format. The following example uses the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"courses.json"}]},{"type":"text","value":", which contains information about some courses:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"x"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-x"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-x"]},"children":[{"type":"text","value":"[\n {\n \"name\": \"Introduction to Programming\",\n \"abbreviation\": \"ItP\",\n \"periods\": [1, 3]\n },\n {\n \"name\": \"Advanced Course in Programming\",\n \"abbreviation\": \"ACiP\",\n \"periods\": [2, 4]\n },\n {\n \"name\": \"Database Application\",\n \"abbreviation\": \"DbApp\",\n \"periods\": [1, 2, 3, 4]\n }\n]"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The structure of a JSON file might look quite familiar to you by know. The JSON file above looks exactly like a Python list, which contains three Python dictionaries."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The standard library has a module for working with JSON files: "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/json.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"json"}]},{"type":"text","value":". The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"loads"}]},{"type":"text","value":" takes any argument passed in a JSON format and transforms it into a Python data structure. So, processing the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"courses.json"}]},{"type":"text","value":" file with the code below"}]},{"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":"import"}]},{"type":"text","value":" json\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"with"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"open"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"courses.json\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"as"}]},{"type":"text","value":" my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n data "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_file"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"read"},{"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\ncourses "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" json"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"loads"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"data"},{"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":"courses"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"would print out the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[{'name': 'Introduction to Programming', 'abbreviation': 'ItP', 'periods': [1, 3]}, {'name': 'Advanced Course in Programming', 'abbreviation': 'ACiP', 'periods': [2, 4]}, {'name': 'Database Application', 'abbreviation': 'DbApp', 'periods': [1, 2, 3, 4]}]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we also wanted to print out the name of each course, we could expand our program with a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" loop:"}]},{"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":"for"}]},{"type":"text","value":" course "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" courses"},{"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":"course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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":"Introduction to Programming\nAdvanced Course in Programming\nDatabase Application"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Handling JSON files","tmcname":"part07-12_json_files"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at a JSON file, which contains some information about students in the following format:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"json"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-json"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"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":"\"Peter Pythons\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"age\""}]},{"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":"27"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"hobbies\""}]},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"coding\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"knitting\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"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","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"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":"\"Jean Javanese\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"age\""}]},{"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":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","property"]},"children":[{"type":"text","value":"\"hobbies\""}]},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"coding\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"rock climbing\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"reading\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]}]}]}]},{"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":"print_persons(filename: str)"}]},{"type":"text","value":", which reads a JSON file in the above format, and prints the contents as shown below. The file may contain any number of entries."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter Pythons 27 years (coding, knitting)\nJean Javanese 24 years (coding, rock climbing, reading)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The hobbies should be listed in the same order as they appear in the JSON file."}]}]},{"type":"element","tagName":"h2","properties":{"id":"retrieving-a-file-from-the-internet","style":"position:relative;"},"children":[{"type":"text","value":"Retrieving a file from the internet"},{"type":"element","tagName":"a","properties":{"href":"#retrieving-a-file-from-the-internet","ariaLabel":"retrieving a file from the internet 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 Python standard library also contains modules for dealing with online content, and one useful function is "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/urllib.request.html#urllib.request.urlopen","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"urllib.request.urlopen"}]},{"type":"text","value":". You are encouraged to have a look at the entire module, but the following example should be enough for you to get to grips with the function. It can be used to retrieve content from the internet, so it can be processed in your programs."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following code would print out the contents of the University of Helsinki front page:"}]},{"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":"import"}]},{"type":"text","value":" urllib"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"request\n\nmy_request "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" urllib"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"request"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"urlopen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"https://helsinki.fi\""}]},{"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_request"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"read"},{"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":"p","properties":{},"children":[{"type":"text","value":"Pages intended for human eyes do not usually look very pretty when their code is printed out. In the following examples, however, we will work with machine-readable "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"data"}]},{"type":"text","value":" from an online source. Much of the machine-readable data available online is in JSON format."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Course statistics","tmcname":"part07-13_course_statistics"},"children":[{"type":"element","tagName":"h4","properties":{"id":"retrieving-the-list-of-active-courses","style":"position:relative;"},"children":[{"type":"text","value":"Retrieving the list of active courses"},{"type":"element","tagName":"a","properties":{"href":"#retrieving-the-list-of-active-courses","ariaLabel":"retrieving the list of active courses 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":"At the address "},{"type":"element","tagName":"a","properties":{"href":"https://studies.cs.helsinki.fi/stats-mock/api/courses","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://studies.cs.helsinki.fi/stats-mock/api/courses"}]},{"type":"text","value":" you will find basic information about some of the courses offered by the University of Helsinki Department of Computer Science, in JSON format."}]},{"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":"retrieve_all()"}]},{"type":"text","value":", which retrieves the data of all the courses which are currently active (the field "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"enabled"}]},{"type":"text","value":" has the value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"true"}]},{"type":"text","value":"). These should be returned as a list of tuples, in the following format:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"[\n ('Full Stack Open 2020', 'ofs2019', 2020, 201),\n ('DevOps with Docker 2019', 'docker2019', 2019, 36),\n ('DevOps with Docker 2020', 'docker2020', 2020, 36),\n ('Beta DevOps with Kubernetes', 'beta-dwk-20', 2020, 28)\n]\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each tuple contains the following fields from the original data:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the name of the course: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fullName"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"year"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the sum of the values listed in "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exercises"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB"}]},{"type":"text","value":": It is essential that you retrieve the data with the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"urllib.request.urlopen"}]},{"type":"text","value":", or the automated tests may not work correctly."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB2:"}]},{"type":"text","value":" The tests are designed so that they slightly modify the data retrieved from the internet, to make sure you do not hard-code your return values."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB3:"}]},{"type":"text","value":" Some Mac users have come across the following issue:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"text","value":"File "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py\""}]},{"type":"text","value":", line "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1353"}]},{"type":"text","value":", "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" do_open\n raise URLError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"err"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nurllib.error.URLError: "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":"urlopen error "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"SSL: CERTIFICATE_VERIFY_FAILED"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":" certificate verify failed: unable to get "},{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":"local"}]},{"type":"text","value":" issuer certificate "},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"_ssl.c:1124"},{"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":"p","properties":{},"children":[{"type":"text","value":"The solution depends on how Python is installed on your machine. In some cases executing the following in a terminal helps:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"sh"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-sh"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","builtin","class-name"]},"children":[{"type":"text","value":"cd"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"/Applications/Python 3.8/\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"sudo"}]},{"type":"text","value":" \"./Install Certificates.command"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The path used in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cd"}]},{"type":"text","value":" command above depends on the version of Python you have installed. The path may also be, for example, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"/Applications/Python 3.9/\""}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://stackoverflow.com/questions/27835619/urllib-and-ssl-certificate-verify-failed-error","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"Various solutions"}]},{"type":"text","value":" to the problem have been suggested."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"One trick some have found useful:"}]},{"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":"import"}]},{"type":"text","value":" urllib"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"request\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" json\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" ssl "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# add this library to your import section"}]},{"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":"retrieve_all"}]},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# add the following line to the beginning of all your functions"}]},{"type":"text","value":"\n context "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ssl"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"_create_unverified_context"},{"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":"# the rest of your function"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Another potential workaround:"}]},{"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":"import"}]},{"type":"text","value":" urllib"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"request\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" certifi "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# add this library to your import section"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" json\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":"retrieve_all"}]},{"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 address "},{"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":"\"https://studies.cs.helsinki.fi/stats-mock/api/courses\""}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# add a second argument to the function call"}]},{"type":"text","value":"\n request "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" urllib"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"request"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"urlopen"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"address"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" cafile"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":"certifi"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"where"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the rest of your function"}]}]}]}]},{"type":"element","tagName":"h4","properties":{"id":"retrieving-the-data-for-a-single-course","style":"position:relative;"},"children":[{"type":"text","value":"Retrieving the data for a single course"},{"type":"element","tagName":"a","properties":{"href":"#retrieving-the-data-for-a-single-course","ariaLabel":"retrieving the data for a single course 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":"Each course also has its own URL, where more specific weekly data about the course is available. The URLs follow the format "},{"type":"element","tagName":"a","properties":{"href":"https://studies.cs.helsinki.fi/stats-mock/api/courses/****/stats","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://studies.cs.helsinki.fi/stats-mock/api/courses/****/stats"}]},{"type":"text","value":", where you would replace the stars with the contents of the field "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" for the course you want to access."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, the data for the course "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"docker2019"}]},{"type":"text","value":" is at the address "},{"type":"element","tagName":"a","properties":{"href":"https://studies.cs.helsinki.fi/stats-mock/api/courses/docker2019/stats","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://studies.cs.helsinki.fi/stats-mock/api/courses/docker2019/stats"}]},{"type":"text","value":"."}]},{"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":"retrieve_course(course_name: str)"}]},{"type":"text","value":", which returns statistics for the specified course, in dictionary format."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, the function call "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"retrieve_course(\"docker2019\")"}]},{"type":"text","value":" would return a dictionary with the following contents:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"{\n 'weeks': 4,\n 'students': 220,\n 'hours': 5966,\n 'hours_average': 27,\n 'exercises': 4988,\n 'exercises_average': 22\n}\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The values in the dictionary are determined as follows:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"weeks"}]},{"type":"text","value":": the number of JSON object literals retrieved"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"students"}]},{"type":"text","value":": the maximum number of students in all the weeks"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hours"}]},{"type":"text","value":": the sum of all "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hour_total"}]},{"type":"text","value":" values in the different weeks"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hours_average"}]},{"type":"text","value":": the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"hours"}]},{"type":"text","value":" value divided by the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"students"}]},{"type":"text","value":" value (rounded down to the closest integer value)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exercises"}]},{"type":"text","value":": the sum of all "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exercise_total"}]},{"type":"text","value":" values in the different weeks"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exercises_average"}]},{"type":"text","value":": the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"exercises"}]},{"type":"text","value":" value divided by the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"students"}]},{"type":"text","value":" value (rounded down to the closest integer value)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB"}]},{"type":"text","value":": See the notices in Part 1 of the exercise, as they apply here, too."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB2"}]},{"type":"text","value":": The Python "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/math.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" module has a useful function for rounding down integers."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Who cheated","tmcname":"part07-14_who_cheated"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"start_times.csv"}]},{"type":"text","value":" contains individual start times for a programming exam, in the format "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name;hh:mm"}]},{"type":"text","value":". An example:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"jarmo;09:00"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"timo;18:42"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"kalle;13:23"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Additionally, the file "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"submissions.csv"}]},{"type":"text","value":" contains points and handin times for individual exercises. The format here is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name;task;points;hh:mm"}]},{"type":"text","value":". An example:"}]},{"type":"element","tagName":"div","properties":{"className":["gatsby-highlight"],"dataLanguage":"csv"},"children":[{"type":"element","tagName":"pre","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"code","properties":{"className":["language-csv"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"jarmo;1;8;16:05"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"timo;2;10;21:22"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"jarmo;2;10;19:15"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","value"]},"children":[{"type":"text","value":"jne..."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your task is to find the students who spent over 3 hours on the exam tasks. That is, any student whose "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"any"}]},{"type":"text","value":" task was handed in over 3 hours later than their exam start time is labelled a cheater. There may be more than one submission for the same task for each student. You may assume all times are within the same day."}]},{"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":"cheaters()"}]},{"type":"text","value":", which returns a list containing the names of the students who cheated"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Who cheated, version 2","tmcname":"part07-15_who_cheated_2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You have the CSV files from the previous exercise at your disposal again. Please write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"final_points()"}]},{"type":"text","value":", which returns the final exam points received by the students, in a dictionary format, following these criteria:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If there are multiple submissions for the same task, the submission with the highest number of points is taken into account."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"If the submission was made over 3 hours after the start time, the submission is ignored."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The tasks are numbered 1 to 8, and each submission is graded with 0 to 6 points."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the dicionary returned the key should be the name of the student, and the value the total points received by the student."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hint: nested dictionaries might be a good approach when processing the tasks and submission times of each student."}]}]},{"type":"element","tagName":"h2","properties":{"id":"looking-for-modules","style":"position:relative;"},"children":[{"type":"text","value":"Looking for modules"},{"type":"element","tagName":"a","properties":{"href":"#looking-for-modules","ariaLabel":"looking for modules 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 official Python documentation contains information on all modules available in the standard library:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://docs.python.org/3/library/"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In addition to the standard library, the internet is full of freely available Python modules for different purposes. Some commonly used modules are listed here:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"a","properties":{"href":"https://wiki.python.org/moin/UsefulModules","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"https://wiki.python.org/moin/UsefulModules"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Spell checker, version 2","tmcname":"part07-16_spellchecker_2"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will write an improved version of the Spell checker from the "},{"type":"element","tagName":"a","properties":{"href":"/part-6/1-reading-files"},"children":[{"type":"text","value":"previous part"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Just like in the previous version, the program should ask the user to type in a line of text. Your program should then perform a spell check, and print out feedback to the user, so that all misspelled words have stars around them. Additionally, "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"the program should print out a list of suggestions for the misspelled words"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please have a look at the following two examples."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"write text: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"We use ptython to make a spell checker"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"We use *ptython* to make a spell checker\nsuggestions:\nptython: python, pythons, typhon\n"}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"write text: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"this is acually a good and usefull program"}]}]},{"type":"element","tagName":"pre","properties":{},"children":[{"type":"text","value":"this is *acually* a good and *usefull* program\nsuggestions:\nacually: actually, tactually, factually\nusefull: usefully, useful, museful\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The suggestions should be determined with the function "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/difflib.html#difflib.get_close_matches","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"get_close_matches"}]},{"type":"text","value":" from the Python standard library module "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/difflib.html","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"difflib"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB"}]},{"type":"text","value":": For the automatic tests to work correctly, please use the function with the \"default settings\". That is, please pass only two arguments to the function: the misspelled word, and the word list."}]}]},{"type":"comment","value":"-\n\n"}]},"html":"

    After this section

      \n
    • You will know how to use a module to process CSV files
    • \n
    • You will know how to use a module to process JSON files
    • \n
    • You will be able to retrieve and read files from the internet
    • \n

    Reading CSV files

    CSV is such a simple format that so far we have accessed the with hand-written code. There is, however, a ready-made module in the Python standard library for working with CSV files: csv. It works like this:

    import csv\n\nwith open(\"test.csv\") as my_file:\n    for line in csv.reader(my_file, delimiter=\";\"):\n        print(line)

    The above code reads all lines in the CSV file test.csv, separates the contents of each line into a list using the delimiter ;, and prints each list. So, assuming the contents of the line are as follows:

    012121212;5\n012345678;2\n015151515;4

    The code would print out this:

    ['012121212', '5']\n['012345678', '2']\n['015151515', '4']

    Since the CSV format is so simple, what's the use of having a separate module when we can just as well use the split function? Well, for one, the way the module is built, it will also work correctly if the values in the file are strings, which may also contain the delimiter character. If some line in the file looked like this

    "aaa;bbb";"ccc;ddd"

    the above code would produce this:

    ['aaa;bbb', 'ccc;ddd']

    Using the split function would also split within the strings, which would likely break the data, and our program in the process.

    Reading JSON files

    CSV is just one of many machine-readable data formats. JSON is another, and it is used often when data has to be transferred between applications.

    JSON files are text files with a strict format, which is perhaps a little less accessible to the human eye than the CSV format. The following example uses the file courses.json, which contains information about some courses:

    [\n    {\n        "name": "Introduction to Programming",\n        "abbreviation": "ItP",\n        "periods": [1, 3]\n    },\n    {\n        "name": "Advanced Course in Programming",\n        "abbreviation": "ACiP",\n        "periods": [2, 4]\n    },\n    {\n        "name": "Database Application",\n        "abbreviation": "DbApp",\n        "periods": [1, 2, 3, 4]\n    }\n]

    The structure of a JSON file might look quite familiar to you by know. The JSON file above looks exactly like a Python list, which contains three Python dictionaries.

    The standard library has a module for working with JSON files: json. The function loads takes any argument passed in a JSON format and transforms it into a Python data structure. So, processing the courses.json file with the code below

    import json\n\nwith open(\"courses.json\") as my_file:\n    data = my_file.read()\n\ncourses = json.loads(data)\nprint(courses)

    would print out the following:

    [{'name': 'Introduction to Programming', 'abbreviation': 'ItP', 'periods': [1, 3]}, {'name': 'Advanced Course in Programming', 'abbreviation': 'ACiP', 'periods': [2, 4]}, {'name': 'Database Application', 'abbreviation': 'DbApp', 'periods': [1, 2, 3, 4]}]

    If we also wanted to print out the name of each course, we could expand our program with a for loop:

    for course in courses:\n    print(course[\"name\"])

    Introduction to Programming\nAdvanced Course in Programming\nDatabase Application

    Let's have a look at a JSON file, which contains some information about students in the following format:

    [\n    {\n        \"name\": \"Peter Pythons\",\n        \"age\": 27,\n        \"hobbies\": [\n            \"coding\",\n            \"knitting\"\n        ]\n    },\n    {\n        \"name\": \"Jean Javanese\",\n        \"age\": 24,\n        \"hobbies\": [\n            \"coding\",\n            \"rock climbing\",\n            \"reading\"\n        ]\n    }\n]

    Please write a function named print_persons(filename: str), which reads a JSON file in the above format, and prints the contents as shown below. The file may contain any number of entries.

    Peter Pythons 27 years (coding, knitting)\nJean Javanese 24 years (coding, rock climbing, reading)

    The hobbies should be listed in the same order as they appear in the JSON file.

    Retrieving a file from the internet

    The Python standard library also contains modules for dealing with online content, and one useful function is urllib.request.urlopen. You are encouraged to have a look at the entire module, but the following example should be enough for you to get to grips with the function. It can be used to retrieve content from the internet, so it can be processed in your programs.

    The following code would print out the contents of the University of Helsinki front page:

    import urllib.request\n\nmy_request = urllib.request.urlopen(\"https://helsinki.fi\")\nprint(my_request.read())

    Pages intended for human eyes do not usually look very pretty when their code is printed out. In the following examples, however, we will work with machine-readable data from an online source. Much of the machine-readable data available online is in JSON format.

    Retrieving the list of active courses

    At the address https://studies.cs.helsinki.fi/stats-mock/api/courses you will find basic information about some of the courses offered by the University of Helsinki Department of Computer Science, in JSON format.

    Please write a function named retrieve_all(), which retrieves the data of all the courses which are currently active (the field enabled has the value true). These should be returned as a list of tuples, in the following format:

    \n[\n    ('Full Stack Open 2020', 'ofs2019', 2020, 201),\n    ('DevOps with Docker 2019', 'docker2019', 2019, 36),\n    ('DevOps with Docker 2020', 'docker2020', 2020, 36),\n    ('Beta DevOps with Kubernetes', 'beta-dwk-20', 2020, 28)\n]\n

    Each tuple contains the following fields from the original data:

      \n
    • the name of the course: fullName
    • \n
    • name
    • \n
    • year
    • \n
    • the sum of the values listed in exercises
    • \n

    NB: It is essential that you retrieve the data with the function urllib.request.urlopen, or the automated tests may not work correctly.

    NB2: The tests are designed so that they slightly modify the data retrieved from the internet, to make sure you do not hard-code your return values.

    NB3: Some Mac users have come across the following issue:

    File \"/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/urllib/request.py\", line 1353, in do_open\n    raise URLError(err)\nurllib.error.URLError: <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1124)>

    The solution depends on how Python is installed on your machine. In some cases executing the following in a terminal helps:

    cd \"/Applications/Python 3.8/\"\nsudo \"./Install Certificates.command

    The path used in the cd command above depends on the version of Python you have installed. The path may also be, for example, \"/Applications/Python 3.9/\".

    Various solutions to the problem have been suggested.

    One trick some have found useful:

    import urllib.request\nimport json\nimport ssl # add this library to your import section\n\ndef retrieve_all():\n    # add the following line to the beginning of all your functions\n    context = ssl._create_unverified_context()\n    # the rest of your function

    Another potential workaround:

    import urllib.request\nimport certifi # add this library to your import section\nimport json\n\ndef retrieve_all():\n   address = \"https://studies.cs.helsinki.fi/stats-mock/api/courses\"\n   # add a second argument to the function call\n   request = urllib.request.urlopen(address, cafile=certifi.where())\n   # the rest of your function

    Retrieving the data for a single course

    Each course also has its own URL, where more specific weekly data about the course is available. The URLs follow the format https://studies.cs.helsinki.fi/stats-mock/api/courses/****/stats, where you would replace the stars with the contents of the field name for the course you want to access.

    For example, the data for the course docker2019 is at the address https://studies.cs.helsinki.fi/stats-mock/api/courses/docker2019/stats.

    Please write a function named retrieve_course(course_name: str), which returns statistics for the specified course, in dictionary format.

    For example, the function call retrieve_course(\"docker2019\") would return a dictionary with the following contents:

    \n{\n    'weeks': 4,\n    'students': 220,\n    'hours': 5966,\n    'hours_average': 27,\n    'exercises': 4988,\n    'exercises_average': 22\n}\n

    The values in the dictionary are determined as follows:

      \n
    • weeks: the number of JSON object literals retrieved
    • \n
    • students: the maximum number of students in all the weeks
    • \n
    • hours: the sum of all hour_total values in the different weeks
    • \n
    • hours_average: the hours value divided by the students value (rounded down to the closest integer value)
    • \n
    • exercises: the sum of all exercise_total values in the different weeks
    • \n
    • exercises_average: the exercises value divided by the students value (rounded down to the closest integer value)
    • \n

    NB: See the notices in Part 1 of the exercise, as they apply here, too.

    NB2: The Python math module has a useful function for rounding down integers.

    The file start_times.csv contains individual start times for a programming exam, in the format name;hh:mm. An example:

    jarmo;09:00\ntimo;18:42\nkalle;13:23

    Additionally, the file submissions.csv contains points and handin times for individual exercises. The format here is name;task;points;hh:mm. An example:

    jarmo;1;8;16:05\ntimo;2;10;21:22\njarmo;2;10;19:15\njne...

    Your task is to find the students who spent over 3 hours on the exam tasks. That is, any student whose any task was handed in over 3 hours later than their exam start time is labelled a cheater. There may be more than one submission for the same task for each student. You may assume all times are within the same day.

    Please write a function named cheaters(), which returns a list containing the names of the students who cheated

    You have the CSV files from the previous exercise at your disposal again. Please write a function named final_points(), which returns the final exam points received by the students, in a dictionary format, following these criteria:

      \n
    • If there are multiple submissions for the same task, the submission with the highest number of points is taken into account.
    • \n
    • If the submission was made over 3 hours after the start time, the submission is ignored.
    • \n

    The tasks are numbered 1 to 8, and each submission is graded with 0 to 6 points.

    In the dicionary returned the key should be the name of the student, and the value the total points received by the student.

    Hint: nested dictionaries might be a good approach when processing the tasks and submission times of each student.

    Looking for modules

    The official Python documentation contains information on all modules available in the standard library:

    In addition to the standard library, the internet is full of freely available Python modules for different purposes. Some commonly used modules are listed here:

    In this exercise you will write an improved version of the Spell checker from the previous part.

    Just like in the previous version, the program should ask the user to type in a line of text. Your program should then perform a spell check, and print out feedback to the user, so that all misspelled words have stars around them. Additionally, the program should print out a list of suggestions for the misspelled words.

    Please have a look at the following two examples.

    write text: We use ptython to make a spell checker

    \nWe use *ptython* to make a spell checker\nsuggestions:\nptython: python, pythons, typhon\n

    write text: this is acually a good and usefull program

    \nthis is *acually* a good and *usefull* program\nsuggestions:\nacually: actually, tactually, factually\nusefull: usefully, useful, museful\n

    The suggestions should be determined with the function get_close_matches from the Python standard library module difflib.

    NB: For the automatic tests to work correctly, please use the function with the \"default settings\". That is, please pass only two arguments to the function: the misspelled word, and the word list.

    ","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-7/4-data-processing.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-7/5-creating-modules/page-data.json b/page-data/part-7/5-creating-modules/page-data.json index 8789b8658..4d4bac47c 100644 --- a/page-data/part-7/5-creating-modules/page-data.json +++ b/page-data/part-7/5-creating-modules/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-7/5-creating-modules", - "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 able to write your own modules"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what the Python variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__name__"}]},{"type":"text","value":" and the value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__main__"}]},{"type":"text","value":" signify"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Writing your own Python modules is easy. Any file containing valid Python code can be imported as a module. Let's assume we have a file named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"words.py"}]},{"type":"text","value":" with the following contents:"}]},{"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_word"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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 parts "},{"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":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" parts"},{"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":"\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":"last_word"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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 parts "},{"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":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" parts"},{"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\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":"number_of_words"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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 parts "},{"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":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"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":"parts"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The functions defined in the file can be accessed by importing the file:"}]},{"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":"import"}]},{"type":"text","value":" words\n\nmy_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":"\"Sheila sells seashells by the seashore\""}]},{"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":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"first_word"},{"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":"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":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"last_word"},{"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":"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":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_words"},{"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":"Sheila\nseashore\n6"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the file containing the Python module must be located either in the same directory with the program importing it, or in one of the default Python directories, or else the Python interpreter will not find it when the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement is executed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We can use our own modules just as we have learnt to use the modules from the Python standard library:"}]},{"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":"from"}]},{"type":"text","value":" words "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" first_word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" last_word\n\nsentence "},{"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 sentence: \""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The first word was: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" first_word"},{"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":"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 last word was: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" last_word"},{"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":"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 sentence: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Python is a swell programming language"}]},{"type":"text","value":"\nThe first word was: Python\nThe last word was: language"}]}]},{"type":"element","tagName":"h2","properties":{"id":"putting-type-hints-to-use","style":"position:relative;"},"children":[{"type":"text","value":"Putting type hints to use"},{"type":"element","tagName":"a","properties":{"href":"#putting-type-hints-to-use","ariaLabel":"putting type hints to use 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":"When using modules, type hinting becomes especially useful. If you are using an editor which has built-in support for type hinting, using different modules becomes much easier."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, Visual Studio Code will display the type hints when you are writing code:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 519px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/7772b5ee03bb1ca30f17560902916121/b23ad/7_vihje.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: 23.91304347826087%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAsklEQVQY032Q2w6DIBBE+Q4DmpSLilwEtG82sf//T9OwxD7VPpzM7IadJcv6vscdQojbPuec/DAMxOXZv0AuOKy12Pcn1jUh54JSCo7jhfN8I4RI/WmaEeMK7wNY3XRH13Wwcwvctp2ooc55Gq5B4zjBmJF8VWaMglIKWmtIKWGMofrq5ZKRc/7+poZJqfB4SNL2VpOvsBAsvHeIMWJZFqRUh1qdcoILrt1McNLKdbdffADLIo82jUnA7AAAAABJRU5ErkJggg=='); 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":["/static/7772b5ee03bb1ca30f17560902916121/a0b58/7_vihje.webp 230w","/static/7772b5ee03bb1ca30f17560902916121/bc10c/7_vihje.webp 460w","/static/7772b5ee03bb1ca30f17560902916121/ad2dd/7_vihje.webp 519w"],"sizes":"(max-width: 519px) 100vw, 519px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7772b5ee03bb1ca30f17560902916121/81c8e/7_vihje.png 230w","/static/7772b5ee03bb1ca30f17560902916121/08a84/7_vihje.png 460w","/static/7772b5ee03bb1ca30f17560902916121/b23ad/7_vihje.png 519w"],"sizes":"(max-width: 519px) 100vw, 519px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/7772b5ee03bb1ca30f17560902916121/b23ad/7_vihje.png","alt":"7 vihje","title":"7 vihje","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":"h2","properties":{"id":"main-function-code-in-a-module","style":"position:relative;"},"children":[{"type":"text","value":"Main function code in a module"},{"type":"element","tagName":"a","properties":{"href":"#main-function-code-in-a-module","ariaLabel":"main function code in a module 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":"If a module contains any code which is not contained within a function definition (that is, if the module contains code in the main function of the module), this code is executed automatically whenever the module is imported."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's assume our "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"words.py"}]},{"type":"text","value":" file also contained some test cases:"}]},{"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_word"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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 parts "},{"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":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" parts"},{"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":"\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":"last_word"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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 parts "},{"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":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" parts"},{"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\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":"number_of_words"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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 parts "},{"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":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"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":"parts"},{"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":"first_word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This is a test\""}]},{"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":"last_word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Here we are still testing\""}]},{"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":"number_of_words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"One two three four five\""}]},{"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, if we import the module with an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement, all the code in the module which is outside the defined functions is automatically executed:"}]},{"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":"import"}]},{"type":"text","value":" words\n\nmy_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":"\"Sheila sells seashells by the seashore\""}]},{"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":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"first_word"},{"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":"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":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"last_word"},{"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":"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":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_words"},{"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":"This\ntesting\n5\nSheila\nseashore\n6"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, this is not a good outcome, because the program we are trying to write is messed with by the test cases from the module itself."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Luckily, there is a solution, and it is one which you have used many times before in the exercises on this course. We just need to test if the program is being executed on its own, or if the code has been imported with an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement. Python has a built-in variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__name__"}]},{"type":"text","value":", which contains the name of the program being executed. If the program is being executed on its own, the value of the variable is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__main__"}]},{"type":"text","value":". If the program has been imported, the value of the variable is the name of the imported module (in this case, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"words"}]},{"type":"text","value":")."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Knowing this, we can add a conditional statement, which lets us only execute the text cases if the program is executed on its own. As you can see below, the structure looks familiar:"}]},{"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_word"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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":"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"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":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" parts"},{"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":"\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":"last_word"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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":"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"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":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" parts"},{"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\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":"number_of_words"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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":"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":"text","value":"\n parts "},{"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":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"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":"parts"},{"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":"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":"# testing functionality"}]},{"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":"first_word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This is a test\""}]},{"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":"last_word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Here we are still testing\""}]},{"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":"number_of_words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"One two three four five\""}]},{"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":"If you execute the module on its own, the test cases are printed out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This\ntesting\n5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the module is imported into another program, the test cases are not executed:"}]},{"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":"import"}]},{"type":"text","value":" words\n\nmy_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":"\"Sheila sells seashells by the seashore\""}]},{"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":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"first_word"},{"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":"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":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"last_word"},{"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":"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":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_words"},{"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":"Sheila\nseashore\n6"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the exercises on this course, whenever you were asked to write functions, you were usually also expected to wrap test cases in an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" block exactly like the one above. Now you know why."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"String helper","tmcname":"part07-17_string_helper"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a module named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"string_helper"}]},{"type":"text","value":", which contains the following functions:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"change_case(orig_string: str)"}]},{"type":"text","value":" creates and returns a new version of the parameter string. The lowercase letters in the original should be uppercase, and uppercase letters should be lowercase."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split_in_half(orig_string: str)"}]},{"type":"text","value":" splits the parameter string in half, and returns the results in a tuple. If the original has an odd number of characters, the first half should be shorter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"remove_special_characters(orig_string: str)"}]},{"type":"text","value":" returns a new version of the parameter string, with all special characters removed. Only lowercase and uppercase letters, numbers and spaces are allowed in the returned string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of how the module would be used:"}]},{"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":"import"}]},{"type":"text","value":" string_helper\n\nmy_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":"\"Well hello there!\""}]},{"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":"string_helper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"change_case"},{"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":"text","value":"\n\np1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" string_helper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split_in_half"},{"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":"\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":"p1"},{"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":"p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nm2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" string_helper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove_special_characters"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This is a test, lets see how it goes!!!11!\""}]},{"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":"m2"},{"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":"wELL HELLO THERE!\nWell hel\nlo there!\nThis is a test lets see how it goes11"}]}]}]},{"type":"comment","value":"-\n\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this week's materials."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will be able to write your own modules
    • \n
    • You will know what the Python variable __name__ and the value __main__ signify
    • \n

    Writing your own Python modules is easy. Any file containing valid Python code can be imported as a module. Let's assume we have a file named words.py with the following contents:

    def first_word(my_string: str):\n    parts = my_string.split(\" \")\n    return parts[0]\n\ndef last_word(my_string: str):\n    parts = my_string.split(\" \")\n    return parts[-1]\n\ndef number_of_words(my_string: str):\n    parts = my_string.split(\" \")\n    return len(parts)

    The functions defined in the file can be accessed by importing the file:

    import words\n\nmy_string = \"Sheila sells seashells by the seashore\"\n\nprint(words.first_word(my_string))\nprint(words.last_word(my_string))\nprint(words.number_of_words(my_string))

    Sheila\nseashore\n6

    NB: the file containing the Python module must be located either in the same directory with the program importing it, or in one of the default Python directories, or else the Python interpreter will not find it when the import statement is executed.

    We can use our own modules just as we have learnt to use the modules from the Python standard library:

    from words import first_word, last_word\n\nsentence = input(\"Please type in a sentence: \")\n\nprint(\"The first word was: \" + first_word(sentence))\nprint(\"The last word was: \" + last_word(sentence))

    Please type in a sentence: Python is a swell programming language\nThe first word was: Python\nThe last word was: language

    Putting type hints to use

    When using modules, type hinting becomes especially useful. If you are using an editor which has built-in support for type hinting, using different modules becomes much easier.

    For example, Visual Studio Code will display the type hints when you are writing code:

    \n \n \n \n \n \n \"7\n \n \n

    Main function code in a module

    If a module contains any code which is not contained within a function definition (that is, if the module contains code in the main function of the module), this code is executed automatically whenever the module is imported.

    Let's assume our words.py file also contained some test cases:

    def first_word(my_string: str):\n    parts = my_string.split(\" \")\n    return parts[0]\n\ndef last_word(my_string: str):\n    parts = my_string.split(\" \")\n    return parts[-1]\n\ndef number_of_words(my_string: str):\n    parts = my_string.split(\" \")\n    return len(parts)\n\nprint(first_word(\"This is a test\"))\nprint(last_word(\"Here we are still testing\"))\nprint(number_of_words(\"One two three four five\"))

    Now, if we import the module with an import statement, all the code in the module which is outside the defined functions is automatically executed:

    import words\n\nmy_string = \"Sheila sells seashells by the seashore\"\n\nprint(words.first_word(my_string))\nprint(words.last_word(my_string))\nprint(words.number_of_words(my_string))

    This\ntesting\n5\nSheila\nseashore\n6

    As you can see above, this is not a good outcome, because the program we are trying to write is messed with by the test cases from the module itself.

    Luckily, there is a solution, and it is one which you have used many times before in the exercises on this course. We just need to test if the program is being executed on its own, or if the code has been imported with an import statement. Python has a built-in variable __name__, which contains the name of the program being executed. If the program is being executed on its own, the value of the variable is __main__. If the program has been imported, the value of the variable is the name of the imported module (in this case, words).

    Knowing this, we can add a conditional statement, which lets us only execute the text cases if the program is executed on its own. As you can see below, the structure looks familiar:

    def first_word(my_string: str) -> str:\n    parts = my_string.split(\" \")\n    return parts[0]\n\ndef last_word(my_string: str) -> str:\n    parts = my_string.split(\" \")\n    return parts[-1]\n\ndef number_of_words(my_string: str) -> int:\n    parts = my_string.split(\" \")\n    return len(parts)\n\nif __name__ == \"__main__\":\n    # testing functionality\n    print(first_word(\"This is a test\"))\n    print(last_word(\"Here we are still testing\"))\n    print(number_of_words(\"One two three four five\"))

    If you execute the module on its own, the test cases are printed out:

    This\ntesting\n5

    When the module is imported into another program, the test cases are not executed:

    import words\n\nmy_string = \"Sheila sells seashells by the seashore\"\n\nprint(words.first_word(my_string))\nprint(words.last_word(my_string))\nprint(words.number_of_words(my_string))

    Sheila\nseashore\n6

    In the exercises on this course, whenever you were asked to write functions, you were usually also expected to wrap test cases in an if __name__ == \"__main__\" block exactly like the one above. Now you know why.

    Please write a module named string_helper, which contains the following functions:

    The function change_case(orig_string: str) creates and returns a new version of the parameter string. The lowercase letters in the original should be uppercase, and uppercase letters should be lowercase.

    The function split_in_half(orig_string: str) splits the parameter string in half, and returns the results in a tuple. If the original has an odd number of characters, the first half should be shorter.

    The function remove_special_characters(orig_string: str) returns a new version of the parameter string, with all special characters removed. Only lowercase and uppercase letters, numbers and spaces are allowed in the returned string.

    Some examples of how the module would be used:

    import string_helper\n\nmy_string = \"Well hello there!\"\n\nprint(string_helper.change_case(my_string))\n\np1, p2 = string_helper.split_in_half(my_string)\n\nprint(p1)\nprint(p2)\n\nm2 = string_helper.remove_special_characters(\"This is a test, lets see how it goes!!!11!\")\nprint(m2)

    wELL HELLO THERE!\nWell hel\nlo there!\nThis is a test lets see how it goes11

    Please respond to a quick questionnaire on this week's materials.

    ","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-7/5-creating-modules.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 able to write your own modules"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what the Python variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__name__"}]},{"type":"text","value":" and the value "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__main__"}]},{"type":"text","value":" signify"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Writing your own Python modules is easy. Any file containing valid Python code can be imported as a module. Let's assume we have a file named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"words.py"}]},{"type":"text","value":" with the following contents:"}]},{"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_word"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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 parts "},{"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":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" parts"},{"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":"\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":"last_word"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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 parts "},{"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":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" parts"},{"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\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":"number_of_words"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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 parts "},{"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":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"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":"parts"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The functions defined in the file can be accessed by importing the file:"}]},{"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":"import"}]},{"type":"text","value":" words\n\nmy_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":"\"Sheila sells seashells by the seashore\""}]},{"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":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"first_word"},{"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":"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":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"last_word"},{"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":"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":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_words"},{"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":"Sheila\nseashore\n6"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: the file containing the Python module must be located either in the same directory with the program importing it, or in one of the default Python directories, or else the Python interpreter will not find it when the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement is executed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We can use our own modules just as we have learnt to use the modules from the Python standard library:"}]},{"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":"from"}]},{"type":"text","value":" words "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" first_word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" last_word\n\nsentence "},{"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 sentence: \""}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The first word was: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" first_word"},{"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":"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 last word was: \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" last_word"},{"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":"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 sentence: "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Python is a swell programming language"}]},{"type":"text","value":"\nThe first word was: Python\nThe last word was: language"}]}]},{"type":"element","tagName":"h2","properties":{"id":"putting-type-hints-to-use","style":"position:relative;"},"children":[{"type":"text","value":"Putting type hints to use"},{"type":"element","tagName":"a","properties":{"href":"#putting-type-hints-to-use","ariaLabel":"putting type hints to use 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":"When using modules, type hinting becomes especially useful. If you are using an editor which has built-in support for type hinting, using different modules becomes much easier."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, Visual Studio Code will display the type hints when you are writing code:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 519px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/7772b5ee03bb1ca30f17560902916121/b23ad/7_vihje.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: 23.91304347826087%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAFCAYAAABFA8wzAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAAsklEQVQY032Q2w6DIBBE+Q4DmpSLilwEtG82sf//T9OwxD7VPpzM7IadJcv6vscdQojbPuec/DAMxOXZv0AuOKy12Pcn1jUh54JSCo7jhfN8I4RI/WmaEeMK7wNY3XRH13Wwcwvctp2ooc55Gq5B4zjBmJF8VWaMglIKWmtIKWGMofrq5ZKRc/7+poZJqfB4SNL2VpOvsBAsvHeIMWJZFqRUh1qdcoILrt1McNLKdbdffADLIo82jUnA7AAAAABJRU5ErkJggg=='); 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":["/static/7772b5ee03bb1ca30f17560902916121/a0b58/7_vihje.webp 230w","/static/7772b5ee03bb1ca30f17560902916121/bc10c/7_vihje.webp 460w","/static/7772b5ee03bb1ca30f17560902916121/ad2dd/7_vihje.webp 519w"],"sizes":"(max-width: 519px) 100vw, 519px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/7772b5ee03bb1ca30f17560902916121/81c8e/7_vihje.png 230w","/static/7772b5ee03bb1ca30f17560902916121/08a84/7_vihje.png 460w","/static/7772b5ee03bb1ca30f17560902916121/b23ad/7_vihje.png 519w"],"sizes":"(max-width: 519px) 100vw, 519px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/7772b5ee03bb1ca30f17560902916121/b23ad/7_vihje.png","alt":"7 vihje","title":"7 vihje","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":"h2","properties":{"id":"main-function-code-in-a-module","style":"position:relative;"},"children":[{"type":"text","value":"Main function code in a module"},{"type":"element","tagName":"a","properties":{"href":"#main-function-code-in-a-module","ariaLabel":"main function code in a module 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":"If a module contains any code which is not contained within a function definition (that is, if the module contains code in the main function of the module), this code is executed automatically whenever the module is imported."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's assume our "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"words.py"}]},{"type":"text","value":" file also contained some test cases:"}]},{"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_word"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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 parts "},{"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":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" parts"},{"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":"\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":"last_word"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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 parts "},{"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":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" parts"},{"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\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":"number_of_words"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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 parts "},{"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":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"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":"parts"},{"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":"first_word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This is a test\""}]},{"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":"last_word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Here we are still testing\""}]},{"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":"number_of_words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"One two three four five\""}]},{"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, if we import the module with an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement, all the code in the module which is outside the defined functions is automatically executed:"}]},{"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":"import"}]},{"type":"text","value":" words\n\nmy_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":"\"Sheila sells seashells by the seashore\""}]},{"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":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"first_word"},{"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":"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":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"last_word"},{"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":"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":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_words"},{"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":"This\ntesting\n5\nSheila\nseashore\n6"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, this is not a good outcome, because the program we are trying to write is messed with by the test cases from the module itself."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Luckily, there is a solution, and it is one which you have used many times before in the exercises on this course. We just need to test if the program is being executed on its own, or if the code has been imported with an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement. Python has a built-in variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__name__"}]},{"type":"text","value":", which contains the name of the program being executed. If the program is being executed on its own, the value of the variable is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__main__"}]},{"type":"text","value":". If the program has been imported, the value of the variable is the name of the imported module (in this case, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"words"}]},{"type":"text","value":")."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Knowing this, we can add a conditional statement, which lets us only execute the text cases if the program is executed on its own. As you can see below, the structure looks familiar:"}]},{"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_word"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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":"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"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":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" parts"},{"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":"\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":"last_word"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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":"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n parts "},{"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":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" parts"},{"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\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":"number_of_words"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"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":"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":"text","value":"\n parts "},{"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":"split"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"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":"parts"},{"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":"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":"# testing functionality"}]},{"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":"first_word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This is a test\""}]},{"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":"last_word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Here we are still testing\""}]},{"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":"number_of_words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"One two three four five\""}]},{"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":"If you execute the module on its own, the test cases are printed out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This\ntesting\n5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the module is imported into another program, the test cases are not executed:"}]},{"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":"import"}]},{"type":"text","value":" words\n\nmy_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":"\"Sheila sells seashells by the seashore\""}]},{"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":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"first_word"},{"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":"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":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"last_word"},{"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":"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":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_words"},{"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":"Sheila\nseashore\n6"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the exercises on this course, whenever you were asked to write functions, you were usually also expected to wrap test cases in an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" block exactly like the one above. Now you know why."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"String helper","tmcname":"part07-17_string_helper"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a module named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"string_helper"}]},{"type":"text","value":", which contains the following functions:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"change_case(orig_string: str)"}]},{"type":"text","value":" creates and returns a new version of the parameter string. The lowercase letters in the original should be uppercase, and uppercase letters should be lowercase."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"split_in_half(orig_string: str)"}]},{"type":"text","value":" splits the parameter string in half, and returns the results in a tuple. If the original has an odd number of characters, the first half should be shorter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"remove_special_characters(orig_string: str)"}]},{"type":"text","value":" returns a new version of the parameter string, with all special characters removed. Only lowercase and uppercase letters, numbers and spaces are allowed in the returned string."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Some examples of how the module would be used:"}]},{"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":"import"}]},{"type":"text","value":" string_helper\n\nmy_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":"\"Well hello there!\""}]},{"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":"string_helper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"change_case"},{"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":"text","value":"\n\np1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" p2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" string_helper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split_in_half"},{"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":"\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":"p1"},{"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":"p2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nm2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" string_helper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove_special_characters"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This is a test, lets see how it goes!!!11!\""}]},{"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":"m2"},{"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":"wELL HELLO THERE!\nWell hel\nlo there!\nThis is a test lets see how it goes11"}]}]}]},{"type":"comment","value":"-\n\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this week's materials."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will be able to write your own modules
    • \n
    • You will know what the Python variable __name__ and the value __main__ signify
    • \n

    Writing your own Python modules is easy. Any file containing valid Python code can be imported as a module. Let's assume we have a file named words.py with the following contents:

    def first_word(my_string: str):\n    parts = my_string.split(\" \")\n    return parts[0]\n\ndef last_word(my_string: str):\n    parts = my_string.split(\" \")\n    return parts[-1]\n\ndef number_of_words(my_string: str):\n    parts = my_string.split(\" \")\n    return len(parts)

    The functions defined in the file can be accessed by importing the file:

    import words\n\nmy_string = \"Sheila sells seashells by the seashore\"\n\nprint(words.first_word(my_string))\nprint(words.last_word(my_string))\nprint(words.number_of_words(my_string))

    Sheila\nseashore\n6

    NB: the file containing the Python module must be located either in the same directory with the program importing it, or in one of the default Python directories, or else the Python interpreter will not find it when the import statement is executed.

    We can use our own modules just as we have learnt to use the modules from the Python standard library:

    from words import first_word, last_word\n\nsentence = input(\"Please type in a sentence: \")\n\nprint(\"The first word was: \" + first_word(sentence))\nprint(\"The last word was: \" + last_word(sentence))

    Please type in a sentence: Python is a swell programming language\nThe first word was: Python\nThe last word was: language

    Putting type hints to use

    When using modules, type hinting becomes especially useful. If you are using an editor which has built-in support for type hinting, using different modules becomes much easier.

    For example, Visual Studio Code will display the type hints when you are writing code:

    \n \n \n \n \n \n \"7\n \n \n

    Main function code in a module

    If a module contains any code which is not contained within a function definition (that is, if the module contains code in the main function of the module), this code is executed automatically whenever the module is imported.

    Let's assume our words.py file also contained some test cases:

    def first_word(my_string: str):\n    parts = my_string.split(\" \")\n    return parts[0]\n\ndef last_word(my_string: str):\n    parts = my_string.split(\" \")\n    return parts[-1]\n\ndef number_of_words(my_string: str):\n    parts = my_string.split(\" \")\n    return len(parts)\n\nprint(first_word(\"This is a test\"))\nprint(last_word(\"Here we are still testing\"))\nprint(number_of_words(\"One two three four five\"))

    Now, if we import the module with an import statement, all the code in the module which is outside the defined functions is automatically executed:

    import words\n\nmy_string = \"Sheila sells seashells by the seashore\"\n\nprint(words.first_word(my_string))\nprint(words.last_word(my_string))\nprint(words.number_of_words(my_string))

    This\ntesting\n5\nSheila\nseashore\n6

    As you can see above, this is not a good outcome, because the program we are trying to write is messed with by the test cases from the module itself.

    Luckily, there is a solution, and it is one which you have used many times before in the exercises on this course. We just need to test if the program is being executed on its own, or if the code has been imported with an import statement. Python has a built-in variable __name__, which contains the name of the program being executed. If the program is being executed on its own, the value of the variable is __main__. If the program has been imported, the value of the variable is the name of the imported module (in this case, words).

    Knowing this, we can add a conditional statement, which lets us only execute the text cases if the program is executed on its own. As you can see below, the structure looks familiar:

    def first_word(my_string: str) -> str:\n    parts = my_string.split(\" \")\n    return parts[0]\n\ndef last_word(my_string: str) -> str:\n    parts = my_string.split(\" \")\n    return parts[-1]\n\ndef number_of_words(my_string: str) -> int:\n    parts = my_string.split(\" \")\n    return len(parts)\n\nif __name__ == \"__main__\":\n    # testing functionality\n    print(first_word(\"This is a test\"))\n    print(last_word(\"Here we are still testing\"))\n    print(number_of_words(\"One two three four five\"))

    If you execute the module on its own, the test cases are printed out:

    This\ntesting\n5

    When the module is imported into another program, the test cases are not executed:

    import words\n\nmy_string = \"Sheila sells seashells by the seashore\"\n\nprint(words.first_word(my_string))\nprint(words.last_word(my_string))\nprint(words.number_of_words(my_string))

    Sheila\nseashore\n6

    In the exercises on this course, whenever you were asked to write functions, you were usually also expected to wrap test cases in an if __name__ == \"__main__\" block exactly like the one above. Now you know why.

    Please write a module named string_helper, which contains the following functions:

    The function change_case(orig_string: str) creates and returns a new version of the parameter string. The lowercase letters in the original should be uppercase, and uppercase letters should be lowercase.

    The function split_in_half(orig_string: str) splits the parameter string in half, and returns the results in a tuple. If the original has an odd number of characters, the first half should be shorter.

    The function remove_special_characters(orig_string: str) returns a new version of the parameter string, with all special characters removed. Only lowercase and uppercase letters, numbers and spaces are allowed in the returned string.

    Some examples of how the module would be used:

    import string_helper\n\nmy_string = \"Well hello there!\"\n\nprint(string_helper.change_case(my_string))\n\np1, p2 = string_helper.split_in_half(my_string)\n\nprint(p1)\nprint(p2)\n\nm2 = string_helper.remove_special_characters(\"This is a test, lets see how it goes!!!11!\")\nprint(m2)

    wELL HELLO THERE!\nWell hel\nlo there!\nThis is a test lets see how it goes11

    Please respond to a quick questionnaire on this week's materials.

    ","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-7/5-creating-modules.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-7/6-more-features/page-data.json b/page-data/part-7/6-more-features/page-data.json index b07595eb1..ad70606c8 100644 --- a/page-data/part-7/6-more-features/page-data.json +++ b/page-data/part-7/6-more-features/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-7/6-more-features", - "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 some more Python features"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To finish off this course, here you will find various useful Python features."}]},{"type":"element","tagName":"h2","properties":{"id":"single-line-conditionals","style":"position:relative;"},"children":[{"type":"text","value":"Single line conditionals"},{"type":"element","tagName":"a","properties":{"href":"#single-line-conditionals","ariaLabel":"single line conditionals 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 following two statements produce the exact same results:"}]},{"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":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"even\""}]},{"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":"else"}]},{"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":"\"odd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"\"even\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"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":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"odd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the latter example we have a conditional expression on a single line: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a if [condition] else b"}]},{"type":"text","value":". The value of this expression evaluates to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" if the condition is true, and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" if it is false. This structure is sometimes referred to as a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"ternary operator"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Conditional expressions can be very useful when you need to assign something "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"conditionally"}]},{"type":"text","value":". For example, if you had the variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":", and you wanted to either increment or set the value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" depending on the parity of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":", you could write it in a normal "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if else"}]},{"type":"text","value":" statement, like so:"}]},{"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":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n y "},{"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":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n y "},{"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":"p","properties":{},"children":[{"type":"text","value":"The same could be achieved with a nifty one-liner:"}]},{"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":"y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" y "},{"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","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"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":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"an-empty-block","style":"position:relative;"},"children":[{"type":"text","value":"An \"empty\" block"},{"type":"element","tagName":"a","properties":{"href":"#an-empty-block","ariaLabel":"an empty block 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":"You may remember from the previous part that you are not allowed to have an empty block in a Python program. If you need to have a block of code which does nothing, for example when testing some other functionality, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":" command will let you do this. You could, for instance, write a function which does nothing:"}]},{"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":"testing"}]},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This function would simply return immediately. Leaving the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":" command out, i.e. having a completely empty block, would produce an error."}]},{"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":"testing"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# this causes an error!"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"loops-with-else-blocks","style":"position:relative;"},"children":[{"type":"text","value":"Loops with else blocks"},{"type":"element","tagName":"a","properties":{"href":"#loops-with-else-blocks","ariaLabel":"loops with else blocks 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 Python, loops can have "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" blocks, too. This section of code is executed if the loop finishes normally."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, in the following example we are looking through a list of numbers. If there is an even number on the list, the program prints out a message and the loop is broken. If there are no even numbers, the loop finishes normally, but a different message is then printed out."}]},{"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":"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":"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":"8"}]},{"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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"found an even number\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" x"},{"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":"break"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"\"there were no even numbers\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A more traditional way to achieve this would be to use a helper variable to remember whether the desired item was found:"}]},{"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":"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":"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":"8"}]},{"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":"text","value":"\nfound "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"found an even number\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n found "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" found"},{"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":"\"there were no even numbers\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Using a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for else"}]},{"type":"text","value":" statement saves us the trouble of writing a separate variable."}]},{"type":"element","tagName":"h2","properties":{"id":"default-parameter-value","style":"position:relative;"},"children":[{"type":"text","value":"Default parameter value"},{"type":"element","tagName":"a","properties":{"href":"#default-parameter-value","ariaLabel":"default parameter value 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":"A Python function can have a default parameter value. It is used whenever no argument is passed to the function. See the following example:"}]},{"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":"say_hello"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"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":"\"Hi there,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsay_hello"},{"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":"\nsay_hello"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsay_hello"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Matthew\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsay_hello"},{"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":"Hi there, Emily\nHi there, Eric\nHi there, Matthew\nHi there,"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: an empty string is still a string, so the default parameter is not used if an empty string is passed to the function."}]},{"type":"element","tagName":"h2","properties":{"id":"a-variable-number-of-parameters","style":"position:relative;"},"children":[{"type":"text","value":"A variable number of parameters"},{"type":"element","tagName":"a","properties":{"href":"#a-variable-number-of-parameters","ariaLabel":"a variable number of parameters 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":"You can also define a function with a variable number of parameters, by adding a star before the parameter name. All the remaining arguments passed to the function are contained in a tuple, and can be accessed through the named parameter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following function counts the number and sum of its arguments:"}]},{"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":"testing"}]},{"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":"text","value":"my_args"},{"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":"\"You passed\""}]},{"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":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_args"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"arguments\""}]},{"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 sum of the arguments is\""}]},{"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":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_args"},{"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\ntesting"},{"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":"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":" "},{"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":" "},{"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":"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You passed 5 arguments\nThe sum of the arguments is 15"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Your own programming language","tmcname":"part07-18_own_programming_language"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will write your own programming language executor. You can use any techniques and skills you have learnt on this course thus far."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The programs will consist of rows, and each row will be in one of the following formats:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PRINT [value]"}]},{"type":"text","value":": prints the value"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MOV [variable] [value]"}]},{"type":"text","value":": assigns the value to the variable"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ADD [variable] [value]"}]},{"type":"text","value":": adds the value to the variable"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SUB [variable] [value]"}]},{"type":"text","value":": subtracts the value from the variable"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MUL [variable] [value]"}]},{"type":"text","value":": multiplies the variable by the value"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[location]:"}]},{"type":"text","value":": names a line of code, so it can be jumped to from elsewhere"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"JUMP [location]"}]},{"type":"text","value":": jumps to the location specified"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"IF [condition] JUMP [location]"}]},{"type":"text","value":": if the condition is true, jump to the location specified"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"END"}]},{"type":"text","value":": finish execution"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The square brackets above are just a notation to signify operands; see below for usage examples."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program is executed line by line from the first line onwards. The execution ends when the executor comes across the command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"END"}]},{"type":"text","value":", or when there are no more lines to execute."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each program has 26 pre-defined variables, named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"A"}]},{"type":"text","value":" to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Z"}]},{"type":"text","value":". Each variable has the value 0 when the program begins. The notation "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[variable]"}]},{"type":"text","value":" refers to one of these 26 variables."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"All the values processed by the program are integer numbers. The notation "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[value]"}]},{"type":"text","value":" refers either to a value stored in a variable, or an integer number typed in directly."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The notation "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[location]"}]},{"type":"text","value":" refers to any name of a location which consists of lowercase letters "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"z"}]},{"type":"text","value":" and/or numbers "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"9"}]},{"type":"text","value":". Two different locations may not have the same name."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The notation "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[condition]"}]},{"type":"text","value":" refers to any expression in the format "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[value] [comparison] [value]"}]},{"type":"text","value":", where "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[comparison]"}]},{"type":"text","value":" is one of the following operators: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":"."}]},{"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":"run(program)"}]},{"type":"text","value":", which takes a list containing the program commands as its argument. Each item on the list is a line of code in the program. The function should return a new list, which contains the results of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PRINT"}]},{"type":"text","value":" commands executed during the program's run."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume the function will only be given programs which are entirely in the correct format. You do not have to implement any input validation or error handling."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This exercise is worth two points. You will receive one point if the commands "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PRINT"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MOV"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ADD"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SUB"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MUL"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"END"}]},{"type":"text","value":" are working correctly. You will receice another point if the rest of the commands, which are used to implement loops, also work."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Below are some examples, which you may also use for testing. Example 1:"}]},{"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":"program1 "},{"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":"\nprogram1"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV A 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV B 2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD A B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"END\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" run"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"program1"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 3]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Example 2:"}]},{"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":"program2 "},{"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":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV A 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV B 10\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"begin:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF A >= B JUMP quit\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD A 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"SUB B 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"JUMP begin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"quit:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"END\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" run"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"program2"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 10, 2, 9, 3, 8, 4, 7, 5, 6]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Example 3 (factorial):"}]},{"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":"program3 "},{"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":"\nprogram3"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV A 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV B 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"begin:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD B 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MUL A B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF B <= 10 JUMP begin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"END\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" run"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"program3"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Example 4 (prime numbers):"}]},{"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":"program4 "},{"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":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV N 50\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT 2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV A 3\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"begin:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV B 2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV Z 0\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"test:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV C B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"new:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF C == A JUMP error\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF C > A JUMP over\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD C B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"JUMP new\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"error:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV Z 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"JUMP over2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"over:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD B 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF B < A JUMP test\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"over2:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF Z == 1 JUMP over3\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"over3:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD A 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF A <= N JUMP begin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" run"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"program4"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to the course feedback questionnaire. The questionnaire results help us improve the course."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will be familiar with some more Python features
    • \n

    To finish off this course, here you will find various useful Python features.

    Single line conditionals

    The following two statements produce the exact same results:

    if x%2 == 0:\n    print(\"even\")\nelse:\n    print(\"odd\")
    print(\"even\" if x%2 == 0 else \"odd\")

    In the latter example we have a conditional expression on a single line: a if [condition] else b. The value of this expression evaluates to a if the condition is true, and b if it is false. This structure is sometimes referred to as a ternary operator.

    Conditional expressions can be very useful when you need to assign something conditionally. For example, if you had the variables x and y, and you wanted to either increment or set the value of y depending on the parity of x, you could write it in a normal if else statement, like so:

    if x%2 == 0:\n    y += 1\nelse:\n    y = 0

    The same could be achieved with a nifty one-liner:

    y = y + 1 if x%2 == 0 else 0

    An \"empty\" block

    You may remember from the previous part that you are not allowed to have an empty block in a Python program. If you need to have a block of code which does nothing, for example when testing some other functionality, the pass command will let you do this. You could, for instance, write a function which does nothing:

    def testing():\n    pass

    This function would simply return immediately. Leaving the pass command out, i.e. having a completely empty block, would produce an error.

    def testing():  # this causes an error!

    Loops with else blocks

    In Python, loops can have else blocks, too. This section of code is executed if the loop finishes normally.

    For example, in the following example we are looking through a list of numbers. If there is an even number on the list, the program prints out a message and the loop is broken. If there are no even numbers, the loop finishes normally, but a different message is then printed out.

    my_list = [3,5,2,8,1]\nfor x in my_list:\n    if x%2 == 0:\n        print(\"found an even number\", x)\n        break\nelse:\n    print(\"there were no even numbers\")

    A more traditional way to achieve this would be to use a helper variable to remember whether the desired item was found:

    my_list = [3,5,2,8,1]\nfound = False\nfor x in my_list:\n    if x%2 == 0:\n        print(\"found an even number\", x)\n        found = True\n        break\nif not found:\n    print(\"there were no even numbers\")

    Using a for else statement saves us the trouble of writing a separate variable.

    Default parameter value

    A Python function can have a default parameter value. It is used whenever no argument is passed to the function. See the following example:

    def say_hello(name=\"Emily\"):\n    print(\"Hi there,\", name)\n\nsay_hello()\nsay_hello(\"Eric\")\nsay_hello(\"Matthew\")\nsay_hello(\"\")

    Hi there, Emily\nHi there, Eric\nHi there, Matthew\nHi there,

    NB: an empty string is still a string, so the default parameter is not used if an empty string is passed to the function.

    A variable number of parameters

    You can also define a function with a variable number of parameters, by adding a star before the parameter name. All the remaining arguments passed to the function are contained in a tuple, and can be accessed through the named parameter.

    The following function counts the number and sum of its arguments:

    def testing(*my_args):\n    print(\"You passed\", len(my_args), \"arguments\")\n    print(\"The sum of the arguments is\", sum(my_args))\n\ntesting(1, 2, 3, 4, 5)

    You passed 5 arguments\nThe sum of the arguments is 15

    In this exercise you will write your own programming language executor. You can use any techniques and skills you have learnt on this course thus far.

    The programs will consist of rows, and each row will be in one of the following formats:

      \n
    • PRINT [value]: prints the value
    • \n
    • MOV [variable] [value]: assigns the value to the variable
    • \n
    • ADD [variable] [value]: adds the value to the variable
    • \n
    • SUB [variable] [value]: subtracts the value from the variable
    • \n
    • MUL [variable] [value]: multiplies the variable by the value
    • \n
    • [location]:: names a line of code, so it can be jumped to from elsewhere
    • \n
    • JUMP [location]: jumps to the location specified
    • \n
    • IF [condition] JUMP [location]: if the condition is true, jump to the location specified
    • \n
    • END: finish execution
    • \n

    The square brackets above are just a notation to signify operands; see below for usage examples.

    The program is executed line by line from the first line onwards. The execution ends when the executor comes across the command END, or when there are no more lines to execute.

    Each program has 26 pre-defined variables, named A to Z. Each variable has the value 0 when the program begins. The notation [variable] refers to one of these 26 variables.

    All the values processed by the program are integer numbers. The notation [value] refers either to a value stored in a variable, or an integer number typed in directly.

    The notation [location] refers to any name of a location which consists of lowercase letters a to z and/or numbers 0 to 9. Two different locations may not have the same name.

    The notation [condition] refers to any expression in the format [value] [comparison] [value], where [comparison] is one of the following operators: ==, !=, <, <=, > and >=.

    Please write a function named run(program), which takes a list containing the program commands as its argument. Each item on the list is a line of code in the program. The function should return a new list, which contains the results of the PRINT commands executed during the program's run.

    You may assume the function will only be given programs which are entirely in the correct format. You do not have to implement any input validation or error handling.

    This exercise is worth two points. You will receive one point if the commands PRINT, MOV, ADD, SUB, MUL and END are working correctly. You will receice another point if the rest of the commands, which are used to implement loops, also work.

    Below are some examples, which you may also use for testing. Example 1:

    program1 = []\nprogram1.append(\"MOV A 1\")\nprogram1.append(\"MOV B 2\")\nprogram1.append(\"PRINT A\")\nprogram1.append(\"PRINT B\")\nprogram1.append(\"ADD A B\")\nprogram1.append(\"PRINT A\")\nprogram1.append(\"END\")\nresult = run(program1)\nprint(result)

    [1, 2, 3]

    Example 2:

    program2 = []\nprogram2.append(\"MOV A 1\")\nprogram2.append(\"MOV B 10\")\nprogram2.append(\"begin:\")\nprogram2.append(\"IF A >= B JUMP quit\")\nprogram2.append(\"PRINT A\")\nprogram2.append(\"PRINT B\")\nprogram2.append(\"ADD A 1\")\nprogram2.append(\"SUB B 1\")\nprogram2.append(\"JUMP begin\")\nprogram2.append(\"quit:\")\nprogram2.append(\"END\")\nresult = run(program2)\nprint(result)

    [1, 10, 2, 9, 3, 8, 4, 7, 5, 6]

    Example 3 (factorial):

    program3 = []\nprogram3.append(\"MOV A 1\")\nprogram3.append(\"MOV B 1\")\nprogram3.append(\"begin:\")\nprogram3.append(\"PRINT A\")\nprogram3.append(\"ADD B 1\")\nprogram3.append(\"MUL A B\")\nprogram3.append(\"IF B <= 10 JUMP begin\")\nprogram3.append(\"END\")\nresult = run(program3)\nprint(result)

    [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]

    Example 4 (prime numbers):

    program4 = []\nprogram4.append(\"MOV N 50\")\nprogram4.append(\"PRINT 2\")\nprogram4.append(\"MOV A 3\")\nprogram4.append(\"begin:\")\nprogram4.append(\"MOV B 2\")\nprogram4.append(\"MOV Z 0\")\nprogram4.append(\"test:\")\nprogram4.append(\"MOV C B\")\nprogram4.append(\"new:\")\nprogram4.append(\"IF C == A JUMP error\")\nprogram4.append(\"IF C > A JUMP over\")\nprogram4.append(\"ADD C B\")\nprogram4.append(\"JUMP new\")\nprogram4.append(\"error:\")\nprogram4.append(\"MOV Z 1\")\nprogram4.append(\"JUMP over2\")\nprogram4.append(\"over:\")\nprogram4.append(\"ADD B 1\")\nprogram4.append(\"IF B < A JUMP test\")\nprogram4.append(\"over2:\")\nprogram4.append(\"IF Z == 1 JUMP over3\")\nprogram4.append(\"PRINT A\")\nprogram4.append(\"over3:\")\nprogram4.append(\"ADD A 1\")\nprogram4.append(\"IF A <= N JUMP begin\")\nresult = run(program4)\nprint(result)

    [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

    Please respond to the course feedback questionnaire. The questionnaire results help us improve the course.

    ","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-7/6-more-features.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 some more Python features"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To finish off this course, here you will find various useful Python features."}]},{"type":"element","tagName":"h2","properties":{"id":"single-line-conditionals","style":"position:relative;"},"children":[{"type":"text","value":"Single line conditionals"},{"type":"element","tagName":"a","properties":{"href":"#single-line-conditionals","ariaLabel":"single line conditionals 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 following two statements produce the exact same results:"}]},{"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":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"even\""}]},{"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":"else"}]},{"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":"\"odd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"\"even\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"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":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"odd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the latter example we have a conditional expression on a single line: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a if [condition] else b"}]},{"type":"text","value":". The value of this expression evaluates to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" if the condition is true, and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"b"}]},{"type":"text","value":" if it is false. This structure is sometimes referred to as a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"ternary operator"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Conditional expressions can be very useful when you need to assign something "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"conditionally"}]},{"type":"text","value":". For example, if you had the variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":", and you wanted to either increment or set the value of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"y"}]},{"type":"text","value":" depending on the parity of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"x"}]},{"type":"text","value":", you could write it in a normal "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if else"}]},{"type":"text","value":" statement, like so:"}]},{"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":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n y "},{"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":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n y "},{"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":"p","properties":{},"children":[{"type":"text","value":"The same could be achieved with a nifty one-liner:"}]},{"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":"y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" y "},{"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","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"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":"0"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"0"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"an-empty-block","style":"position:relative;"},"children":[{"type":"text","value":"An \"empty\" block"},{"type":"element","tagName":"a","properties":{"href":"#an-empty-block","ariaLabel":"an empty block 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":"You may remember from the previous part that you are not allowed to have an empty block in a Python program. If you need to have a block of code which does nothing, for example when testing some other functionality, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":" command will let you do this. You could, for instance, write a function which does nothing:"}]},{"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":"testing"}]},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"pass"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This function would simply return immediately. Leaving the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pass"}]},{"type":"text","value":" command out, i.e. having a completely empty block, would produce an error."}]},{"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":"testing"}]},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# this causes an error!"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"loops-with-else-blocks","style":"position:relative;"},"children":[{"type":"text","value":"Loops with else blocks"},{"type":"element","tagName":"a","properties":{"href":"#loops-with-else-blocks","ariaLabel":"loops with else blocks 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 Python, loops can have "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"else"}]},{"type":"text","value":" blocks, too. This section of code is executed if the loop finishes normally."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, in the following example we are looking through a list of numbers. If there is an even number on the list, the program prints out a message and the loop is broken. If there are no even numbers, the loop finishes normally, but a different message is then printed out."}]},{"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":"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":"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":"8"}]},{"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":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"found an even number\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" x"},{"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":"break"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"\"there were no even numbers\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A more traditional way to achieve this would be to use a helper variable to remember whether the desired item was found:"}]},{"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":"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":"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":"8"}]},{"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":"text","value":"\nfound "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" x "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"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":"if"}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"%"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2"}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"found an even number\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n found "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"break"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" found"},{"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":"\"there were no even numbers\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Using a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"for else"}]},{"type":"text","value":" statement saves us the trouble of writing a separate variable."}]},{"type":"element","tagName":"h2","properties":{"id":"default-parameter-value","style":"position:relative;"},"children":[{"type":"text","value":"Default parameter value"},{"type":"element","tagName":"a","properties":{"href":"#default-parameter-value","ariaLabel":"default parameter value 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":"A Python function can have a default parameter value. It is used whenever no argument is passed to the function. See the following example:"}]},{"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":"say_hello"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"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":"\"Hi there,\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsay_hello"},{"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":"\nsay_hello"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsay_hello"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Matthew\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsay_hello"},{"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":"Hi there, Emily\nHi there, Eric\nHi there, Matthew\nHi there,"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: an empty string is still a string, so the default parameter is not used if an empty string is passed to the function."}]},{"type":"element","tagName":"h2","properties":{"id":"a-variable-number-of-parameters","style":"position:relative;"},"children":[{"type":"text","value":"A variable number of parameters"},{"type":"element","tagName":"a","properties":{"href":"#a-variable-number-of-parameters","ariaLabel":"a variable number of parameters 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":"You can also define a function with a variable number of parameters, by adding a star before the parameter name. All the remaining arguments passed to the function are contained in a tuple, and can be accessed through the named parameter."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following function counts the number and sum of its arguments:"}]},{"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":"testing"}]},{"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":"text","value":"my_args"},{"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":"\"You passed\""}]},{"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":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_args"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"arguments\""}]},{"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 sum of the arguments is\""}]},{"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":"sum"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_args"},{"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\ntesting"},{"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":"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":" "},{"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":" "},{"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":"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You passed 5 arguments\nThe sum of the arguments is 15"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Your own programming language","tmcname":"part07-18_own_programming_language"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will write your own programming language executor. You can use any techniques and skills you have learnt on this course thus far."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The programs will consist of rows, and each row will be in one of the following formats:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PRINT [value]"}]},{"type":"text","value":": prints the value"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MOV [variable] [value]"}]},{"type":"text","value":": assigns the value to the variable"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ADD [variable] [value]"}]},{"type":"text","value":": adds the value to the variable"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SUB [variable] [value]"}]},{"type":"text","value":": subtracts the value from the variable"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MUL [variable] [value]"}]},{"type":"text","value":": multiplies the variable by the value"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[location]:"}]},{"type":"text","value":": names a line of code, so it can be jumped to from elsewhere"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"JUMP [location]"}]},{"type":"text","value":": jumps to the location specified"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"IF [condition] JUMP [location]"}]},{"type":"text","value":": if the condition is true, jump to the location specified"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"END"}]},{"type":"text","value":": finish execution"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The square brackets above are just a notation to signify operands; see below for usage examples."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The program is executed line by line from the first line onwards. The execution ends when the executor comes across the command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"END"}]},{"type":"text","value":", or when there are no more lines to execute."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each program has 26 pre-defined variables, named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"A"}]},{"type":"text","value":" to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Z"}]},{"type":"text","value":". Each variable has the value 0 when the program begins. The notation "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[variable]"}]},{"type":"text","value":" refers to one of these 26 variables."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"All the values processed by the program are integer numbers. The notation "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[value]"}]},{"type":"text","value":" refers either to a value stored in a variable, or an integer number typed in directly."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The notation "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[location]"}]},{"type":"text","value":" refers to any name of a location which consists of lowercase letters "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a"}]},{"type":"text","value":" to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"z"}]},{"type":"text","value":" and/or numbers "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"0"}]},{"type":"text","value":" to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"9"}]},{"type":"text","value":". Two different locations may not have the same name."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The notation "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[condition]"}]},{"type":"text","value":" refers to any expression in the format "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[value] [comparison] [value]"}]},{"type":"text","value":", where "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"[comparison]"}]},{"type":"text","value":" is one of the following operators: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"!="}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":"."}]},{"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":"run(program)"}]},{"type":"text","value":", which takes a list containing the program commands as its argument. Each item on the list is a line of code in the program. The function should return a new list, which contains the results of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PRINT"}]},{"type":"text","value":" commands executed during the program's run."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume the function will only be given programs which are entirely in the correct format. You do not have to implement any input validation or error handling."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This exercise is worth two points. You will receive one point if the commands "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PRINT"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MOV"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ADD"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SUB"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"MUL"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"END"}]},{"type":"text","value":" are working correctly. You will receice another point if the rest of the commands, which are used to implement loops, also work."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Below are some examples, which you may also use for testing. Example 1:"}]},{"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":"program1 "},{"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":"\nprogram1"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV A 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV B 2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD A B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram1"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"END\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" run"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"program1"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 3]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Example 2:"}]},{"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":"program2 "},{"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":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV A 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV B 10\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"begin:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF A >= B JUMP quit\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD A 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"SUB B 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"JUMP begin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"quit:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram2"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"END\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" run"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"program2"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 10, 2, 9, 3, 8, 4, 7, 5, 6]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Example 3 (factorial):"}]},{"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":"program3 "},{"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":"\nprogram3"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV A 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV B 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"begin:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD B 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MUL A B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF B <= 10 JUMP begin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram3"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"END\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" run"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"program3"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Example 4 (prime numbers):"}]},{"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":"program4 "},{"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":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV N 50\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT 2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV A 3\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"begin:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV B 2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV Z 0\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"test:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV C B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"new:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF C == A JUMP error\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF C > A JUMP over\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD C B\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"JUMP new\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"error:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"MOV Z 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"JUMP over2\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"over:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD B 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF B < A JUMP test\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"over2:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF Z == 1 JUMP over3\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"PRINT A\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"over3:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ADD A 1\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nprogram4"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"IF A <= N JUMP begin\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" run"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"program4"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to the course feedback questionnaire. The questionnaire results help us improve the course."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will be familiar with some more Python features
    • \n

    To finish off this course, here you will find various useful Python features.

    Single line conditionals

    The following two statements produce the exact same results:

    if x%2 == 0:\n    print(\"even\")\nelse:\n    print(\"odd\")
    print(\"even\" if x%2 == 0 else \"odd\")

    In the latter example we have a conditional expression on a single line: a if [condition] else b. The value of this expression evaluates to a if the condition is true, and b if it is false. This structure is sometimes referred to as a ternary operator.

    Conditional expressions can be very useful when you need to assign something conditionally. For example, if you had the variables x and y, and you wanted to either increment or set the value of y depending on the parity of x, you could write it in a normal if else statement, like so:

    if x%2 == 0:\n    y += 1\nelse:\n    y = 0

    The same could be achieved with a nifty one-liner:

    y = y + 1 if x%2 == 0 else 0

    An \"empty\" block

    You may remember from the previous part that you are not allowed to have an empty block in a Python program. If you need to have a block of code which does nothing, for example when testing some other functionality, the pass command will let you do this. You could, for instance, write a function which does nothing:

    def testing():\n    pass

    This function would simply return immediately. Leaving the pass command out, i.e. having a completely empty block, would produce an error.

    def testing():  # this causes an error!

    Loops with else blocks

    In Python, loops can have else blocks, too. This section of code is executed if the loop finishes normally.

    For example, in the following example we are looking through a list of numbers. If there is an even number on the list, the program prints out a message and the loop is broken. If there are no even numbers, the loop finishes normally, but a different message is then printed out.

    my_list = [3,5,2,8,1]\nfor x in my_list:\n    if x%2 == 0:\n        print(\"found an even number\", x)\n        break\nelse:\n    print(\"there were no even numbers\")

    A more traditional way to achieve this would be to use a helper variable to remember whether the desired item was found:

    my_list = [3,5,2,8,1]\nfound = False\nfor x in my_list:\n    if x%2 == 0:\n        print(\"found an even number\", x)\n        found = True\n        break\nif not found:\n    print(\"there were no even numbers\")

    Using a for else statement saves us the trouble of writing a separate variable.

    Default parameter value

    A Python function can have a default parameter value. It is used whenever no argument is passed to the function. See the following example:

    def say_hello(name=\"Emily\"):\n    print(\"Hi there,\", name)\n\nsay_hello()\nsay_hello(\"Eric\")\nsay_hello(\"Matthew\")\nsay_hello(\"\")

    Hi there, Emily\nHi there, Eric\nHi there, Matthew\nHi there,

    NB: an empty string is still a string, so the default parameter is not used if an empty string is passed to the function.

    A variable number of parameters

    You can also define a function with a variable number of parameters, by adding a star before the parameter name. All the remaining arguments passed to the function are contained in a tuple, and can be accessed through the named parameter.

    The following function counts the number and sum of its arguments:

    def testing(*my_args):\n    print(\"You passed\", len(my_args), \"arguments\")\n    print(\"The sum of the arguments is\", sum(my_args))\n\ntesting(1, 2, 3, 4, 5)

    You passed 5 arguments\nThe sum of the arguments is 15

    In this exercise you will write your own programming language executor. You can use any techniques and skills you have learnt on this course thus far.

    The programs will consist of rows, and each row will be in one of the following formats:

      \n
    • PRINT [value]: prints the value
    • \n
    • MOV [variable] [value]: assigns the value to the variable
    • \n
    • ADD [variable] [value]: adds the value to the variable
    • \n
    • SUB [variable] [value]: subtracts the value from the variable
    • \n
    • MUL [variable] [value]: multiplies the variable by the value
    • \n
    • [location]:: names a line of code, so it can be jumped to from elsewhere
    • \n
    • JUMP [location]: jumps to the location specified
    • \n
    • IF [condition] JUMP [location]: if the condition is true, jump to the location specified
    • \n
    • END: finish execution
    • \n

    The square brackets above are just a notation to signify operands; see below for usage examples.

    The program is executed line by line from the first line onwards. The execution ends when the executor comes across the command END, or when there are no more lines to execute.

    Each program has 26 pre-defined variables, named A to Z. Each variable has the value 0 when the program begins. The notation [variable] refers to one of these 26 variables.

    All the values processed by the program are integer numbers. The notation [value] refers either to a value stored in a variable, or an integer number typed in directly.

    The notation [location] refers to any name of a location which consists of lowercase letters a to z and/or numbers 0 to 9. Two different locations may not have the same name.

    The notation [condition] refers to any expression in the format [value] [comparison] [value], where [comparison] is one of the following operators: ==, !=, <, <=, > and >=.

    Please write a function named run(program), which takes a list containing the program commands as its argument. Each item on the list is a line of code in the program. The function should return a new list, which contains the results of the PRINT commands executed during the program's run.

    You may assume the function will only be given programs which are entirely in the correct format. You do not have to implement any input validation or error handling.

    This exercise is worth two points. You will receive one point if the commands PRINT, MOV, ADD, SUB, MUL and END are working correctly. You will receice another point if the rest of the commands, which are used to implement loops, also work.

    Below are some examples, which you may also use for testing. Example 1:

    program1 = []\nprogram1.append(\"MOV A 1\")\nprogram1.append(\"MOV B 2\")\nprogram1.append(\"PRINT A\")\nprogram1.append(\"PRINT B\")\nprogram1.append(\"ADD A B\")\nprogram1.append(\"PRINT A\")\nprogram1.append(\"END\")\nresult = run(program1)\nprint(result)

    [1, 2, 3]

    Example 2:

    program2 = []\nprogram2.append(\"MOV A 1\")\nprogram2.append(\"MOV B 10\")\nprogram2.append(\"begin:\")\nprogram2.append(\"IF A >= B JUMP quit\")\nprogram2.append(\"PRINT A\")\nprogram2.append(\"PRINT B\")\nprogram2.append(\"ADD A 1\")\nprogram2.append(\"SUB B 1\")\nprogram2.append(\"JUMP begin\")\nprogram2.append(\"quit:\")\nprogram2.append(\"END\")\nresult = run(program2)\nprint(result)

    [1, 10, 2, 9, 3, 8, 4, 7, 5, 6]

    Example 3 (factorial):

    program3 = []\nprogram3.append(\"MOV A 1\")\nprogram3.append(\"MOV B 1\")\nprogram3.append(\"begin:\")\nprogram3.append(\"PRINT A\")\nprogram3.append(\"ADD B 1\")\nprogram3.append(\"MUL A B\")\nprogram3.append(\"IF B <= 10 JUMP begin\")\nprogram3.append(\"END\")\nresult = run(program3)\nprint(result)

    [1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800]

    Example 4 (prime numbers):

    program4 = []\nprogram4.append(\"MOV N 50\")\nprogram4.append(\"PRINT 2\")\nprogram4.append(\"MOV A 3\")\nprogram4.append(\"begin:\")\nprogram4.append(\"MOV B 2\")\nprogram4.append(\"MOV Z 0\")\nprogram4.append(\"test:\")\nprogram4.append(\"MOV C B\")\nprogram4.append(\"new:\")\nprogram4.append(\"IF C == A JUMP error\")\nprogram4.append(\"IF C > A JUMP over\")\nprogram4.append(\"ADD C B\")\nprogram4.append(\"JUMP new\")\nprogram4.append(\"error:\")\nprogram4.append(\"MOV Z 1\")\nprogram4.append(\"JUMP over2\")\nprogram4.append(\"over:\")\nprogram4.append(\"ADD B 1\")\nprogram4.append(\"IF B < A JUMP test\")\nprogram4.append(\"over2:\")\nprogram4.append(\"IF Z == 1 JUMP over3\")\nprogram4.append(\"PRINT A\")\nprogram4.append(\"over3:\")\nprogram4.append(\"ADD A 1\")\nprogram4.append(\"IF A <= N JUMP begin\")\nresult = run(program4)\nprint(result)

    [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]

    Please respond to the course feedback questionnaire. The questionnaire results help us improve the course.

    ","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-7/6-more-features.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-7/page-data.json b/page-data/part-7/page-data.json index 12895c2c5..f59cf858c 100644 --- a/page-data/part-7/page-data.json +++ b/page-data/part-7/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/part-7", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-7","title":"Part 7"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-7/index.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-7","title":"Part 7"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-7/index.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-8/1-objects-and-methods/page-data.json b/page-data/part-8/1-objects-and-methods/page-data.json index b929880a6..3dfad5d8b 100644 --- a/page-data/part-8/1-objects-and-methods/page-data.json +++ b/page-data/part-8/1-objects-and-methods/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-8/1-objects-and-methods", - "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 know what an object is in programming"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will understand what is meant by the independence of individual objects"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to create and access objects"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is the first part of the Advanced Course in Programming. The material is designed to be used with the Visual Studio Code editor, just like the preceding Introduction to Programming course was. If you haven't use Visual Studio Code before, you will find the installation instructions "},{"type":"element","tagName":"a","properties":{"href":"https://www.mooc.fi/en/installation/vscode","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"here"}]},{"type":"text","value":", and an introduction to the programming environment from the previous course "},{"type":"element","tagName":"a","properties":{"href":"/part-4/1-vscode"},"children":[{"type":"text","value":"here"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the Introduction to Programming course we noticed that it often makes sense to group related data together in our programs. For example, if we are trying to store information about a book, it might make sense to use a tuple or a dictionary to organize the data into a single data structure."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The solution could look like this when using a tuple:"}]},{"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":"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":"\"In Search of Lost Typing\""}]},{"type":"text","value":"\nauthor "},{"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":"\"Marcel Pythons\""}]},{"type":"text","value":"\nyear "},{"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":"1992"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Combine these in a tuple"}]},{"type":"text","value":"\nbook "},{"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":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" author"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" year"},{"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":"# Print the name of the book"}]},{"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":"book"},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In a case like this, the advantage of using a dictionary is that we can use strings instead of indexes as the keys. That is, we can give descriptive names to the items stored in the data structure:"}]},{"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":"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":"\"In Search of Lost Typing\""}]},{"type":"text","value":"\nauthor "},{"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":"\"Marcel Pythons\""}]},{"type":"text","value":"\nyear "},{"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":"1992"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Combine these in a dictionary"}]},{"type":"text","value":"\nbook "},{"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":"\"name\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" name"},{"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":"\"author\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" author"},{"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":"\"year\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" year"},{"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":"# Print the name of the book"}]},{"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":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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":"In both cases we are creating a new "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"object"}]},{"type":"text","value":". In programming, the term has the specific meaning of an independent whole, in this case containing some bits of data which are somehow related. Being independent means that any changes made to one object will not affect other objects."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we were to create two structurally identical representations of books, using dictionaries with identical keys, any changes made to one of them would not have any effect on the other:"}]},{"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":"book1 "},{"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":"\"name\""}]},{"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":"\"The Old Man and the Pythons\""}]},{"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":"\"author\""}]},{"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":"\"Ernest Pythons\""}]},{"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":"\"year\""}]},{"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":"1952"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nbook2 "},{"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":"\"name\""}]},{"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":"\"Seven Pythons\""}]},{"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":"\"author\""}]},{"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":"\"Aleksis 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":"\"year\""}]},{"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":"1894"}]},{"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":"book1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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":"book2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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\nbook1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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 Farewell to ARM Processors\""}]},{"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":"book1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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":"book2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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":"The Old Man and the Pythons\nSeven Pythons\nA Farewell to ARM Processors\nSeven Pythons"}]}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 381px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/1f7ff83eb36468673c6071ee6c67b5b0/2add2/8_1_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: 40.869565217391305%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA/klEQVQoz4VS1wqEQAz0/7/MJxVFxK7Yexd7jgRWroAXGLK7JJNJshw82DzPUBQFpGkKZVlCnueEJEnIZ1kGcRzTPQxDeuMwEJPGcYSu66Dve/LDMIDv+5QkiiKdTdMEVVVBEASQZRkMwyAyjAmCANq2BQ6TEed53jiOgxTWdU3qFEWBKIpA0zRomoaUY+FlWX664p5aXtcVqqoCVhTJUDkDkqKqd/wlZCRsHGwkzH+D2/cdtm37afm6rluV67q0FBy853l0t22b1KNh7N0yzsZxnA8V6HFJ+I4kPM+DZVkgSRItBUl1XadlMEKGx5Zx6FgAlUzTRN/i297VocIXJqtkWFpF9p0AAAAASUVORK5CYII='); 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":["/static/1f7ff83eb36468673c6071ee6c67b5b0/a0b58/8_1_1.webp 230w","/static/1f7ff83eb36468673c6071ee6c67b5b0/25250/8_1_1.webp 381w"],"sizes":"(max-width: 381px) 100vw, 381px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/1f7ff83eb36468673c6071ee6c67b5b0/81c8e/8_1_1.png 230w","/static/1f7ff83eb36468673c6071ee6c67b5b0/2add2/8_1_1.png 381w"],"sizes":"(max-width: 381px) 100vw, 381px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/1f7ff83eb36468673c6071ee6c67b5b0/2add2/8_1_1.png","alt":"8 1 1","title":"8 1 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":"text-box","properties":{"variant":"info","name":"Python objects"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may remember from the Introduction to Programming course that any value in Python is internally handled as an object. This means that the value stored in a variable is a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reference to an object"}]},{"type":"text","value":". The data itself is stored within the object in computer memory. If you assign a value to a new variable with the command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a = 3"}]},{"type":"text","value":", the value stored in the variable is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" 3, but a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reference to an object which contains the value 3"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Most other programming languages (at least those which support object-oriented programming) include some specially defined "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"primitive data types"}]},{"type":"text","value":". These usually include at least integer numbers, floating point numbers and Boolean truth values. Primitives are processed directly, meaning that they are stored directly in variables, not as references. Python has no such primitives, but working with the basic data types in Python is practically very similar. Objects of these basic data types (such as numbers, Boolean values and strings) are "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"immutable"}]},{"type":"text","value":", meaning that they cannot be changed in memory. If the value stored in a variable of a basic data type needs to be changed, the entire reference is replaced, but the object itself remains intact in memory."}]}]},{"type":"element","tagName":"h2","properties":{"id":"objects-and-methods","style":"position:relative;"},"children":[{"type":"text","value":"Objects and methods"},{"type":"element","tagName":"a","properties":{"href":"#objects-and-methods","ariaLabel":"objects and methods 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 data stored in an object can be accessed through "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"methods"}]},{"type":"text","value":". A method is a function which operates on the specific object it is attached to. The way to distinguish methods from other functions is the way in which they are called: first you write the name of the object targeted, followed by a dot, and then the name of the method, with arguments if any. For example, the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"values"}]},{"type":"text","value":" returns all the values stored in an object of type dictionary, or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dict"}]},{"type":"text","value":":"}]},{"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":"# this creates an object of type dictionary with the name book"}]},{"type":"text","value":"\nbook "},{"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":"\"name\""}]},{"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":"\"The Old Man and the Pythons\""}]},{"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":"\"author\""}]},{"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":"\"Ernest Pythons\""}]},{"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":"\"year\""}]},{"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":"1952"}]},{"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":"# Print out all the values"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# The method call values() is written after the name of the variable"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Remember the dot notation!"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" value "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"values"},{"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 "},{"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":"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":"The Old Man and the Pythons\nErnest Pythons\n1952"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Similarly, string methods target the string object which they are called on. Some examples of string methods include "},{"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":"find"}]},{"type":"text","value":":"}]},{"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":"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":"\"Imaginary Irene\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Print out the number of times the letter I is found"}]},{"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"},{"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":"\"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# The number of letters I found in another string"}]},{"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":"\"Irreverent Irises in Islington\""}]},{"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":"\"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# The index of the substring Irene"}]},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Irene\""}]},{"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","comment"]},"children":[{"type":"text","value":"# This string has no such substring"}]},{"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":"\"A completely different string\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Irene\""}]},{"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":"2\n3\n10\n-1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"String methods return values, but they will not change the contents of a string. As stated above, strings in Python are immutable. This does not apply to all methods, however. Python lists are mutable, so Python list methods may change the contents of the list they are called on:"}]},{"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":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Add a couple of items"}]},{"type":"text","value":"\nmy_list"},{"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":"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":"\nmy_list"},{"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":"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\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":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Remove the first item"}]},{"type":"text","value":"\nmy_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"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":"\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":"my_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":"[1, 2, 3, 5, 1]\n[2, 3, 5, 1]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The smallest average result","tmcname":"part08-01_smallest_average"},"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":"smallest_average(person1: dict, person2: dict, person3: dict)"}]},{"type":"text","value":", which takes three dictionary objects as its arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each dictionary object contains values mapped to the following keys:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"name\""}]},{"type":"text","value":": The name of the contestant"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"result1\""}]},{"type":"text","value":": the first result of the contestant (an integer between 1 and 10)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"result2\""}]},{"type":"text","value":": the second result of the contestant (as above)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"result3\""}]},{"type":"text","value":": the third result of the contestant (as above)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should calculate the average of the three results for each contestant, and then return the contestant whose average result was the smallest. The return value should be the entire dictionary object containing the contestant's information."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume there will be no ties, i.e. a single contestant will have the smallest average result."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"person1 "},{"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":"\"name\""}]},{"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":"\"Mary\""}]},{"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":"\"result1\""}]},{"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":"2"}]},{"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":"\"result2\""}]},{"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":"3"}]},{"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":"\"result3\""}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nperson2 "},{"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":"\"name\""}]},{"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":"\"Gary\""}]},{"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":"\"result1\""}]},{"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":"5"}]},{"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":"\"result2\""}]},{"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":"1"}]},{"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":"\"result3\""}]},{"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":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nperson3 "},{"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":"\"name\""}]},{"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":"\"Larry\""}]},{"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":"\"result1\""}]},{"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":"3"}]},{"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":"\"result2\""}]},{"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":"1"}]},{"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":"\"result3\""}]},{"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":"1"}]},{"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":"smallest_average"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person3"},{"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":"{'name': 'Larry', 'result1': 3, 'result2': 1, 'result3': 1}"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Row sums","tmcname":"part08-02_row_sums "},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In Python, every value stored in a variable is a reference to an object, so any value stored in a list is also a reference to an object. This is also true when modelling a matrix data structure: each value in the top level list is a reference to another list, which in turn contains references to the objects representing the elements of the matrix."}]},{"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":"row_sums(my_matrix: list)"}]},{"type":"text","value":", which takes an integer matrix as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should add a new element on each row in the matrix. This element contains the sum of the other elements on that row. The function does not have a return value. It should modify the parameter matrix in place."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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_matrix "},{"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":"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":" "},{"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":" "},{"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":" "},{"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","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nrow_sums"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_matrix"},{"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_matrix"},{"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":"[[1, 2, 3], [3, 4, 7]]"}]}]}]}]},"html":"

    After this section

      \n
    • You will know what an object is in programming
    • \n
    • You will understand what is meant by the independence of individual objects
    • \n
    • You will be able to create and access objects
    • \n

    This is the first part of the Advanced Course in Programming. The material is designed to be used with the Visual Studio Code editor, just like the preceding Introduction to Programming course was. If you haven't use Visual Studio Code before, you will find the installation instructions here, and an introduction to the programming environment from the previous course here.

    In the Introduction to Programming course we noticed that it often makes sense to group related data together in our programs. For example, if we are trying to store information about a book, it might make sense to use a tuple or a dictionary to organize the data into a single data structure.

    The solution could look like this when using a tuple:

    name = \"In Search of Lost Typing\"\nauthor = \"Marcel Pythons\"\nyear = 1992\n\n# Combine these in a tuple\nbook = (name, author, year)\n\n# Print the name of the book\nprint(book[0])

    In a case like this, the advantage of using a dictionary is that we can use strings instead of indexes as the keys. That is, we can give descriptive names to the items stored in the data structure:

    name = \"In Search of Lost Typing\"\nauthor = \"Marcel Pythons\"\nyear = 1992\n\n# Combine these in a dictionary\nbook = {\"name\": name, \"author\": author, \"year\": year}\n\n# Print the name of the book\nprint(book[\"name\"])

    In both cases we are creating a new object. In programming, the term has the specific meaning of an independent whole, in this case containing some bits of data which are somehow related. Being independent means that any changes made to one object will not affect other objects.

    If we were to create two structurally identical representations of books, using dictionaries with identical keys, any changes made to one of them would not have any effect on the other:

    book1 = {\"name\": \"The Old Man and the Pythons\", \"author\": \"Ernest Pythons\", \"year\": 1952}\nbook2 = {\"name\": \"Seven Pythons\", \"author\": \"Aleksis Python\", \"year\": 1894}\n\nprint(book1[\"name\"])\nprint(book2[\"name\"])\n\nbook1[\"name\"] = \"A Farewell to ARM Processors\"\n\nprint(book1[\"name\"])\nprint(book2[\"name\"])

    The Old Man and the Pythons\nSeven Pythons\nA Farewell to ARM Processors\nSeven Pythons

    \n \n

    Please write a function named smallest_average(person1: dict, person2: dict, person3: dict), which takes three dictionary objects as its arguments.

    Each dictionary object contains values mapped to the following keys:

      \n
    • \"name\": The name of the contestant
    • \n
    • \"result1\": the first result of the contestant (an integer between 1 and 10)
    • \n
    • \"result2\": the second result of the contestant (as above)
    • \n
    • \"result3\": the third result of the contestant (as above)
    • \n

    The function should calculate the average of the three results for each contestant, and then return the contestant whose average result was the smallest. The return value should be the entire dictionary object containing the contestant's information.

    You may assume there will be no ties, i.e. a single contestant will have the smallest average result.

    An example of the function in action:

    person1 = {\"name\": \"Mary\", \"result1\": 2, \"result2\": 3, \"result3\": 3}\nperson2 = {\"name\": \"Gary\", \"result1\": 5, \"result2\": 1, \"result3\": 8}\nperson3 = {\"name\": \"Larry\", \"result1\": 3, \"result2\": 1, \"result3\": 1}\n\nprint(smallest_average(person1, person2, person3))

    {'name': 'Larry', 'result1': 3, 'result2': 1, 'result3': 1}

    In Python, every value stored in a variable is a reference to an object, so any value stored in a list is also a reference to an object. This is also true when modelling a matrix data structure: each value in the top level list is a reference to another list, which in turn contains references to the objects representing the elements of the matrix.

    Please write a function named row_sums(my_matrix: list), which takes an integer matrix as its argument.

    The function should add a new element on each row in the matrix. This element contains the sum of the other elements on that row. The function does not have a return value. It should modify the parameter matrix in place.

    An example of the function in action:

    my_matrix = [[1, 2], [3, 4]]\nrow_sums(my_matrix)\nprint(my_matrix)

    [[1, 2, 3], [3, 4, 7]]

    ","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-8/1-objects-and-methods.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know what an object is in programming"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will understand what is meant by the independence of individual objects"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to create and access objects"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This is the first part of the Advanced Course in Programming. The material is designed to be used with the Visual Studio Code editor, just like the preceding Introduction to Programming course was. If you haven't use Visual Studio Code before, you will find the installation instructions "},{"type":"element","tagName":"a","properties":{"href":"https://www.mooc.fi/en/installation/vscode","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"here"}]},{"type":"text","value":", and an introduction to the programming environment from the previous course "},{"type":"element","tagName":"a","properties":{"href":"/part-4/1-vscode"},"children":[{"type":"text","value":"here"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the Introduction to Programming course we noticed that it often makes sense to group related data together in our programs. For example, if we are trying to store information about a book, it might make sense to use a tuple or a dictionary to organize the data into a single data structure."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The solution could look like this when using a tuple:"}]},{"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":"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":"\"In Search of Lost Typing\""}]},{"type":"text","value":"\nauthor "},{"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":"\"Marcel Pythons\""}]},{"type":"text","value":"\nyear "},{"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":"1992"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Combine these in a tuple"}]},{"type":"text","value":"\nbook "},{"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":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" author"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" year"},{"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":"# Print the name of the book"}]},{"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":"book"},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In a case like this, the advantage of using a dictionary is that we can use strings instead of indexes as the keys. That is, we can give descriptive names to the items stored in the data structure:"}]},{"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":"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":"\"In Search of Lost Typing\""}]},{"type":"text","value":"\nauthor "},{"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":"\"Marcel Pythons\""}]},{"type":"text","value":"\nyear "},{"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":"1992"}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Combine these in a dictionary"}]},{"type":"text","value":"\nbook "},{"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":"\"name\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" name"},{"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":"\"author\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" author"},{"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":"\"year\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" year"},{"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":"# Print the name of the book"}]},{"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":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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":"In both cases we are creating a new "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"object"}]},{"type":"text","value":". In programming, the term has the specific meaning of an independent whole, in this case containing some bits of data which are somehow related. Being independent means that any changes made to one object will not affect other objects."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If we were to create two structurally identical representations of books, using dictionaries with identical keys, any changes made to one of them would not have any effect on the other:"}]},{"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":"book1 "},{"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":"\"name\""}]},{"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":"\"The Old Man and the Pythons\""}]},{"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":"\"author\""}]},{"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":"\"Ernest Pythons\""}]},{"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":"\"year\""}]},{"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":"1952"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nbook2 "},{"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":"\"name\""}]},{"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":"\"Seven Pythons\""}]},{"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":"\"author\""}]},{"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":"\"Aleksis 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":"\"year\""}]},{"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":"1894"}]},{"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":"book1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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":"book2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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\nbook1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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 Farewell to ARM Processors\""}]},{"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":"book1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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":"book2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"name\""}]},{"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":"The Old Man and the Pythons\nSeven Pythons\nA Farewell to ARM Processors\nSeven Pythons"}]}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 381px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/1f7ff83eb36468673c6071ee6c67b5b0/2add2/8_1_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: 40.869565217391305%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAICAYAAAD5nd/tAAAACXBIWXMAAAsTAAALEwEAmpwYAAAA/klEQVQoz4VS1wqEQAz0/7/MJxVFxK7Yexd7jgRWroAXGLK7JJNJshw82DzPUBQFpGkKZVlCnueEJEnIZ1kGcRzTPQxDeuMwEJPGcYSu66Dve/LDMIDv+5QkiiKdTdMEVVVBEASQZRkMwyAyjAmCANq2BQ6TEed53jiOgxTWdU3qFEWBKIpA0zRomoaUY+FlWX664p5aXtcVqqoCVhTJUDkDkqKqd/wlZCRsHGwkzH+D2/cdtm37afm6rluV67q0FBy853l0t22b1KNh7N0yzsZxnA8V6HFJ+I4kPM+DZVkgSRItBUl1XadlMEKGx5Zx6FgAlUzTRN/i297VocIXJqtkWFpF9p0AAAAASUVORK5CYII='); 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":["/static/1f7ff83eb36468673c6071ee6c67b5b0/a0b58/8_1_1.webp 230w","/static/1f7ff83eb36468673c6071ee6c67b5b0/25250/8_1_1.webp 381w"],"sizes":"(max-width: 381px) 100vw, 381px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/1f7ff83eb36468673c6071ee6c67b5b0/81c8e/8_1_1.png 230w","/static/1f7ff83eb36468673c6071ee6c67b5b0/2add2/8_1_1.png 381w"],"sizes":"(max-width: 381px) 100vw, 381px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/1f7ff83eb36468673c6071ee6c67b5b0/2add2/8_1_1.png","alt":"8 1 1","title":"8 1 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":"text-box","properties":{"variant":"info","name":"Python objects"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may remember from the Introduction to Programming course that any value in Python is internally handled as an object. This means that the value stored in a variable is a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reference to an object"}]},{"type":"text","value":". The data itself is stored within the object in computer memory. If you assign a value to a new variable with the command "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"a = 3"}]},{"type":"text","value":", the value stored in the variable is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" 3, but a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reference to an object which contains the value 3"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Most other programming languages (at least those which support object-oriented programming) include some specially defined "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"primitive data types"}]},{"type":"text","value":". These usually include at least integer numbers, floating point numbers and Boolean truth values. Primitives are processed directly, meaning that they are stored directly in variables, not as references. Python has no such primitives, but working with the basic data types in Python is practically very similar. Objects of these basic data types (such as numbers, Boolean values and strings) are "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"immutable"}]},{"type":"text","value":", meaning that they cannot be changed in memory. If the value stored in a variable of a basic data type needs to be changed, the entire reference is replaced, but the object itself remains intact in memory."}]}]},{"type":"element","tagName":"h2","properties":{"id":"objects-and-methods","style":"position:relative;"},"children":[{"type":"text","value":"Objects and methods"},{"type":"element","tagName":"a","properties":{"href":"#objects-and-methods","ariaLabel":"objects and methods 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 data stored in an object can be accessed through "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"methods"}]},{"type":"text","value":". A method is a function which operates on the specific object it is attached to. The way to distinguish methods from other functions is the way in which they are called: first you write the name of the object targeted, followed by a dot, and then the name of the method, with arguments if any. For example, the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"values"}]},{"type":"text","value":" returns all the values stored in an object of type dictionary, or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dict"}]},{"type":"text","value":":"}]},{"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":"# this creates an object of type dictionary with the name book"}]},{"type":"text","value":"\nbook "},{"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":"\"name\""}]},{"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":"\"The Old Man and the Pythons\""}]},{"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":"\"author\""}]},{"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":"\"Ernest Pythons\""}]},{"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":"\"year\""}]},{"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":"1952"}]},{"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":"# Print out all the values"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# The method call values() is written after the name of the variable"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Remember the dot notation!"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" value "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"values"},{"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 "},{"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":"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":"The Old Man and the Pythons\nErnest Pythons\n1952"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Similarly, string methods target the string object which they are called on. Some examples of string methods include "},{"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":"find"}]},{"type":"text","value":":"}]},{"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":"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":"\"Imaginary Irene\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Print out the number of times the letter I is found"}]},{"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"},{"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":"\"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# The number of letters I found in another string"}]},{"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":"\"Irreverent Irises in Islington\""}]},{"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":"\"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# The index of the substring Irene"}]},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Irene\""}]},{"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","comment"]},"children":[{"type":"text","value":"# This string has no such substring"}]},{"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":"\"A completely different string\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Irene\""}]},{"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":"2\n3\n10\n-1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"String methods return values, but they will not change the contents of a string. As stated above, strings in Python are immutable. This does not apply to all methods, however. Python lists are mutable, so Python list methods may change the contents of the list they are called on:"}]},{"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":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Add a couple of items"}]},{"type":"text","value":"\nmy_list"},{"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":"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":"\nmy_list"},{"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":"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\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":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Remove the first item"}]},{"type":"text","value":"\nmy_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"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":"\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":"my_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":"[1, 2, 3, 5, 1]\n[2, 3, 5, 1]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The smallest average result","tmcname":"part08-01_smallest_average"},"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":"smallest_average(person1: dict, person2: dict, person3: dict)"}]},{"type":"text","value":", which takes three dictionary objects as its arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each dictionary object contains values mapped to the following keys:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"name\""}]},{"type":"text","value":": The name of the contestant"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"result1\""}]},{"type":"text","value":": the first result of the contestant (an integer between 1 and 10)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"result2\""}]},{"type":"text","value":": the second result of the contestant (as above)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"result3\""}]},{"type":"text","value":": the third result of the contestant (as above)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should calculate the average of the three results for each contestant, and then return the contestant whose average result was the smallest. The return value should be the entire dictionary object containing the contestant's information."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume there will be no ties, i.e. a single contestant will have the smallest average result."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"person1 "},{"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":"\"name\""}]},{"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":"\"Mary\""}]},{"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":"\"result1\""}]},{"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":"2"}]},{"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":"\"result2\""}]},{"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":"3"}]},{"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":"\"result3\""}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nperson2 "},{"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":"\"name\""}]},{"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":"\"Gary\""}]},{"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":"\"result1\""}]},{"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":"5"}]},{"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":"\"result2\""}]},{"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":"1"}]},{"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":"\"result3\""}]},{"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":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]},{"type":"text","value":"\nperson3 "},{"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":"\"name\""}]},{"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":"\"Larry\""}]},{"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":"\"result1\""}]},{"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":"3"}]},{"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":"\"result2\""}]},{"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":"1"}]},{"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":"\"result3\""}]},{"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":"1"}]},{"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":"smallest_average"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person3"},{"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":"{'name': 'Larry', 'result1': 3, 'result2': 1, 'result3': 1}"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Row sums","tmcname":"part08-02_row_sums "},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In Python, every value stored in a variable is a reference to an object, so any value stored in a list is also a reference to an object. This is also true when modelling a matrix data structure: each value in the top level list is a reference to another list, which in turn contains references to the objects representing the elements of the matrix."}]},{"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":"row_sums(my_matrix: list)"}]},{"type":"text","value":", which takes an integer matrix as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should add a new element on each row in the matrix. This element contains the sum of the other elements on that row. The function does not have a return value. It should modify the parameter matrix in place."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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_matrix "},{"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":"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":" "},{"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":" "},{"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":" "},{"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","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\nrow_sums"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_matrix"},{"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_matrix"},{"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":"[[1, 2, 3], [3, 4, 7]]"}]}]}]}]},"html":"

    After this section

      \n
    • You will know what an object is in programming
    • \n
    • You will understand what is meant by the independence of individual objects
    • \n
    • You will be able to create and access objects
    • \n

    This is the first part of the Advanced Course in Programming. The material is designed to be used with the Visual Studio Code editor, just like the preceding Introduction to Programming course was. If you haven't use Visual Studio Code before, you will find the installation instructions here, and an introduction to the programming environment from the previous course here.

    In the Introduction to Programming course we noticed that it often makes sense to group related data together in our programs. For example, if we are trying to store information about a book, it might make sense to use a tuple or a dictionary to organize the data into a single data structure.

    The solution could look like this when using a tuple:

    name = \"In Search of Lost Typing\"\nauthor = \"Marcel Pythons\"\nyear = 1992\n\n# Combine these in a tuple\nbook = (name, author, year)\n\n# Print the name of the book\nprint(book[0])

    In a case like this, the advantage of using a dictionary is that we can use strings instead of indexes as the keys. That is, we can give descriptive names to the items stored in the data structure:

    name = \"In Search of Lost Typing\"\nauthor = \"Marcel Pythons\"\nyear = 1992\n\n# Combine these in a dictionary\nbook = {\"name\": name, \"author\": author, \"year\": year}\n\n# Print the name of the book\nprint(book[\"name\"])

    In both cases we are creating a new object. In programming, the term has the specific meaning of an independent whole, in this case containing some bits of data which are somehow related. Being independent means that any changes made to one object will not affect other objects.

    If we were to create two structurally identical representations of books, using dictionaries with identical keys, any changes made to one of them would not have any effect on the other:

    book1 = {\"name\": \"The Old Man and the Pythons\", \"author\": \"Ernest Pythons\", \"year\": 1952}\nbook2 = {\"name\": \"Seven Pythons\", \"author\": \"Aleksis Python\", \"year\": 1894}\n\nprint(book1[\"name\"])\nprint(book2[\"name\"])\n\nbook1[\"name\"] = \"A Farewell to ARM Processors\"\n\nprint(book1[\"name\"])\nprint(book2[\"name\"])

    The Old Man and the Pythons\nSeven Pythons\nA Farewell to ARM Processors\nSeven Pythons

    \n \n

    Please write a function named smallest_average(person1: dict, person2: dict, person3: dict), which takes three dictionary objects as its arguments.

    Each dictionary object contains values mapped to the following keys:

      \n
    • \"name\": The name of the contestant
    • \n
    • \"result1\": the first result of the contestant (an integer between 1 and 10)
    • \n
    • \"result2\": the second result of the contestant (as above)
    • \n
    • \"result3\": the third result of the contestant (as above)
    • \n

    The function should calculate the average of the three results for each contestant, and then return the contestant whose average result was the smallest. The return value should be the entire dictionary object containing the contestant's information.

    You may assume there will be no ties, i.e. a single contestant will have the smallest average result.

    An example of the function in action:

    person1 = {\"name\": \"Mary\", \"result1\": 2, \"result2\": 3, \"result3\": 3}\nperson2 = {\"name\": \"Gary\", \"result1\": 5, \"result2\": 1, \"result3\": 8}\nperson3 = {\"name\": \"Larry\", \"result1\": 3, \"result2\": 1, \"result3\": 1}\n\nprint(smallest_average(person1, person2, person3))

    {'name': 'Larry', 'result1': 3, 'result2': 1, 'result3': 1}

    In Python, every value stored in a variable is a reference to an object, so any value stored in a list is also a reference to an object. This is also true when modelling a matrix data structure: each value in the top level list is a reference to another list, which in turn contains references to the objects representing the elements of the matrix.

    Please write a function named row_sums(my_matrix: list), which takes an integer matrix as its argument.

    The function should add a new element on each row in the matrix. This element contains the sum of the other elements on that row. The function does not have a return value. It should modify the parameter matrix in place.

    An example of the function in action:

    my_matrix = [[1, 2], [3, 4]]\nrow_sums(my_matrix)\nprint(my_matrix)

    [[1, 2, 3], [3, 4, 7]]

    ","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-8/1-objects-and-methods.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-8/2-classes-and-objects/page-data.json b/page-data/part-8/2-classes-and-objects/page-data.json index b50ceb668..96866a96c 100644 --- a/page-data/part-8/2-classes-and-objects/page-data.json +++ b/page-data/part-8/2-classes-and-objects/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-8/2-classes-and-objects", - "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 know what a class is"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will understand what the connection between a class and an object is"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what is meant by object oriented programming"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the previous section we worked with lists, tuples, dictionaries and strings. These are all rather special cases in Python programming. Python syntax features a unique, pre-defined method of declaring an object belonging to each of these types:"}]},{"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":"# Lists are declared with square brackets"}]},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Strings are declared with quotation marks"}]},{"type":"text","value":"\nmy_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":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Dictionaries are declared with curly brackets"}]},{"type":"text","value":"\nmy_dict "},{"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":"\"one\""}]},{"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":"1"}]},{"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":"\"two:\""}]},{"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":"2"}]},{"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":"# Tuples are declared with parentheses"}]},{"type":"text","value":"\nmy_tuple "},{"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":"p","properties":{},"children":[{"type":"text","value":"When some other type of object is declared, we need to call a special initialization function called a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"constructor"}]},{"type":"text","value":". Let's have a look at working with fractions through the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":" class."}]},{"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":"# we are using the class Fraction from the module fractions"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" fractions "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Fraction\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# create some new fraactions"}]},{"type":"text","value":"\nhalf "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fraction"},{"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":"text","value":"\n\nthird "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fraction"},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nanother "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fraction"},{"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":"11"}]},{"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":"# print these out"}]},{"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":"half"},{"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":"third"},{"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":"another"},{"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":"# Fractions can be added together, for 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":"half "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" third"},{"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":"1/2\n1/3\n3/11\n5/6"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, constructor method calls look a little different than the normal method calls we have come across before. For one, they are not attached to any object with the dot notation (as the constructor call is needed to create an object in the first place). The constructor method is also capitalized: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"half = Fraction(1,2)"}]},{"type":"text","value":". Let's have a closer look at how objects are constructed by getting familiar with the concept of the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"class"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"a-class-is-the-blueprint-of-an-object","style":"position:relative;"},"children":[{"type":"text","value":"A class is the blueprint of an object"},{"type":"element","tagName":"a","properties":{"href":"#a-class-is-the-blueprint-of-an-object","ariaLabel":"a class is the blueprint of an object 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 have already used the term "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" in the material many times. For instance, in the example above we imported the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":" class from the module "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fractions"}]},{"type":"text","value":". New fraction objects were created by calling the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"constructor"}]},{"type":"text","value":" method of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":" class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A class definition contains the structure and functionalities of any object which represents it. That is why classes are sometimes referred to as the blueprints of objects. So, a class definition tells you what kind of data an object contains, and defines also the methods which can be used on the object. "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Object oriented programming"}]},{"type":"text","value":" refers to a programming paradigm where the functionality of the program is tied into the use of classes and objects created based on them."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A single class definition can be used to create multiple objects. As mentioned before, objects are independent. Changes made to one object generally do not affect the other objects representing the same class. Each object has its own unique set of data attributes. It might be helpful to consider this simplification of the class-object relationship:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a class defines the variables"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"when an object is created, those variables are assigned values"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, we can use an object of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":" to access the numerator and denominator of a fractional number:"}]},{"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":"from"}]},{"type":"text","value":" fractions "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Fraction\n\nnumber "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fraction"},{"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":"5"}]},{"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":"# Print the numerator"}]},{"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":"numerator"},{"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":"# ...and the denominator"}]},{"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":"denominator"},{"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":"2\n5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":" class definition contains declarations for the variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"numerator"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"denominator"}]},{"type":"text","value":". Each object created based on the class has its own specific values assigned to these variables."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Similarly, objects created based on the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" class each contain their own unique values for the year, month and day of the date:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" date\n\nxmas_eve "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"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":"12"}]},{"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":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmidsummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"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":"6"}]},{"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":"20"}]},{"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":"# print only the month attribute of both objects"}]},{"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":"xmas_eve"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month"},{"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":"midsummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month"},{"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":"12\n6"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" class definition contains declarations of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"year"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"month"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"day"}]},{"type":"text","value":" variables. When a new "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" object is created based on the class, these variables are assigned values. Each object has its own unique values assigned to these variables."}]},{"type":"element","tagName":"h2","properties":{"id":"functions-which-work-with-objects","style":"position:relative;"},"children":[{"type":"text","value":"Functions which work with objects"},{"type":"element","tagName":"a","properties":{"href":"#functions-which-work-with-objects","ariaLabel":"functions which work with objects 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":"Passing an object as an argument to a function should be familiar to you by now, as we have done so many times on this course so far. Let's have a look at the following example. Here we have a function which checks if the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" object passed as an argument falls on a weekend:"}]},{"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":"is_it_weekend"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" date"},{"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 weekday "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isoweekday"},{"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":" weekday "},{"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":"6"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"or"}]},{"type":"text","value":" weekday "},{"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":"7"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This function uses the method "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html#datetime.date.isoweekday","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"isoweekday"}]},{"type":"text","value":", which is defined in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" class definition, and returns an integer value so that if the date given is a Monday, it returns 1, and if it is a Tuesday, it returns 2, and so forth."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can use the above function 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":"text","value":"xmas_eve "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"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":"12"}]},{"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":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmidsummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"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":"6"}]},{"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":"20"}]},{"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":"is_it_weekend"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"xmas_eve"},{"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_weekend"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"midsummer"},{"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":"False\nTrue"}]}]},{"type":"element","tagName":"h2","properties":{"id":"methods-vs-variables","style":"position:relative;"},"children":[{"type":"text","value":"Methods vs variables"},{"type":"element","tagName":"a","properties":{"href":"#methods-vs-variables","ariaLabel":"methods vs variables 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":"When working with an object of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" you may notice there is a slight difference between how the variables contained in the object are accessed, as opposed to how the methods attached to the objects are used:"}]},{"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_date "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"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":"12"}]},{"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":"24"}]},{"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":"# calling a method"}]},{"type":"text","value":"\nweekday "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isoweekday"},{"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","comment"]},"children":[{"type":"text","value":"# accessing a variable"}]},{"type":"text","value":"\nmy_month "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month\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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The day of the week:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" weekday"},{"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 month:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" my_month"},{"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":"The day of the week: 4\nThe month: 12"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The day of the week the date falls on is available through the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"method"}]},{"type":"text","value":" "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"isoweekday"}]},{"type":"text","value":":"}]},{"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":"weekday "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isoweekday"},{"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":"This is a method call, so there are parentheses after the name of the method. Leaving the parentheses out does not cause an error, but the results are weird:"}]},{"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":"weekday "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isoweekday\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 day of the week:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" weekday"},{"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":"The day of the week: "}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The month of a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" object is a variable, so the value attached can be accessed with a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reference"}]},{"type":"text","value":"."}]},{"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_month "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice there are "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"no parentheses"}]},{"type":"text","value":" here. Adding parentheses "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"would"}]},{"type":"text","value":" cause an error:"}]},{"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_month "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month"},{"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":"div","properties":{},"children":[{"type":"text","value":"Traceback (most recent call last):\nFile \""},{"type":"element","tagName":"stdin","properties":{},"children":[{"type":"text","value":"\", line 1, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nTypeError: 'int' object is not callable"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"List of years","tmcname":"part08-03_list_years"},"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":"list_years(dates: list)"}]},{"type":"text","value":" which takes a list of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" type objects as its argument. The function should return a new list, which contains the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"years in the original list in chronological order"}]},{"type":"text","value":", from earliest to latest."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"date1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2019"}]},{"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":"2"}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndate2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2006"}]},{"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":"10"}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndate3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1993"}]},{"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":"5"}]},{"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":"9"}]},{"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":" list_years"},{"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":"date1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date3"},{"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":"years"},{"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":"[1993, 2006, 2019]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Shopping list","tmcname":"part08-04_shopping_list"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains a pre-defined "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ShoppingList"}]},{"type":"text","value":" class, which can be used to model a shopping list. Your task is to add a method to the class definition. You do not need to change any methods already defined."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Assuming we have a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ShoppingList"}]},{"type":"text","value":" object referenced in a variable named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"shopping_list"}]},{"type":"text","value":", the object can be handled with the following methods:"}]},{"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":"\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":"shopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_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":"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":"shopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"item"},{"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":"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":"shopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"amount"},{"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":"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":"shopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"item"},{"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":"\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":"shopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"amount"},{"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":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\nBananas\n4\nMilk\n1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We can also do 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","comment"]},"children":[{"type":"text","value":"# the items on the shopping list are indexed from 1"}]},{"type":"text","value":"\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":"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":" shopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_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":"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":"text","value":"\n item "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" shopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"item"},{"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":"text","value":"\n amount "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" shopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"amount"},{"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":"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":"item"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"amount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" units\""}]}]},{"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":"Bananas 4 units\nMilk 1 units"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see, a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ShoppingList"}]},{"type":"text","value":" works a bit like a normal list, but it is accessed via the methods provided by the ShoppingList class. Unlike normal Python lists, indexing starts from 1, not 0."}]},{"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":"total_units(my_list: ShoppingList)"}]},{"type":"text","value":", which takes a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ShoppingList"}]},{"type":"text","value":" type object as its argument. The function should calculate the total number of units listed, and return the value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can use the following code to test your 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","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 my_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ShoppingList"},{"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 my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"bananas\""}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apples\""}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"pineapple\""}]},{"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":"1"}]},{"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":"total_units"},{"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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"16"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" the definition of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ShoppingList"}]},{"type":"text","value":" class is already included in the exercise template. You do not need to use an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement to import it, unlike in the examples above with the Python standard library classes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":"."}]}]}]},"html":"

    After this section

      \n
    • You will know what a class is
    • \n
    • You will understand what the connection between a class and an object is
    • \n
    • You will know what is meant by object oriented programming
    • \n

    In the previous section we worked with lists, tuples, dictionaries and strings. These are all rather special cases in Python programming. Python syntax features a unique, pre-defined method of declaring an object belonging to each of these types:

    # Lists are declared with square brackets\nmy_list = [1,2,3]\n\n# Strings are declared with quotation marks\nmy_string = \"Hi there!\"\n\n# Dictionaries are declared with curly brackets\nmy_dict = {\"one\": 1, \"two:\": 2}\n\n# Tuples are declared with parentheses\nmy_tuple = (1,2,3)

    When some other type of object is declared, we need to call a special initialization function called a constructor. Let's have a look at working with fractions through the Fraction class.

    # we are using the class Fraction from the module fractions\nfrom fractions import Fraction\n\n# create some new fraactions\nhalf = Fraction(1,2)\n\nthird = Fraction(1,3)\n\nanother = Fraction(3,11)\n\n# print these out\nprint(half)\nprint(third)\nprint(another)\n\n# Fractions can be added together, for example\nprint(half + third)

    1/2\n1/3\n3/11\n5/6

    As you can see above, constructor method calls look a little different than the normal method calls we have come across before. For one, they are not attached to any object with the dot notation (as the constructor call is needed to create an object in the first place). The constructor method is also capitalized: half = Fraction(1,2). Let's have a closer look at how objects are constructed by getting familiar with the concept of the class.

    A class is the blueprint of an object

    We have already used the term class in the material many times. For instance, in the example above we imported the Fraction class from the module fractions. New fraction objects were created by calling the constructor method of the Fraction class.

    A class definition contains the structure and functionalities of any object which represents it. That is why classes are sometimes referred to as the blueprints of objects. So, a class definition tells you what kind of data an object contains, and defines also the methods which can be used on the object. Object oriented programming refers to a programming paradigm where the functionality of the program is tied into the use of classes and objects created based on them.

    A single class definition can be used to create multiple objects. As mentioned before, objects are independent. Changes made to one object generally do not affect the other objects representing the same class. Each object has its own unique set of data attributes. It might be helpful to consider this simplification of the class-object relationship:

      \n
    • a class defines the variables
    • \n
    • when an object is created, those variables are assigned values
    • \n

    So, we can use an object of type Fraction to access the numerator and denominator of a fractional number:

    from fractions import Fraction\n\nnumber = Fraction(2,5)\n\n# Print the numerator\nprint(number.numerator)\n\n# ...and the denominator\nprint(number.denominator)

    2\n5

    The Fraction class definition contains declarations for the variables numerator and denominator. Each object created based on the class has its own specific values assigned to these variables.

    Similarly, objects created based on the date class each contain their own unique values for the year, month and day of the date:

    from datetime import date\n\nxmas_eve = date(2020, 12, 24)\nmidsummer = date(2020, 6, 20)\n\n# print only the month attribute of both objects\nprint(xmas_eve.month)\nprint(midsummer.month)

    12\n6

    The date class definition contains declarations of the year, month and day variables. When a new date object is created based on the class, these variables are assigned values. Each object has its own unique values assigned to these variables.

    Functions which work with objects

    Passing an object as an argument to a function should be familiar to you by now, as we have done so many times on this course so far. Let's have a look at the following example. Here we have a function which checks if the date object passed as an argument falls on a weekend:

    def is_it_weekend(my_date: date):\n    weekday = my_date.isoweekday()\n    return weekday == 6 or weekday == 7

    This function uses the method isoweekday, which is defined in the date class definition, and returns an integer value so that if the date given is a Monday, it returns 1, and if it is a Tuesday, it returns 2, and so forth.

    You can use the above function like this:

    xmas_eve = date(2020, 12, 24)\nmidsummer = date(2020, 6, 20)\n\nprint(is_it_weekend(xmas_eve))\nprint(is_it_weekend(midsummer))

    False\nTrue

    Methods vs variables

    When working with an object of type date you may notice there is a slight difference between how the variables contained in the object are accessed, as opposed to how the methods attached to the objects are used:

    my_date = date(2020, 12, 24)\n\n# calling a method\nweekday = my_date.isoweekday()\n\n# accessing a variable\nmy_month = my_date.month\n\nprint(\"The day of the week:\", weekday)\nprint(\"The month:\", my_month)

    The day of the week: 4\nThe month: 12

    The day of the week the date falls on is available through the method isoweekday:

    weekday = my_date.isoweekday()

    This is a method call, so there are parentheses after the name of the method. Leaving the parentheses out does not cause an error, but the results are weird:

    weekday =  my_date.isoweekday\nprint(\"The day of the week:\", weekday)

    The day of the week: <built-in method isoweekday of datetime.date object at 0x10ed66450>

    The month of a date object is a variable, so the value attached can be accessed with a reference.

    my_month = my_date.month

    Notice there are no parentheses here. Adding parentheses would cause an error:

    my_month = my_date.month()
    Traceback (most recent call last):\nFile \"\", line 1, in \nTypeError: 'int' object is not callable

    Please write a function named list_years(dates: list) which takes a list of date type objects as its argument. The function should return a new list, which contains the years in the original list in chronological order, from earliest to latest.

    An example of the function in action:

    date1 = date(2019, 2, 3)\ndate2 = date(2006, 10, 10)\ndate3 = date(1993, 5, 9)\n\nyears = list_years([date1, date2, date3])\nprint(years)

    [1993, 2006, 2019]

    The exercise template contains a pre-defined ShoppingList class, which can be used to model a shopping list. Your task is to add a method to the class definition. You do not need to change any methods already defined.

    Assuming we have a ShoppingList object referenced in a variable named shopping_list, the object can be handled with the following methods:

    \nprint(shopping_list.number_of_items())\nprint(shopping_list.item(1))\nprint(shopping_list.amount(1))\nprint(shopping_list.item(2))\nprint(shopping_list.amount(2))\n

    2\nBananas\n4\nMilk\n1

    We can also do this:

    # the items on the shopping list are indexed from 1\nfor i in range(1, shopping_list.number_of_items()+1):\n    item = shopping_list.item(i)\n    amount = shopping_list.amount(i)\n    print(f\"{item}: {amount} units\")

    Bananas 4 units\nMilk 1 units

    As you can see, a ShoppingList works a bit like a normal list, but it is accessed via the methods provided by the ShoppingList class. Unlike normal Python lists, indexing starts from 1, not 0.

    Please write a function named total_units(my_list: ShoppingList), which takes a ShoppingList type object as its argument. The function should calculate the total number of units listed, and return the value.

    You can use the following code to test your function:

    if __name__ == \"__main__\":\n    my_list = ShoppingList()\n    my_list.add(\"bananas\", 10)\n    my_list.add(\"apples\", 5)\n    my_list.add(\"pineapple\", 1)\n\n    print(total_units(my_list))

    16

    NB: the definition of the ShoppingList class is already included in the exercise template. You do not need to use an import statement to import it, unlike in the examples above with the Python standard library classes Fraction and date.

    ","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-8/2-classes-and-objects.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know what a class is"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will understand what the connection between a class and an object is"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what is meant by object oriented programming"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the previous section we worked with lists, tuples, dictionaries and strings. These are all rather special cases in Python programming. Python syntax features a unique, pre-defined method of declaring an object belonging to each of these types:"}]},{"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":"# Lists are declared with square brackets"}]},{"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":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Strings are declared with quotation marks"}]},{"type":"text","value":"\nmy_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":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Dictionaries are declared with curly brackets"}]},{"type":"text","value":"\nmy_dict "},{"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":"\"one\""}]},{"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":"1"}]},{"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":"\"two:\""}]},{"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":"2"}]},{"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":"# Tuples are declared with parentheses"}]},{"type":"text","value":"\nmy_tuple "},{"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":"p","properties":{},"children":[{"type":"text","value":"When some other type of object is declared, we need to call a special initialization function called a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"constructor"}]},{"type":"text","value":". Let's have a look at working with fractions through the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":" class."}]},{"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":"# we are using the class Fraction from the module fractions"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" fractions "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Fraction\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# create some new fraactions"}]},{"type":"text","value":"\nhalf "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fraction"},{"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":"text","value":"\n\nthird "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fraction"},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nanother "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fraction"},{"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":"11"}]},{"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":"# print these out"}]},{"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":"half"},{"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":"third"},{"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":"another"},{"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":"# Fractions can be added together, for 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":"half "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" third"},{"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":"1/2\n1/3\n3/11\n5/6"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, constructor method calls look a little different than the normal method calls we have come across before. For one, they are not attached to any object with the dot notation (as the constructor call is needed to create an object in the first place). The constructor method is also capitalized: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"half = Fraction(1,2)"}]},{"type":"text","value":". Let's have a closer look at how objects are constructed by getting familiar with the concept of the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"class"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"a-class-is-the-blueprint-of-an-object","style":"position:relative;"},"children":[{"type":"text","value":"A class is the blueprint of an object"},{"type":"element","tagName":"a","properties":{"href":"#a-class-is-the-blueprint-of-an-object","ariaLabel":"a class is the blueprint of an object 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 have already used the term "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" in the material many times. For instance, in the example above we imported the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":" class from the module "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fractions"}]},{"type":"text","value":". New fraction objects were created by calling the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"constructor"}]},{"type":"text","value":" method of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":" class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A class definition contains the structure and functionalities of any object which represents it. That is why classes are sometimes referred to as the blueprints of objects. So, a class definition tells you what kind of data an object contains, and defines also the methods which can be used on the object. "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Object oriented programming"}]},{"type":"text","value":" refers to a programming paradigm where the functionality of the program is tied into the use of classes and objects created based on them."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A single class definition can be used to create multiple objects. As mentioned before, objects are independent. Changes made to one object generally do not affect the other objects representing the same class. Each object has its own unique set of data attributes. It might be helpful to consider this simplification of the class-object relationship:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a class defines the variables"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"when an object is created, those variables are assigned values"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, we can use an object of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":" to access the numerator and denominator of a fractional number:"}]},{"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":"from"}]},{"type":"text","value":" fractions "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Fraction\n\nnumber "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Fraction"},{"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":"5"}]},{"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":"# Print the numerator"}]},{"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":"numerator"},{"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":"# ...and the denominator"}]},{"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":"denominator"},{"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":"2\n5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":" class definition contains declarations for the variables "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"numerator"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"denominator"}]},{"type":"text","value":". Each object created based on the class has its own specific values assigned to these variables."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Similarly, objects created based on the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" class each contain their own unique values for the year, month and day of the date:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" date\n\nxmas_eve "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"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":"12"}]},{"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":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmidsummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"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":"6"}]},{"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":"20"}]},{"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":"# print only the month attribute of both objects"}]},{"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":"xmas_eve"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month"},{"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":"midsummer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month"},{"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":"12\n6"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" class definition contains declarations of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"year"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"month"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"day"}]},{"type":"text","value":" variables. When a new "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" object is created based on the class, these variables are assigned values. Each object has its own unique values assigned to these variables."}]},{"type":"element","tagName":"h2","properties":{"id":"functions-which-work-with-objects","style":"position:relative;"},"children":[{"type":"text","value":"Functions which work with objects"},{"type":"element","tagName":"a","properties":{"href":"#functions-which-work-with-objects","ariaLabel":"functions which work with objects 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":"Passing an object as an argument to a function should be familiar to you by now, as we have done so many times on this course so far. Let's have a look at the following example. Here we have a function which checks if the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" object passed as an argument falls on a weekend:"}]},{"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":"is_it_weekend"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"my_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" date"},{"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 weekday "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isoweekday"},{"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":" weekday "},{"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":"6"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"or"}]},{"type":"text","value":" weekday "},{"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":"7"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This function uses the method "},{"type":"element","tagName":"a","properties":{"href":"https://docs.python.org/3/library/datetime.html#datetime.date.isoweekday","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"isoweekday"}]},{"type":"text","value":", which is defined in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" class definition, and returns an integer value so that if the date given is a Monday, it returns 1, and if it is a Tuesday, it returns 2, and so forth."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can use the above function 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":"text","value":"xmas_eve "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"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":"12"}]},{"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":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nmidsummer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"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":"6"}]},{"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":"20"}]},{"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":"is_it_weekend"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"xmas_eve"},{"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_weekend"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"midsummer"},{"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":"False\nTrue"}]}]},{"type":"element","tagName":"h2","properties":{"id":"methods-vs-variables","style":"position:relative;"},"children":[{"type":"text","value":"Methods vs variables"},{"type":"element","tagName":"a","properties":{"href":"#methods-vs-variables","ariaLabel":"methods vs variables 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":"When working with an object of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" you may notice there is a slight difference between how the variables contained in the object are accessed, as opposed to how the methods attached to the objects are used:"}]},{"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_date "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"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":"12"}]},{"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":"24"}]},{"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":"# calling a method"}]},{"type":"text","value":"\nweekday "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isoweekday"},{"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","comment"]},"children":[{"type":"text","value":"# accessing a variable"}]},{"type":"text","value":"\nmy_month "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month\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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The day of the week:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" weekday"},{"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 month:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" my_month"},{"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":"The day of the week: 4\nThe month: 12"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The day of the week the date falls on is available through the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"method"}]},{"type":"text","value":" "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"isoweekday"}]},{"type":"text","value":":"}]},{"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":"weekday "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isoweekday"},{"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":"This is a method call, so there are parentheses after the name of the method. Leaving the parentheses out does not cause an error, but the results are weird:"}]},{"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":"weekday "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"isoweekday\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 day of the week:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" weekday"},{"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":"The day of the week: "}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The month of a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" object is a variable, so the value attached can be accessed with a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"reference"}]},{"type":"text","value":"."}]},{"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_month "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Notice there are "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"no parentheses"}]},{"type":"text","value":" here. Adding parentheses "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"would"}]},{"type":"text","value":" cause an error:"}]},{"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_month "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" my_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"month"},{"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":"div","properties":{},"children":[{"type":"text","value":"Traceback (most recent call last):\nFile \""},{"type":"element","tagName":"stdin","properties":{},"children":[{"type":"text","value":"\", line 1, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nTypeError: 'int' object is not callable"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"List of years","tmcname":"part08-03_list_years"},"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":"list_years(dates: list)"}]},{"type":"text","value":" which takes a list of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":" type objects as its argument. The function should return a new list, which contains the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"years in the original list in chronological order"}]},{"type":"text","value":", from earliest to latest."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of the function in action:"}]},{"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":"date1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2019"}]},{"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":"2"}]},{"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":"3"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndate2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2006"}]},{"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":"10"}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndate3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1993"}]},{"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":"5"}]},{"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":"9"}]},{"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":" list_years"},{"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":"date1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date3"},{"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":"years"},{"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":"[1993, 2006, 2019]"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Shopping list","tmcname":"part08-04_shopping_list"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains a pre-defined "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ShoppingList"}]},{"type":"text","value":" class, which can be used to model a shopping list. Your task is to add a method to the class definition. You do not need to change any methods already defined."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Assuming we have a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ShoppingList"}]},{"type":"text","value":" object referenced in a variable named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"shopping_list"}]},{"type":"text","value":", the object can be handled with the following methods:"}]},{"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":"\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":"shopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_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":"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":"shopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"item"},{"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":"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":"shopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"amount"},{"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":"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":"shopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"item"},{"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":"\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":"shopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"amount"},{"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":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\nBananas\n4\nMilk\n1"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We can also do 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","comment"]},"children":[{"type":"text","value":"# the items on the shopping list are indexed from 1"}]},{"type":"text","value":"\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":"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":" shopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_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":"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":"text","value":"\n item "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" shopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"item"},{"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":"text","value":"\n amount "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" shopping_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"amount"},{"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":"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":"item"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"amount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" units\""}]}]},{"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":"Bananas 4 units\nMilk 1 units"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see, a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ShoppingList"}]},{"type":"text","value":" works a bit like a normal list, but it is accessed via the methods provided by the ShoppingList class. Unlike normal Python lists, indexing starts from 1, not 0."}]},{"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":"total_units(my_list: ShoppingList)"}]},{"type":"text","value":", which takes a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ShoppingList"}]},{"type":"text","value":" type object as its argument. The function should calculate the total number of units listed, and return the value."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can use the following code to test your 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","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 my_list "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ShoppingList"},{"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 my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"bananas\""}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"apples\""}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n my_list"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"pineapple\""}]},{"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":"1"}]},{"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":"total_units"},{"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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"16"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" the definition of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ShoppingList"}]},{"type":"text","value":" class is already included in the exercise template. You do not need to use an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement to import it, unlike in the examples above with the Python standard library classes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Fraction"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":"."}]}]}]},"html":"

    After this section

      \n
    • You will know what a class is
    • \n
    • You will understand what the connection between a class and an object is
    • \n
    • You will know what is meant by object oriented programming
    • \n

    In the previous section we worked with lists, tuples, dictionaries and strings. These are all rather special cases in Python programming. Python syntax features a unique, pre-defined method of declaring an object belonging to each of these types:

    # Lists are declared with square brackets\nmy_list = [1,2,3]\n\n# Strings are declared with quotation marks\nmy_string = \"Hi there!\"\n\n# Dictionaries are declared with curly brackets\nmy_dict = {\"one\": 1, \"two:\": 2}\n\n# Tuples are declared with parentheses\nmy_tuple = (1,2,3)

    When some other type of object is declared, we need to call a special initialization function called a constructor. Let's have a look at working with fractions through the Fraction class.

    # we are using the class Fraction from the module fractions\nfrom fractions import Fraction\n\n# create some new fraactions\nhalf = Fraction(1,2)\n\nthird = Fraction(1,3)\n\nanother = Fraction(3,11)\n\n# print these out\nprint(half)\nprint(third)\nprint(another)\n\n# Fractions can be added together, for example\nprint(half + third)

    1/2\n1/3\n3/11\n5/6

    As you can see above, constructor method calls look a little different than the normal method calls we have come across before. For one, they are not attached to any object with the dot notation (as the constructor call is needed to create an object in the first place). The constructor method is also capitalized: half = Fraction(1,2). Let's have a closer look at how objects are constructed by getting familiar with the concept of the class.

    A class is the blueprint of an object

    We have already used the term class in the material many times. For instance, in the example above we imported the Fraction class from the module fractions. New fraction objects were created by calling the constructor method of the Fraction class.

    A class definition contains the structure and functionalities of any object which represents it. That is why classes are sometimes referred to as the blueprints of objects. So, a class definition tells you what kind of data an object contains, and defines also the methods which can be used on the object. Object oriented programming refers to a programming paradigm where the functionality of the program is tied into the use of classes and objects created based on them.

    A single class definition can be used to create multiple objects. As mentioned before, objects are independent. Changes made to one object generally do not affect the other objects representing the same class. Each object has its own unique set of data attributes. It might be helpful to consider this simplification of the class-object relationship:

      \n
    • a class defines the variables
    • \n
    • when an object is created, those variables are assigned values
    • \n

    So, we can use an object of type Fraction to access the numerator and denominator of a fractional number:

    from fractions import Fraction\n\nnumber = Fraction(2,5)\n\n# Print the numerator\nprint(number.numerator)\n\n# ...and the denominator\nprint(number.denominator)

    2\n5

    The Fraction class definition contains declarations for the variables numerator and denominator. Each object created based on the class has its own specific values assigned to these variables.

    Similarly, objects created based on the date class each contain their own unique values for the year, month and day of the date:

    from datetime import date\n\nxmas_eve = date(2020, 12, 24)\nmidsummer = date(2020, 6, 20)\n\n# print only the month attribute of both objects\nprint(xmas_eve.month)\nprint(midsummer.month)

    12\n6

    The date class definition contains declarations of the year, month and day variables. When a new date object is created based on the class, these variables are assigned values. Each object has its own unique values assigned to these variables.

    Functions which work with objects

    Passing an object as an argument to a function should be familiar to you by now, as we have done so many times on this course so far. Let's have a look at the following example. Here we have a function which checks if the date object passed as an argument falls on a weekend:

    def is_it_weekend(my_date: date):\n    weekday = my_date.isoweekday()\n    return weekday == 6 or weekday == 7

    This function uses the method isoweekday, which is defined in the date class definition, and returns an integer value so that if the date given is a Monday, it returns 1, and if it is a Tuesday, it returns 2, and so forth.

    You can use the above function like this:

    xmas_eve = date(2020, 12, 24)\nmidsummer = date(2020, 6, 20)\n\nprint(is_it_weekend(xmas_eve))\nprint(is_it_weekend(midsummer))

    False\nTrue

    Methods vs variables

    When working with an object of type date you may notice there is a slight difference between how the variables contained in the object are accessed, as opposed to how the methods attached to the objects are used:

    my_date = date(2020, 12, 24)\n\n# calling a method\nweekday = my_date.isoweekday()\n\n# accessing a variable\nmy_month = my_date.month\n\nprint(\"The day of the week:\", weekday)\nprint(\"The month:\", my_month)

    The day of the week: 4\nThe month: 12

    The day of the week the date falls on is available through the method isoweekday:

    weekday = my_date.isoweekday()

    This is a method call, so there are parentheses after the name of the method. Leaving the parentheses out does not cause an error, but the results are weird:

    weekday =  my_date.isoweekday\nprint(\"The day of the week:\", weekday)

    The day of the week: <built-in method isoweekday of datetime.date object at 0x10ed66450>

    The month of a date object is a variable, so the value attached can be accessed with a reference.

    my_month = my_date.month

    Notice there are no parentheses here. Adding parentheses would cause an error:

    my_month = my_date.month()
    Traceback (most recent call last):\nFile \"\", line 1, in \nTypeError: 'int' object is not callable

    Please write a function named list_years(dates: list) which takes a list of date type objects as its argument. The function should return a new list, which contains the years in the original list in chronological order, from earliest to latest.

    An example of the function in action:

    date1 = date(2019, 2, 3)\ndate2 = date(2006, 10, 10)\ndate3 = date(1993, 5, 9)\n\nyears = list_years([date1, date2, date3])\nprint(years)

    [1993, 2006, 2019]

    The exercise template contains a pre-defined ShoppingList class, which can be used to model a shopping list. Your task is to add a method to the class definition. You do not need to change any methods already defined.

    Assuming we have a ShoppingList object referenced in a variable named shopping_list, the object can be handled with the following methods:

    \nprint(shopping_list.number_of_items())\nprint(shopping_list.item(1))\nprint(shopping_list.amount(1))\nprint(shopping_list.item(2))\nprint(shopping_list.amount(2))\n

    2\nBananas\n4\nMilk\n1

    We can also do this:

    # the items on the shopping list are indexed from 1\nfor i in range(1, shopping_list.number_of_items()+1):\n    item = shopping_list.item(i)\n    amount = shopping_list.amount(i)\n    print(f\"{item}: {amount} units\")

    Bananas 4 units\nMilk 1 units

    As you can see, a ShoppingList works a bit like a normal list, but it is accessed via the methods provided by the ShoppingList class. Unlike normal Python lists, indexing starts from 1, not 0.

    Please write a function named total_units(my_list: ShoppingList), which takes a ShoppingList type object as its argument. The function should calculate the total number of units listed, and return the value.

    You can use the following code to test your function:

    if __name__ == \"__main__\":\n    my_list = ShoppingList()\n    my_list.add(\"bananas\", 10)\n    my_list.add(\"apples\", 5)\n    my_list.add(\"pineapple\", 1)\n\n    print(total_units(my_list))

    16

    NB: the definition of the ShoppingList class is already included in the exercise template. You do not need to use an import statement to import it, unlike in the examples above with the Python standard library classes Fraction and date.

    ","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-8/2-classes-and-objects.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-8/3-defining-classes/page-data.json b/page-data/part-8/3-defining-classes/page-data.json index 7e6e9b17c..48a668f0b 100644 --- a/page-data/part-8/3-defining-classes/page-data.json +++ b/page-data/part-8/3-defining-classes/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-8/3-defining-classes", - "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 know how to define your own classes"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to create objects based on classes you've defined yourself"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to write a constructor"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You are familiar with the parameter name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what attributes are and how they are used"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A class is defined with the keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":". The syntax is as follows:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"NameOfClass"}]},{"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":"# class defition goes here"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Classes are usually named in "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"camel case"}]},{"type":"text","value":". This means that all the words in the class name are written together, without spaces, and each word is capitalised. The following class names follow this convention:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Weekday"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LibraryDatabase"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PythonCourseGrades"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A single class definition should represent a single whole, the contents of which should be atomically linked together in some way. In more complicated programs classes can contain members of other classes. For example, the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Course"}]},{"type":"text","value":" could contain objects of class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lecture"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ExerciseSession"}]},{"type":"text","value":" etc."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lets have a look at a skeleton of a class definition. The functionalities are still missing at this 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":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"pass"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The above piece of code tells Python that here we are defining a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":". The class does not contain any functionality yet, but we can still create an object based on the class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lets have a look at a program where two variables are added to a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" object: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balance"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"owner"}]},{"type":"text","value":". Any variables attached to an object are called its "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"attributes"}]},{"type":"text","value":", or more specifically, "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"data attributes"}]},{"type":"text","value":", or sometimes "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"instance variables"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The attributes attached to an object can be accessed through the object:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"pass"}]},{"type":"text","value":"\n\npeters_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"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":"\npeters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"owner "},{"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":"\"Peter Python\""}]},{"type":"text","value":"\npeters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"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":"5.0"}]},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"owner"},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"Peter Python\n5.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The data attributes are available only through the object they are attached to. Each "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" object created based on the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" class has its own values attached to the data attributes. Those values can be accessed by referring to the object in question:"}]},{"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":"account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"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":"\naccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"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":"155.50"}]},{"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":"account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This refers to the data attribute balance attached to the account"}]},{"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":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# THIS CAUSES AN ERROR, as there is no such independent variable available, and the object reference is missing"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"adding-a-constructor","style":"position:relative;"},"children":[{"type":"text","value":"Adding a constructor"},{"type":"element","tagName":"a","properties":{"href":"#adding-a-constructor","ariaLabel":"adding a constructor 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 the above example we saw that a new instance of a class can be created by calling the constructor method of the class like so: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NameOfClass()"}]},{"type":"text","value":". Above we then attached data attributes to the object separately, but it is often more convenient to pass these initial values of attributes directly as the object is created. In the above example we first had a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" object without these attributes, and the attributes only existed after they were explicitly declared."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Declaring attributes outside the constructor results in a situation where different instances of the same class can have different attributes. The following code produces an error because we now have another "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" object, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"paulas_account"}]},{"type":"text","value":", which does not contain the same attributes:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"pass"}]},{"type":"text","value":"\n\npeters_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"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":"\npeters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"owner "},{"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":"\"Peter\""}]},{"type":"text","value":"\npeters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"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":"1400"}]},{"type":"text","value":"\n\npaulas_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"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":"\npaulas_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"owner "},{"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":"\"Paula\""}]},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"paulas_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# THIS CAUSES AN ERROR"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, instead of declaring attributes after each instance of the class is created, it is usually a better idea to initialize the values of the attributes as the class constructor is called. As the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" class definition is currently just a skeleton, the constructor method is implicitly assumed by the Python interpreter, but it is possible to define your own constructor methods, and that is exactly what we will do now."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A constructor method is a method declaration with the special name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__init__"}]},{"type":"text","value":", usually included at the very beginning of a class definition."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lets have a look at a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" class with a constructor method added in:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"# The constructor"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" owner"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"owner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" owner"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The name of the constructor method is always "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__init__"}]},{"type":"text","value":". Notice the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"two underscores on both sides"}]},{"type":"text","value":" of the word "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"init"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The first parameter in a constructor definition is always named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":". This refers to the object itself, and is necessary for declaring any attributes attached to the object. The assignment"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.balance = balance"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"assigns the balance received as an argument to the balance attribute of the object. It is a common convention to use the same variable names for the parameters and the data attributes defined in a constructor, but the variable names "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.balance"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balance"}]},{"type":"text","value":" above refer to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"two different variables"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.balance"}]},{"type":"text","value":" is an attribute of the object. Each "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" object has its own balance."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balance"}]},{"type":"text","value":" is a parameter in the constructor method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__init__"}]},{"type":"text","value":". Its value is set to the value passed as an argument to the method as the constructor is called (that is, when a new insctance of the class is created)."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now that we have defined the parameters of the constructor method, we can pass the desired initial values of the data attributes as arguments as a new object is created:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"# The constructor"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" owner"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"owner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" owner\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# As the method is called, no argument should be given for the self parameter"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Python assigns the value for self automatically"}]},{"type":"text","value":"\npeters_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npaulas_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20000"}]},{"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":"\"Paula Pythons\""}]},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"paulas_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"100\n20000"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is now much easier to work with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" objects, as the values can be passed at object creation, and the resulting two separate instances can be handled more predictably and uniformly. Declaring data attributes in the constructor also ensures the attributes are actually declared, and the desired initial values are always given by the programmer using the class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is still possible to change the initial values of the data attributes later in the program:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"# The constructor"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" owner"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"owner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" owner\n\npeters_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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":"\"Peter Python\""}]},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"# Change the balance to 1500"}]},{"type":"text","value":"\npeters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"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":"1500"}]},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"# Add 2000 to the balance"}]},{"type":"text","value":"\npeters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"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":"2000"}]},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"100\n1500\n3500"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at another example of classes and objects. We'll write a class which models a single draw of lottery numbers:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" date\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"LotteryDraw"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" round_week"},{"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":"text","value":" round_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" date"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"round_week "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" round_week\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"round_date "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" round_date\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" numbers\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Create a new LotteryDraw object"}]},{"type":"text","value":"\nround1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LotteryDraw"},{"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":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2021"}]},{"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":"1"}]},{"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":"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":" "},{"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":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"13"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"33"}]},{"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","comment"]},"children":[{"type":"text","value":"# Tulostetaan tiedot"}]},{"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":"round1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"round_week"},{"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":"round1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"round_date"},{"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":" number "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" round1"},{"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":"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1\n2021-01-02\n1\n4\n8\n12\n13\n14\n33"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, the attributes can be of any type. Here, each LotteryDraw object has attributes of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Book","tmcname":"part08-05_book"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Book"}]},{"type":"text","value":" with the attributes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"author"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"genre"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"year"}]},{"type":"text","value":", along with a constructor which assigns initial values to these attributes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your class should work 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":"text","value":"python "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fluent 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":"\"Luciano Ramalho\""}]},{"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":"\"programming\""}]},{"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":"2015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\neverest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"High Adventure\""}]},{"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":"\"Edmund Hillary\""}]},{"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":"\"autobiography\""}]},{"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":"1956"}]},{"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":"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":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"author"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"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"},{"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\"The genre of the book "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"everest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" 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":"everest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"genre"},{"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":"Luciano Ramalho: Fluent Python (2015)\nThe genre of the book High Adventure is autobiography"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Three classes","tmcname":"part08-06_three_classes"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write the three classes specified below. Each class should have exactly the same names and types of attributes as listed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please also include a constructor in each class. The constructor should take the initial values of the attributes as its arguments, in the order listed below."}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Class Checklist"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribute header (string)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribute entries (list)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"ol","properties":{"start":2},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Class Customer"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribute id (string)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribute balance (float)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribute discount (integer)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"ol","properties":{"start":3},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Class Cable"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribute model (string)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribute length (float)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribute max_speed (integer)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribute bidirectional (Boolean)"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"using-objecs-formed-from-your-own-classes","style":"position:relative;"},"children":[{"type":"text","value":"Using objecs formed from your own classes"},{"type":"element","tagName":"a","properties":{"href":"#using-objecs-formed-from-your-own-classes","ariaLabel":"using objecs formed from your own classes 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":"Objects formed from your own class definitions are no different from any other Python objects. They can be passed as arguments and return values just like any other object. We could, for example, write some helper functions for working with bank accounts:"}]},{"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":"# this function creates a new bank account object and returns it"}]},{"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":"open_account"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"str"}]},{"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 new_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"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":" name"},{"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":" new_account\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# this function adds the amount passed as an argument to the balance of the bank account also passed as an argument"}]},{"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":"deposit_money_on_account"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" amount"},{"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 account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" amount\n\npeters_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" open_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ndeposit_money_on_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_account"},{"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":"500"}]},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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\n500"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Define class: Pet","tmcname":"part08-07_pet"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please define the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Pet"}]},{"type":"text","value":". The class should include a constructor, which takes the initial values of the attributes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"species"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"year_of_birth"}]},{"type":"text","value":" as its arguments, in that specific order."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please also write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"new_pet(name: str, species: str, year_of_birth: int)"}]},{"type":"text","value":" "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"outside the class definition"}]},{"type":"text","value":". The function should create and return a new object of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Pet"}]},{"type":"text","value":", as defined in the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Pet"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how the function is used:"}]},{"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":"fluffy "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" new_pet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fluffy\""}]},{"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":"\"dog\""}]},{"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":"2017"}]},{"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":"fluffy"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"fluffy"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"species"},{"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":"fluffy"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth"},{"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":"Fluffy\ndog\n2017"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The older book","tmcname":"part08-08_older_book"},"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":"older_book(book1: Book, book2: Book)"}]},{"type":"text","value":" which takes two objects of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Book"}]},{"type":"text","value":" as its arguments. The function should print out a message with the details of whichever is the older. It should print out a different message if the two books were written in the same year. Please see the examples below."}]},{"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":"python "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fluent 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":"\"Luciano Ramalho\""}]},{"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":"\"programming\""}]},{"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":"2015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\neverest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"High Adventure\""}]},{"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":"\"Edmund Hillary\""}]},{"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":"\"autobiography\""}]},{"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":"1956"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnorma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Norma\""}]},{"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":"\"Sofi Oksanen\""}]},{"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":"\"crime\""}]},{"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":"2015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nolder_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" everest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nolder_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" norma"},{"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":"High Adventure is older, it was published in 1956\nFluent Python and Norma were published in 2015"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Books of a genre","tmcname":"part08-09_books_of_genre"},"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":"books_of_genre(books: list, genre: str)"}]},{"type":"text","value":" which takes a list of objects of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Book"}]},{"type":"text","value":" and a string representing a genre as its arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"new"}]},{"type":"text","value":" list, which contains the books with the desired genre from the original list. Please see the examples below."}]},{"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":"python "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fluent 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":"\"Luciano Ramalho\""}]},{"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":"\"programming\""}]},{"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":"2015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\neverest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"High Adventure\""}]},{"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":"\"Edmund Hillary\""}]},{"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":"\"autobiography\""}]},{"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":"1956"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnorma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Norma\""}]},{"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":"\"Sofi Oksanen\""}]},{"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":"\"crime\""}]},{"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":"2015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbooks "},{"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":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" everest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" norma"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" Book"},{"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 Snowman\""}]},{"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":"\"Jo Nesbø\""}]},{"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":"\"crime\""}]},{"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":"2007"}]},{"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":"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":"\"Books in the crime genre:\""}]},{"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":" book "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" books_of_genre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"books"},{"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":"\"crime\""}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"author"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"Books in the crime genre:\nSofi Oksanen: Norma\nJo Nesbø: The Snowman"}]}]}]}]},"html":"

    After this section

      \n
    • You will know how to define your own classes
    • \n
    • You will be able to create objects based on classes you've defined yourself
    • \n
    • You will know how to write a constructor
    • \n
    • You are familiar with the parameter name self
    • \n
    • You will know what attributes are and how they are used
    • \n

    A class is defined with the keyword class. The syntax is as follows:

    class NameOfClass:\n    # class defition goes here

    Classes are usually named in camel case. This means that all the words in the class name are written together, without spaces, and each word is capitalised. The following class names follow this convention:

      \n
    • Weekday
    • \n
    • BankAccount
    • \n
    • LibraryDatabase
    • \n
    • PythonCourseGrades
    • \n

    A single class definition should represent a single whole, the contents of which should be atomically linked together in some way. In more complicated programs classes can contain members of other classes. For example, the class Course could contain objects of class Lecture, ExerciseSession etc.

    Lets have a look at a skeleton of a class definition. The functionalities are still missing at this point.

    class BankAccount:\n    pass

    The above piece of code tells Python that here we are defining a class named BankAccount. The class does not contain any functionality yet, but we can still create an object based on the class.

    Lets have a look at a program where two variables are added to a BankAccount object: balance and owner. Any variables attached to an object are called its attributes, or more specifically, data attributes, or sometimes instance variables.

    The attributes attached to an object can be accessed through the object:

    class BankAccount:\n    pass\n\npeters_account = BankAccount()\npeters_account.owner = \"Peter Python\"\npeters_account.balance = 5.0\n\nprint(peters_account.owner)\nprint(peters_account.balance)

    Peter Python\n5.0

    The data attributes are available only through the object they are attached to. Each BankAccount object created based on the BankAccount class has its own values attached to the data attributes. Those values can be accessed by referring to the object in question:

    account = BankAccount()\naccount.balance = 155.50\n\nprint(account.balance) # This refers to the data attribute balance attached to the account\nprint(balance) # THIS CAUSES AN ERROR, as there is no such independent variable available, and the object reference is missing

    Adding a constructor

    In the above example we saw that a new instance of a class can be created by calling the constructor method of the class like so: NameOfClass(). Above we then attached data attributes to the object separately, but it is often more convenient to pass these initial values of attributes directly as the object is created. In the above example we first had a BankAccount object without these attributes, and the attributes only existed after they were explicitly declared.

    Declaring attributes outside the constructor results in a situation where different instances of the same class can have different attributes. The following code produces an error because we now have another BankAccount object, paulas_account, which does not contain the same attributes:

    class BankAccount:\n    pass\n\npeters_account = BankAccount()\npeters_account.owner = \"Peter\"\npeters_account.balance = 1400\n\npaulas_account = BankAccount()\npaulas_account.owner = \"Paula\"\n\nprint(peters_account.balance)\nprint(paulas_account.balance) # THIS CAUSES AN ERROR

    So, instead of declaring attributes after each instance of the class is created, it is usually a better idea to initialize the values of the attributes as the class constructor is called. As the BankAccount class definition is currently just a skeleton, the constructor method is implicitly assumed by the Python interpreter, but it is possible to define your own constructor methods, and that is exactly what we will do now.

    A constructor method is a method declaration with the special name __init__, usually included at the very beginning of a class definition.

    Lets have a look at a BankAccount class with a constructor method added in:

    class BankAccount:\n\n    # The constructor\n    def __init__(self, balance: float, owner: str):\n        self.balance = balance\n        self.owner = owner

    The name of the constructor method is always __init__. Notice the two underscores on both sides of the word init.

    The first parameter in a constructor definition is always named self. This refers to the object itself, and is necessary for declaring any attributes attached to the object. The assignment

    self.balance = balance

    assigns the balance received as an argument to the balance attribute of the object. It is a common convention to use the same variable names for the parameters and the data attributes defined in a constructor, but the variable names self.balance and balance above refer to two different variables:

      \n
    • \n

      The variable self.balance is an attribute of the object. Each BankAccount object has its own balance.

      \n
    • \n
    • \n

      The variable balance is a parameter in the constructor method __init__. Its value is set to the value passed as an argument to the method as the constructor is called (that is, when a new insctance of the class is created).

      \n
    • \n

    Now that we have defined the parameters of the constructor method, we can pass the desired initial values of the data attributes as arguments as a new object is created:

    class BankAccount:\n\n    # The constructor\n    def __init__(self, balance: float, owner: str):\n        self.balance = balance\n        self.owner = owner\n\n# As the method is called, no argument should be given for the self parameter\n# Python assigns the value for self automatically\npeters_account = BankAccount(100, \"Peter Python\")\npaulas_account = BankAccount(20000, \"Paula Pythons\")\n\nprint(peters_account.balance)\nprint(paulas_account.balance)

    100\n20000

    It is now much easier to work with the BankAccount objects, as the values can be passed at object creation, and the resulting two separate instances can be handled more predictably and uniformly. Declaring data attributes in the constructor also ensures the attributes are actually declared, and the desired initial values are always given by the programmer using the class.

    It is still possible to change the initial values of the data attributes later in the program:

    class BankAccount:\n\n    # The constructor\n    def __init__(self, balance: float, owner: str):\n        self.balance = balance\n        self.owner = owner\n\npeters_account = BankAccount(100, \"Peter Python\")\nprint(peters_account.balance)\n\n# Change the balance to 1500\npeters_account.balance = 1500\nprint(peters_account.balance)\n\n# Add 2000 to the balance\npeters_account.balance += 2000\nprint(peters_account.balance)

    100\n1500\n3500

    Let's have a look at another example of classes and objects. We'll write a class which models a single draw of lottery numbers:

    from datetime import date\n\nclass LotteryDraw:\n\n    def __init__(self, round_week: int, round_date: date, numbers: list):\n        self.round_week = round_week\n        self.round_date = round_date\n        self.numbers = numbers\n\n\n# Create a new LotteryDraw object\nround1 = LotteryDraw(1, date(2021, 1, 2), [1,4,8,12,13,14,33])\n\n# Tulostetaan tiedot\nprint(round1.round_week)\nprint(round1.round_date)\n\nfor number in round1.numbers:\n    print(number)

    1\n2021-01-02\n1\n4\n8\n12\n13\n14\n33

    As you can see above, the attributes can be of any type. Here, each LotteryDraw object has attributes of type list and date.

    Please write a class named Book with the attributes name, author, genre and year, along with a constructor which assigns initial values to these attributes.

    Your class should work like this:

    python = Book(\"Fluent Python\", \"Luciano Ramalho\", \"programming\", 2015)\neverest = Book(\"High Adventure\", \"Edmund Hillary\", \"autobiography\", 1956)\n\nprint(f\"{python.author}: {python.name} ({python.year})\")\nprint(f\"The genre of the book {everest.name} is {everest.genre}\")

    Luciano Ramalho: Fluent Python (2015)\nThe genre of the book High Adventure is autobiography

    Please write the three classes specified below. Each class should have exactly the same names and types of attributes as listed.

    Please also include a constructor in each class. The constructor should take the initial values of the attributes as its arguments, in the order listed below.

      \n
    1. Class Checklist
    2. \n
      \n
    • attribute header (string)
    • \n
    • attribute entries (list)
    • \n
      \n
    1. Class Customer
    2. \n
      \n
    • attribute id (string)
    • \n
    • attribute balance (float)
    • \n
    • attribute discount (integer)
    • \n
      \n
    1. Class Cable
    2. \n
      \n
    • attribute model (string)
    • \n
    • attribute length (float)
    • \n
    • attribute max_speed (integer)
    • \n
    • attribute bidirectional (Boolean)
    • \n

    Using objecs formed from your own classes

    Objects formed from your own class definitions are no different from any other Python objects. They can be passed as arguments and return values just like any other object. We could, for example, write some helper functions for working with bank accounts:

    # this function creates a new bank account object and returns it\ndef open_account(name: str):\n    new_account =  BankAccount(0, name)\n    return new_account\n\n# this function adds the amount passed as an argument to the balance of the bank account also passed as an argument\ndef deposit_money_on_account(account: BankAccount, amount: int):\n    account.balance += amount\n\npeters_account = open_account(\"Peter Python\")\nprint(peters_account.balance)\n\ndeposit_money_on_account(peters_account, 500)\n\nprint(peters_account.balance)

    0\n500

    Please define the class Pet. The class should include a constructor, which takes the initial values of the attributes name, species and year_of_birth as its arguments, in that specific order.

    Please also write a function named new_pet(name: str, species: str, year_of_birth: int) outside the class definition. The function should create and return a new object of type Pet, as defined in the class Pet.

    An example of how the function is used:

    fluffy = new_pet(\"Fluffy\", \"dog\", 2017)\nprint(fluffy.name)\nprint(fluffy.species)\nprint(fluffy.year_of_birth)

    Fluffy\ndog\n2017

    Please write a function named older_book(book1: Book, book2: Book) which takes two objects of type Book as its arguments. The function should print out a message with the details of whichever is the older. It should print out a different message if the two books were written in the same year. Please see the examples below.

    python = Book(\"Fluent Python\", \"Luciano Ramalho\", \"programming\", 2015)\neverest = Book(\"High Adventure\", \"Edmund Hillary\", \"autobiography\", 1956)\nnorma = Book(\"Norma\", \"Sofi Oksanen\", \"crime\", 2015)\n\nolder_book(python, everest)\nolder_book(python, norma)

    High Adventure is older, it was published in 1956\nFluent Python and Norma were published in 2015

    Please write a function named books_of_genre(books: list, genre: str) which takes a list of objects of type Book and a string representing a genre as its arguments.

    The function should return a new list, which contains the books with the desired genre from the original list. Please see the examples below.

    python = Book(\"Fluent Python\", \"Luciano Ramalho\", \"programming\", 2015)\neverest = Book(\"High Adventure\", \"Edmund Hillary\", \"autobiography\", 1956)\nnorma = Book(\"Norma\", \"Sofi Oksanen\", \"crime\", 2015)\n\nbooks = [python, everest, norma, Book(\"The Snowman\", \"Jo Nesbø\", \"crime\", 2007)]\n\nprint(\"Books in the crime genre:\")\nfor book in books_of_genre(books, \"crime\"):\n    print(f\"{book.author}: {book.name}\")

    Books in the crime genre:\nSofi Oksanen: Norma\nJo Nesbø: The Snowman

    ","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-8/3-defining-classes.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know how to define your own classes"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to create objects based on classes you've defined yourself"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to write a constructor"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You are familiar with the parameter name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know what attributes are and how they are used"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A class is defined with the keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":". The syntax is as follows:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"NameOfClass"}]},{"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":"# class defition goes here"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Classes are usually named in "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"camel case"}]},{"type":"text","value":". This means that all the words in the class name are written together, without spaces, and each word is capitalised. The following class names follow this convention:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Weekday"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LibraryDatabase"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PythonCourseGrades"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A single class definition should represent a single whole, the contents of which should be atomically linked together in some way. In more complicated programs classes can contain members of other classes. For example, the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Course"}]},{"type":"text","value":" could contain objects of class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Lecture"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ExerciseSession"}]},{"type":"text","value":" etc."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lets have a look at a skeleton of a class definition. The functionalities are still missing at this 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":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"pass"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The above piece of code tells Python that here we are defining a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":". The class does not contain any functionality yet, but we can still create an object based on the class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lets have a look at a program where two variables are added to a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" object: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balance"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"owner"}]},{"type":"text","value":". Any variables attached to an object are called its "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"attributes"}]},{"type":"text","value":", or more specifically, "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"data attributes"}]},{"type":"text","value":", or sometimes "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"instance variables"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The attributes attached to an object can be accessed through the object:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"pass"}]},{"type":"text","value":"\n\npeters_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"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":"\npeters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"owner "},{"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":"\"Peter Python\""}]},{"type":"text","value":"\npeters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"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":"5.0"}]},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"owner"},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"Peter Python\n5.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The data attributes are available only through the object they are attached to. Each "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" object created based on the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" class has its own values attached to the data attributes. Those values can be accessed by referring to the object in question:"}]},{"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":"account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"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":"\naccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"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":"155.50"}]},{"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":"account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This refers to the data attribute balance attached to the account"}]},{"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":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# THIS CAUSES AN ERROR, as there is no such independent variable available, and the object reference is missing"}]}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"adding-a-constructor","style":"position:relative;"},"children":[{"type":"text","value":"Adding a constructor"},{"type":"element","tagName":"a","properties":{"href":"#adding-a-constructor","ariaLabel":"adding a constructor 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 the above example we saw that a new instance of a class can be created by calling the constructor method of the class like so: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NameOfClass()"}]},{"type":"text","value":". Above we then attached data attributes to the object separately, but it is often more convenient to pass these initial values of attributes directly as the object is created. In the above example we first had a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" object without these attributes, and the attributes only existed after they were explicitly declared."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Declaring attributes outside the constructor results in a situation where different instances of the same class can have different attributes. The following code produces an error because we now have another "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" object, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"paulas_account"}]},{"type":"text","value":", which does not contain the same attributes:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"pass"}]},{"type":"text","value":"\n\npeters_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"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":"\npeters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"owner "},{"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":"\"Peter\""}]},{"type":"text","value":"\npeters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"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":"1400"}]},{"type":"text","value":"\n\npaulas_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"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":"\npaulas_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"owner "},{"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":"\"Paula\""}]},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"paulas_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# THIS CAUSES AN ERROR"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, instead of declaring attributes after each instance of the class is created, it is usually a better idea to initialize the values of the attributes as the class constructor is called. As the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" class definition is currently just a skeleton, the constructor method is implicitly assumed by the Python interpreter, but it is possible to define your own constructor methods, and that is exactly what we will do now."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A constructor method is a method declaration with the special name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__init__"}]},{"type":"text","value":", usually included at the very beginning of a class definition."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Lets have a look at a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" class with a constructor method added in:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"# The constructor"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" owner"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"owner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" owner"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The name of the constructor method is always "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__init__"}]},{"type":"text","value":". Notice the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"two underscores on both sides"}]},{"type":"text","value":" of the word "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"init"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The first parameter in a constructor definition is always named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":". This refers to the object itself, and is necessary for declaring any attributes attached to the object. The assignment"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.balance = balance"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"assigns the balance received as an argument to the balance attribute of the object. It is a common convention to use the same variable names for the parameters and the data attributes defined in a constructor, but the variable names "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.balance"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balance"}]},{"type":"text","value":" above refer to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"two different variables"}]},{"type":"text","value":":"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.balance"}]},{"type":"text","value":" is an attribute of the object. Each "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" object has its own balance."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balance"}]},{"type":"text","value":" is a parameter in the constructor method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__init__"}]},{"type":"text","value":". Its value is set to the value passed as an argument to the method as the constructor is called (that is, when a new insctance of the class is created)."}]},{"type":"text","value":"\n"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Now that we have defined the parameters of the constructor method, we can pass the desired initial values of the data attributes as arguments as a new object is created:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"# The constructor"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" owner"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"owner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" owner\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# As the method is called, no argument should be given for the self parameter"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Python assigns the value for self automatically"}]},{"type":"text","value":"\npeters_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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":"\"Peter Python\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npaulas_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20000"}]},{"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":"\"Paula Pythons\""}]},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"paulas_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"100\n20000"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is now much easier to work with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" objects, as the values can be passed at object creation, and the resulting two separate instances can be handled more predictably and uniformly. Declaring data attributes in the constructor also ensures the attributes are actually declared, and the desired initial values are always given by the programmer using the class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is still possible to change the initial values of the data attributes later in the program:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"# The constructor"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" owner"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"owner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" owner\n\npeters_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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":"\"Peter Python\""}]},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"# Change the balance to 1500"}]},{"type":"text","value":"\npeters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"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":"1500"}]},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"# Add 2000 to the balance"}]},{"type":"text","value":"\npeters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"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":"2000"}]},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"100\n1500\n3500"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at another example of classes and objects. We'll write a class which models a single draw of lottery numbers:"}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" date\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"LotteryDraw"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" round_week"},{"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":"text","value":" round_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" date"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"round_week "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" round_week\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"round_date "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" round_date\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" numbers\n\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Create a new LotteryDraw object"}]},{"type":"text","value":"\nround1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LotteryDraw"},{"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":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2021"}]},{"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":"1"}]},{"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":"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":" "},{"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":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"13"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"33"}]},{"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","comment"]},"children":[{"type":"text","value":"# Tulostetaan tiedot"}]},{"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":"round1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"round_week"},{"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":"round1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"round_date"},{"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":" number "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" round1"},{"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":"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":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"1\n2021-01-02\n1\n4\n8\n12\n13\n14\n33"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, the attributes can be of any type. Here, each LotteryDraw object has attributes of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"list"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"date"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Book","tmcname":"part08-05_book"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Book"}]},{"type":"text","value":" with the attributes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"author"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"genre"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"year"}]},{"type":"text","value":", along with a constructor which assigns initial values to these attributes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your class should work 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":"text","value":"python "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fluent 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":"\"Luciano Ramalho\""}]},{"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":"\"programming\""}]},{"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":"2015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\neverest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"High Adventure\""}]},{"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":"\"Edmund Hillary\""}]},{"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":"\"autobiography\""}]},{"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":"1956"}]},{"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":"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":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"author"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"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"},{"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\"The genre of the book "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"everest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" 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":"everest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"genre"},{"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":"Luciano Ramalho: Fluent Python (2015)\nThe genre of the book High Adventure is autobiography"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Three classes","tmcname":"part08-06_three_classes"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write the three classes specified below. Each class should have exactly the same names and types of attributes as listed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please also include a constructor in each class. The constructor should take the initial values of the attributes as its arguments, in the order listed below."}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Class Checklist"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribute header (string)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribute entries (list)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"ol","properties":{"start":2},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Class Customer"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribute id (string)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribute balance (float)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribute discount (integer)"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"ol","properties":{"start":3},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Class Cable"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribute model (string)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribute length (float)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribute max_speed (integer)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"attribute bidirectional (Boolean)"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"using-objecs-formed-from-your-own-classes","style":"position:relative;"},"children":[{"type":"text","value":"Using objecs formed from your own classes"},{"type":"element","tagName":"a","properties":{"href":"#using-objecs-formed-from-your-own-classes","ariaLabel":"using objecs formed from your own classes 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":"Objects formed from your own class definitions are no different from any other Python objects. They can be passed as arguments and return values just like any other object. We could, for example, write some helper functions for working with bank accounts:"}]},{"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":"# this function creates a new bank account object and returns it"}]},{"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":"open_account"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"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":"str"}]},{"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 new_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"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":" name"},{"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":" new_account\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# this function adds the amount passed as an argument to the balance of the bank account also passed as an argument"}]},{"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":"deposit_money_on_account"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" amount"},{"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 account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" amount\n\npeters_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" open_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Python\""}]},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ndeposit_money_on_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_account"},{"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":"500"}]},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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\n500"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Define class: Pet","tmcname":"part08-07_pet"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please define the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Pet"}]},{"type":"text","value":". The class should include a constructor, which takes the initial values of the attributes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"species"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"year_of_birth"}]},{"type":"text","value":" as its arguments, in that specific order."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please also write a function named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"new_pet(name: str, species: str, year_of_birth: int)"}]},{"type":"text","value":" "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"outside the class definition"}]},{"type":"text","value":". The function should create and return a new object of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Pet"}]},{"type":"text","value":", as defined in the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Pet"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how the function is used:"}]},{"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":"fluffy "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" new_pet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fluffy\""}]},{"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":"\"dog\""}]},{"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":"2017"}]},{"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":"fluffy"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"fluffy"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"species"},{"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":"fluffy"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth"},{"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":"Fluffy\ndog\n2017"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The older book","tmcname":"part08-08_older_book"},"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":"older_book(book1: Book, book2: Book)"}]},{"type":"text","value":" which takes two objects of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Book"}]},{"type":"text","value":" as its arguments. The function should print out a message with the details of whichever is the older. It should print out a different message if the two books were written in the same year. Please see the examples below."}]},{"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":"python "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fluent 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":"\"Luciano Ramalho\""}]},{"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":"\"programming\""}]},{"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":"2015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\neverest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"High Adventure\""}]},{"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":"\"Edmund Hillary\""}]},{"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":"\"autobiography\""}]},{"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":"1956"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnorma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Norma\""}]},{"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":"\"Sofi Oksanen\""}]},{"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":"\"crime\""}]},{"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":"2015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nolder_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" everest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nolder_book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" norma"},{"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":"High Adventure is older, it was published in 1956\nFluent Python and Norma were published in 2015"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Books of a genre","tmcname":"part08-09_books_of_genre"},"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":"books_of_genre(books: list, genre: str)"}]},{"type":"text","value":" which takes a list of objects of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Book"}]},{"type":"text","value":" and a string representing a genre as its arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"new"}]},{"type":"text","value":" list, which contains the books with the desired genre from the original list. Please see the examples below."}]},{"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":"python "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fluent 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":"\"Luciano Ramalho\""}]},{"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":"\"programming\""}]},{"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":"2015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\neverest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"High Adventure\""}]},{"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":"\"Edmund Hillary\""}]},{"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":"\"autobiography\""}]},{"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":"1956"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nnorma "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Norma\""}]},{"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":"\"Sofi Oksanen\""}]},{"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":"\"crime\""}]},{"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":"2015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbooks "},{"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":"text","value":"python"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" everest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" norma"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" Book"},{"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 Snowman\""}]},{"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":"\"Jo Nesbø\""}]},{"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":"\"crime\""}]},{"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":"2007"}]},{"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":"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":"\"Books in the crime genre:\""}]},{"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":" book "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" books_of_genre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"books"},{"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":"\"crime\""}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"author"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"Books in the crime genre:\nSofi Oksanen: Norma\nJo Nesbø: The Snowman"}]}]}]}]},"html":"

    After this section

      \n
    • You will know how to define your own classes
    • \n
    • You will be able to create objects based on classes you've defined yourself
    • \n
    • You will know how to write a constructor
    • \n
    • You are familiar with the parameter name self
    • \n
    • You will know what attributes are and how they are used
    • \n

    A class is defined with the keyword class. The syntax is as follows:

    class NameOfClass:\n    # class defition goes here

    Classes are usually named in camel case. This means that all the words in the class name are written together, without spaces, and each word is capitalised. The following class names follow this convention:

      \n
    • Weekday
    • \n
    • BankAccount
    • \n
    • LibraryDatabase
    • \n
    • PythonCourseGrades
    • \n

    A single class definition should represent a single whole, the contents of which should be atomically linked together in some way. In more complicated programs classes can contain members of other classes. For example, the class Course could contain objects of class Lecture, ExerciseSession etc.

    Lets have a look at a skeleton of a class definition. The functionalities are still missing at this point.

    class BankAccount:\n    pass

    The above piece of code tells Python that here we are defining a class named BankAccount. The class does not contain any functionality yet, but we can still create an object based on the class.

    Lets have a look at a program where two variables are added to a BankAccount object: balance and owner. Any variables attached to an object are called its attributes, or more specifically, data attributes, or sometimes instance variables.

    The attributes attached to an object can be accessed through the object:

    class BankAccount:\n    pass\n\npeters_account = BankAccount()\npeters_account.owner = \"Peter Python\"\npeters_account.balance = 5.0\n\nprint(peters_account.owner)\nprint(peters_account.balance)

    Peter Python\n5.0

    The data attributes are available only through the object they are attached to. Each BankAccount object created based on the BankAccount class has its own values attached to the data attributes. Those values can be accessed by referring to the object in question:

    account = BankAccount()\naccount.balance = 155.50\n\nprint(account.balance) # This refers to the data attribute balance attached to the account\nprint(balance) # THIS CAUSES AN ERROR, as there is no such independent variable available, and the object reference is missing

    Adding a constructor

    In the above example we saw that a new instance of a class can be created by calling the constructor method of the class like so: NameOfClass(). Above we then attached data attributes to the object separately, but it is often more convenient to pass these initial values of attributes directly as the object is created. In the above example we first had a BankAccount object without these attributes, and the attributes only existed after they were explicitly declared.

    Declaring attributes outside the constructor results in a situation where different instances of the same class can have different attributes. The following code produces an error because we now have another BankAccount object, paulas_account, which does not contain the same attributes:

    class BankAccount:\n    pass\n\npeters_account = BankAccount()\npeters_account.owner = \"Peter\"\npeters_account.balance = 1400\n\npaulas_account = BankAccount()\npaulas_account.owner = \"Paula\"\n\nprint(peters_account.balance)\nprint(paulas_account.balance) # THIS CAUSES AN ERROR

    So, instead of declaring attributes after each instance of the class is created, it is usually a better idea to initialize the values of the attributes as the class constructor is called. As the BankAccount class definition is currently just a skeleton, the constructor method is implicitly assumed by the Python interpreter, but it is possible to define your own constructor methods, and that is exactly what we will do now.

    A constructor method is a method declaration with the special name __init__, usually included at the very beginning of a class definition.

    Lets have a look at a BankAccount class with a constructor method added in:

    class BankAccount:\n\n    # The constructor\n    def __init__(self, balance: float, owner: str):\n        self.balance = balance\n        self.owner = owner

    The name of the constructor method is always __init__. Notice the two underscores on both sides of the word init.

    The first parameter in a constructor definition is always named self. This refers to the object itself, and is necessary for declaring any attributes attached to the object. The assignment

    self.balance = balance

    assigns the balance received as an argument to the balance attribute of the object. It is a common convention to use the same variable names for the parameters and the data attributes defined in a constructor, but the variable names self.balance and balance above refer to two different variables:

      \n
    • \n

      The variable self.balance is an attribute of the object. Each BankAccount object has its own balance.

      \n
    • \n
    • \n

      The variable balance is a parameter in the constructor method __init__. Its value is set to the value passed as an argument to the method as the constructor is called (that is, when a new insctance of the class is created).

      \n
    • \n

    Now that we have defined the parameters of the constructor method, we can pass the desired initial values of the data attributes as arguments as a new object is created:

    class BankAccount:\n\n    # The constructor\n    def __init__(self, balance: float, owner: str):\n        self.balance = balance\n        self.owner = owner\n\n# As the method is called, no argument should be given for the self parameter\n# Python assigns the value for self automatically\npeters_account = BankAccount(100, \"Peter Python\")\npaulas_account = BankAccount(20000, \"Paula Pythons\")\n\nprint(peters_account.balance)\nprint(paulas_account.balance)

    100\n20000

    It is now much easier to work with the BankAccount objects, as the values can be passed at object creation, and the resulting two separate instances can be handled more predictably and uniformly. Declaring data attributes in the constructor also ensures the attributes are actually declared, and the desired initial values are always given by the programmer using the class.

    It is still possible to change the initial values of the data attributes later in the program:

    class BankAccount:\n\n    # The constructor\n    def __init__(self, balance: float, owner: str):\n        self.balance = balance\n        self.owner = owner\n\npeters_account = BankAccount(100, \"Peter Python\")\nprint(peters_account.balance)\n\n# Change the balance to 1500\npeters_account.balance = 1500\nprint(peters_account.balance)\n\n# Add 2000 to the balance\npeters_account.balance += 2000\nprint(peters_account.balance)

    100\n1500\n3500

    Let's have a look at another example of classes and objects. We'll write a class which models a single draw of lottery numbers:

    from datetime import date\n\nclass LotteryDraw:\n\n    def __init__(self, round_week: int, round_date: date, numbers: list):\n        self.round_week = round_week\n        self.round_date = round_date\n        self.numbers = numbers\n\n\n# Create a new LotteryDraw object\nround1 = LotteryDraw(1, date(2021, 1, 2), [1,4,8,12,13,14,33])\n\n# Tulostetaan tiedot\nprint(round1.round_week)\nprint(round1.round_date)\n\nfor number in round1.numbers:\n    print(number)

    1\n2021-01-02\n1\n4\n8\n12\n13\n14\n33

    As you can see above, the attributes can be of any type. Here, each LotteryDraw object has attributes of type list and date.

    Please write a class named Book with the attributes name, author, genre and year, along with a constructor which assigns initial values to these attributes.

    Your class should work like this:

    python = Book(\"Fluent Python\", \"Luciano Ramalho\", \"programming\", 2015)\neverest = Book(\"High Adventure\", \"Edmund Hillary\", \"autobiography\", 1956)\n\nprint(f\"{python.author}: {python.name} ({python.year})\")\nprint(f\"The genre of the book {everest.name} is {everest.genre}\")

    Luciano Ramalho: Fluent Python (2015)\nThe genre of the book High Adventure is autobiography

    Please write the three classes specified below. Each class should have exactly the same names and types of attributes as listed.

    Please also include a constructor in each class. The constructor should take the initial values of the attributes as its arguments, in the order listed below.

      \n
    1. Class Checklist
    2. \n
      \n
    • attribute header (string)
    • \n
    • attribute entries (list)
    • \n
      \n
    1. Class Customer
    2. \n
      \n
    • attribute id (string)
    • \n
    • attribute balance (float)
    • \n
    • attribute discount (integer)
    • \n
      \n
    1. Class Cable
    2. \n
      \n
    • attribute model (string)
    • \n
    • attribute length (float)
    • \n
    • attribute max_speed (integer)
    • \n
    • attribute bidirectional (Boolean)
    • \n

    Using objecs formed from your own classes

    Objects formed from your own class definitions are no different from any other Python objects. They can be passed as arguments and return values just like any other object. We could, for example, write some helper functions for working with bank accounts:

    # this function creates a new bank account object and returns it\ndef open_account(name: str):\n    new_account =  BankAccount(0, name)\n    return new_account\n\n# this function adds the amount passed as an argument to the balance of the bank account also passed as an argument\ndef deposit_money_on_account(account: BankAccount, amount: int):\n    account.balance += amount\n\npeters_account = open_account(\"Peter Python\")\nprint(peters_account.balance)\n\ndeposit_money_on_account(peters_account, 500)\n\nprint(peters_account.balance)

    0\n500

    Please define the class Pet. The class should include a constructor, which takes the initial values of the attributes name, species and year_of_birth as its arguments, in that specific order.

    Please also write a function named new_pet(name: str, species: str, year_of_birth: int) outside the class definition. The function should create and return a new object of type Pet, as defined in the class Pet.

    An example of how the function is used:

    fluffy = new_pet(\"Fluffy\", \"dog\", 2017)\nprint(fluffy.name)\nprint(fluffy.species)\nprint(fluffy.year_of_birth)

    Fluffy\ndog\n2017

    Please write a function named older_book(book1: Book, book2: Book) which takes two objects of type Book as its arguments. The function should print out a message with the details of whichever is the older. It should print out a different message if the two books were written in the same year. Please see the examples below.

    python = Book(\"Fluent Python\", \"Luciano Ramalho\", \"programming\", 2015)\neverest = Book(\"High Adventure\", \"Edmund Hillary\", \"autobiography\", 1956)\nnorma = Book(\"Norma\", \"Sofi Oksanen\", \"crime\", 2015)\n\nolder_book(python, everest)\nolder_book(python, norma)

    High Adventure is older, it was published in 1956\nFluent Python and Norma were published in 2015

    Please write a function named books_of_genre(books: list, genre: str) which takes a list of objects of type Book and a string representing a genre as its arguments.

    The function should return a new list, which contains the books with the desired genre from the original list. Please see the examples below.

    python = Book(\"Fluent Python\", \"Luciano Ramalho\", \"programming\", 2015)\neverest = Book(\"High Adventure\", \"Edmund Hillary\", \"autobiography\", 1956)\nnorma = Book(\"Norma\", \"Sofi Oksanen\", \"crime\", 2015)\n\nbooks = [python, everest, norma, Book(\"The Snowman\", \"Jo Nesbø\", \"crime\", 2007)]\n\nprint(\"Books in the crime genre:\")\nfor book in books_of_genre(books, \"crime\"):\n    print(f\"{book.author}: {book.name}\")

    Books in the crime genre:\nSofi Oksanen: Norma\nJo Nesbø: The Snowman

    ","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-8/3-defining-classes.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-8/4-defining-methods/page-data.json b/page-data/part-8/4-defining-methods/page-data.json index 8dfebcf1b..ffaa3085a 100644 --- a/page-data/part-8/4-defining-methods/page-data.json +++ b/page-data/part-8/4-defining-methods/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-8/4-defining-methods", - "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 know how class methods work"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to write new methods in your own classes"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will understand the concepts of encapsulation and client in object oriented programming"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Classes which contain only data attributes are not very different from dictionaries. Below you will find two ways to model a bank account, first with a class definition, and then using a dictionary."}]},{"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":"# Example 1: bank account with class definition"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" account_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" owner"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" annual_interest"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"account_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" account_number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"owner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" owner\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"annual_interest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" annual_interest\n\npeters_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"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":"\"Peter Python\""}]},{"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":"1500.0"}]},{"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":"0.015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"# Example 2: bank account with dictionary"}]},{"type":"text","value":"\npeters_account "},{"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":"\"account_number\""}]},{"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":"\"12345-678\""}]},{"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":"\"owner\""}]},{"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":"\"Peter 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":"\"balance\""}]},{"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":"1500.0"}]},{"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":"\"annual_interest\""}]},{"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":"0.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With a dictionary the implementation is much shorter and more straightforward. With a class, however, the structure is more \"tightly bound\", so that we can expect all "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" objects to be structurally alike. A class is also named. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" class is referenced when creating a new bank account, and the type of the object is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":", not "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dict"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Another significant advantage of classes is that in addition to data, they can contain functionality. One of the guiding principles of object oriented programming is that an object is used to access both the data attached to an object and the functionality to process that data."}]},{"type":"element","tagName":"h2","properties":{"id":"methods-in-classes","style":"position:relative;"},"children":[{"type":"text","value":"Methods in classes"},{"type":"element","tagName":"a","properties":{"href":"#methods-in-classes","ariaLabel":"methods in classes 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":"A method is a subprogram or function that is bound to a specific class. Usually a method only affects a single object. A method is defined within the class definition, and it can access the data attributes of the class just like any other variable."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's continue with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" class introduced above. Below we have a new method which adds interest to the account:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" account_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" owner"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" annual_interest"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"account_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" account_number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"owner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" owner\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"annual_interest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" annual_interest\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This method adds the annual interest to the balance of the account"}]},{"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":"add_interest"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"annual_interest\n\n\npeters_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"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":"\"Peter Python\""}]},{"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":"1500.0"}]},{"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":"0.015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_interest"},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"1522.5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_interest"}]},{"type":"text","value":" method multiplies the balance of the account by the annual interest percentage, and then adds the result to the current balance. The method acts only on the object which it is called on."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's see how this works when we have created multiple instances of the class:"}]},{"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":"# The class BankAccount is defined in the previous example"}]},{"type":"text","value":"\n\npeters_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"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":"\"Peter Python\""}]},{"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":"1500.0"}]},{"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":"0.015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npaulas_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"99999-999\""}]},{"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":"\"Paula Pythonen\""}]},{"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":"1500.0"}]},{"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":"0.05"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npippas_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1111-222\""}]},{"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":"\"Pippa Programmer\""}]},{"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":"1500.0"}]},{"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":"0.001"}]},{"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":"# Add interest on Peter's and Paula's accounts, but not on Pippa's"}]},{"type":"text","value":"\npeters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_interest"},{"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":"\npaulas_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_interest"},{"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","comment"]},"children":[{"type":"text","value":"# Print all account balances"}]},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"paulas_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"pippas_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"1522.5\n1575.0\n1500.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, the annual interest is added only to those accounts which the method is called on. As the annual interest rates are different for Peter's and Paula's accounts, the results are different for these two accounts. The balance on Pippa's account does not change, because the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_interest"}]},{"type":"text","value":" method is not called on the object "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pippas_account"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"encapsulation","style":"position:relative;"},"children":[{"type":"text","value":"Encapsulation"},{"type":"element","tagName":"a","properties":{"href":"#encapsulation","ariaLabel":"encapsulation 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 object oriented programming the word "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"client"}]},{"type":"text","value":" comes up from time to time. This is used to refer to a section of code which creates an object and uses the service provided by its methods. When the data contained in an object is used only through the methods it provides, the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"internal integrity"}]},{"type":"text","value":" of the object is guaranteed. In practice this means that, for example, a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" class offers methods to handle the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balance"}]},{"type":"text","value":" attribute, so the balance is never accessed directly by the client. These methods can then verify that the balance is not allowed to go below zero, for instance."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how this would 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":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" account_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" owner"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" annual_interest"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"account_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" account_number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"owner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" owner\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"annual_interest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" annual_interest\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This method adds the annual interest to the balance of the account"}]},{"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":"add_interest"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"annual_interest\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This method \"withdraws\" money from the account"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If the withdrawal is successful the method returns True, and False otherwise"}]},{"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":"withdraw"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" amount"},{"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":"float"}]},{"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":"if"}]},{"type":"text","value":" amount "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" amount\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\npeters_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"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":"\"Peter Python\""}]},{"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":"1500.0"}]},{"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":"0.015"}]},{"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":"if"}]},{"type":"text","value":" peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"withdraw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"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 withdrawal was successful, the balance is now\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"else"}]},{"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 withdrawal was unsuccessful, the balance is insufficient\""}]},{"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":"# Yritetään uudestaan"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"withdraw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"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 withdrawal was successful, the balance is now\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"else"}]},{"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 withdrawal was unsuccessful, the balance is insufficient\""}]},{"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":"The withdrawal was successful, the balance is now 500.0\nThe withdrawal was unsuccessful, the balance is insufficient"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Maintaining the internal integrity of the object and offering suitable methods to ensure this is called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"encapsulation"}]},{"type":"text","value":". The idea is that the inner workings of the object are hidden from the client, but the object offers methods which can be used to access the data stored in the object."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Adding a method does not automatically hide the attribute. Even though the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" class definition contains the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"withdraw"}]},{"type":"text","value":" method for withdrawing money, the client code can still access and change the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balance"}]},{"type":"text","value":" attribute directly:"}]},{"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":"peters_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"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":"\"Peter Python\""}]},{"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":"1500.0"}]},{"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":"0.015"}]},{"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":"# Attempt to withdraw 2000"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"withdraw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2000"}]},{"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 withdrawal was successful, the balance is now\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"else"}]},{"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 withdrawal was unsuccessful, the balance is insufficient\""}]},{"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":"# \"Force\" the withdrawal of 2000"}]},{"type":"text","value":"\n peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"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":"2000"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The balance is now:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"The withdrawal was unsuccessful, the balance is insufficient\nThe balance is now: -500.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is possible to hide the data attributes from the client code, which can help in solving this problem. We will return to this topic in the next part."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Decreasing counter","tmcname":"part08-10_decreasing_counter"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This exercise has multiple parts. You can submit the parts separately. Each part is worth one exercise point."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains a partially completed class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"DecreasingCounter"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"DecreasingCounter"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" initial_value"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"value "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" initial_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":"print_value"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"\"value:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"decrease"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"pass"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# define the rest of your methods here"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The class can now be used as shown below, and should produce the following printout after completing the first part of the exercise:"}]},{"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":"counter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" DecreasingCounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"decrease"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"decrease"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"value: 10\nvalue: 9\nvalue: 8"}]}]},{"type":"element","tagName":"h3","properties":{"id":"decreasing-the-value-of-the-counter","style":"position:relative;"},"children":[{"type":"text","value":"Decreasing the value of the counter"},{"type":"element","tagName":"a","properties":{"href":"#decreasing-the-value-of-the-counter","ariaLabel":"decreasing the value of the counter 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":"Please complete the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"decrease"}]},{"type":"text","value":" defined in the template, so that it decreases the value stored in the counter by one. See the example above for expected behaviour."}]},{"type":"element","tagName":"h3","properties":{"id":"the-counter-must-not-have-a-negative-value","style":"position:relative;"},"children":[{"type":"text","value":"The counter must not have a negative value"},{"type":"element","tagName":"a","properties":{"href":"#the-counter-must-not-have-a-negative-value","ariaLabel":"the counter must not have a negative value 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":"Please add functionality to your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"decrease"}]},{"type":"text","value":" method, so that the value of the counter will never reach negative values. If the value of the counter is 0, it will not be further decreased."}]},{"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":"counter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" DecreasingCounter"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"decrease"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"decrease"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"decrease"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"value: 2\nvalue: 1\nvalue: 0\nvalue: 0"}]}]},{"type":"element","tagName":"h3","properties":{"id":"setting-the-value-to-zero","style":"position:relative;"},"children":[{"type":"text","value":"Setting the value to zero"},{"type":"element","tagName":"a","properties":{"href":"#setting-the-value-to-zero","ariaLabel":"setting the value to zero 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":"Please add a method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"set_to_zero"}]},{"type":"text","value":" which sets the value of the counter to 0:"}]},{"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":"counter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" DecreasingCounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_to_zero"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"value: 100\nvalue: 0"}]}]},{"type":"element","tagName":"h3","properties":{"id":"resetting-the-counter","style":"position:relative;"},"children":[{"type":"text","value":"Resetting the counter"},{"type":"element","tagName":"a","properties":{"href":"#resetting-the-counter","ariaLabel":"resetting the counter 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":"Please add a method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reset_original_value()"}]},{"type":"text","value":" which resets the counter to its initial state:"}]},{"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":"counter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" DecreasingCounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"55"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"decrease"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"decrease"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"decrease"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"decrease"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"reset_original_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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"value: 51\nvalue: 55"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To finish off this section, lets have a look at a class which models the personal best of a player. The class definition contains separate validator methods which ascertain that the arguments passed are valid. The methods are called already within the constructor. This ensures the object created is internally sound."}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" date\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PersonalBest"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" day"},{"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":"text","value":" month"},{"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":"text","value":" year"},{"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":"text","value":" points"},{"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","comment"]},"children":[{"type":"text","value":"# Default values"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"player "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"date_of_pb "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1900"}]},{"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":"1"}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"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":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name_ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"player"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"player "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" player\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"date_ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"day"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" month"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" year"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"date_of_pb "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"year"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" month"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" day"},{"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":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"points_ok"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"points "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" points\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Helper methods to check the arguments are valid"}]},{"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":"name_ok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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":" "},{"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"},{"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":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Name should be at least two characters long"}]},{"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":"date_ok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" day"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" month"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" year"},{"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":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"year"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" month"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" day"},{"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","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"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":"# an exception is raised if the arguments are not valid"}]},{"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","boolean"]},"children":[{"type":"text","value":"False"}]},{"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":"points_ok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" 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":"0"}]},{"type":"text","value":"\n\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 result1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PersonalBest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"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":"1"}]},{"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":"11"}]},{"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":"2020"}]},{"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":"235"}]},{"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":"result1"},{"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":"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":"result1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"player"},{"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":"result1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"date_of_pb"},{"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":"# The date was not valid"}]},{"type":"text","value":"\n result2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PersonalBest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Paula\""}]},{"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":"4"}]},{"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":"13"}]},{"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":"2019"}]},{"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":"4555"}]},{"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":"result2"},{"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":"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":"result2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"player"},{"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":"result2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"date_of_pb"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Tulostaa oletusarvon 1900-01-01"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"235\nPeter\n2020-11-01\n4555\nPaula\n1900-01-01"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the example above also the helper methods were called via the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" parameter name when they were used in the constructor. It is possible to also include "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"static"}]},{"type":"text","value":" method definitions in class definitions. These are methods which can be called without ever creating an instance of the class. We will return to this subject in the next part."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The parameter name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" is only used when referring to the features of the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"object as an instance of the class"}]},{"type":"text","value":". These include both the data attributes and the methods attached to an object. To make the terminology more confusing, the data attributes and methods are together sometimes referred to simply as the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"attributes"}]},{"type":"text","value":" of the object, which is why in this material we have often specified "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"data attributes"}]},{"type":"text","value":" when we mean the variables defined within the class. This is where the terminology of some Python programmers slightly differs from the terminology used in object oriented programming more widely, where "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"attributes"}]},{"type":"text","value":" usually refers to just the data attributes of an object."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is also possible to create local variables within method definitions without referring to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":". You should do so if there is no need to access the variables outside the method. Local variables within methods have no special keywords; they are used just like any normal variables you have come across thus far."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, for example this would 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":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BonusCard"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\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":"add_bonus"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# The variable bonus below is a local variable."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# It is not a data attribute of the object."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# It can not be accessed directly through the object."}]},{"type":"text","value":"\n bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"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.25"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" bonus\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":"add_superbonus"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# The superbonus variable is also a local variable."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Usually helper variables are local variables because"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# there is no need to access them from the other"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# methods in the class or directly through an object."}]},{"type":"text","value":"\n superbonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"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.5"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" superbonus\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"BonusCard(name="}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", balance="}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"programming-exercise","properties":{"name":"First and last name","tmcname":"part08-11_first_and_last_name"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" with a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"single attribute"}]},{"type":"text","value":" "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":", which is set with an argument given to the constructor."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please also add two methods:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return_first_name"}]},{"type":"text","value":" should return the first name of the person, while the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return_last_name"}]},{"type":"text","value":" should return the last name of the person."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume that the name passed to the constructor will contain exactly two name elements separated with a space character."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example use case:"}]},{"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":"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 peter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Pythons\""}]},{"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":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"return_first_name"},{"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 "},{"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":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"return_last_name"},{"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\n paula "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Paula Pythonnen\""}]},{"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":"paula"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"return_first_name"},{"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 "},{"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":"paula"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"return_last_name"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter\nPythons\nPaula\nPythonnen"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Statistics on numbers","tmcname":"part08-12_number_stats"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you are asked to create a program for working with numbers, similarly to the exercise completed at the "},{"type":"element","tagName":"a","properties":{"href":"/part-2/4-simple-loops#programming-exercise-working-with-numbers"},"children":[{"type":"text","value":"end of part 2"}]},{"type":"text","value":" in the Introduction to Programming course. This time you will define a class for the purpose."}]},{"type":"element","tagName":"h3","properties":{"id":"count-the-numbers","style":"position:relative;"},"children":[{"type":"text","value":"Count the numbers"},{"type":"element","tagName":"a","properties":{"href":"#count-the-numbers","ariaLabel":"count the numbers 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":"Please write a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NumberStats"}]},{"type":"text","value":" with the following methods:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_number"}]},{"type":"text","value":" adds a new number to the statistical record"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"count_numbers"}]},{"type":"text","value":" returns the count of how many numbers have been added"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"At this point there is no need to store the numbers themselves in any data structure. It is enough to simply remember how many have been added. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_number"}]},{"type":"text","value":" method does take an argument, but there is no need to process the actual value in any way just yet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the following skeleton for the class definition:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"NumberStats"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"numbers "},{"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":"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":"add_number"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","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":"pass"}]},{"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":"count_numbers"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"pass"}]}]}]}]},{"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":"stats "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" NumberStats"},{"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":"\nstats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"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":"\nstats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"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":"\nstats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"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":"text","value":"\nstats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Numbers added:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" stats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"count_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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Numbers added: 4"}]}]},{"type":"element","tagName":"h3","properties":{"id":"the-sum-and-the-mean","style":"position:relative;"},"children":[{"type":"text","value":"The sum and the mean"},{"type":"element","tagName":"a","properties":{"href":"#the-sum-and-the-mean","ariaLabel":"the sum and the mean 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":"Please add the following methods to your class definition:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"get_sum"}]},{"type":"text","value":" should return the sum of the numbers added (if no numbers have been added, the method should return 0)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"average"}]},{"type":"text","value":" should return the mean of the numbers added (if no numbers have been added, the method should return 0)"}]},{"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":"stats "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" NumberStats"},{"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":"\nstats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"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":"\nstats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"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":"\nstats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"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":"text","value":"\nstats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Numbers added:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" stats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"count_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","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":"\"Sum of numbers:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" stats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_sum"},{"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"},{"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":"\"Mean of numbers:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" stats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"average"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Numbers added: 4\nSum of numbers: 11\nMean of numbers: 2.75"}]}]},{"type":"element","tagName":"h3","properties":{"id":"user-input","style":"position:relative;"},"children":[{"type":"text","value":"User input"},{"type":"element","tagName":"a","properties":{"href":"#user-input","ariaLabel":"user input 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":"Please write a main program which keeps asking the user for integer numbers until the user types in -1. The program should then print out the sum and the mean of the numbers typed in."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your program should use a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NumberStats"}]},{"type":"text","value":" object to keep a record of the numbers added."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: you do not need to change the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NumberStats"}]},{"type":"text","value":" class in this part of the exercise, provided it passed the tests for the previous part of the exercise. Use an instance of the class to complete this part."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB2: your main program should not be contained in a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" block, or the tests will not work."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in integer numbers:\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nSum of numbers: 13\nMean of numbers: 3.25"}]}]},{"type":"element","tagName":"h3","properties":{"id":"multiple-sums","style":"position:relative;"},"children":[{"type":"text","value":"Multiple sums"},{"type":"element","tagName":"a","properties":{"href":"#multiple-sums","ariaLabel":"multiple sums 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":"Please add to your main program so that it also counts separately the sum of the even and the odd numbers added."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: do not change your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NumberStats"}]},{"type":"text","value":" class definition in this part of exercise, either. Instead, define three "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NumberStats"}]},{"type":"text","value":" objects. One of them should keep track of all the numbers, another should track the even numbers, and the third should track the odd numbers typed in."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB2: your main program should not be contained in a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" block, or the tests will not work."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please have look at this example of how your main function should work:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in integer numbers:\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nSum of numbers: 13\nMean of numbers: 3.25\nSum of even numbers: 8\nSum of odd numbers: 5"}]}]}]}]},"html":"

    After this section

      \n
    • You will know how class methods work
    • \n
    • You will be able to write new methods in your own classes
    • \n
    • You will understand the concepts of encapsulation and client in object oriented programming
    • \n

    Classes which contain only data attributes are not very different from dictionaries. Below you will find two ways to model a bank account, first with a class definition, and then using a dictionary.

    # Example 1: bank account with class definition\nclass BankAccount:\n\n    def __init__(self, account_number: str, owner: str, balance: float, annual_interest: float):\n        self.account_number = account_number\n        self.owner = owner\n        self.balance = balance\n        self.annual_interest = annual_interest\n\npeters_account = BankAccount(\"12345-678\", \"Peter Python\", 1500.0, 0.015)
    # Example 2: bank account with dictionary\npeters_account = {\"account_number\": \"12345-678\", \"owner\": \"Peter Python\", \"balance\": 1500.0, \"annual_interest\": 0.0}

    With a dictionary the implementation is much shorter and more straightforward. With a class, however, the structure is more \"tightly bound\", so that we can expect all BankAccount objects to be structurally alike. A class is also named. The BankAccount class is referenced when creating a new bank account, and the type of the object is BankAccount, not dict.

    Another significant advantage of classes is that in addition to data, they can contain functionality. One of the guiding principles of object oriented programming is that an object is used to access both the data attached to an object and the functionality to process that data.

    Methods in classes

    A method is a subprogram or function that is bound to a specific class. Usually a method only affects a single object. A method is defined within the class definition, and it can access the data attributes of the class just like any other variable.

    Let's continue with the BankAccount class introduced above. Below we have a new method which adds interest to the account:

    class BankAccount:\n\n    def __init__(self, account_number: str, owner: str, balance: float, annual_interest: float):\n        self.account_number = account_number\n        self.owner = owner\n        self.balance = balance\n        self.annual_interest = annual_interest\n\n    # This method adds the annual interest to the balance of the account\n    def add_interest(self):\n        self.balance += self.balance * self.annual_interest\n\n\npeters_account = BankAccount(\"12345-678\", \"Peter Python\", 1500.0, 0.015)\npeters_account.add_interest()\nprint(peters_account.balance)

    1522.5

    The add_interest method multiplies the balance of the account by the annual interest percentage, and then adds the result to the current balance. The method acts only on the object which it is called on.

    Let's see how this works when we have created multiple instances of the class:

    # The class BankAccount is defined in the previous example\n\npeters_account = BankAccount(\"12345-678\", \"Peter Python\", 1500.0, 0.015)\npaulas_account = BankAccount(\"99999-999\", \"Paula Pythonen\", 1500.0, 0.05)\npippas_account = BankAccount(\"1111-222\", \"Pippa Programmer\", 1500.0, 0.001)\n\n# Add interest on Peter's and Paula's accounts, but not on Pippa's\npeters_account.add_interest()\npaulas_account.add_interest()\n\n# Print all account balances\nprint(peters_account.balance)\nprint(paulas_account.balance)\nprint(pippas_account.balance)

    1522.5\n1575.0\n1500.0

    As you can see above, the annual interest is added only to those accounts which the method is called on. As the annual interest rates are different for Peter's and Paula's accounts, the results are different for these two accounts. The balance on Pippa's account does not change, because the add_interest method is not called on the object pippas_account.

    Encapsulation

    In object oriented programming the word client comes up from time to time. This is used to refer to a section of code which creates an object and uses the service provided by its methods. When the data contained in an object is used only through the methods it provides, the internal integrity of the object is guaranteed. In practice this means that, for example, a BankAccount class offers methods to handle the balance attribute, so the balance is never accessed directly by the client. These methods can then verify that the balance is not allowed to go below zero, for instance.

    An example of how this would work:

    class BankAccount:\n\n    def __init__(self, account_number: str, owner: str, balance: float, annual_interest: float):\n        self.account_number = account_number\n        self.owner = owner\n        self.balance = balance\n        self.annual_interest = annual_interest\n\n    # This method adds the annual interest to the balance of the account\n    def add_interest(self):\n        self.balance += self.balance * self.annual_interest\n\n    # This method \"withdraws\" money from the account\n    # If the withdrawal is successful the method returns True, and False otherwise\n    def withdraw(self, amount: float):\n        if amount <= self.balance:\n            self.balance -= amount\n            return True\n\n        return False\n\npeters_account = BankAccount(\"12345-678\", \"Peter Python\", 1500.0, 0.015)\n\nif peters_account.withdraw(1000):\n    print(\"The withdrawal was successful, the balance is now\", peters_account.balance)\nelse:\n    print(\"The withdrawal was unsuccessful, the balance is insufficient\")\n\n# Yritetään uudestaan\nif peters_account.withdraw(1000):\n    print(\"The withdrawal was successful, the balance is now\", peters_account.balance)\nelse:\n    print(\"The withdrawal was unsuccessful, the balance is insufficient\")

    The withdrawal was successful, the balance is now 500.0\nThe withdrawal was unsuccessful, the balance is insufficient

    Maintaining the internal integrity of the object and offering suitable methods to ensure this is called encapsulation. The idea is that the inner workings of the object are hidden from the client, but the object offers methods which can be used to access the data stored in the object.

    Adding a method does not automatically hide the attribute. Even though the BankAccount class definition contains the withdraw method for withdrawing money, the client code can still access and change the balance attribute directly:

    peters_account = BankAccount(\"12345-678\", \"Peter Python\", 1500.0, 0.015)\n\n# Attempt to withdraw 2000\nif peters_account.withdraw(2000):\n    print(\"The withdrawal was successful, the balance is now\", peters_account.balance)\nelse:\n    print(\"The withdrawal was unsuccessful, the balance is insufficient\")\n\n    # \"Force\" the withdrawal of 2000\n    peters_account.balance -= 2000\n\nprint(\"The balance is now:\", peters_account.balance)

    The withdrawal was unsuccessful, the balance is insufficient\nThe balance is now: -500.0

    It is possible to hide the data attributes from the client code, which can help in solving this problem. We will return to this topic in the next part.

    This exercise has multiple parts. You can submit the parts separately. Each part is worth one exercise point.

    The exercise template contains a partially completed class DecreasingCounter:

    class DecreasingCounter:\n    def __init__(self, initial_value: int):\n        self.value = initial_value\n\n    def print_value(self):\n        print(\"value:\", self.value)\n\n    def decrease(self):\n        pass\n\n    # define the rest of your methods here

    The class can now be used as shown below, and should produce the following printout after completing the first part of the exercise:

    counter = DecreasingCounter(10)\ncounter.print_value()\ncounter.decrease()\ncounter.print_value()\ncounter.decrease()\ncounter.print_value()

    value: 10\nvalue: 9\nvalue: 8

    Decreasing the value of the counter

    Please complete the method decrease defined in the template, so that it decreases the value stored in the counter by one. See the example above for expected behaviour.

    The counter must not have a negative value

    Please add functionality to your decrease method, so that the value of the counter will never reach negative values. If the value of the counter is 0, it will not be further decreased.

    counter = DecreasingCounter(2)\ncounter.print_value()\ncounter.decrease()\ncounter.print_value()\ncounter.decrease()\ncounter.print_value()\ncounter.decrease()\ncounter.print_value()

    value: 2\nvalue: 1\nvalue: 0\nvalue: 0

    Setting the value to zero

    Please add a method set_to_zero which sets the value of the counter to 0:

    counter = DecreasingCounter(100)\ncounter.print_value()\ncounter.set_to_zero()\ncounter.print_value()

    value: 100\nvalue: 0

    Resetting the counter

    Please add a method reset_original_value() which resets the counter to its initial state:

    counter = DecreasingCounter(55)\ncounter.decrease()\ncounter.decrease()\ncounter.decrease()\ncounter.decrease()\ncounter.print_value()\ncounter.reset_original_value()\ncounter.print_value()

    value: 51\nvalue: 55

    To finish off this section, lets have a look at a class which models the personal best of a player. The class definition contains separate validator methods which ascertain that the arguments passed are valid. The methods are called already within the constructor. This ensures the object created is internally sound.

    from datetime import date\n\nclass PersonalBest:\n\n    def __init__(self, player: str, day: int, month: int, year: int, points: int):\n        # Default values\n        self.player = \"\"\n        self.date_of_pb = date(1900, 1, 1)\n        self.points = 0\n\n        if self.name_ok(player):\n            self.player = player\n\n        if self.date_ok(day, month, year):\n            self.date_of_pb = date(year, month, day)\n\n        if self.points_ok(points):\n            self.points = points\n\n    # Helper methods to check the arguments are valid\n    def name_ok(self, name: str):\n        return len(name) >= 2 # Name should be at least two characters long\n\n    def date_ok(self, day, month, year):\n        try:\n            date(year, month, day)\n            return True\n        except:\n            # an exception is raised if the arguments are not valid\n            return False\n\n    def points_ok(self, points):\n        return points >= 0\n\nif __name__ == \"__main__\":\n    result1 = PersonalBest(\"Peter\", 1, 11, 2020, 235)\n    print(result1.points)\n    print(result1.player)\n    print(result1.date_of_pb)\n\n    # The date was not valid\n    result2 = PersonalBest(\"Paula\", 4, 13, 2019, 4555)\n    print(result2.points)\n    print(result2.player)\n    print(result2.date_of_pb) # Tulostaa oletusarvon 1900-01-01

    235\nPeter\n2020-11-01\n4555\nPaula\n1900-01-01

    In the example above also the helper methods were called via the self parameter name when they were used in the constructor. It is possible to also include static method definitions in class definitions. These are methods which can be called without ever creating an instance of the class. We will return to this subject in the next part.

    The parameter name self is only used when referring to the features of the object as an instance of the class. These include both the data attributes and the methods attached to an object. To make the terminology more confusing, the data attributes and methods are together sometimes referred to simply as the attributes of the object, which is why in this material we have often specified data attributes when we mean the variables defined within the class. This is where the terminology of some Python programmers slightly differs from the terminology used in object oriented programming more widely, where attributes usually refers to just the data attributes of an object.

    It is also possible to create local variables within method definitions without referring to self. You should do so if there is no need to access the variables outside the method. Local variables within methods have no special keywords; they are used just like any normal variables you have come across thus far.

    So, for example this would work:

    class BonusCard:\n    def __init__(self, name: str, balance: float):\n        self.name = name\n        self.balance = balance\n\n    def add_bonus(self):\n        # The variable bonus below is a local variable.\n        # It is not a data attribute of the object.\n        # It can not be accessed directly through the object.\n        bonus = self.balance * 0.25\n        self.balance += bonus\n\n    def add_superbonus(self):\n        # The superbonus variable is also a local variable.\n        # Usually helper variables are local variables because\n        # there is no need to access them from the other\n        # methods in the class or directly through an object.\n        superbonus = self.balance * 0.5\n        self.balance += superbonus\n\n    def __str__(self):\n        return f\"BonusCard(name={self.name}, balance={self.balance})\"

    Please write a class named Person with a single attribute name, which is set with an argument given to the constructor.

    Please also add two methods:

    The method return_first_name should return the first name of the person, while the method return_last_name should return the last name of the person.

    You may assume that the name passed to the constructor will contain exactly two name elements separated with a space character.

    An example use case:

    if __name__ == \"__main__\":\n    peter = Person(\"Peter Pythons\")\n    print(peter.return_first_name())\n    print(peter.return_last_name())\n\n    paula = Person(\"Paula Pythonnen\")\n    print(paula.return_first_name())\n    print(paula.return_last_name())

    Peter\nPythons\nPaula\nPythonnen

    In this exercise you are asked to create a program for working with numbers, similarly to the exercise completed at the end of part 2 in the Introduction to Programming course. This time you will define a class for the purpose.

    Count the numbers

    Please write a class named NumberStats with the following methods:

      \n
    • the method add_number adds a new number to the statistical record
    • \n
    • the method count_numbers returns the count of how many numbers have been added
    • \n

    At this point there is no need to store the numbers themselves in any data structure. It is enough to simply remember how many have been added. The add_number method does take an argument, but there is no need to process the actual value in any way just yet.

    The exercise template contains the following skeleton for the class definition:

    class  NumberStats:\n    def __init__(self):\n        self.numbers = 0\n\n    def add_number(self, number:int):\n        pass\n\n    def count_numbers(self):\n        pass
    stats = NumberStats()\nstats.add_number(3)\nstats.add_number(5)\nstats.add_number(1)\nstats.add_number(2)\nprint(\"Numbers added:\", stats.count_numbers())

    Numbers added: 4

    The sum and the mean

    Please add the following methods to your class definition:

      \n
    • the method get_sum should return the sum of the numbers added (if no numbers have been added, the method should return 0)
    • \n
    • the method average should return the mean of the numbers added (if no numbers have been added, the method should return 0)
    • \n
    stats = NumberStats()\nstats.add_number(3)\nstats.add_number(5)\nstats.add_number(1)\nstats.add_number(2)\nprint(\"Numbers added:\", stats.count_numbers())\nprint(\"Sum of numbers:\", stats.get_sum())\nprint(\"Mean of numbers:\", stats.average())

    Numbers added: 4\nSum of numbers: 11\nMean of numbers: 2.75

    User input

    Please write a main program which keeps asking the user for integer numbers until the user types in -1. The program should then print out the sum and the mean of the numbers typed in.

    Your program should use a NumberStats object to keep a record of the numbers added.

    NB: you do not need to change the NumberStats class in this part of the exercise, provided it passed the tests for the previous part of the exercise. Use an instance of the class to complete this part.

    NB2: your main program should not be contained in a if __name__ == \"__main__\" block, or the tests will not work.

    Please type in integer numbers:\n4\n2\n5\n2\n-1\nSum of numbers: 13\nMean of numbers: 3.25

    Multiple sums

    Please add to your main program so that it also counts separately the sum of the even and the odd numbers added.

    NB: do not change your NumberStats class definition in this part of exercise, either. Instead, define three NumberStats objects. One of them should keep track of all the numbers, another should track the even numbers, and the third should track the odd numbers typed in.

    NB2: your main program should not be contained in a if __name__ == \"__main__\" block, or the tests will not work.

    Please have look at this example of how your main function should work:

    Please type in integer numbers:\n4\n2\n5\n2\n-1\nSum of numbers: 13\nMean of numbers: 3.25\nSum of even numbers: 8\nSum of odd numbers: 5

    ","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-8/4-defining-methods.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know how class methods work"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to write new methods in your own classes"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will understand the concepts of encapsulation and client in object oriented programming"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Classes which contain only data attributes are not very different from dictionaries. Below you will find two ways to model a bank account, first with a class definition, and then using a dictionary."}]},{"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":"# Example 1: bank account with class definition"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" account_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" owner"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" annual_interest"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"account_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" account_number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"owner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" owner\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"annual_interest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" annual_interest\n\npeters_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"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":"\"Peter Python\""}]},{"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":"1500.0"}]},{"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":"0.015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"# Example 2: bank account with dictionary"}]},{"type":"text","value":"\npeters_account "},{"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":"\"account_number\""}]},{"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":"\"12345-678\""}]},{"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":"\"owner\""}]},{"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":"\"Peter 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":"\"balance\""}]},{"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":"1500.0"}]},{"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":"\"annual_interest\""}]},{"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":"0.0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"With a dictionary the implementation is much shorter and more straightforward. With a class, however, the structure is more \"tightly bound\", so that we can expect all "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" objects to be structurally alike. A class is also named. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" class is referenced when creating a new bank account, and the type of the object is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":", not "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"dict"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Another significant advantage of classes is that in addition to data, they can contain functionality. One of the guiding principles of object oriented programming is that an object is used to access both the data attached to an object and the functionality to process that data."}]},{"type":"element","tagName":"h2","properties":{"id":"methods-in-classes","style":"position:relative;"},"children":[{"type":"text","value":"Methods in classes"},{"type":"element","tagName":"a","properties":{"href":"#methods-in-classes","ariaLabel":"methods in classes 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":"A method is a subprogram or function that is bound to a specific class. Usually a method only affects a single object. A method is defined within the class definition, and it can access the data attributes of the class just like any other variable."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's continue with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" class introduced above. Below we have a new method which adds interest to the account:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" account_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" owner"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" annual_interest"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"account_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" account_number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"owner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" owner\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"annual_interest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" annual_interest\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This method adds the annual interest to the balance of the account"}]},{"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":"add_interest"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"annual_interest\n\n\npeters_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"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":"\"Peter Python\""}]},{"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":"1500.0"}]},{"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":"0.015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_interest"},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"1522.5"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_interest"}]},{"type":"text","value":" method multiplies the balance of the account by the annual interest percentage, and then adds the result to the current balance. The method acts only on the object which it is called on."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's see how this works when we have created multiple instances of the class:"}]},{"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":"# The class BankAccount is defined in the previous example"}]},{"type":"text","value":"\n\npeters_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"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":"\"Peter Python\""}]},{"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":"1500.0"}]},{"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":"0.015"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npaulas_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"99999-999\""}]},{"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":"\"Paula Pythonen\""}]},{"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":"1500.0"}]},{"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":"0.05"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npippas_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1111-222\""}]},{"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":"\"Pippa Programmer\""}]},{"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":"1500.0"}]},{"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":"0.001"}]},{"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":"# Add interest on Peter's and Paula's accounts, but not on Pippa's"}]},{"type":"text","value":"\npeters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_interest"},{"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":"\npaulas_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_interest"},{"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","comment"]},"children":[{"type":"text","value":"# Print all account balances"}]},{"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":"peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"paulas_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"pippas_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"1522.5\n1575.0\n1500.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, the annual interest is added only to those accounts which the method is called on. As the annual interest rates are different for Peter's and Paula's accounts, the results are different for these two accounts. The balance on Pippa's account does not change, because the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_interest"}]},{"type":"text","value":" method is not called on the object "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pippas_account"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"encapsulation","style":"position:relative;"},"children":[{"type":"text","value":"Encapsulation"},{"type":"element","tagName":"a","properties":{"href":"#encapsulation","ariaLabel":"encapsulation 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 object oriented programming the word "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"client"}]},{"type":"text","value":" comes up from time to time. This is used to refer to a section of code which creates an object and uses the service provided by its methods. When the data contained in an object is used only through the methods it provides, the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"internal integrity"}]},{"type":"text","value":" of the object is guaranteed. In practice this means that, for example, a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" class offers methods to handle the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balance"}]},{"type":"text","value":" attribute, so the balance is never accessed directly by the client. These methods can then verify that the balance is not allowed to go below zero, for instance."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how this would 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":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BankAccount"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" account_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" owner"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" annual_interest"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"account_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" account_number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"owner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" owner\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"annual_interest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" annual_interest\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This method adds the annual interest to the balance of the account"}]},{"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":"add_interest"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"annual_interest\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This method \"withdraws\" money from the account"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If the withdrawal is successful the method returns True, and False otherwise"}]},{"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":"withdraw"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" amount"},{"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":"float"}]},{"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":"if"}]},{"type":"text","value":" amount "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" amount\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\npeters_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"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":"\"Peter Python\""}]},{"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":"1500.0"}]},{"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":"0.015"}]},{"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":"if"}]},{"type":"text","value":" peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"withdraw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"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 withdrawal was successful, the balance is now\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"else"}]},{"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 withdrawal was unsuccessful, the balance is insufficient\""}]},{"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":"# Yritetään uudestaan"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"withdraw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"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 withdrawal was successful, the balance is now\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"else"}]},{"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 withdrawal was unsuccessful, the balance is insufficient\""}]},{"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":"The withdrawal was successful, the balance is now 500.0\nThe withdrawal was unsuccessful, the balance is insufficient"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Maintaining the internal integrity of the object and offering suitable methods to ensure this is called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"encapsulation"}]},{"type":"text","value":". The idea is that the inner workings of the object are hidden from the client, but the object offers methods which can be used to access the data stored in the object."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Adding a method does not automatically hide the attribute. Even though the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" class definition contains the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"withdraw"}]},{"type":"text","value":" method for withdrawing money, the client code can still access and change the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balance"}]},{"type":"text","value":" attribute directly:"}]},{"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":"peters_account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345-678\""}]},{"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":"\"Peter Python\""}]},{"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":"1500.0"}]},{"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":"0.015"}]},{"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":"# Attempt to withdraw 2000"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"withdraw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2000"}]},{"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 withdrawal was successful, the balance is now\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"else"}]},{"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 withdrawal was unsuccessful, the balance is insufficient\""}]},{"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":"# \"Force\" the withdrawal of 2000"}]},{"type":"text","value":"\n peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"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":"2000"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The balance is now:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" peters_account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"The withdrawal was unsuccessful, the balance is insufficient\nThe balance is now: -500.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is possible to hide the data attributes from the client code, which can help in solving this problem. We will return to this topic in the next part."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Decreasing counter","tmcname":"part08-10_decreasing_counter"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This exercise has multiple parts. You can submit the parts separately. Each part is worth one exercise point."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains a partially completed class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"DecreasingCounter"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"DecreasingCounter"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" initial_value"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"value "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" initial_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":"print_value"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"\"value:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"decrease"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"pass"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# define the rest of your methods here"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The class can now be used as shown below, and should produce the following printout after completing the first part of the exercise:"}]},{"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":"counter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" DecreasingCounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"decrease"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"decrease"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"value: 10\nvalue: 9\nvalue: 8"}]}]},{"type":"element","tagName":"h3","properties":{"id":"decreasing-the-value-of-the-counter","style":"position:relative;"},"children":[{"type":"text","value":"Decreasing the value of the counter"},{"type":"element","tagName":"a","properties":{"href":"#decreasing-the-value-of-the-counter","ariaLabel":"decreasing the value of the counter 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":"Please complete the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"decrease"}]},{"type":"text","value":" defined in the template, so that it decreases the value stored in the counter by one. See the example above for expected behaviour."}]},{"type":"element","tagName":"h3","properties":{"id":"the-counter-must-not-have-a-negative-value","style":"position:relative;"},"children":[{"type":"text","value":"The counter must not have a negative value"},{"type":"element","tagName":"a","properties":{"href":"#the-counter-must-not-have-a-negative-value","ariaLabel":"the counter must not have a negative value 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":"Please add functionality to your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"decrease"}]},{"type":"text","value":" method, so that the value of the counter will never reach negative values. If the value of the counter is 0, it will not be further decreased."}]},{"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":"counter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" DecreasingCounter"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"decrease"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"decrease"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"decrease"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"value: 2\nvalue: 1\nvalue: 0\nvalue: 0"}]}]},{"type":"element","tagName":"h3","properties":{"id":"setting-the-value-to-zero","style":"position:relative;"},"children":[{"type":"text","value":"Setting the value to zero"},{"type":"element","tagName":"a","properties":{"href":"#setting-the-value-to-zero","ariaLabel":"setting the value to zero 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":"Please add a method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"set_to_zero"}]},{"type":"text","value":" which sets the value of the counter to 0:"}]},{"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":"counter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" DecreasingCounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"set_to_zero"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"value: 100\nvalue: 0"}]}]},{"type":"element","tagName":"h3","properties":{"id":"resetting-the-counter","style":"position:relative;"},"children":[{"type":"text","value":"Resetting the counter"},{"type":"element","tagName":"a","properties":{"href":"#resetting-the-counter","ariaLabel":"resetting the counter 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":"Please add a method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"reset_original_value()"}]},{"type":"text","value":" which resets the counter to its initial state:"}]},{"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":"counter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" DecreasingCounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"55"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"decrease"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"decrease"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"decrease"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"decrease"},{"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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"reset_original_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":"\ncounter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"value: 51\nvalue: 55"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To finish off this section, lets have a look at a class which models the personal best of a player. The class definition contains separate validator methods which ascertain that the arguments passed are valid. The methods are called already within the constructor. This ensures the object created is internally sound."}]},{"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":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" date\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PersonalBest"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" day"},{"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":"text","value":" month"},{"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":"text","value":" year"},{"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":"text","value":" points"},{"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","comment"]},"children":[{"type":"text","value":"# Default values"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"player "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"date_of_pb "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1900"}]},{"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":"1"}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"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":"0"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name_ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"player"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"player "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" player\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"date_ok"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"day"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" month"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" year"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"date_of_pb "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"year"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" month"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" day"},{"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":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"points_ok"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"points "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" points\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Helper methods to check the arguments are valid"}]},{"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":"name_ok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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":" "},{"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"},{"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":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Name should be at least two characters long"}]},{"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":"date_ok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" day"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" month"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" year"},{"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":"try"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"year"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" month"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" day"},{"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","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"except"}]},{"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":"# an exception is raised if the arguments are not valid"}]},{"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","boolean"]},"children":[{"type":"text","value":"False"}]},{"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":"points_ok"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" 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":"0"}]},{"type":"text","value":"\n\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 result1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PersonalBest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"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":"1"}]},{"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":"11"}]},{"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":"2020"}]},{"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":"235"}]},{"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":"result1"},{"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":"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":"result1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"player"},{"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":"result1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"date_of_pb"},{"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":"# The date was not valid"}]},{"type":"text","value":"\n result2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PersonalBest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Paula\""}]},{"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":"4"}]},{"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":"13"}]},{"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":"2019"}]},{"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":"4555"}]},{"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":"result2"},{"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":"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":"result2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"player"},{"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":"result2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"date_of_pb"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Tulostaa oletusarvon 1900-01-01"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"235\nPeter\n2020-11-01\n4555\nPaula\n1900-01-01"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the example above also the helper methods were called via the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" parameter name when they were used in the constructor. It is possible to also include "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"static"}]},{"type":"text","value":" method definitions in class definitions. These are methods which can be called without ever creating an instance of the class. We will return to this subject in the next part."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The parameter name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" is only used when referring to the features of the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"object as an instance of the class"}]},{"type":"text","value":". These include both the data attributes and the methods attached to an object. To make the terminology more confusing, the data attributes and methods are together sometimes referred to simply as the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"attributes"}]},{"type":"text","value":" of the object, which is why in this material we have often specified "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"data attributes"}]},{"type":"text","value":" when we mean the variables defined within the class. This is where the terminology of some Python programmers slightly differs from the terminology used in object oriented programming more widely, where "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"attributes"}]},{"type":"text","value":" usually refers to just the data attributes of an object."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is also possible to create local variables within method definitions without referring to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":". You should do so if there is no need to access the variables outside the method. Local variables within methods have no special keywords; they are used just like any normal variables you have come across thus far."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, for example this would 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":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BonusCard"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\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":"add_bonus"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# The variable bonus below is a local variable."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# It is not a data attribute of the object."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# It can not be accessed directly through the object."}]},{"type":"text","value":"\n bonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"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.25"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" bonus\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":"add_superbonus"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# The superbonus variable is also a local variable."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Usually helper variables are local variables because"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# there is no need to access them from the other"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# methods in the class or directly through an object."}]},{"type":"text","value":"\n superbonus "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"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.5"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" superbonus\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"BonusCard(name="}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":", balance="}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"programming-exercise","properties":{"name":"First and last name","tmcname":"part08-11_first_and_last_name"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please write a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" with a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"single attribute"}]},{"type":"text","value":" "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":", which is set with an argument given to the constructor."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please also add two methods:"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return_first_name"}]},{"type":"text","value":" should return the first name of the person, while the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"return_last_name"}]},{"type":"text","value":" should return the last name of the person."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may assume that the name passed to the constructor will contain exactly two name elements separated with a space character."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example use case:"}]},{"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":"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 peter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Pythons\""}]},{"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":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"return_first_name"},{"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 "},{"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":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"return_last_name"},{"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\n paula "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Paula Pythonnen\""}]},{"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":"paula"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"return_first_name"},{"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 "},{"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":"paula"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"return_last_name"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter\nPythons\nPaula\nPythonnen"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Statistics on numbers","tmcname":"part08-12_number_stats"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you are asked to create a program for working with numbers, similarly to the exercise completed at the "},{"type":"element","tagName":"a","properties":{"href":"/part-2/4-simple-loops#programming-exercise-working-with-numbers"},"children":[{"type":"text","value":"end of part 2"}]},{"type":"text","value":" in the Introduction to Programming course. This time you will define a class for the purpose."}]},{"type":"element","tagName":"h3","properties":{"id":"count-the-numbers","style":"position:relative;"},"children":[{"type":"text","value":"Count the numbers"},{"type":"element","tagName":"a","properties":{"href":"#count-the-numbers","ariaLabel":"count the numbers 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":"Please write a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NumberStats"}]},{"type":"text","value":" with the following methods:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_number"}]},{"type":"text","value":" adds a new number to the statistical record"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"count_numbers"}]},{"type":"text","value":" returns the count of how many numbers have been added"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"At this point there is no need to store the numbers themselves in any data structure. It is enough to simply remember how many have been added. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_number"}]},{"type":"text","value":" method does take an argument, but there is no need to process the actual value in any way just yet."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the following skeleton for the class definition:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"NumberStats"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"numbers "},{"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":"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":"add_number"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","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":"pass"}]},{"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":"count_numbers"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"pass"}]}]}]}]},{"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":"stats "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" NumberStats"},{"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":"\nstats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"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":"\nstats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"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":"\nstats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"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":"text","value":"\nstats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Numbers added:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" stats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"count_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","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Numbers added: 4"}]}]},{"type":"element","tagName":"h3","properties":{"id":"the-sum-and-the-mean","style":"position:relative;"},"children":[{"type":"text","value":"The sum and the mean"},{"type":"element","tagName":"a","properties":{"href":"#the-sum-and-the-mean","ariaLabel":"the sum and the mean 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":"Please add the following methods to your class definition:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"get_sum"}]},{"type":"text","value":" should return the sum of the numbers added (if no numbers have been added, the method should return 0)"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"average"}]},{"type":"text","value":" should return the mean of the numbers added (if no numbers have been added, the method should return 0)"}]},{"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":"stats "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" NumberStats"},{"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":"\nstats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"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":"\nstats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"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":"\nstats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"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":"text","value":"\nstats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_number"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Numbers added:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" stats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"count_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","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":"\"Sum of numbers:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" stats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_sum"},{"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"},{"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":"\"Mean of numbers:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" stats"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"average"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Numbers added: 4\nSum of numbers: 11\nMean of numbers: 2.75"}]}]},{"type":"element","tagName":"h3","properties":{"id":"user-input","style":"position:relative;"},"children":[{"type":"text","value":"User input"},{"type":"element","tagName":"a","properties":{"href":"#user-input","ariaLabel":"user input 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":"Please write a main program which keeps asking the user for integer numbers until the user types in -1. The program should then print out the sum and the mean of the numbers typed in."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your program should use a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NumberStats"}]},{"type":"text","value":" object to keep a record of the numbers added."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: you do not need to change the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NumberStats"}]},{"type":"text","value":" class in this part of the exercise, provided it passed the tests for the previous part of the exercise. Use an instance of the class to complete this part."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB2: your main program should not be contained in a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" block, or the tests will not work."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in integer numbers:\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nSum of numbers: 13\nMean of numbers: 3.25"}]}]},{"type":"element","tagName":"h3","properties":{"id":"multiple-sums","style":"position:relative;"},"children":[{"type":"text","value":"Multiple sums"},{"type":"element","tagName":"a","properties":{"href":"#multiple-sums","ariaLabel":"multiple sums 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":"Please add to your main program so that it also counts separately the sum of the even and the odd numbers added."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: do not change your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NumberStats"}]},{"type":"text","value":" class definition in this part of exercise, either. Instead, define three "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"NumberStats"}]},{"type":"text","value":" objects. One of them should keep track of all the numbers, another should track the even numbers, and the third should track the odd numbers typed in."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB2: your main program should not be contained in a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if __name__ == \"__main__\""}]},{"type":"text","value":" block, or the tests will not work."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please have look at this example of how your main function should work:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please type in integer numbers:\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"4"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"5"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"2"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"-1"}]},{"type":"text","value":"\nSum of numbers: 13\nMean of numbers: 3.25\nSum of even numbers: 8\nSum of odd numbers: 5"}]}]}]}]},"html":"

    After this section

      \n
    • You will know how class methods work
    • \n
    • You will be able to write new methods in your own classes
    • \n
    • You will understand the concepts of encapsulation and client in object oriented programming
    • \n

    Classes which contain only data attributes are not very different from dictionaries. Below you will find two ways to model a bank account, first with a class definition, and then using a dictionary.

    # Example 1: bank account with class definition\nclass BankAccount:\n\n    def __init__(self, account_number: str, owner: str, balance: float, annual_interest: float):\n        self.account_number = account_number\n        self.owner = owner\n        self.balance = balance\n        self.annual_interest = annual_interest\n\npeters_account = BankAccount(\"12345-678\", \"Peter Python\", 1500.0, 0.015)
    # Example 2: bank account with dictionary\npeters_account = {\"account_number\": \"12345-678\", \"owner\": \"Peter Python\", \"balance\": 1500.0, \"annual_interest\": 0.0}

    With a dictionary the implementation is much shorter and more straightforward. With a class, however, the structure is more \"tightly bound\", so that we can expect all BankAccount objects to be structurally alike. A class is also named. The BankAccount class is referenced when creating a new bank account, and the type of the object is BankAccount, not dict.

    Another significant advantage of classes is that in addition to data, they can contain functionality. One of the guiding principles of object oriented programming is that an object is used to access both the data attached to an object and the functionality to process that data.

    Methods in classes

    A method is a subprogram or function that is bound to a specific class. Usually a method only affects a single object. A method is defined within the class definition, and it can access the data attributes of the class just like any other variable.

    Let's continue with the BankAccount class introduced above. Below we have a new method which adds interest to the account:

    class BankAccount:\n\n    def __init__(self, account_number: str, owner: str, balance: float, annual_interest: float):\n        self.account_number = account_number\n        self.owner = owner\n        self.balance = balance\n        self.annual_interest = annual_interest\n\n    # This method adds the annual interest to the balance of the account\n    def add_interest(self):\n        self.balance += self.balance * self.annual_interest\n\n\npeters_account = BankAccount(\"12345-678\", \"Peter Python\", 1500.0, 0.015)\npeters_account.add_interest()\nprint(peters_account.balance)

    1522.5

    The add_interest method multiplies the balance of the account by the annual interest percentage, and then adds the result to the current balance. The method acts only on the object which it is called on.

    Let's see how this works when we have created multiple instances of the class:

    # The class BankAccount is defined in the previous example\n\npeters_account = BankAccount(\"12345-678\", \"Peter Python\", 1500.0, 0.015)\npaulas_account = BankAccount(\"99999-999\", \"Paula Pythonen\", 1500.0, 0.05)\npippas_account = BankAccount(\"1111-222\", \"Pippa Programmer\", 1500.0, 0.001)\n\n# Add interest on Peter's and Paula's accounts, but not on Pippa's\npeters_account.add_interest()\npaulas_account.add_interest()\n\n# Print all account balances\nprint(peters_account.balance)\nprint(paulas_account.balance)\nprint(pippas_account.balance)

    1522.5\n1575.0\n1500.0

    As you can see above, the annual interest is added only to those accounts which the method is called on. As the annual interest rates are different for Peter's and Paula's accounts, the results are different for these two accounts. The balance on Pippa's account does not change, because the add_interest method is not called on the object pippas_account.

    Encapsulation

    In object oriented programming the word client comes up from time to time. This is used to refer to a section of code which creates an object and uses the service provided by its methods. When the data contained in an object is used only through the methods it provides, the internal integrity of the object is guaranteed. In practice this means that, for example, a BankAccount class offers methods to handle the balance attribute, so the balance is never accessed directly by the client. These methods can then verify that the balance is not allowed to go below zero, for instance.

    An example of how this would work:

    class BankAccount:\n\n    def __init__(self, account_number: str, owner: str, balance: float, annual_interest: float):\n        self.account_number = account_number\n        self.owner = owner\n        self.balance = balance\n        self.annual_interest = annual_interest\n\n    # This method adds the annual interest to the balance of the account\n    def add_interest(self):\n        self.balance += self.balance * self.annual_interest\n\n    # This method \"withdraws\" money from the account\n    # If the withdrawal is successful the method returns True, and False otherwise\n    def withdraw(self, amount: float):\n        if amount <= self.balance:\n            self.balance -= amount\n            return True\n\n        return False\n\npeters_account = BankAccount(\"12345-678\", \"Peter Python\", 1500.0, 0.015)\n\nif peters_account.withdraw(1000):\n    print(\"The withdrawal was successful, the balance is now\", peters_account.balance)\nelse:\n    print(\"The withdrawal was unsuccessful, the balance is insufficient\")\n\n# Yritetään uudestaan\nif peters_account.withdraw(1000):\n    print(\"The withdrawal was successful, the balance is now\", peters_account.balance)\nelse:\n    print(\"The withdrawal was unsuccessful, the balance is insufficient\")

    The withdrawal was successful, the balance is now 500.0\nThe withdrawal was unsuccessful, the balance is insufficient

    Maintaining the internal integrity of the object and offering suitable methods to ensure this is called encapsulation. The idea is that the inner workings of the object are hidden from the client, but the object offers methods which can be used to access the data stored in the object.

    Adding a method does not automatically hide the attribute. Even though the BankAccount class definition contains the withdraw method for withdrawing money, the client code can still access and change the balance attribute directly:

    peters_account = BankAccount(\"12345-678\", \"Peter Python\", 1500.0, 0.015)\n\n# Attempt to withdraw 2000\nif peters_account.withdraw(2000):\n    print(\"The withdrawal was successful, the balance is now\", peters_account.balance)\nelse:\n    print(\"The withdrawal was unsuccessful, the balance is insufficient\")\n\n    # \"Force\" the withdrawal of 2000\n    peters_account.balance -= 2000\n\nprint(\"The balance is now:\", peters_account.balance)

    The withdrawal was unsuccessful, the balance is insufficient\nThe balance is now: -500.0

    It is possible to hide the data attributes from the client code, which can help in solving this problem. We will return to this topic in the next part.

    This exercise has multiple parts. You can submit the parts separately. Each part is worth one exercise point.

    The exercise template contains a partially completed class DecreasingCounter:

    class DecreasingCounter:\n    def __init__(self, initial_value: int):\n        self.value = initial_value\n\n    def print_value(self):\n        print(\"value:\", self.value)\n\n    def decrease(self):\n        pass\n\n    # define the rest of your methods here

    The class can now be used as shown below, and should produce the following printout after completing the first part of the exercise:

    counter = DecreasingCounter(10)\ncounter.print_value()\ncounter.decrease()\ncounter.print_value()\ncounter.decrease()\ncounter.print_value()

    value: 10\nvalue: 9\nvalue: 8

    Decreasing the value of the counter

    Please complete the method decrease defined in the template, so that it decreases the value stored in the counter by one. See the example above for expected behaviour.

    The counter must not have a negative value

    Please add functionality to your decrease method, so that the value of the counter will never reach negative values. If the value of the counter is 0, it will not be further decreased.

    counter = DecreasingCounter(2)\ncounter.print_value()\ncounter.decrease()\ncounter.print_value()\ncounter.decrease()\ncounter.print_value()\ncounter.decrease()\ncounter.print_value()

    value: 2\nvalue: 1\nvalue: 0\nvalue: 0

    Setting the value to zero

    Please add a method set_to_zero which sets the value of the counter to 0:

    counter = DecreasingCounter(100)\ncounter.print_value()\ncounter.set_to_zero()\ncounter.print_value()

    value: 100\nvalue: 0

    Resetting the counter

    Please add a method reset_original_value() which resets the counter to its initial state:

    counter = DecreasingCounter(55)\ncounter.decrease()\ncounter.decrease()\ncounter.decrease()\ncounter.decrease()\ncounter.print_value()\ncounter.reset_original_value()\ncounter.print_value()

    value: 51\nvalue: 55

    To finish off this section, lets have a look at a class which models the personal best of a player. The class definition contains separate validator methods which ascertain that the arguments passed are valid. The methods are called already within the constructor. This ensures the object created is internally sound.

    from datetime import date\n\nclass PersonalBest:\n\n    def __init__(self, player: str, day: int, month: int, year: int, points: int):\n        # Default values\n        self.player = \"\"\n        self.date_of_pb = date(1900, 1, 1)\n        self.points = 0\n\n        if self.name_ok(player):\n            self.player = player\n\n        if self.date_ok(day, month, year):\n            self.date_of_pb = date(year, month, day)\n\n        if self.points_ok(points):\n            self.points = points\n\n    # Helper methods to check the arguments are valid\n    def name_ok(self, name: str):\n        return len(name) >= 2 # Name should be at least two characters long\n\n    def date_ok(self, day, month, year):\n        try:\n            date(year, month, day)\n            return True\n        except:\n            # an exception is raised if the arguments are not valid\n            return False\n\n    def points_ok(self, points):\n        return points >= 0\n\nif __name__ == \"__main__\":\n    result1 = PersonalBest(\"Peter\", 1, 11, 2020, 235)\n    print(result1.points)\n    print(result1.player)\n    print(result1.date_of_pb)\n\n    # The date was not valid\n    result2 = PersonalBest(\"Paula\", 4, 13, 2019, 4555)\n    print(result2.points)\n    print(result2.player)\n    print(result2.date_of_pb) # Tulostaa oletusarvon 1900-01-01

    235\nPeter\n2020-11-01\n4555\nPaula\n1900-01-01

    In the example above also the helper methods were called via the self parameter name when they were used in the constructor. It is possible to also include static method definitions in class definitions. These are methods which can be called without ever creating an instance of the class. We will return to this subject in the next part.

    The parameter name self is only used when referring to the features of the object as an instance of the class. These include both the data attributes and the methods attached to an object. To make the terminology more confusing, the data attributes and methods are together sometimes referred to simply as the attributes of the object, which is why in this material we have often specified data attributes when we mean the variables defined within the class. This is where the terminology of some Python programmers slightly differs from the terminology used in object oriented programming more widely, where attributes usually refers to just the data attributes of an object.

    It is also possible to create local variables within method definitions without referring to self. You should do so if there is no need to access the variables outside the method. Local variables within methods have no special keywords; they are used just like any normal variables you have come across thus far.

    So, for example this would work:

    class BonusCard:\n    def __init__(self, name: str, balance: float):\n        self.name = name\n        self.balance = balance\n\n    def add_bonus(self):\n        # The variable bonus below is a local variable.\n        # It is not a data attribute of the object.\n        # It can not be accessed directly through the object.\n        bonus = self.balance * 0.25\n        self.balance += bonus\n\n    def add_superbonus(self):\n        # The superbonus variable is also a local variable.\n        # Usually helper variables are local variables because\n        # there is no need to access them from the other\n        # methods in the class or directly through an object.\n        superbonus = self.balance * 0.5\n        self.balance += superbonus\n\n    def __str__(self):\n        return f\"BonusCard(name={self.name}, balance={self.balance})\"

    Please write a class named Person with a single attribute name, which is set with an argument given to the constructor.

    Please also add two methods:

    The method return_first_name should return the first name of the person, while the method return_last_name should return the last name of the person.

    You may assume that the name passed to the constructor will contain exactly two name elements separated with a space character.

    An example use case:

    if __name__ == \"__main__\":\n    peter = Person(\"Peter Pythons\")\n    print(peter.return_first_name())\n    print(peter.return_last_name())\n\n    paula = Person(\"Paula Pythonnen\")\n    print(paula.return_first_name())\n    print(paula.return_last_name())

    Peter\nPythons\nPaula\nPythonnen

    In this exercise you are asked to create a program for working with numbers, similarly to the exercise completed at the end of part 2 in the Introduction to Programming course. This time you will define a class for the purpose.

    Count the numbers

    Please write a class named NumberStats with the following methods:

      \n
    • the method add_number adds a new number to the statistical record
    • \n
    • the method count_numbers returns the count of how many numbers have been added
    • \n

    At this point there is no need to store the numbers themselves in any data structure. It is enough to simply remember how many have been added. The add_number method does take an argument, but there is no need to process the actual value in any way just yet.

    The exercise template contains the following skeleton for the class definition:

    class  NumberStats:\n    def __init__(self):\n        self.numbers = 0\n\n    def add_number(self, number:int):\n        pass\n\n    def count_numbers(self):\n        pass
    stats = NumberStats()\nstats.add_number(3)\nstats.add_number(5)\nstats.add_number(1)\nstats.add_number(2)\nprint(\"Numbers added:\", stats.count_numbers())

    Numbers added: 4

    The sum and the mean

    Please add the following methods to your class definition:

      \n
    • the method get_sum should return the sum of the numbers added (if no numbers have been added, the method should return 0)
    • \n
    • the method average should return the mean of the numbers added (if no numbers have been added, the method should return 0)
    • \n
    stats = NumberStats()\nstats.add_number(3)\nstats.add_number(5)\nstats.add_number(1)\nstats.add_number(2)\nprint(\"Numbers added:\", stats.count_numbers())\nprint(\"Sum of numbers:\", stats.get_sum())\nprint(\"Mean of numbers:\", stats.average())

    Numbers added: 4\nSum of numbers: 11\nMean of numbers: 2.75

    User input

    Please write a main program which keeps asking the user for integer numbers until the user types in -1. The program should then print out the sum and the mean of the numbers typed in.

    Your program should use a NumberStats object to keep a record of the numbers added.

    NB: you do not need to change the NumberStats class in this part of the exercise, provided it passed the tests for the previous part of the exercise. Use an instance of the class to complete this part.

    NB2: your main program should not be contained in a if __name__ == \"__main__\" block, or the tests will not work.

    Please type in integer numbers:\n4\n2\n5\n2\n-1\nSum of numbers: 13\nMean of numbers: 3.25

    Multiple sums

    Please add to your main program so that it also counts separately the sum of the even and the odd numbers added.

    NB: do not change your NumberStats class definition in this part of exercise, either. Instead, define three NumberStats objects. One of them should keep track of all the numbers, another should track the even numbers, and the third should track the odd numbers typed in.

    NB2: your main program should not be contained in a if __name__ == \"__main__\" block, or the tests will not work.

    Please have look at this example of how your main function should work:

    Please type in integer numbers:\n4\n2\n5\n2\n-1\nSum of numbers: 13\nMean of numbers: 3.25\nSum of even numbers: 8\nSum of odd numbers: 5

    ","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-8/4-defining-methods.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-8/5-more-examples-of-classes/page-data.json b/page-data/part-8/5-more-examples-of-classes/page-data.json index 51439de71..adc776c4a 100644 --- a/page-data/part-8/5-more-examples-of-classes/page-data.json +++ b/page-data/part-8/5-more-examples-of-classes/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-8/5-more-examples-of-classes", - "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 able to create more versatile classes"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to add a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method to your class definitions"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"example-1-the-rectangle-class","style":"position:relative;"},"children":[{"type":"text","value":"Example 1: the Rectangle class"},{"type":"element","tagName":"a","properties":{"href":"#example-1-the-rectangle-class","ariaLabel":"example 1 the rectangle class 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":"Let's have a look at a class which models a rectangle in two-dimensional space:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Rectangle"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" left_upper"},{"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":"tuple"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" right_lower"},{"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":"tuple"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_upper "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" left_upper\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_lower "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" right_lower\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" right_lower"},{"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","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"left_upper"},{"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":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" right_lower"},{"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","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"left_upper"},{"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":"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":"area"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height\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":"perimeter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width "},{"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":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height "},{"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":"2"}]},{"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":"move"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" x_change"},{"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":"text","value":" y_change"},{"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 corner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_upper\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_upper "},{"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":"text","value":"corner"},{"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","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"x_change"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" corner"},{"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","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"y_change"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n corner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_lower\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_lower "},{"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":"text","value":"corner"},{"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","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"x_change"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" corner"},{"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","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"y_change"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A new "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rectangle"}]},{"type":"text","value":" is created with two tuples as arguments. These tuples contain the x and y coordinates of the upper left corner and the lower right corner. The constructor calculates the height and width of the rectangle based on these values."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The methods "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"area"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"perimeter"}]},{"type":"text","value":" calculate the area and perimeter of the rectangle based on the height and width. The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"move"}]},{"type":"text","value":" moves the rectangle by the x and y values given as arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The rectanlge is represented in a coordinate system where the x coordinates increase from left to right, and the y coordinates increase from top to bottom. This is a common way of handling coordinates in programming because it is often easier and more natural to consider the top left corner of the computer screen as the point where x and y equal zero."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program tests the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rectangle"}]},{"type":"text","value":" class:"}]},{"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":"rectangle "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rectangle"},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"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":"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":"4"}]},{"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":"3"}]},{"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":"rectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_upper"},{"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":"rectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_lower"},{"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":"rectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width"},{"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":"rectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height"},{"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":"rectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"perimeter"},{"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"},{"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":"rectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"area"},{"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\nrectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"move"},{"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":" "},{"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":"\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":"rectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_upper"},{"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":"rectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_lower"},{"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":"(1, 1)\n(4, 3)\n3\n2\n10\n6\n(4, 4)\n(7, 6)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"printing-an-object","style":"position:relative;"},"children":[{"type":"text","value":"Printing an object"},{"type":"element","tagName":"a","properties":{"href":"#printing-an-object","ariaLabel":"printing an object 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":"When you have an object created from a class defined by yourself, the default reaction to calling the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command with that object as its argument is not very informative:"}]},{"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":"rectangle "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rectangle"},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"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":"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":"4"}]},{"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":"3"}]},{"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":"rectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The printout should look a bit like this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"<"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"main"}]},{"type":"text","value":".Rectangle object at 0x000002D7BF148A90>"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obviously, we want more control over what is printed out. The easiest way to do this is to add a special "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method to the class definition. Its purpose is to return a snapshot of the state of the object in string format. If the class definition contains a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method, the value returned by the method is the one printed out when the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command is executed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, let's add a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method definition to our "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rectangle"}]},{"type":"text","value":" class:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Rectangle"}]},{"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":"# ...the rest of the class goes here the same as above..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This method returns the state of the object in string format"}]},{"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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"rectangle "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_upper"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_lower"},{"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":"p","properties":{},"children":[{"type":"text","value":"Now the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command produces something more user-friendly:"}]},{"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":"rectangle "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rectangle"},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"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":"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":"4"}]},{"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":"3"}]},{"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":"rectangle"},{"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":"rectangle (1, 1) ... (4, 3)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method is perhaps more often used for formulating a string representation of the object with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":" function, as seen in the following program:"}]},{"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":"rectangle "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rectangle"},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"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":"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":"4"}]},{"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":"3"}]},{"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":"\nstr_rep "},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rectangle"},{"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":"str_rep"},{"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":"rectangle (1, 1) ... (4, 3)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are many more special underscored methods which can be defined for classes. One rather similar to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method is the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":" method. Its purpose is to provide a technical representation of the state of the object. We will come across this method later."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Stopwatch","tmcname":"part08-13_stopwatch"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the following skeleton for the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Stopwatch"}]},{"type":"text","value":" class:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Stopwatch"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"seconds "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minutes "},{"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":"p","properties":{},"children":[{"type":"text","value":"Please add to the class definition so that it works as follows:"}]},{"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":"watch "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Stopwatch"},{"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":"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3600"}]},{"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":"watch"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n watch"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"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":"00:00\n00:01\n00:02\n... many more lines printed out\n00:59\n01:00\n01:01\n... many, many more lines printed out\n59:58\n59:59\n00:00\n00:01"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":" adds one second to the stopwatch. The maximum value for both seconds and minutes is 59. Your class definition should also contain a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method, which returns a string representation of the state of the stopwatch, as shown in the example above."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint:"}]},{"type":"text","value":" it might make it easier to test the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":" method if you temporarily set the initial values of the seconds and minutes to some value closer to 59 in the constructor. If you do change the initial values, remember to change them back before submitting."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Clock","tmcname":"part08-14_clock"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please define a new class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Clock"}]},{"type":"text","value":" which expands on the capabilities of your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Stopwatch"}]},{"type":"text","value":" class. It should work as follows:"}]},{"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":"clock "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"23"}]},{"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":"59"}]},{"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":"55"}]},{"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":"clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nclock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"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":"clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nclock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"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":"clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nclock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"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":"clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nclock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"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":"clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nclock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"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":"clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nclock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"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":"clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nclock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"set"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"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":"5"}]},{"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":"clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\n23:59:55\n23:59:56\n23:59:57\n23:59:58\n23:59:59\n00:00:00\n00:00:01\n12:05:00\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, the constructor should take initial values for the hours, minutes and seconds as arguments, and set these appropriately. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":" method adds one second to the clock. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"set"}]},{"type":"text","value":" method sets new values for the hours and the minutes, and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"sets the seconds to zero"}]},{"type":"text","value":"."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"LunchCard","tmcname":"part08-15_lunchcard"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"At Unicafe, the student cafeteria at the University of Helsinki, students can pay for their lunch with a special debit card."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will write a class called "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LunchCard"}]},{"type":"text","value":", with the purpose of emulating the functions provided by the cafeteria's debit card."}]},{"type":"element","tagName":"h3","properties":{"id":"the-structure-of-the-new-class","style":"position:relative;"},"children":[{"type":"text","value":"The structure of the new class"},{"type":"element","tagName":"a","properties":{"href":"#the-structure-of-the-new-class","ariaLabel":"the structure of the new class 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":"Please create a new class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LunchCard"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First write the constructor for the class. It should take the initial balance available on the card as an argument, and save it as an attribute. This is provided for you in the skeleton below."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Next, write a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method, which returns a string containing the balance: \"The balance is X euros\". The available balance should be printed out with one decimal place precision. Please see the example below for usage."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here is a skeleton implementation for the class:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"LunchCard"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"pass"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A usage example:"}]},{"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":"card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LunchCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Executing the above should produce the following printout:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The balance is 50.0 euros"}]}]},{"type":"element","tagName":"h3","properties":{"id":"paying-for-lunch","style":"position:relative;"},"children":[{"type":"text","value":"Paying for lunch"},{"type":"element","tagName":"a","properties":{"href":"#paying-for-lunch","ariaLabel":"paying for lunch 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":"Please implement the following methods in your LunchCard class:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"eat_lunch"}]},{"type":"text","value":" subtracts 2.60 euros from the balance on the card"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"eat_special"}]},{"type":"text","value":" subtracts 4.60 euros from the balance on the card"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can use the following main function to test your class:"}]},{"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":"card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LunchCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_lunch"},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_special"},{"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":"\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_lunch"},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This should produce the following printout:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The balance is 50.0 euros\nThe balance is 47.4 euros\nThe balance is 40.2 euros"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Make sure the balance is never allowed to reach numbers below zero:"}]},{"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":"card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LunchCard"},{"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":"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_lunch"},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_lunch"},{"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":"card"},{"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":"The balance is 4.0 euros\nThe balance is 1.4 euros\nThe balance is 1.4 euros"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If there is not enough money on the card to pay for the lunch, the price of the lunch should not be subtracted from the balance."}]},{"type":"element","tagName":"h3","properties":{"id":"depositing-money-on-the-card","style":"position:relative;"},"children":[{"type":"text","value":"Depositing money on the card"},{"type":"element","tagName":"a","properties":{"href":"#depositing-money-on-the-card","ariaLabel":"depositing money on the card 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":"Implement the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"deposit_money"}]},{"type":"text","value":" method in your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LunchCard"}]},{"type":"text","value":" class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method increases the balance on the card by the amount given as an argument."}]},{"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":"card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LunchCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"deposit_money"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"deposit_money"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"deposit_money"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"200"}]},{"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":"card"},{"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":"The balance is 10.0 euros\nThe balance is 25.0 euros\nThe balance is 35.0 euros\nThe balance is 235.0 euros"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method should account for arguments below zero by "},{"type":"element","tagName":"a","properties":{"href":"/part-6/3-errors#raising-exceptions"},"children":[{"type":"text","value":"raising an exception"}]},{"type":"text","value":" of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":":"}]},{"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":"card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LunchCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"deposit_money"},{"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":"10"}]},{"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":"File \"testi.py\", line 3, in lunchcard\nValueError: You cannot deposit an amount of money less than zero"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" this method should "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" an exception. Please see the instructions for raising exceptions in "},{"type":"element","tagName":"a","properties":{"href":"/part-6/3-errors#raising-exceptions"},"children":[{"type":"text","value":"part 6"}]},{"type":"text","value":". Under no circumstances should the method itself print out anything - the example above is a printout from the Python interpreter coming across the exception."}]},{"type":"element","tagName":"h3","properties":{"id":"multiple-cards","style":"position:relative;"},"children":[{"type":"text","value":"Multiple cards"},{"type":"element","tagName":"a","properties":{"href":"#multiple-cards","ariaLabel":"multiple cards 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":"Please write a main function which contains the following sequence of events:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Create a lunch card for Peter. The initial balance on the card is 20 euros."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Create a lunch card for Grace. The initial balance on the card is 30 euros."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Peter eats a special"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Grace eats a regular lunch"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Print out the balance on each card (on separate lines, with the name of the owner at the beginning of the line)"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Peter deposits 20 euros"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Grace eats the special"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Print out the balance on each card (on separate lines, with the name of the owner at the beginning of the line)"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Peter eats a regular lunch"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Peter eats a regular lunch"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Grace deposits 50 euros"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Print out the balance on each card (on separate lines, with the name of the owner at the beginning of the line)"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Body of the main program"}]},{"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":"peters_card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LunchCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngraces_card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LunchCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"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":"# the rest of your main function"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your main function should print out exactly the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter: The balance is 15.4 euros\nGrace: The balance is 27.4 euros\nPeter: The balance is 35.4 euros\nGrace: The balance is 22.8 euros\nPeter: The balance is 30.2 euros\nGrace: The balance is 72.8 euros"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"example-2-task-list","style":"position:relative;"},"children":[{"type":"text","value":"Example 2: Task list"},{"type":"element","tagName":"a","properties":{"href":"#example-2-task-list","ariaLabel":"example 2 task list 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 following class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TaskList"}]},{"type":"text","value":" models a list of tasks:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"TaskList"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tasks "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_task"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" priority"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tasks"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"priority"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"get_next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"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":"# The list method pop removes and returns the last item in a list"}]},{"type":"text","value":"\n task "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"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":"# Return the name of the task (the second item in the tuple)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" task"},{"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":"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":"number_of_tasks"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tasks"},{"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":"clear_tasks"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tasks "},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_task"}]},{"type":"text","value":" adds a new task to the list. Each task also has a priority attached, which is used for sorting the tasks. The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"get_next"}]},{"type":"text","value":" removes and returns the task with the highest priority on the list. There is also the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number_of_tasks"}]},{"type":"text","value":" method, which returns the number of tasks on the list, and finally the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"clear_tasks"}]},{"type":"text","value":", which clears the task list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Within the object, the tasks are stored in a list. Each task is of a tuple containing the priority of the task and its name. The priority value is stored first, so that when the list is sorted, the task with the highest priority is the last item on the list. This is why we can then simply use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":" method to retrieve and remove the highest priority item."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please have a look at the following program with the task list in action:"}]},{"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":"tasks "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" TaskList"},{"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":"\ntasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_task"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"studying\""}]},{"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":"50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_task"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exercise\""}]},{"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":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_task"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cleaning\""}]},{"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":"10"}]},{"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":"tasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_tasks"},{"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"},{"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":"tasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_next"},{"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"},{"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":"tasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_tasks"},{"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":"\ntasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_task"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"date\""}]},{"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":"100"}]},{"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":"tasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_tasks"},{"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"},{"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":"tasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_next"},{"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"},{"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":"tasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_next"},{"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"},{"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":"tasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_tasks"},{"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":"\ntasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"clear_tasks"},{"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":"tasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_tasks"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3\nexercise\n2\n3\ndate\nstudying\n1\n0"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Series","tmcname":"part08-16_series"},"children":[{"type":"element","tagName":"h3","properties":{"id":"a-class-named-series","style":"position:relative;"},"children":[{"type":"text","value":"A class named Series"},{"type":"element","tagName":"a","properties":{"href":"#a-class-named-series","ariaLabel":"a class named series 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":"Please write a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Series"}]},{"type":"text","value":" with the following functionality:"}]},{"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":"dexter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Series"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dexter\""}]},{"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":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Crime\""}]},{"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":"\"Drama\""}]},{"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":"\"Mystery\""}]},{"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":"\"Thriller\""}]},{"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":"dexter"},{"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":"Dexter (8 seasons)\ngenres: Crime, Drama, Mystery, Thriller\nno ratings"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The constructor should take the title, the number of seasons and a list of genres for the series as its arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint:"}]},{"type":"text","value":" whenever you need to produce a string from a list containing strings, with a separating character of your choice in between the entries, you can use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"join"}]},{"type":"text","value":" method as follows:"}]},{"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":"genre_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":"\"Crime\""}]},{"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":"\"Drama\""}]},{"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":"\"Mystery\""}]},{"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":"\"Thriller\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\ngenre_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":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"genre_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":"genre_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":"Crime, Drama, Mystery, Thriller"}]}]},{"type":"element","tagName":"h3","properties":{"id":"adding-reviews","style":"position:relative;"},"children":[{"type":"text","value":"Adding reviews"},{"type":"element","tagName":"a","properties":{"href":"#adding-reviews","ariaLabel":"adding reviews 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":"Please implement the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rate(rating: int)"}]},{"type":"text","value":" which lets you add a rating between 0 and 5 to any series object. You will also need to adjust the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method so that in case there are ratings, the method prints out the number of ratings added, and their average rounded to one 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":"dexter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Series"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dexter\""}]},{"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":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Crime\""}]},{"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":"\"Drama\""}]},{"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":"\"Mystery\""}]},{"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":"\"Thriller\""}]},{"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":"\ndexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rate"},{"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":"text","value":"\ndexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rate"},{"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":"\ndexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rate"},{"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":"\ndexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rate"},{"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":"\ndexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rate"},{"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":"\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":"dexter"},{"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":"Dexter (8 seasons)\ngenres: Crime, Drama, Mystery, Thriller\n5 ratings, average 3.4 points"}]}]},{"type":"element","tagName":"h3","properties":{"id":"searching-for-series","style":"position:relative;"},"children":[{"type":"text","value":"Searching for series"},{"type":"element","tagName":"a","properties":{"href":"#searching-for-series","ariaLabel":"searching for series 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":"Please implement these two functions which allow you to search through a list of series: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"minimum_grade(rating: float, series_list: list)"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"includes_genre(genre: str, series_list: list)"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here is an example of how the new methods are used:"}]},{"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":"s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Series"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dexter\""}]},{"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":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Crime\""}]},{"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":"\"Drama\""}]},{"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":"\"Mystery\""}]},{"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":"\"Thriller\""}]},{"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":"\ns1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rate"},{"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\ns2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Series"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"South Park\""}]},{"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":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Animation\""}]},{"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":"\"Comedy\""}]},{"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":"\ns2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rate"},{"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":"\n\ns3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Series"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Friends\""}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Romance\""}]},{"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":"\"Comedy\""}]},{"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":"\ns3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rate"},{"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\nseries_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":"text","value":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"a minimum grade of 4.5:\""}]},{"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":" series "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" minimum_grade"},{"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.5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" series_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":"text","value":"series"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"title"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"genre Comedy:\""}]},{"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":" series "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" includes_genre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Comedy\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" series_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":"text","value":"series"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"title"},{"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":"a minimum rating of 4.5:\nDexter\ngenre Comedy:\nSouth Park\nFriends"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The code above and the automatic tests for this exercise assume your class contains an attribute "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"title"}]},{"type":"text","value":". If you used some other attribute name to refer to the name of the series, please change it before submitting."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this week's materials."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will be able to create more versatile classes
    • \n
    • You will know how to add a __str__ method to your class definitions
    • \n

    Example 1: the Rectangle class

    Let's have a look at a class which models a rectangle in two-dimensional space:

    class Rectangle:\n    def __init__(self, left_upper: tuple, right_lower: tuple):\n        self.left_upper = left_upper\n        self.right_lower = right_lower\n        self.width = right_lower[0]-left_upper[0]\n        self.height = right_lower[1]-left_upper[1]\n\n    def area(self):\n        return self.width * self.height\n\n    def perimeter(self):\n        return self.width * 2 + self.height * 2\n\n    def move(self, x_change: int, y_change: int):\n        corner = self.left_upper\n        self.left_upper = (corner[0]+x_change, corner[1]+y_change)\n        corner = self.right_lower\n        self.right_lower = (corner[0]+x_change, corner[1]+y_change)

    A new Rectangle is created with two tuples as arguments. These tuples contain the x and y coordinates of the upper left corner and the lower right corner. The constructor calculates the height and width of the rectangle based on these values.

    The methods area and perimeter calculate the area and perimeter of the rectangle based on the height and width. The method move moves the rectangle by the x and y values given as arguments.

    The rectanlge is represented in a coordinate system where the x coordinates increase from left to right, and the y coordinates increase from top to bottom. This is a common way of handling coordinates in programming because it is often easier and more natural to consider the top left corner of the computer screen as the point where x and y equal zero.

    The following program tests the Rectangle class:

    rectangle = Rectangle((1, 1), (4, 3))\nprint(rectangle.left_upper)\nprint(rectangle.right_lower)\nprint(rectangle.width)\nprint(rectangle.height)\nprint(rectangle.perimeter())\nprint(rectangle.area())\n\nrectangle.move(3, 3)\nprint(rectangle.left_upper)\nprint(rectangle.right_lower)

    (1, 1)\n(4, 3)\n3\n2\n10\n6\n(4, 4)\n(7, 6)

    Printing an object

    When you have an object created from a class defined by yourself, the default reaction to calling the print command with that object as its argument is not very informative:

    rectangle = Rectangle((1, 1), (4, 3))\nprint(rectangle)

    The printout should look a bit like this:

    <main.Rectangle object at 0x000002D7BF148A90>

    Obviously, we want more control over what is printed out. The easiest way to do this is to add a special __str__ method to the class definition. Its purpose is to return a snapshot of the state of the object in string format. If the class definition contains a __str__ method, the value returned by the method is the one printed out when the print command is executed.

    So, let's add a __str__ method definition to our Rectangle class:

    class Rectangle:\n\n    # ...the rest of the class goes here the same as above...\n\n    # This method returns the state of the object in string format\n    def __str__(self):\n        return f\"rectangle {self.left_upper} ... {self.right_lower}\"

    Now the print command produces something more user-friendly:

    rectangle = Rectangle((1, 1), (4, 3))\nprint(rectangle)

    rectangle (1, 1) ... (4, 3)

    The __str__ method is perhaps more often used for formulating a string representation of the object with the str function, as seen in the following program:

    rectangle = Rectangle((1, 1), (4, 3))\nstr_rep = str(rectangle)\nprint(str_rep)

    rectangle (1, 1) ... (4, 3)

    There are many more special underscored methods which can be defined for classes. One rather similar to the __str__ method is the __repr__ method. Its purpose is to provide a technical representation of the state of the object. We will come across this method later.

    The exercise template contains the following skeleton for the Stopwatch class:

    class Stopwatch:\n    def __init__(self):\n        self.seconds = 0\n        self.minutes = 0

    Please add to the class definition so that it works as follows:

    watch = Stopwatch()\nfor i in range(3600):\n    print(watch)\n    watch.tick()

    00:00\n00:01\n00:02\n... many more lines printed out\n00:59\n01:00\n01:01\n... many, many more lines printed out\n59:58\n59:59\n00:00\n00:01

    So, the method tick adds one second to the stopwatch. The maximum value for both seconds and minutes is 59. Your class definition should also contain a __str__ method, which returns a string representation of the state of the stopwatch, as shown in the example above.

    Hint: it might make it easier to test the tick method if you temporarily set the initial values of the seconds and minutes to some value closer to 59 in the constructor. If you do change the initial values, remember to change them back before submitting.

    Please define a new class named Clock which expands on the capabilities of your Stopwatch class. It should work as follows:

    clock = Clock(23, 59, 55)\nprint(clock)\nclock.tick()\nprint(clock)\nclock.tick()\nprint(clock)\nclock.tick()\nprint(clock)\nclock.tick()\nprint(clock)\nclock.tick()\nprint(clock)\nclock.tick()\nprint(clock)\n\nclock.set(12, 5)\nprint(clock)
    \n23:59:55\n23:59:56\n23:59:57\n23:59:58\n23:59:59\n00:00:00\n00:00:01\n12:05:00\n

    As you can see above, the constructor should take initial values for the hours, minutes and seconds as arguments, and set these appropriately. The tick method adds one second to the clock. The set method sets new values for the hours and the minutes, and sets the seconds to zero.

    At Unicafe, the student cafeteria at the University of Helsinki, students can pay for their lunch with a special debit card.

    In this exercise you will write a class called LunchCard, with the purpose of emulating the functions provided by the cafeteria's debit card.

    The structure of the new class

    Please create a new class named LunchCard.

    First write the constructor for the class. It should take the initial balance available on the card as an argument, and save it as an attribute. This is provided for you in the skeleton below.

    Next, write a __str__ method, which returns a string containing the balance: \"The balance is X euros\". The available balance should be printed out with one decimal place precision. Please see the example below for usage.

    Here is a skeleton implementation for the class:

    class LunchCard:\n    def __init__(self, balance: float):\n        self.balance = balance\n\n    def __str__(self):\n        pass

    A usage example:

    card = LunchCard(50)\nprint(card)

    Executing the above should produce the following printout:

    The balance is 50.0 euros

    Paying for lunch

    Please implement the following methods in your LunchCard class:

      \n
    • eat_lunch subtracts 2.60 euros from the balance on the card
    • \n
    • eat_special subtracts 4.60 euros from the balance on the card
    • \n

    You can use the following main function to test your class:

    card = LunchCard(50)\nprint(card)\n\ncard.eat_lunch()\nprint(card)\n\ncard.eat_special()\ncard.eat_lunch()\nprint(card)

    This should produce the following printout:

    The balance is 50.0 euros\nThe balance is 47.4 euros\nThe balance is 40.2 euros

    Make sure the balance is never allowed to reach numbers below zero:

    card = LunchCard(4)\nprint(card)\n\ncard.eat_lunch()\nprint(card)\n\ncard.eat_lunch()\nprint(card)

    The balance is 4.0 euros\nThe balance is 1.4 euros\nThe balance is 1.4 euros

    If there is not enough money on the card to pay for the lunch, the price of the lunch should not be subtracted from the balance.

    Depositing money on the card

    Implement the deposit_money method in your LunchCard class.

    The method increases the balance on the card by the amount given as an argument.

    card = LunchCard(10)\nprint(card)\ncard.deposit_money(15)\nprint(card)\ncard.deposit_money(10)\nprint(card)\ncard.deposit_money(200)\nprint(card)

    The balance is 10.0 euros\nThe balance is 25.0 euros\nThe balance is 35.0 euros\nThe balance is 235.0 euros

    The method should account for arguments below zero by raising an exception of type ValueError:

    card = LunchCard(10)\ncard.deposit_money(-10)

    File \"testi.py\", line 3, in lunchcard\nValueError: You cannot deposit an amount of money less than zero

    NB: this method should raise an exception. Please see the instructions for raising exceptions in part 6. Under no circumstances should the method itself print out anything - the example above is a printout from the Python interpreter coming across the exception.

    Multiple cards

    Please write a main function which contains the following sequence of events:

      \n
    • Create a lunch card for Peter. The initial balance on the card is 20 euros.
    • \n
    • Create a lunch card for Grace. The initial balance on the card is 30 euros.
    • \n
    • Peter eats a special
    • \n
    • Grace eats a regular lunch
    • \n
    • Print out the balance on each card (on separate lines, with the name of the owner at the beginning of the line)
    • \n
    • Peter deposits 20 euros
    • \n
    • Grace eats the special
    • \n
    • Print out the balance on each card (on separate lines, with the name of the owner at the beginning of the line)
    • \n
    • Peter eats a regular lunch
    • \n
    • Peter eats a regular lunch
    • \n
    • Grace deposits 50 euros
    • \n
    • Print out the balance on each card (on separate lines, with the name of the owner at the beginning of the line)
    • \n

    Body of the main program

    peters_card = LunchCard(20)\ngraces_card = LunchCard(30)\n# the rest of your main function

    Your main function should print out exactly the following:

    Peter: The balance is 15.4 euros\nGrace: The balance is 27.4 euros\nPeter: The balance is 35.4 euros\nGrace: The balance is 22.8 euros\nPeter: The balance is 30.2 euros\nGrace: The balance is 72.8 euros

    Example 2: Task list

    The following class TaskList models a list of tasks:

    class TaskList:\n    def __init__(self):\n        self.tasks = []\n\n    def add_task(self, name: str, priority: int):\n        self.tasks.append((priority, name))\n\n    def get_next(self):\n        self.tasks.sort()\n        # The list method pop removes and returns the last item in a list\n        task = self.tasks.pop()\n        # Return the name of the task (the second item in the tuple)\n        return task[1]\n\n    def number_of_tasks(self):\n        return len(self.tasks)\n\n    def clear_tasks(self):\n        self.tasks = []

    The method add_task adds a new task to the list. Each task also has a priority attached, which is used for sorting the tasks. The method get_next removes and returns the task with the highest priority on the list. There is also the number_of_tasks method, which returns the number of tasks on the list, and finally the method clear_tasks, which clears the task list.

    Within the object, the tasks are stored in a list. Each task is of a tuple containing the priority of the task and its name. The priority value is stored first, so that when the list is sorted, the task with the highest priority is the last item on the list. This is why we can then simply use the pop method to retrieve and remove the highest priority item.

    Please have a look at the following program with the task list in action:

    tasks = TaskList()\ntasks.add_task(\"studying\", 50)\ntasks.add_task(\"exercise\", 60)\ntasks.add_task(\"cleaning\", 10)\nprint(tasks.number_of_tasks())\nprint(tasks.get_next())\nprint(tasks.number_of_tasks())\ntasks.add_task(\"date\", 100)\nprint(tasks.number_of_tasks())\nprint(tasks.get_next())\nprint(tasks.get_next())\nprint(tasks.number_of_tasks())\ntasks.clear_tasks()\nprint(tasks.number_of_tasks())

    3\nexercise\n2\n3\ndate\nstudying\n1\n0

    A class named Series

    Please write a class named Series with the following functionality:

    dexter = Series(\"Dexter\", 8, [\"Crime\", \"Drama\", \"Mystery\", \"Thriller\"])\nprint(dexter)

    Dexter (8 seasons)\ngenres: Crime, Drama, Mystery, Thriller\nno ratings

    The constructor should take the title, the number of seasons and a list of genres for the series as its arguments.

    Hint: whenever you need to produce a string from a list containing strings, with a separating character of your choice in between the entries, you can use the join method as follows:

    genre_list = [\"Crime\", \"Drama\", \"Mystery\", \"Thriller\"]\ngenre_string = \", \".join(genre_list)\nprint(genre_string)

    Crime, Drama, Mystery, Thriller

    Adding reviews

    Please implement the method rate(rating: int) which lets you add a rating between 0 and 5 to any series object. You will also need to adjust the __str__ method so that in case there are ratings, the method prints out the number of ratings added, and their average rounded to one decimal point.

    dexter = Series(\"Dexter\", 8, [\"Crime\", \"Drama\", \"Mystery\", \"Thriller\"])\ndexter.rate(4)\ndexter.rate(5)\ndexter.rate(5)\ndexter.rate(3)\ndexter.rate(0)\nprint(dexter)

    Dexter (8 seasons)\ngenres: Crime, Drama, Mystery, Thriller\n5 ratings, average 3.4 points

    Searching for series

    Please implement these two functions which allow you to search through a list of series: minimum_grade(rating: float, series_list: list) and includes_genre(genre: str, series_list: list).

    Here is an example of how the new methods are used:

    s1 = Series(\"Dexter\", 8, [\"Crime\", \"Drama\", \"Mystery\", \"Thriller\"])\ns1.rate(5)\n\ns2 = Series(\"South Park\", 24, [\"Animation\", \"Comedy\"])\ns2.rate(3)\n\ns3 = Series(\"Friends\", 10, [\"Romance\", \"Comedy\"])\ns3.rate(2)\n\nseries_list = [s1, s2, s3]\n\nprint(\"a minimum grade of 4.5:\")\nfor series in minimum_grade(4.5, series_list):\n    print(series.title)\n\nprint(\"genre Comedy:\")\nfor series in includes_genre(\"Comedy\", series_list):\n    print(series.title)

    a minimum rating of 4.5:\nDexter\ngenre Comedy:\nSouth Park\nFriends

    The code above and the automatic tests for this exercise assume your class contains an attribute title. If you used some other attribute name to refer to the name of the series, please change it before submitting.

    Please respond to a quick questionnaire on this week's materials.

    ","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-8/5-more-examples-of-classes.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 able to create more versatile classes"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to add a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method to your class definitions"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"h2","properties":{"id":"example-1-the-rectangle-class","style":"position:relative;"},"children":[{"type":"text","value":"Example 1: the Rectangle class"},{"type":"element","tagName":"a","properties":{"href":"#example-1-the-rectangle-class","ariaLabel":"example 1 the rectangle class 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":"Let's have a look at a class which models a rectangle in two-dimensional space:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Rectangle"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" left_upper"},{"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":"tuple"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" right_lower"},{"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":"tuple"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_upper "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" left_upper\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_lower "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" right_lower\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" right_lower"},{"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","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"left_upper"},{"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":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" right_lower"},{"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","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"left_upper"},{"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":"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":"area"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height\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":"perimeter"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width "},{"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":"2"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height "},{"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":"2"}]},{"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":"move"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" x_change"},{"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":"text","value":" y_change"},{"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 corner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_upper\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_upper "},{"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":"text","value":"corner"},{"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","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"x_change"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" corner"},{"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","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"y_change"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n corner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_lower\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_lower "},{"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":"text","value":"corner"},{"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","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"x_change"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" corner"},{"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","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":"y_change"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A new "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rectangle"}]},{"type":"text","value":" is created with two tuples as arguments. These tuples contain the x and y coordinates of the upper left corner and the lower right corner. The constructor calculates the height and width of the rectangle based on these values."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The methods "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"area"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"perimeter"}]},{"type":"text","value":" calculate the area and perimeter of the rectangle based on the height and width. The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"move"}]},{"type":"text","value":" moves the rectangle by the x and y values given as arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The rectanlge is represented in a coordinate system where the x coordinates increase from left to right, and the y coordinates increase from top to bottom. This is a common way of handling coordinates in programming because it is often easier and more natural to consider the top left corner of the computer screen as the point where x and y equal zero."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following program tests the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rectangle"}]},{"type":"text","value":" class:"}]},{"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":"rectangle "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rectangle"},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"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":"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":"4"}]},{"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":"3"}]},{"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":"rectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_upper"},{"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":"rectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_lower"},{"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":"rectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"width"},{"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":"rectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height"},{"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":"rectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"perimeter"},{"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"},{"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":"rectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"area"},{"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\nrectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"move"},{"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":" "},{"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":"\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":"rectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_upper"},{"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":"rectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_lower"},{"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":"(1, 1)\n(4, 3)\n3\n2\n10\n6\n(4, 4)\n(7, 6)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"printing-an-object","style":"position:relative;"},"children":[{"type":"text","value":"Printing an object"},{"type":"element","tagName":"a","properties":{"href":"#printing-an-object","ariaLabel":"printing an object 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":"When you have an object created from a class defined by yourself, the default reaction to calling the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command with that object as its argument is not very informative:"}]},{"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":"rectangle "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rectangle"},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"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":"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":"4"}]},{"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":"3"}]},{"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":"rectangle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The printout should look a bit like this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"<"},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"main"}]},{"type":"text","value":".Rectangle object at 0x000002D7BF148A90>"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Obviously, we want more control over what is printed out. The easiest way to do this is to add a special "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method to the class definition. Its purpose is to return a snapshot of the state of the object in string format. If the class definition contains a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method, the value returned by the method is the one printed out when the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command is executed."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, let's add a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method definition to our "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Rectangle"}]},{"type":"text","value":" class:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Rectangle"}]},{"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":"# ...the rest of the class goes here the same as above..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This method returns the state of the object in string format"}]},{"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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"rectangle "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"left_upper"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"right_lower"},{"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":"p","properties":{},"children":[{"type":"text","value":"Now the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print"}]},{"type":"text","value":" command produces something more user-friendly:"}]},{"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":"rectangle "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rectangle"},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"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":"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":"4"}]},{"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":"3"}]},{"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":"rectangle"},{"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":"rectangle (1, 1) ... (4, 3)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method is perhaps more often used for formulating a string representation of the object with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"str"}]},{"type":"text","value":" function, as seen in the following program:"}]},{"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":"rectangle "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Rectangle"},{"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"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":"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":"4"}]},{"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":"3"}]},{"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":"\nstr_rep "},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"rectangle"},{"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":"str_rep"},{"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":"rectangle (1, 1) ... (4, 3)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are many more special underscored methods which can be defined for classes. One rather similar to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method is the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__repr__"}]},{"type":"text","value":" method. Its purpose is to provide a technical representation of the state of the object. We will come across this method later."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Stopwatch","tmcname":"part08-13_stopwatch"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the following skeleton for the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Stopwatch"}]},{"type":"text","value":" class:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Stopwatch"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"seconds "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"minutes "},{"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":"p","properties":{},"children":[{"type":"text","value":"Please add to the class definition so that it works as follows:"}]},{"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":"watch "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Stopwatch"},{"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":"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":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"3600"}]},{"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":"watch"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n watch"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"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":"00:00\n00:01\n00:02\n... many more lines printed out\n00:59\n01:00\n01:01\n... many, many more lines printed out\n59:58\n59:59\n00:00\n00:01"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":" adds one second to the stopwatch. The maximum value for both seconds and minutes is 59. Your class definition should also contain a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method, which returns a string representation of the state of the stopwatch, as shown in the example above."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint:"}]},{"type":"text","value":" it might make it easier to test the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":" method if you temporarily set the initial values of the seconds and minutes to some value closer to 59 in the constructor. If you do change the initial values, remember to change them back before submitting."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Clock","tmcname":"part08-14_clock"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please define a new class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Clock"}]},{"type":"text","value":" which expands on the capabilities of your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Stopwatch"}]},{"type":"text","value":" class. It should work as follows:"}]},{"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":"clock "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"23"}]},{"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":"59"}]},{"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":"55"}]},{"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":"clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nclock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"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":"clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nclock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"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":"clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nclock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"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":"clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nclock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"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":"clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nclock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"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":"clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nclock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tick"},{"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":"clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nclock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"set"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"12"}]},{"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":"5"}]},{"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":"clock"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"text","value":"\n23:59:55\n23:59:56\n23:59:57\n23:59:58\n23:59:59\n00:00:00\n00:00:01\n12:05:00\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As you can see above, the constructor should take initial values for the hours, minutes and seconds as arguments, and set these appropriately. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"tick"}]},{"type":"text","value":" method adds one second to the clock. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"set"}]},{"type":"text","value":" method sets new values for the hours and the minutes, and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"sets the seconds to zero"}]},{"type":"text","value":"."}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"LunchCard","tmcname":"part08-15_lunchcard"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"At Unicafe, the student cafeteria at the University of Helsinki, students can pay for their lunch with a special debit card."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will write a class called "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LunchCard"}]},{"type":"text","value":", with the purpose of emulating the functions provided by the cafeteria's debit card."}]},{"type":"element","tagName":"h3","properties":{"id":"the-structure-of-the-new-class","style":"position:relative;"},"children":[{"type":"text","value":"The structure of the new class"},{"type":"element","tagName":"a","properties":{"href":"#the-structure-of-the-new-class","ariaLabel":"the structure of the new class 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":"Please create a new class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LunchCard"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First write the constructor for the class. It should take the initial balance available on the card as an argument, and save it as an attribute. This is provided for you in the skeleton below."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Next, write a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method, which returns a string containing the balance: \"The balance is X euros\". The available balance should be printed out with one decimal place precision. Please see the example below for usage."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here is a skeleton implementation for the class:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"LunchCard"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"pass"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A usage example:"}]},{"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":"card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LunchCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Executing the above should produce the following printout:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The balance is 50.0 euros"}]}]},{"type":"element","tagName":"h3","properties":{"id":"paying-for-lunch","style":"position:relative;"},"children":[{"type":"text","value":"Paying for lunch"},{"type":"element","tagName":"a","properties":{"href":"#paying-for-lunch","ariaLabel":"paying for lunch 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":"Please implement the following methods in your LunchCard class:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"eat_lunch"}]},{"type":"text","value":" subtracts 2.60 euros from the balance on the card"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"eat_special"}]},{"type":"text","value":" subtracts 4.60 euros from the balance on the card"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can use the following main function to test your class:"}]},{"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":"card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LunchCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_lunch"},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_special"},{"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":"\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_lunch"},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This should produce the following printout:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The balance is 50.0 euros\nThe balance is 47.4 euros\nThe balance is 40.2 euros"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Make sure the balance is never allowed to reach numbers below zero:"}]},{"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":"card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LunchCard"},{"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":"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_lunch"},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_lunch"},{"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":"card"},{"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":"The balance is 4.0 euros\nThe balance is 1.4 euros\nThe balance is 1.4 euros"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If there is not enough money on the card to pay for the lunch, the price of the lunch should not be subtracted from the balance."}]},{"type":"element","tagName":"h3","properties":{"id":"depositing-money-on-the-card","style":"position:relative;"},"children":[{"type":"text","value":"Depositing money on the card"},{"type":"element","tagName":"a","properties":{"href":"#depositing-money-on-the-card","ariaLabel":"depositing money on the card 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":"Implement the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"deposit_money"}]},{"type":"text","value":" method in your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LunchCard"}]},{"type":"text","value":" class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method increases the balance on the card by the amount given as an argument."}]},{"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":"card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LunchCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"deposit_money"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"deposit_money"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"deposit_money"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"200"}]},{"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":"card"},{"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":"The balance is 10.0 euros\nThe balance is 25.0 euros\nThe balance is 35.0 euros\nThe balance is 235.0 euros"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method should account for arguments below zero by "},{"type":"element","tagName":"a","properties":{"href":"/part-6/3-errors#raising-exceptions"},"children":[{"type":"text","value":"raising an exception"}]},{"type":"text","value":" of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":":"}]},{"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":"card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LunchCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"deposit_money"},{"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":"10"}]},{"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":"File \"testi.py\", line 3, in lunchcard\nValueError: You cannot deposit an amount of money less than zero"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" this method should "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"raise"}]},{"type":"text","value":" an exception. Please see the instructions for raising exceptions in "},{"type":"element","tagName":"a","properties":{"href":"/part-6/3-errors#raising-exceptions"},"children":[{"type":"text","value":"part 6"}]},{"type":"text","value":". Under no circumstances should the method itself print out anything - the example above is a printout from the Python interpreter coming across the exception."}]},{"type":"element","tagName":"h3","properties":{"id":"multiple-cards","style":"position:relative;"},"children":[{"type":"text","value":"Multiple cards"},{"type":"element","tagName":"a","properties":{"href":"#multiple-cards","ariaLabel":"multiple cards 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":"Please write a main function which contains the following sequence of events:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Create a lunch card for Peter. The initial balance on the card is 20 euros."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Create a lunch card for Grace. The initial balance on the card is 30 euros."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Peter eats a special"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Grace eats a regular lunch"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Print out the balance on each card (on separate lines, with the name of the owner at the beginning of the line)"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Peter deposits 20 euros"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Grace eats the special"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Print out the balance on each card (on separate lines, with the name of the owner at the beginning of the line)"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Peter eats a regular lunch"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Peter eats a regular lunch"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Grace deposits 50 euros"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"Print out the balance on each card (on separate lines, with the name of the owner at the beginning of the line)"}]}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Body of the main program"}]},{"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":"peters_card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LunchCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngraces_card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LunchCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"30"}]},{"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":"# the rest of your main function"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your main function should print out exactly the following:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Peter: The balance is 15.4 euros\nGrace: The balance is 27.4 euros\nPeter: The balance is 35.4 euros\nGrace: The balance is 22.8 euros\nPeter: The balance is 30.2 euros\nGrace: The balance is 72.8 euros"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"example-2-task-list","style":"position:relative;"},"children":[{"type":"text","value":"Example 2: Task list"},{"type":"element","tagName":"a","properties":{"href":"#example-2-task-list","ariaLabel":"example 2 task list 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 following class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"TaskList"}]},{"type":"text","value":" models a list of tasks:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"TaskList"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tasks "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_task"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" priority"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tasks"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"priority"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"get_next"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sort"},{"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":"# The list method pop removes and returns the last item in a list"}]},{"type":"text","value":"\n task "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"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":"# Return the name of the task (the second item in the tuple)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" task"},{"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":"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":"number_of_tasks"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tasks"},{"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":"clear_tasks"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"tasks "},{"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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_task"}]},{"type":"text","value":" adds a new task to the list. Each task also has a priority attached, which is used for sorting the tasks. The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"get_next"}]},{"type":"text","value":" removes and returns the task with the highest priority on the list. There is also the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number_of_tasks"}]},{"type":"text","value":" method, which returns the number of tasks on the list, and finally the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"clear_tasks"}]},{"type":"text","value":", which clears the task list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Within the object, the tasks are stored in a list. Each task is of a tuple containing the priority of the task and its name. The priority value is stored first, so that when the list is sorted, the task with the highest priority is the last item on the list. This is why we can then simply use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"pop"}]},{"type":"text","value":" method to retrieve and remove the highest priority item."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please have a look at the following program with the task list in action:"}]},{"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":"tasks "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" TaskList"},{"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":"\ntasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_task"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"studying\""}]},{"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":"50"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_task"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"exercise\""}]},{"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":"60"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ntasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_task"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"cleaning\""}]},{"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":"10"}]},{"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":"tasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_tasks"},{"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"},{"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":"tasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_next"},{"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"},{"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":"tasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_tasks"},{"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":"\ntasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_task"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"date\""}]},{"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":"100"}]},{"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":"tasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_tasks"},{"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"},{"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":"tasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_next"},{"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"},{"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":"tasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"get_next"},{"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"},{"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":"tasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_tasks"},{"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":"\ntasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"clear_tasks"},{"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":"tasks"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_tasks"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"3\nexercise\n2\n3\ndate\nstudying\n1\n0"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Series","tmcname":"part08-16_series"},"children":[{"type":"element","tagName":"h3","properties":{"id":"a-class-named-series","style":"position:relative;"},"children":[{"type":"text","value":"A class named Series"},{"type":"element","tagName":"a","properties":{"href":"#a-class-named-series","ariaLabel":"a class named series 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":"Please write a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Series"}]},{"type":"text","value":" with the following functionality:"}]},{"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":"dexter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Series"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dexter\""}]},{"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":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Crime\""}]},{"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":"\"Drama\""}]},{"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":"\"Mystery\""}]},{"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":"\"Thriller\""}]},{"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":"dexter"},{"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":"Dexter (8 seasons)\ngenres: Crime, Drama, Mystery, Thriller\nno ratings"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The constructor should take the title, the number of seasons and a list of genres for the series as its arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint:"}]},{"type":"text","value":" whenever you need to produce a string from a list containing strings, with a separating character of your choice in between the entries, you can use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"join"}]},{"type":"text","value":" method as follows:"}]},{"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":"genre_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":"\"Crime\""}]},{"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":"\"Drama\""}]},{"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":"\"Mystery\""}]},{"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":"\"Thriller\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\ngenre_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":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"join"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"genre_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":"genre_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":"Crime, Drama, Mystery, Thriller"}]}]},{"type":"element","tagName":"h3","properties":{"id":"adding-reviews","style":"position:relative;"},"children":[{"type":"text","value":"Adding reviews"},{"type":"element","tagName":"a","properties":{"href":"#adding-reviews","ariaLabel":"adding reviews 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":"Please implement the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"rate(rating: int)"}]},{"type":"text","value":" which lets you add a rating between 0 and 5 to any series object. You will also need to adjust the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method so that in case there are ratings, the method prints out the number of ratings added, and their average rounded to one 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":"dexter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Series"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dexter\""}]},{"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":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Crime\""}]},{"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":"\"Drama\""}]},{"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":"\"Mystery\""}]},{"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":"\"Thriller\""}]},{"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":"\ndexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rate"},{"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":"text","value":"\ndexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rate"},{"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":"\ndexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rate"},{"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":"\ndexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rate"},{"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":"\ndexter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rate"},{"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":"\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":"dexter"},{"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":"Dexter (8 seasons)\ngenres: Crime, Drama, Mystery, Thriller\n5 ratings, average 3.4 points"}]}]},{"type":"element","tagName":"h3","properties":{"id":"searching-for-series","style":"position:relative;"},"children":[{"type":"text","value":"Searching for series"},{"type":"element","tagName":"a","properties":{"href":"#searching-for-series","ariaLabel":"searching for series 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":"Please implement these two functions which allow you to search through a list of series: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"minimum_grade(rating: float, series_list: list)"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"includes_genre(genre: str, series_list: list)"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here is an example of how the new methods are used:"}]},{"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":"s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Series"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dexter\""}]},{"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":"8"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Crime\""}]},{"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":"\"Drama\""}]},{"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":"\"Mystery\""}]},{"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":"\"Thriller\""}]},{"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":"\ns1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rate"},{"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\ns2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Series"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"South Park\""}]},{"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":"24"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Animation\""}]},{"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":"\"Comedy\""}]},{"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":"\ns2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rate"},{"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":"\n\ns3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Series"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Friends\""}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"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":"\"Romance\""}]},{"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":"\"Comedy\""}]},{"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":"\ns3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rate"},{"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\nseries_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":"text","value":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"a minimum grade of 4.5:\""}]},{"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":" series "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" minimum_grade"},{"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.5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" series_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":"text","value":"series"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"title"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"genre Comedy:\""}]},{"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":" series "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" includes_genre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Comedy\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" series_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":"text","value":"series"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"title"},{"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":"a minimum rating of 4.5:\nDexter\ngenre Comedy:\nSouth Park\nFriends"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The code above and the automatic tests for this exercise assume your class contains an attribute "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"title"}]},{"type":"text","value":". If you used some other attribute name to refer to the name of the series, please change it before submitting."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this week's materials."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will be able to create more versatile classes
    • \n
    • You will know how to add a __str__ method to your class definitions
    • \n

    Example 1: the Rectangle class

    Let's have a look at a class which models a rectangle in two-dimensional space:

    class Rectangle:\n    def __init__(self, left_upper: tuple, right_lower: tuple):\n        self.left_upper = left_upper\n        self.right_lower = right_lower\n        self.width = right_lower[0]-left_upper[0]\n        self.height = right_lower[1]-left_upper[1]\n\n    def area(self):\n        return self.width * self.height\n\n    def perimeter(self):\n        return self.width * 2 + self.height * 2\n\n    def move(self, x_change: int, y_change: int):\n        corner = self.left_upper\n        self.left_upper = (corner[0]+x_change, corner[1]+y_change)\n        corner = self.right_lower\n        self.right_lower = (corner[0]+x_change, corner[1]+y_change)

    A new Rectangle is created with two tuples as arguments. These tuples contain the x and y coordinates of the upper left corner and the lower right corner. The constructor calculates the height and width of the rectangle based on these values.

    The methods area and perimeter calculate the area and perimeter of the rectangle based on the height and width. The method move moves the rectangle by the x and y values given as arguments.

    The rectanlge is represented in a coordinate system where the x coordinates increase from left to right, and the y coordinates increase from top to bottom. This is a common way of handling coordinates in programming because it is often easier and more natural to consider the top left corner of the computer screen as the point where x and y equal zero.

    The following program tests the Rectangle class:

    rectangle = Rectangle((1, 1), (4, 3))\nprint(rectangle.left_upper)\nprint(rectangle.right_lower)\nprint(rectangle.width)\nprint(rectangle.height)\nprint(rectangle.perimeter())\nprint(rectangle.area())\n\nrectangle.move(3, 3)\nprint(rectangle.left_upper)\nprint(rectangle.right_lower)

    (1, 1)\n(4, 3)\n3\n2\n10\n6\n(4, 4)\n(7, 6)

    Printing an object

    When you have an object created from a class defined by yourself, the default reaction to calling the print command with that object as its argument is not very informative:

    rectangle = Rectangle((1, 1), (4, 3))\nprint(rectangle)

    The printout should look a bit like this:

    <main.Rectangle object at 0x000002D7BF148A90>

    Obviously, we want more control over what is printed out. The easiest way to do this is to add a special __str__ method to the class definition. Its purpose is to return a snapshot of the state of the object in string format. If the class definition contains a __str__ method, the value returned by the method is the one printed out when the print command is executed.

    So, let's add a __str__ method definition to our Rectangle class:

    class Rectangle:\n\n    # ...the rest of the class goes here the same as above...\n\n    # This method returns the state of the object in string format\n    def __str__(self):\n        return f\"rectangle {self.left_upper} ... {self.right_lower}\"

    Now the print command produces something more user-friendly:

    rectangle = Rectangle((1, 1), (4, 3))\nprint(rectangle)

    rectangle (1, 1) ... (4, 3)

    The __str__ method is perhaps more often used for formulating a string representation of the object with the str function, as seen in the following program:

    rectangle = Rectangle((1, 1), (4, 3))\nstr_rep = str(rectangle)\nprint(str_rep)

    rectangle (1, 1) ... (4, 3)

    There are many more special underscored methods which can be defined for classes. One rather similar to the __str__ method is the __repr__ method. Its purpose is to provide a technical representation of the state of the object. We will come across this method later.

    The exercise template contains the following skeleton for the Stopwatch class:

    class Stopwatch:\n    def __init__(self):\n        self.seconds = 0\n        self.minutes = 0

    Please add to the class definition so that it works as follows:

    watch = Stopwatch()\nfor i in range(3600):\n    print(watch)\n    watch.tick()

    00:00\n00:01\n00:02\n... many more lines printed out\n00:59\n01:00\n01:01\n... many, many more lines printed out\n59:58\n59:59\n00:00\n00:01

    So, the method tick adds one second to the stopwatch. The maximum value for both seconds and minutes is 59. Your class definition should also contain a __str__ method, which returns a string representation of the state of the stopwatch, as shown in the example above.

    Hint: it might make it easier to test the tick method if you temporarily set the initial values of the seconds and minutes to some value closer to 59 in the constructor. If you do change the initial values, remember to change them back before submitting.

    Please define a new class named Clock which expands on the capabilities of your Stopwatch class. It should work as follows:

    clock = Clock(23, 59, 55)\nprint(clock)\nclock.tick()\nprint(clock)\nclock.tick()\nprint(clock)\nclock.tick()\nprint(clock)\nclock.tick()\nprint(clock)\nclock.tick()\nprint(clock)\nclock.tick()\nprint(clock)\n\nclock.set(12, 5)\nprint(clock)
    \n23:59:55\n23:59:56\n23:59:57\n23:59:58\n23:59:59\n00:00:00\n00:00:01\n12:05:00\n

    As you can see above, the constructor should take initial values for the hours, minutes and seconds as arguments, and set these appropriately. The tick method adds one second to the clock. The set method sets new values for the hours and the minutes, and sets the seconds to zero.

    At Unicafe, the student cafeteria at the University of Helsinki, students can pay for their lunch with a special debit card.

    In this exercise you will write a class called LunchCard, with the purpose of emulating the functions provided by the cafeteria's debit card.

    The structure of the new class

    Please create a new class named LunchCard.

    First write the constructor for the class. It should take the initial balance available on the card as an argument, and save it as an attribute. This is provided for you in the skeleton below.

    Next, write a __str__ method, which returns a string containing the balance: \"The balance is X euros\". The available balance should be printed out with one decimal place precision. Please see the example below for usage.

    Here is a skeleton implementation for the class:

    class LunchCard:\n    def __init__(self, balance: float):\n        self.balance = balance\n\n    def __str__(self):\n        pass

    A usage example:

    card = LunchCard(50)\nprint(card)

    Executing the above should produce the following printout:

    The balance is 50.0 euros

    Paying for lunch

    Please implement the following methods in your LunchCard class:

      \n
    • eat_lunch subtracts 2.60 euros from the balance on the card
    • \n
    • eat_special subtracts 4.60 euros from the balance on the card
    • \n

    You can use the following main function to test your class:

    card = LunchCard(50)\nprint(card)\n\ncard.eat_lunch()\nprint(card)\n\ncard.eat_special()\ncard.eat_lunch()\nprint(card)

    This should produce the following printout:

    The balance is 50.0 euros\nThe balance is 47.4 euros\nThe balance is 40.2 euros

    Make sure the balance is never allowed to reach numbers below zero:

    card = LunchCard(4)\nprint(card)\n\ncard.eat_lunch()\nprint(card)\n\ncard.eat_lunch()\nprint(card)

    The balance is 4.0 euros\nThe balance is 1.4 euros\nThe balance is 1.4 euros

    If there is not enough money on the card to pay for the lunch, the price of the lunch should not be subtracted from the balance.

    Depositing money on the card

    Implement the deposit_money method in your LunchCard class.

    The method increases the balance on the card by the amount given as an argument.

    card = LunchCard(10)\nprint(card)\ncard.deposit_money(15)\nprint(card)\ncard.deposit_money(10)\nprint(card)\ncard.deposit_money(200)\nprint(card)

    The balance is 10.0 euros\nThe balance is 25.0 euros\nThe balance is 35.0 euros\nThe balance is 235.0 euros

    The method should account for arguments below zero by raising an exception of type ValueError:

    card = LunchCard(10)\ncard.deposit_money(-10)

    File \"testi.py\", line 3, in lunchcard\nValueError: You cannot deposit an amount of money less than zero

    NB: this method should raise an exception. Please see the instructions for raising exceptions in part 6. Under no circumstances should the method itself print out anything - the example above is a printout from the Python interpreter coming across the exception.

    Multiple cards

    Please write a main function which contains the following sequence of events:

      \n
    • Create a lunch card for Peter. The initial balance on the card is 20 euros.
    • \n
    • Create a lunch card for Grace. The initial balance on the card is 30 euros.
    • \n
    • Peter eats a special
    • \n
    • Grace eats a regular lunch
    • \n
    • Print out the balance on each card (on separate lines, with the name of the owner at the beginning of the line)
    • \n
    • Peter deposits 20 euros
    • \n
    • Grace eats the special
    • \n
    • Print out the balance on each card (on separate lines, with the name of the owner at the beginning of the line)
    • \n
    • Peter eats a regular lunch
    • \n
    • Peter eats a regular lunch
    • \n
    • Grace deposits 50 euros
    • \n
    • Print out the balance on each card (on separate lines, with the name of the owner at the beginning of the line)
    • \n

    Body of the main program

    peters_card = LunchCard(20)\ngraces_card = LunchCard(30)\n# the rest of your main function

    Your main function should print out exactly the following:

    Peter: The balance is 15.4 euros\nGrace: The balance is 27.4 euros\nPeter: The balance is 35.4 euros\nGrace: The balance is 22.8 euros\nPeter: The balance is 30.2 euros\nGrace: The balance is 72.8 euros

    Example 2: Task list

    The following class TaskList models a list of tasks:

    class TaskList:\n    def __init__(self):\n        self.tasks = []\n\n    def add_task(self, name: str, priority: int):\n        self.tasks.append((priority, name))\n\n    def get_next(self):\n        self.tasks.sort()\n        # The list method pop removes and returns the last item in a list\n        task = self.tasks.pop()\n        # Return the name of the task (the second item in the tuple)\n        return task[1]\n\n    def number_of_tasks(self):\n        return len(self.tasks)\n\n    def clear_tasks(self):\n        self.tasks = []

    The method add_task adds a new task to the list. Each task also has a priority attached, which is used for sorting the tasks. The method get_next removes and returns the task with the highest priority on the list. There is also the number_of_tasks method, which returns the number of tasks on the list, and finally the method clear_tasks, which clears the task list.

    Within the object, the tasks are stored in a list. Each task is of a tuple containing the priority of the task and its name. The priority value is stored first, so that when the list is sorted, the task with the highest priority is the last item on the list. This is why we can then simply use the pop method to retrieve and remove the highest priority item.

    Please have a look at the following program with the task list in action:

    tasks = TaskList()\ntasks.add_task(\"studying\", 50)\ntasks.add_task(\"exercise\", 60)\ntasks.add_task(\"cleaning\", 10)\nprint(tasks.number_of_tasks())\nprint(tasks.get_next())\nprint(tasks.number_of_tasks())\ntasks.add_task(\"date\", 100)\nprint(tasks.number_of_tasks())\nprint(tasks.get_next())\nprint(tasks.get_next())\nprint(tasks.number_of_tasks())\ntasks.clear_tasks()\nprint(tasks.number_of_tasks())

    3\nexercise\n2\n3\ndate\nstudying\n1\n0

    A class named Series

    Please write a class named Series with the following functionality:

    dexter = Series(\"Dexter\", 8, [\"Crime\", \"Drama\", \"Mystery\", \"Thriller\"])\nprint(dexter)

    Dexter (8 seasons)\ngenres: Crime, Drama, Mystery, Thriller\nno ratings

    The constructor should take the title, the number of seasons and a list of genres for the series as its arguments.

    Hint: whenever you need to produce a string from a list containing strings, with a separating character of your choice in between the entries, you can use the join method as follows:

    genre_list = [\"Crime\", \"Drama\", \"Mystery\", \"Thriller\"]\ngenre_string = \", \".join(genre_list)\nprint(genre_string)

    Crime, Drama, Mystery, Thriller

    Adding reviews

    Please implement the method rate(rating: int) which lets you add a rating between 0 and 5 to any series object. You will also need to adjust the __str__ method so that in case there are ratings, the method prints out the number of ratings added, and their average rounded to one decimal point.

    dexter = Series(\"Dexter\", 8, [\"Crime\", \"Drama\", \"Mystery\", \"Thriller\"])\ndexter.rate(4)\ndexter.rate(5)\ndexter.rate(5)\ndexter.rate(3)\ndexter.rate(0)\nprint(dexter)

    Dexter (8 seasons)\ngenres: Crime, Drama, Mystery, Thriller\n5 ratings, average 3.4 points

    Searching for series

    Please implement these two functions which allow you to search through a list of series: minimum_grade(rating: float, series_list: list) and includes_genre(genre: str, series_list: list).

    Here is an example of how the new methods are used:

    s1 = Series(\"Dexter\", 8, [\"Crime\", \"Drama\", \"Mystery\", \"Thriller\"])\ns1.rate(5)\n\ns2 = Series(\"South Park\", 24, [\"Animation\", \"Comedy\"])\ns2.rate(3)\n\ns3 = Series(\"Friends\", 10, [\"Romance\", \"Comedy\"])\ns3.rate(2)\n\nseries_list = [s1, s2, s3]\n\nprint(\"a minimum grade of 4.5:\")\nfor series in minimum_grade(4.5, series_list):\n    print(series.title)\n\nprint(\"genre Comedy:\")\nfor series in includes_genre(\"Comedy\", series_list):\n    print(series.title)

    a minimum rating of 4.5:\nDexter\ngenre Comedy:\nSouth Park\nFriends

    The code above and the automatic tests for this exercise assume your class contains an attribute title. If you used some other attribute name to refer to the name of the series, please change it before submitting.

    Please respond to a quick questionnaire on this week's materials.

    ","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-8/5-more-examples-of-classes.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-8/page-data.json b/page-data/part-8/page-data.json index 968e1c5e5..86e333641 100644 --- a/page-data/part-8/page-data.json +++ b/page-data/part-8/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/part-8", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-8","title":"Part 8"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-8/index.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-8","title":"Part 8"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-8/index.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-9/1-objects-and-references/page-data.json b/page-data/part-9/1-objects-and-references/page-data.json index 4a5435099..5d7761916 100644 --- a/page-data/part-9/1-objects-and-references/page-data.json +++ b/page-data/part-9/1-objects-and-references/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-9/1-objects-and-references", - "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 able to use different data structures for handling objects"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how objects can be passed as arguments"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Every value in Python is an object. Any object you create based on a class you've defined yourself works exactly the same as any \"regular\" Python object. For example, objects can be stored in 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":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" date\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"CompletedCourse"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" course_name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" credits"},{"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":"text","value":" completion_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" date"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" course_name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" credits\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"completion_date "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" completion_date\n\n\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 we create some completed courses and add these to a list "}]},{"type":"text","value":"\n completed "},{"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 maths1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CompletedCourse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mathematics 1\""}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"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":"3"}]},{"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":"11"}]},{"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 prog1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CompletedCourse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Programming 1\""}]},{"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":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2019"}]},{"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":"12"}]},{"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":"17"}]},{"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 completed"},{"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":"maths1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n completed"},{"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":"prog1"},{"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":"# Let's add a couple more straight to the list"}]},{"type":"text","value":"\n completed"},{"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":"CompletedCourse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Physics 2\""}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2019"}]},{"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":"11"}]},{"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":"10"}]},{"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 completed"},{"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":"CompletedCourse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Programming 2\""}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"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":"5"}]},{"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":"19"}]},{"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\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Go through all the completed courses, print out their names "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# and sum up the credits received"}]},{"type":"text","value":"\n credits "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" course "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" completed"},{"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":"course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits\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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Total credits received:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" credits"},{"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":"Mathematics 1\nProgramming 1\nPhysics 2\nProgramming 2\nTotal credits received: 20"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The fastest car","tmcname":"part09-01_fastest_car"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Car"}]},{"type":"text","value":" which represents the features of a car through two attributes: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"make (str)"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"top_speed (int)"}]},{"type":"text","value":"."}]},{"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":"fastest_car(cars: list)"}]},{"type":"text","value":" which takes a list of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Car"}]},{"type":"text","value":" objects as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return the make of the fastest car. You may assume there will always be a single car with the highest top speed. Do not change the list given as an argument, or make any changes to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Car"}]},{"type":"text","value":" class definition."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may use the following code to test your 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","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 car1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Car"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saab\""}]},{"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":"195"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n car2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Car"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lada\""}]},{"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":"110"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n car3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Car"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ferrari\""}]},{"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":"280"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n car4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Car"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Trabant\""}]},{"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":"85"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n cars "},{"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":"text","value":"car1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" car2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" car3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" car4"},{"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":"fastest_car"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"cars"},{"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":"Ferrari"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Passing submissions","tmcname":"part09-02_passing_submissions"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ExamSubmission"}]},{"type":"text","value":" which, as the name implies, models an examinee's submission in an exam. The class has two attributes defined: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"examinee (str)"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"points (int)"}]},{"type":"text","value":"."}]},{"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":"passed(submissions: list, lowest_passing: int)"}]},{"type":"text","value":" which takes a list of exam submissions and an integer number representing the lowest passing grade as its arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should create and return a new list, which contains only the passed submissions from the original list. Please do not change the list given as an argument, or make any changes to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ExamSubmission"}]},{"type":"text","value":" class definition."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may use the following code to test your 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","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 s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ExamSubmission"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"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":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n s2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ExamSubmission"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pippa\""}]},{"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":"19"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n s3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ExamSubmission"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n s4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ExamSubmission"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Phoebe\""}]},{"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":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n s5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ExamSubmission"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Persephone\""}]},{"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":"17"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n passes "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" passed"},{"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":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s5"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"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":" passing "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" passes"},{"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":"passing"},{"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":"ExamSubmission (examinee: Pippa, points: 19)\nExamSubmission (examinee: Paul, points: 15)\nExamSubmission (examinee: Persephone, points: 17)"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may remember that lists do not contain any objects themselves. They contain "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"references to objects"}]},{"type":"text","value":". The exact same object can appear multiple times in a single list, and it can be referred to multiple times within the list or outside it. Let's have a look at an example:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Product"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"text","value":" unit"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"unit "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" unit\n\n\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 shopping_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","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n milk "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Milk\""}]},{"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":"\"litre\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n shopping_list"},{"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":"milk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n shopping_list"},{"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":"milk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n shopping_list"},{"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":"Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Cucumber\""}]},{"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":"\"piece\""}]},{"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":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 336px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/246fe44dfbe7194a2f4607b6fb229cee/d99f2/9_1_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: 60%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAABRUlEQVQoz32TCYuDQAyF/f8/TwrVWkF7Wax3T4ua5QsbdqplA8N4vLx5yct48hvTNOmeZZkURSFVVUlZllLXtRwOB3m/3wZVzOVyUYzhmqaRNE3FEycgbdtW5nG9Xj8Iv2GI2+32R2gK8zyX7XYr+/1eVquVdF23IAQThqEcj0ddcRxLFEX6vCA8n8+y2WwkCAItwRS5hGDW67Xi2DlwGAYtf0FIz+aBSpeQfhl+jlsQYsj9ftdl5VKiS4gh9Ov5fKp6sDyjfEH4er3k8XioUisLQhdDMhjIfN/Xd0g/THET5u6aq/x3MZQ+juNHjmcgdxkIFX3fqwKUu4EiDnQPYnnyT5DA+DC4SZJoG1i4aRNgim33SNjtdnI6nfQGkMDpzBT9oy80m++YYaRgv7XJ4yfO2jWjNGYKIjMDt0mkn3yfj45L+gMG96HyVCCmjQAAAABJRU5ErkJggg=='); 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":["/static/246fe44dfbe7194a2f4607b6fb229cee/a0b58/9_1_1.webp 230w","/static/246fe44dfbe7194a2f4607b6fb229cee/a6cd2/9_1_1.webp 336w"],"sizes":"(max-width: 336px) 100vw, 336px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/246fe44dfbe7194a2f4607b6fb229cee/81c8e/9_1_1.png 230w","/static/246fe44dfbe7194a2f4607b6fb229cee/d99f2/9_1_1.png 336w"],"sizes":"(max-width: 336px) 100vw, 336px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/246fe44dfbe7194a2f4607b6fb229cee/d99f2/9_1_1.png","alt":"9 1 1","title":"9 1 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":"p","properties":{},"children":[{"type":"text","value":"If there is more than one reference to the same object, it makes no difference which one of the references is used:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Dog"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name\n\ndogs "},{"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":"\nfluffy "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Dog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fluffy\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndogs"},{"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":"fluffy"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndogs"},{"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":"fluffy"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndogs"},{"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":"Dog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fluffy\""}]},{"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":"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":"\"Dogs initially:\""}]},{"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":" dog "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" dogs"},{"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":"dog"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The dog at index 0 is renamed:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndogs"},{"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":"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":"\"Pooch\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" dog "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" dogs"},{"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":"dog"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The dog at index 2 is renamed:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndogs"},{"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":"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":"\"Fifi\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" dog "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" dogs"},{"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":"dog"},{"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":"Dogs initially:\nFluffy\nFluffy\nFluffy\nThe dog at index 0 is renamed:\nPooch\nPooch\nFluffy\nThe dog at index 2 is renamed:\nPooch\nPooch\nFifi"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The references at indexes 0 and 1 in the list refer to the same object. Either one of the references can be used to access the object. The reference at index 2 refers to a different object, albeit with seemingly the same contents. Changing the contents of this latter object does not affect the other one."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" is used for checking if the two references refer to the exact same object, while the operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" will tell you if the contents of the objects are the same. The following example hopefully makes the difference clear:"}]},{"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":"list1 "},{"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":"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":" "},{"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":"\nlist2 "},{"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":"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":" "},{"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":"\nlist3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" list1\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":"list1 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" list2"},{"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":"list1 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" list3"},{"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":"list2 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" list3"},{"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":"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":"list1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" list2"},{"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":"list1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" list3"},{"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":"list2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" list3"},{"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":"False\nTrue\nFalse"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nTrue\nTrue"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Any Python object can also be stored in a dictionary or any other data structure. This also applies to objects created based on a class you've defined yourself."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" cr"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"cr "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" cr\n\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":"# The key in this dictionary is the student number, "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# and the value is an object of type Student"}]},{"type":"text","value":"\n students "},{"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 students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345\""}]},{"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":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saul Student\""}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"54321\""}]},{"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":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sally Student\""}]},{"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":"67"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":" can help in making sense of the above example:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/f80b81163a85d325ab28cff091c2f896/aa61c/9_1_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: 44.34782608695652%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAABeElEQVQoz2WSWW8TMRhF5///I3hD4rFvQANFLW2AJJNZ7fG+zEF2MhFtr2R5Fvvq3qOvWdeVnHNdRWJSnE8z3sX6Xv7fVs44a+uzMZ515Z2alBJqWRi6HikkUiyIacFaVy/HGIsr1kW+/zzw+NwS0srYPzGO92j3jIt7bPhFSD3NlsxozTxOaKXIOd3SbQox8+PxyMvfER9X1LJHih1SP7DoB4Ta4UJ7SWiNYewHFikR08wiS0KL0aZ+00pTzm1KuaDZMY537yvnK5fT4cjQD7V+MSl7RTALvPOvWFbTMKHNAakEzkpWHBBpUk7EEOjalr7rmcaZru0ZuoFpGK4YNHnNV8PXKM7nrxxPHxmXTyj3hcZZg9a61iuyJiKFI8bC9jIB/yd7uwvZMs9PWH8gpI7Ga411F1OlFM4Vs0CIAe99NSz8NoZvq0/DNw7HD8z6M9Lc0WgVkMLjXGToO/7sf1ee3bnD+3C7vE3Deul8MywTkVIgRl+D/AOPArp7NsH/1QAAAABJRU5ErkJggg=='); 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":["/static/f80b81163a85d325ab28cff091c2f896/a0b58/9_1_2.webp 230w","/static/f80b81163a85d325ab28cff091c2f896/bc10c/9_1_2.webp 460w","/static/f80b81163a85d325ab28cff091c2f896/966d8/9_1_2.webp 920w","/static/f80b81163a85d325ab28cff091c2f896/fa9eb/9_1_2.webp 1065w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/f80b81163a85d325ab28cff091c2f896/81c8e/9_1_2.png 230w","/static/f80b81163a85d325ab28cff091c2f896/08a84/9_1_2.png 460w","/static/f80b81163a85d325ab28cff091c2f896/c0255/9_1_2.png 920w","/static/f80b81163a85d325ab28cff091c2f896/aa61c/9_1_2.png 1065w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/f80b81163a85d325ab28cff091c2f896/c0255/9_1_2.png","alt":"9 1 2","title":"9 1 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":"h2","properties":{"id":"self-or-no-self","style":"position:relative;"},"children":[{"type":"text","value":"Self or no self?"},{"type":"element","tagName":"a","properties":{"href":"#self-or-no-self","ariaLabel":"self or no self 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":"Thus far we've only touched upon the surface of using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" parameter name. Let's have a closer look at when it should or should not be used."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Below we have a simple class which lets us create a vocabulary object containing some words:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Vocabulary"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_word"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" word"},{"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":"str"}]},{"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":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words"},{"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":"word"},{"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":"print_vocabulary"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"for"}]},{"type":"text","value":" word "},{"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":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words"},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nvocab "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Vocabulary"},{"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":"\nvocab"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_word"},{"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":"\nvocab"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"object\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nvocab"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"object-oriented programming\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nvocab"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"object\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nvocab"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nerd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nvocab"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_vocabulary"},{"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":"nerd\nobject\nobject-oriented programming\npython"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The list of words is stored in an attribute named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.words"}]},{"type":"text","value":". In this case the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" parameter name is mandatory in both the constructor method of the class and in any other method accessing that variable. If "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" is left out, the different methods will not access the same list of words."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's add a new method to our class definition. The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"longest_word(self)"}]},{"type":"text","value":" returns (one of) the longest words in the vocabulary."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following is one way of completing this task, but we will soon see it is not a very good way:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Vocabulary"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words "},{"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","comment"]},"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":"longest_word"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# define two helper variables"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"longest "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length_of_longest "},{"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":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words"},{"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":"if"}]},{"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":"word"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length_of_longest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length_of_longest "},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"longest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" word\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"longest"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This method uses two helper variables which are declared with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" parameter name. Remember, the names of variables do not matter in the functional sense, so these variables could also be named more confusingly as, for example, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"helper"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"helper2"}]},{"type":"text","value":". The code begins to look a bit cryptic:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Vocabulary"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words "},{"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","comment"]},"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":"longest_word"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# define two helper variables"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper2 "},{"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":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words"},{"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":"if"}]},{"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":"word"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper2 "},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" word\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When a variable is declared with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" parameter name, it becomes an attribute of the object. This means that the variable will exist for as long as the object exists. Specifically, the variable will continue existing also after the method declaring it has finished its execution. In the example above this is quite unnecessary, as the helper variables are meant to be used only within the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"longest_word(self)"}]},{"type":"text","value":". So, declaring helper variables with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" parameter name is not a very good idea here."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Besides causing variables to exist beyond their \"expiration date\", using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" to create new attributes where they aren't necessary can cause difficult bugs in your code. Especially generically named attributes such as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.helper"}]},{"type":"text","value":", which are then used in various different methods, can cause unexpected behaviour which is hard to trace."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, if a helper variable is declared as an attribute and assigned an initial value in the constructor, but the variable is then used in an unrelated context in another method, the results are often unpredictable:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Vocabulary"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words "},{"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":"# define helper variables"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper2 "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper3 "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper4 "},{"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":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"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":"longest_word"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words"},{"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":"# above the helper variables were all assigned string values"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the following will not work because the type of helper2 is wrong"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"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":"word"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper2 "},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" word\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You might think this would be solved by just declaring attributes where they are used, "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"outside"}]},{"type":"text","value":" the constructor, but this results in a situation where the attributes accessible through an object are dependent on "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"which methods have been executed"}]},{"type":"text","value":". In the previous part we saw that the advantage of declaring attributes in the constructor is that all instances of the class will then have the exact same attributes. If this is not the case, using different instances of the class can easily lead to errors."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In conclusion, if you need helper variables for use within a single method, the correct way to do it is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"without"}]},{"type":"text","value":" "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":". To make your code easier to understand, also use informative variable names:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Vocabulary"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words "},{"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","comment"]},"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":"longest_word"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# the correct way of declaring helper variables "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# for use within a single method"}]},{"type":"text","value":"\n longest "},{"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":"text","value":"\n length_of_longest "},{"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":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words"},{"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":"if"}]},{"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":"word"},{"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":" length_of_longest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n length_of_longest "},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n longest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" word\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" longest"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the above implementation the helper variables are only accessible while the method is being executed. The values stored within cannot cause complications in other parts of the program."}]},{"type":"element","tagName":"h2","properties":{"id":"objects-as-arguments-to-functions","style":"position:relative;"},"children":[{"type":"text","value":"Objects as arguments to functions"},{"type":"element","tagName":"a","properties":{"href":"#objects-as-arguments-to-functions","ariaLabel":"objects as arguments to 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":"The objects created based on our own classes are usually mutable. You may remember that, for instance, Python lists are mutable: when passed as arguments to functions, their contents can change as a result of the execution."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at a simple example where a function receives a reference to an object of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" as its argument. The function then changes the name of the student. Both the function and the main function calling it access the same object, so the change is apparent in the main function as well."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" student_number"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"student_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" student_number\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"student_number"},{"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 type hint here uses the name of the class defined above"}]},{"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":"change_name"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Student"},{"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 student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"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":"\"Saul Student\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# create a Student object"}]},{"type":"text","value":"\nsteve "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"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 Student\""}]},{"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":"\"12345\""}]},{"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":"steve"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nchange_name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"steve"},{"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":"steve"},{"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":"Steve Student (12345)\nSaul Student (12345)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is also possible to create objects within functions. If a function returns a reference to the newly created object, it is also accessible within 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","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" choice\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" student_number"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"student_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" student_number\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"student_number"},{"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":"# This function creates and returns a new Student object."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# It randomly selects values for the name and the student number."}]},{"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":"new_student"}]},{"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 first_names "},{"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":"\"Mark\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mindy\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mary\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mike\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n last_names "},{"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":"\"Javanese\""}]},{"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":"\"Rusty\""}]},{"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":"\"Scriptor\""}]},{"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":"\"Pythons\""}]},{"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":"# randomly determine the name"}]},{"type":"text","value":"\n name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" choice"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"first_names"},{"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":"\" \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" choice"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"last_names"},{"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":"# randomly determine the student number"}]},{"type":"text","value":"\n student_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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"99999"}]},{"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","comment"]},"children":[{"type":"text","value":"# Create and return a Student object"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" student_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":"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":"# Call the function five times and store the results in a list"}]},{"type":"text","value":"\n students "},{"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","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":"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","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n students"},{"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":"new_student"},{"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\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Print out the results"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" student "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" students "},{"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":"student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Executing the above could result in the following printout (NB: as randomness is involved, if you try the code yourself, the results will likely be different)."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Mary Rusty (78218)\nMindy Rusty (80068)\nMike Pythons (70396)\nMark Javanese (83307)\nMary Pythons (45149)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"objects-as-arguments-to-methods","style":"position:relative;"},"children":[{"type":"text","value":"Objects as arguments to methods"},{"type":"element","tagName":"a","properties":{"href":"#objects-as-arguments-to-methods","ariaLabel":"objects as arguments to methods 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":"Similarly, objects can act as arguments to methods. Let's have a look at an example from an amusement park:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" height"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" height\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Attraction"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" min_height"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"visitors "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"min_height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" min_height\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":"admit_visitor"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Person"},{"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":"if"}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"min_height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"visitors "},{"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":"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":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" got on board\""}]}]},{"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":"else"}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" was too short :(\""}]}]},{"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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"visitors"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" visitors)\""}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The Attraction contains a method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"admit_visitor"}]},{"type":"text","value":", which takes an object of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" as an argument. If the visitor is tall enough, they are admitted on board and the number of visitors is increased. The classes can be tested as follows:"}]},{"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":"rollercoaster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Attraction"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Rollercoaster\""}]},{"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":"120"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\njared "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jared\""}]},{"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":"172"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nalice "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Alice\""}]},{"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":"105"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrollercoaster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"admit_visitor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"jared"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrollercoaster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"admit_visitor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"alice"},{"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":"rollercoaster"},{"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":"Jared got on board\nAlice was too short :(\nRollercoaster (1 visitors)"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Baby Centre","tmcname":"part09-03_baby_centre"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" and a skeleton implementation for the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BabyCentre"}]},{"type":"text","value":". A "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BabyCentre"}]},{"type":"text","value":" object performs various actions on a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" object. It may, for example, weigh or feed the person. In this exercise you will implement the rest of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BabyCentre"}]},{"type":"text","value":" class. Please do not change the class definition of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" in any way."}]},{"type":"element","tagName":"h2","properties":{"id":"weighing-persons","style":"position:relative;"},"children":[{"type":"text","value":"Weighing persons"},{"type":"element","tagName":"a","properties":{"href":"#weighing-persons","ariaLabel":"weighing persons 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 "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BabyCentre"}]},{"type":"text","value":" class definition contains an outline for the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"weigh"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BabyCentre"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"weigh"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Person"},{"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":"# return the weight of the person passed as an argument"}]},{"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","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method takes a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" object as its argument. It should return the weight of the person. You can access the weight of a person through the appropriate attribute defined in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" class. Please fill in the rest of the implementation for the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"weigh"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Below is an example of a main function where a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BabyCentre"}]},{"type":"text","value":" weighs two separate "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" objects:"}]},{"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":"baby_centre "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BabyCentre"},{"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\neric "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"1"}]},{"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":"110"}]},{"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":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"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":"33"}]},{"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":"176"}]},{"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":"85"}]},{"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":"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":"eric"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" weighs "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"baby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"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":" kg\""}]}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" weighs "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"baby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peter"},{"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":" kg\""}]}]},{"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":"Eric weighs 7 kg\nPeter weighs 85 kg"}]}]},{"type":"element","tagName":"h2","properties":{"id":"feeding","style":"position:relative;"},"children":[{"type":"text","value":"Feeding"},{"type":"element","tagName":"a","properties":{"href":"#feeding","ariaLabel":"feeding 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":"It is possible to change the state of an object passed as an argument. Please implement the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"feed(person: Person)"}]},{"type":"text","value":" which increases by one the weight of the person passed as an argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example two persons are weighed, and then one of them is fed three times. Then the persons are weighed again:"}]},{"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":"baby_centre "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BabyCentre"},{"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\neric "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"1"}]},{"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":"110"}]},{"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":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"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":"33"}]},{"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":"176"}]},{"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":"85"}]},{"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":"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":"eric"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" weighs "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"baby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"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":" kg\""}]}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" weighs "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"baby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peter"},{"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":" kg\""}]}]},{"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\nbaby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"feed"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbaby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"feed"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbaby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"feed"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"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":"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":"eric"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" weighs "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"baby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"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":" kg\""}]}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" weighs "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"baby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peter"},{"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":" kg\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The printout should reveal that Eric's weight has increased by three:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eric weighs 7 kg\nPeter weighs 85 kg"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eric weighs 10 kg\nPeter weighs 85 kg"}]}]},{"type":"element","tagName":"h2","properties":{"id":"counting-weigh-ins","style":"position:relative;"},"children":[{"type":"text","value":"Counting weigh-ins"},{"type":"element","tagName":"a","properties":{"href":"#counting-weigh-ins","ariaLabel":"counting weigh ins 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":"Please implement the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"weigh_ins()"}]},{"type":"text","value":" which returns the total number of weigh-ins a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BabyCentre"}]},{"type":"text","value":" object has performed. NB: you will need a new attribute for keeping track of the number of weigh-ins. You can use the following code to test your method:"}]},{"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":"baby_centre "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BabyCentre"},{"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\neric "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"1"}]},{"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":"110"}]},{"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":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"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":"33"}]},{"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":"176"}]},{"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":"85"}]},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Total number of weigh-ins 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":"baby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh_ins"},{"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","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbaby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbaby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Total number of weigh-ins 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":"baby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh_ins"},{"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","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbaby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbaby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbaby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbaby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Total number of weigh-ins 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":"baby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh_ins"},{"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","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":"Total number of weigh-ins is 0\nTotal number of weigh-ins is 2\nTotal number of weigh-ins is 6"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"LunchCard and PaymentTerminal","tmcname":"part09-04_lunchcard_and_paymentterminal"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the previous part there was an "},{"type":"element","tagName":"a","properties":{"href":"/part-8/5-more-examples-of-classes#programming-exercise-lunchcard"},"children":[{"type":"text","value":"exercise"}]},{"type":"text","value":" where you implemented the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LunchCard"}]},{"type":"text","value":". The card had separate methods for eating a regular and a special lunch, along with a method for depositing money on the card."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LunchCard"}]},{"type":"text","value":" class, as you were asked to implement it, has some problems, however. The card itself had knowledge of the prices of the different lunch options, and knew to subtract the right amount of money from the balance based on these. But imagine the prices changed, or there were new items introduced to the system, but several cards were already registered in the system. This would mean all existing cards would need to be replaced by versions with knowledge of the new prices."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A better solution would be to make the cards \"stupid\", ignorant of the prices of different products. The purpose of the card would be to simply keep track of the available balance. All more complicated features should be contained within another class: the payment terminal."}]},{"type":"element","tagName":"h2","properties":{"id":"a-simpler-lunchcard","style":"position:relative;"},"children":[{"type":"text","value":"A simpler LunchCard"},{"type":"element","tagName":"a","properties":{"href":"#a-simpler-lunchcard","ariaLabel":"a simpler lunchcard 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":"Let's first implement a simpler version of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LunchCard"}]},{"type":"text","value":" class. The card should contain functionality only for finding out the current balance, depositing money on the card, and subtracting from the balance. Please fill in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"subtract_from_balance(amount)"}]},{"type":"text","value":" method in the exercise template according to the instructions in the comments:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"LunchCard"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\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":"deposit_money"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" amount"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" amount\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":"subtract_from_balance"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" amount"},{"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":"float"}]},{"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":"pass"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# The amount should be subtracted from the balance only if"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# there is enough money on the card."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If the payment is successful, the method returns True. "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Otherwise it returns False."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may use the following code to test your 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","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 card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LunchCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"\"Balance\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n result "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"subtract_from_balance"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Payment successful:\""}]},{"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":"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":"\"Balance\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n result "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"subtract_from_balance"},{"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":"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":"\"Payment successful:\""}]},{"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":"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":"\"Balance\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"Balance 10\nPayment successful: True\nBalance 2\nPayment successful: False\nBalance 2"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-payment-terminal-and-dealing-with-cash-payments","style":"position:relative;"},"children":[{"type":"text","value":"The payment terminal and dealing with cash payments"},{"type":"element","tagName":"a","properties":{"href":"#the-payment-terminal-and-dealing-with-cash-payments","ariaLabel":"the payment terminal and dealing with cash payments 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 the student cafeteria it is possible to pay with either cash or a LunchCard. A payment terminal is used to handle both cash and card transactions. Let's start with the cash transactions."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here we have a skeleton implementation for a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PaymentTerminal"}]},{"type":"text","value":" class. Please implement the methods as described in the comments:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PaymentTerminal"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# Initially there is 1000 euros in cash available at the terminal"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"funds "},{"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":"1000"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lunches "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"specials "},{"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":"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":"eat_lunch"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" payment"},{"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":"float"}]},{"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":"# A regular lunch costs 2.50 euros."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Increase the value of the funds at the terminal by the "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# price of the lunch, increase the number of lunches sold, "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# and return the appropriate change."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If the payment passed as an argument is not large enough to cover"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the price, the lunch is not sold, and the entire sum is returned."}]},{"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":"eat_special"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" payment"},{"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":"float"}]},{"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":"# A special lunch costs 4.30 euros."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Increase the value of the funds at the terminal by the "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# price of the lunch, increase the number of lunches sold, "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# and return the appropriate change."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If the payment passed as an argument is not large enough to cover"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the price, the lunch is not sold, and the entire sum is returned."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may use the following code to test your class:"}]},{"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":"exactum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PaymentTerminal"},{"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\nchange "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_lunch"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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 change returned was\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" change"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nchange "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_lunch"},{"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"},{"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 change returned was\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" change"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nchange "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_special"},{"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.3"}]},{"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 change returned was\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" change"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Funds available at the terminal:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"funds"},{"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":"\"Regular lunches sold:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lunches"},{"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":"\"Special lunches sold:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"specials"},{"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":"The change returned was 7.5\nThe change returned was 2.5\nThe change returned was 0.0\nFunds available at the terminal: 1009.3\nRegular lunches sold: 2\nSpecial lunches sold: 1"}]}]},{"type":"element","tagName":"h2","properties":{"id":"dealing-with-card-transactions","style":"position:relative;"},"children":[{"type":"text","value":"Dealing with card transactions"},{"type":"element","tagName":"a","properties":{"href":"#dealing-with-card-transactions","ariaLabel":"dealing with card transactions 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":"Now let's implement card transactions. We will need methods which take a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LunchCard"}]},{"type":"text","value":" as an argument, and reduce the balance on the card by the price of the lunch. Below you will find the outlines of these functions. Please fill in the methods as described in the comments:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PaymentTerminal"}]},{"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":"# ..."}]},{"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":"eat_lunch_lunchcard"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" LunchCard"},{"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":"# A regular lunch costs 2.50 euros."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If there is enough money on the card, "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# subtract the price of the lunch from the balance"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# and return True. If not, return False."}]},{"type":"text","value":"\n\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":"eat_special_lunchcard"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" LunchCard"},{"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":"# A special lunch costs 4.30 euros."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If there is enough money on the card, "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# subtract the price of the lunch from the balance"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# and return True. If not, return False."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" when paying with a LunchCard the cash funds available at the terminal do not change. However, the lunches are still sold whenever there is the required balance available, so remember to increase the number of lunches sold appropriately."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may use the following code to test your class:"}]},{"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":"exactum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PaymentTerminal"},{"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\nchange "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_lunch"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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 change returned was\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" change"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ncard "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LunchCard"},{"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":"text","value":"\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_special_lunchcard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"card"},{"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":"\"Payment successful:\""}]},{"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":"text","value":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_special_lunchcard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"card"},{"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":"\"Payment successful:\""}]},{"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":"text","value":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_lunch_lunchcard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"card"},{"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":"\"Payment successful:\""}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Funds available at the terminal:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"funds"},{"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":"\"Regular lunches sold:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lunches"},{"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":"\"Special lunches sold:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"specials"},{"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":"The change returned was 7.5\nPayment successful: True\nPayment successful: False\nPayment successful: True\nFunds available at the terminal: 1002.5\nRegular lunches sold: 2\nSpecial lunches sold: 1"}]}]},{"type":"element","tagName":"h2","properties":{"id":"depositing-money-on-the-card","style":"position:relative;"},"children":[{"type":"text","value":"Depositing money on the card"},{"type":"element","tagName":"a","properties":{"href":"#depositing-money-on-the-card","ariaLabel":"depositing money on the card 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":"Finally, let's add a method which lets you deposit money on the card. The card owner pays this by cash, so the deposited sum is added to the funds available at the terminal. Here is an outline for the method:"}]},{"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":"deposit_money_on_card"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" LunchCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" amount"},{"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":"float"}]},{"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":"pass"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may use the following code to test your method:"}]},{"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":"exactum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PaymentTerminal"},{"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\ncard "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LunchCard"},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Card balance 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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euros\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_special_lunchcard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"card"},{"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":"\"Payment successful:\""}]},{"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":"text","value":"\n\nexactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"deposit_money_on_card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"card"},{"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":"100"}]},{"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\"Card balance 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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euros\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_special_lunchcard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"card"},{"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":"\"Payment successful:\""}]},{"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":"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\"Card balance 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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euros\""}]}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Funds available at the terminal:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"funds"},{"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":"\"Regular lunches sold:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lunches"},{"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":"\"Special lunches sold:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"specials"},{"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":"Card balance is 2 euros\nPayment successful: False\nCard balance is 102 euros\nPayment successful: True\nCard balance is 97.7 euros\nFunds available at the terminal: 1100\nRegular lunches sold: 0\nSpecial lunches sold: 1"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"an-instance-of-the-same-class-as-an-argument-to-a-method","style":"position:relative;"},"children":[{"type":"text","value":"An instance of the same class as an argument to a method"},{"type":"element","tagName":"a","properties":{"href":"#an-instance-of-the-same-class-as-an-argument-to-a-method","ariaLabel":"an instance of the same class as an argument to a method 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":"Below we have yet another version of the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" year_of_birth"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" year_of_birth"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's assume we want to write a program which compares the ages of objects of type Person. We could write a separate function for this purpose:"}]},{"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":"older_than"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Person"},{"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":"if"}]},{"type":"text","value":" person1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" person2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth"},{"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","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\nmuhammad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Muhammad ibn Musa al-Khwarizmi\""}]},{"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":"780"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npascal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Blaise Pascal\""}]},{"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":"1623"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngrace "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Grace Hopper\""}]},{"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":"1906"}]},{"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":"if"}]},{"type":"text","value":" older_than"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pascal"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" is older than "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" is not older than "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" older_than"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pascal"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" is older than "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" is not older than "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"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":"Muhammad ibn Musa al-Khwarizmi is older than Blaise Pascal\nGrace Hopper is not older than Blaise Pascal"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"One of the principles of object oriented programming is to include any functionality which handles objects of a certain type in the class definition, as methods. So instead of a function we could write a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"method"}]},{"type":"text","value":" which allows us to compare the age of a Person object to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"another"}]},{"type":"text","value":" Person object:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" year_of_birth"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" year_of_birth\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# NB: type hints must be enclosed in quotation marks if the parameter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# is of the same type as the class itself!"}]},{"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":"older_than"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" another"},{"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":"\"Person\""}]},{"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":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" another"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth"},{"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","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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","boolean"]},"children":[{"type":"text","value":"False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here the object which the method is called on is referred to as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":", while the other Person object is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"another"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Remember, calling a method differs from calling a function. A method is attached to an object with the dot notation:"}]},{"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":"muhammad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Muhammad ibn Musa al-Khwarizmi\""}]},{"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":"780"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npascal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Blaise Pascal\""}]},{"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":"1623"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngrace "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Grace Hopper\""}]},{"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":"1906"}]},{"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":"if"}]},{"type":"text","value":" muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"older_than"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pascal"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" is older than "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" is not older than "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"older_than"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pascal"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" is older than "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" is not older than "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"p","properties":{},"children":[{"type":"text","value":"To the left of the dot is the object itself, which is referred to as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" within the method definition. In parentheses is the argument to the method, which is the object referred to as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"another"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The printout from the program is exactly the same as with the function implementation above."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A rather cosmetic point to finish off: the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if...else"}]},{"type":"text","value":" structure in the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"older_than"}]},{"type":"text","value":" is by and large unneccessary. The value of the Boolean expression in the condition is already the exact same truth value which is returned. The method can thus be simplified:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" year_of_birth"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" year_of_birth\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# NB: type hints must be enclosed in quotation marks if the parameter "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# is of the same type as the class itself!"}]},{"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":"older_than"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" another"},{"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":"\"Person\""}]},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" another"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As stated in the comments in the examples above, if the parameter in a method definition is of the same type as the class itself, the type hint must be enclosed in quotation marks. Leaving the quotation marks out causes an error, which you will see if you try the following:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"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":"# ..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# this would cause an error, as Person must be enclosed in quotation marks"}]},{"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":"older_than"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" another"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Person"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" another"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Comparing properties","tmcname":"part09-05_comparing_properties"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The database of a real estate agency keeps records of available properties with objects defined by the following class:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"RealProperty"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rooms"},{"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":"text","value":" square_metres"},{"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":"text","value":" price_per_sqm"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rooms "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rooms\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"square_metres "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" square_metres\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"price_per_sqm "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" price_per_sqm"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your task is to implement methods which allow for comparison between available properties."}]},{"type":"element","tagName":"h2","properties":{"id":"is-it-bigger","style":"position:relative;"},"children":[{"type":"text","value":"Is it bigger?"},{"type":"element","tagName":"a","properties":{"href":"#is-it-bigger","ariaLabel":"is it bigger 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":"Please write a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bigger(self, compared_to)"}]},{"type":"text","value":" which returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" if the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"RealProperty"}]},{"type":"text","value":" object itself is bigger than the one it is compared to."}]},{"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":"central_studio "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"16"}]},{"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":"5500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndowntown_two_bedroom "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"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":"4200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuburbs_three_bedroom "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"78"}]},{"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":"2500"}]},{"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":"central_studio"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bigger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"downtown_two_bedroom"},{"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":"suburbs_three_bedroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bigger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"downtown_two_bedroom"},{"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":"False\nTrue"}]}]},{"type":"element","tagName":"h2","properties":{"id":"price-difference","style":"position:relative;"},"children":[{"type":"text","value":"Price difference"},{"type":"element","tagName":"a","properties":{"href":"#price-difference","ariaLabel":"price difference 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":"Please write a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"price_difference(self, compared_to)"}]},{"type":"text","value":" which returns the difference in price between the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"RealProperty"}]},{"type":"text","value":" object itself and the one it is compared to. The price difference is the absolute value of the difference between the total prices of the two properties. The total price of a property is its price per square metre multiplied by the amount of square metres in the property."}]},{"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":"central_studio "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"16"}]},{"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":"5500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndowntown_two_bedroom "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"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":"4200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuburbs_three_bedroom "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"78"}]},{"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":"2500"}]},{"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":"central_studio"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"price_difference"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"downtown_two_bedroom"},{"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":"suburbs_three_bedroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"price_difference"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"downtown_two_bedroom"},{"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":"71600\n35400"}]}]},{"type":"element","tagName":"h2","properties":{"id":"is-it-more-expensive","style":"position:relative;"},"children":[{"type":"text","value":"Is it more expensive?"},{"type":"element","tagName":"a","properties":{"href":"#is-it-more-expensive","ariaLabel":"is it more expensive 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":"Please write a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"more_expensive(self, compared_to)"}]},{"type":"text","value":" which returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" if the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"RealProperty"}]},{"type":"text","value":" object itself is more expensive that the one it is compared to."}]},{"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":"central_studio "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"16"}]},{"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":"5500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndowntown_two_bedroom "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"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":"4200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuburbs_three_bedroom "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"78"}]},{"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":"2500"}]},{"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":"central_studio"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"more_expensive"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"downtown_two_bedroom"},{"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":"suburbs_three_bedroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"more_expensive"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"downtown_two_bedroom"},{"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":"False\nTrue"}]}]}]}]},"html":"

    After this section

      \n
    • You will be able to use different data structures for handling objects
    • \n
    • You will know how objects can be passed as arguments
    • \n

    Every value in Python is an object. Any object you create based on a class you've defined yourself works exactly the same as any \"regular\" Python object. For example, objects can be stored in a list:

    from datetime import date\n\nclass CompletedCourse:\n\n    def __init__(self, course_name: str, credits: int, completion_date: date):\n        self.name = course_name\n        self.credits = credits\n        self.completion_date = completion_date\n\n\nif __name__ == \"__main__\":\n    # Here we create some completed courses and add these to a list \n    completed = []\n\n    maths1 = CompletedCourse(\"Mathematics 1\", 5, date(2020, 3, 11))\n    prog1 = CompletedCourse(\"Programming 1\", 6, date(2019, 12, 17))\n\n    completed.append(maths1)\n    completed.append(prog1)\n\n    # Let's add a couple more straight to the list\n    completed.append(CompletedCourse(\"Physics 2\", 4, date(2019, 11, 10)))\n    completed.append(CompletedCourse(\"Programming 2\", 5, date(2020, 5, 19)))\n\n    # Go through all the completed courses, print out their names \n    # and sum up the credits received\n    credits = 0\n    for course in completed:\n        print(course.name)\n        credits += course.credits\n\n    print(\"Total credits received:\", credits)

    Mathematics 1\nProgramming 1\nPhysics 2\nProgramming 2\nTotal credits received: 20

    The exercise template contains a class named Car which represents the features of a car through two attributes: make (str) and top_speed (int).

    Please write a function named fastest_car(cars: list) which takes a list of Car objects as its argument.

    The function should return the make of the fastest car. You may assume there will always be a single car with the highest top speed. Do not change the list given as an argument, or make any changes to the Car class definition.

    You may use the following code to test your function:

    if __name__ == \"__main__\":\n    car1 = Car(\"Saab\", 195)\n    car2 = Car(\"Lada\", 110)\n    car3 = Car(\"Ferrari\", 280)\n    car4 = Car(\"Trabant\", 85)\n\n    cars = [car1, car2, car3, car4]\n    print(fastest_car(cars))

    Ferrari

    The exercise template contains a class named ExamSubmission which, as the name implies, models an examinee's submission in an exam. The class has two attributes defined: examinee (str) and points (int).

    Please write a function named passed(submissions: list, lowest_passing: int) which takes a list of exam submissions and an integer number representing the lowest passing grade as its arguments.

    The function should create and return a new list, which contains only the passed submissions from the original list. Please do not change the list given as an argument, or make any changes to the ExamSubmission class definition.

    You may use the following code to test your function:

    if __name__ == \"__main__\":\n    s1 = ExamSubmission(\"Peter\", 12)\n    s2 = ExamSubmission(\"Pippa\", 19)\n    s3 = ExamSubmission(\"Paul\", 15)\n    s4 = ExamSubmission(\"Phoebe\", 9)\n    s5 = ExamSubmission(\"Persephone\", 17)\n\n    passes = passed([s1, s2, s3, s4, s5], 15)\n    for passing in passes:\n        print(passing)

    ExamSubmission (examinee: Pippa, points: 19)\nExamSubmission (examinee: Paul, points: 15)\nExamSubmission (examinee: Persephone, points: 17)

    You may remember that lists do not contain any objects themselves. They contain references to objects. The exact same object can appear multiple times in a single list, and it can be referred to multiple times within the list or outside it. Let's have a look at an example:

    class Product:\n    def __init__(self, name: int, unit: str):\n        self.name = name\n        self.unit = unit\n\n\nif __name__ == \"__main__\":\n    shopping_list = []\n    milk = Product(\"Milk\", \"litre\")\n\n    shopping_list.append(milk)\n    shopping_list.append(milk)\n    shopping_list.append(Product(\"Cucumber\", \"piece\"))
    \n \n \n \n \n \n \"9\n \n \n

    If there is more than one reference to the same object, it makes no difference which one of the references is used:

    class Dog:\n    def __init__(self, name):\n        self.name = name\n\n    def __str__(self):\n        return self.name\n\ndogs = []\nfluffy = Dog(\"Fluffy\")\ndogs.append(fluffy)\ndogs.append(fluffy)\ndogs.append(Dog(\"Fluffy\"))\n\nprint(\"Dogs initially:\")\nfor dog in dogs:\n    print(dog)\n\nprint(\"The dog at index 0 is renamed:\")\ndogs[0].name = \"Pooch\"\nfor dog in dogs:\n    print(dog)\n\nprint(\"The dog at index 2 is renamed:\")\ndogs[2].name = \"Fifi\"\nfor dog in dogs:\n    print(dog)

    Dogs initially:\nFluffy\nFluffy\nFluffy\nThe dog at index 0 is renamed:\nPooch\nPooch\nFluffy\nThe dog at index 2 is renamed:\nPooch\nPooch\nFifi

    The references at indexes 0 and 1 in the list refer to the same object. Either one of the references can be used to access the object. The reference at index 2 refers to a different object, albeit with seemingly the same contents. Changing the contents of this latter object does not affect the other one.

    The operator is is used for checking if the two references refer to the exact same object, while the operator == will tell you if the contents of the objects are the same. The following example hopefully makes the difference clear:

    list1 = [1, 2, 3]\nlist2 = [1, 2, 3]\nlist3 = list1\n\nprint(list1 is list2)\nprint(list1 is list3)\nprint(list2 is list3)\n\nprint()\n\nprint(list1 == list2)\nprint(list1 == list3)\nprint(list2 == list3)

    False\nTrue\nFalse

    True\nTrue\nTrue

    Any Python object can also be stored in a dictionary or any other data structure. This also applies to objects created based on a class you've defined yourself.

    class Student:\n    def __init__(self, name: str, cr: int):\n        self.name = name\n        self.cr = cr\n\nif __name__ == \"__main__\":\n    # The key in this dictionary is the student number, \n    # and the value is an object of type Student\n    students = {}\n    students[\"12345\"] = Student(\"Saul Student\", 10)\n    students[\"54321\"] = Student(\"Sally Student\", 67)

    The visualisation tool can help in making sense of the above example:

    \n \n \n \n \n \n \"9\n \n \n

    Self or no self?

    Thus far we've only touched upon the surface of using the self parameter name. Let's have a closer look at when it should or should not be used.

    Below we have a simple class which lets us create a vocabulary object containing some words:

    class Vocabulary:\n    def __init__(self):\n        self.words = []\n\n    def add_word(self, word: str):\n        if not word in self.words:\n            self.words.append(word)\n\n    def print_vocabulary(self):\n        for word in sorted(self.words):\n            print(word)\n\nvocab = Vocabulary()\nvocab.add_word(\"python\")\nvocab.add_word(\"object\")\nvocab.add_word(\"object-oriented programming\")\nvocab.add_word(\"object\")\nvocab.add_word(\"nerd\")\n\nvocab.print_vocabulary()

    nerd\nobject\nobject-oriented programming\npython

    The list of words is stored in an attribute named self.words. In this case the self parameter name is mandatory in both the constructor method of the class and in any other method accessing that variable. If self is left out, the different methods will not access the same list of words.

    Let's add a new method to our class definition. The method longest_word(self) returns (one of) the longest words in the vocabulary.

    The following is one way of completing this task, but we will soon see it is not a very good way:

    class Vocabulary:\n    def __init__(self):\n        self.words = []\n\n    # ...\n\n    def longest_word(self):\n        # define two helper variables\n        self.longest = \"\"\n        self.length_of_longest = 0\n\n        for word in self.words:\n            if len(word) > self.length_of_longest:\n                self.length_of_longest = len(word)\n                self.longest = word\n\n        return self.longest

    This method uses two helper variables which are declared with the self parameter name. Remember, the names of variables do not matter in the functional sense, so these variables could also be named more confusingly as, for example, helper and helper2. The code begins to look a bit cryptic:

    class Vocabulary:\n    def __init__(self):\n        self.words = []\n\n    # ...\n\n    def longest_word(self):\n        # define two helper variables\n        self.helper = \"\"\n        self.helper2 = 0\n\n        for word in self.words:\n            if len(word) > self.helper2:\n                self.helper2 = len(word)\n                self.helper = word\n\n        return self.helper

    When a variable is declared with the self parameter name, it becomes an attribute of the object. This means that the variable will exist for as long as the object exists. Specifically, the variable will continue existing also after the method declaring it has finished its execution. In the example above this is quite unnecessary, as the helper variables are meant to be used only within the method longest_word(self). So, declaring helper variables with the self parameter name is not a very good idea here.

    Besides causing variables to exist beyond their \"expiration date\", using self to create new attributes where they aren't necessary can cause difficult bugs in your code. Especially generically named attributes such as self.helper, which are then used in various different methods, can cause unexpected behaviour which is hard to trace.

    For example, if a helper variable is declared as an attribute and assigned an initial value in the constructor, but the variable is then used in an unrelated context in another method, the results are often unpredictable:

    class Vocabulary:\n    def __init__(self):\n        self.words = []\n        # define helper variables\n        self.helper = \"\"\n        self.helper2 = \"\"\n        self.helper3 = \"\"\n        self.helper4 = \"\"\n\n    # ...\n\n    def longest_word(self):\n        for word in self.words:\n            # above the helper variables were all assigned string values\n            # the following will not work because the type of helper2 is wrong\n            if len(word) > self.helper2:\n                self.helper2 = len(word)\n                self.helper = word\n\n        return self.helper

    You might think this would be solved by just declaring attributes where they are used, outside the constructor, but this results in a situation where the attributes accessible through an object are dependent on which methods have been executed. In the previous part we saw that the advantage of declaring attributes in the constructor is that all instances of the class will then have the exact same attributes. If this is not the case, using different instances of the class can easily lead to errors.

    In conclusion, if you need helper variables for use within a single method, the correct way to do it is without self. To make your code easier to understand, also use informative variable names:

    class Vocabulary:\n    def __init__(self):\n        self.words = []\n\n    # ...\n\n    def longest_word(self):\n        # the correct way of declaring helper variables \n        # for use within a single method\n        longest = \"\"\n        length_of_longest = 0\n\n        for word in self.words:\n            if len(word) > length_of_longest:\n                length_of_longest = len(word)\n                longest = word\n\n        return longest

    In the above implementation the helper variables are only accessible while the method is being executed. The values stored within cannot cause complications in other parts of the program.

    Objects as arguments to functions

    The objects created based on our own classes are usually mutable. You may remember that, for instance, Python lists are mutable: when passed as arguments to functions, their contents can change as a result of the execution.

    Let's have a look at a simple example where a function receives a reference to an object of type Student as its argument. The function then changes the name of the student. Both the function and the main function calling it access the same object, so the change is apparent in the main function as well.

    class Student:\n    def __init__(self, name: str, student_number: str):\n        self.name = name\n        self.student_number = student_number\n\n    def __str__(self):\n        return f\"{self.name} ({self.student_number})\"\n\n# the type hint here uses the name of the class defined above\ndef change_name(student: Student):\n    student.name = \"Saul Student\"\n\n# create a Student object\nsteve = Student(\"Steve Student\", \"12345\")\n\nprint(steve)\nchange_name(steve)\nprint(steve)

    Steve Student (12345)\nSaul Student (12345)

    It is also possible to create objects within functions. If a function returns a reference to the newly created object, it is also accessible within the main function:

    from random import randint, choice\n\nclass Student:\n    def __init__(self, name: str, student_number: str):\n        self.name = name\n        self.student_number = student_number\n\n    def __str__(self):\n        return f\"{self.name} ({self.student_number})\"\n\n# This function creates and returns a new Student object.\n# It randomly selects values for the name and the student number.\ndef new_student():\n    first_names = [\"Mark\",\"Mindy\",\"Mary\",\"Mike\"]\n    last_names = [\"Javanese\", \"Rusty\", \"Scriptor\", \"Pythons\"]\n\n    # randomly determine the name\n    name = choice(first_names) + \" \" + choice(last_names)\n\n    # randomly determine the student number\n    student_number = str(randint(10000,99999))\n\n    # Create and return a Student object\n    return Student(name, student_number)\n\nif __name__ == \"__main__\":\n    # Call the function five times and store the results in a list\n    students = []\n    for i in range(5):\n        students.append(new_student())\n\n    # Print out the results\n    for student in students :\n        print(student)

    Executing the above could result in the following printout (NB: as randomness is involved, if you try the code yourself, the results will likely be different).

    Mary Rusty (78218)\nMindy Rusty (80068)\nMike Pythons (70396)\nMark Javanese (83307)\nMary Pythons (45149)

    Objects as arguments to methods

    Similarly, objects can act as arguments to methods. Let's have a look at an example from an amusement park:

    class Person:\n    def __init__(self, name: str, height: int):\n        self.name = name\n        self.height = height\n\nclass Attraction:\n    def __init__(self, name: str, min_height: int):\n        self.visitors = 0\n        self.name = name\n        self.min_height = min_height\n\n    def admit_visitor(self, person: Person):\n        if person.height >= self.min_height:\n            self.visitors += 1\n            print(f\"{person.name} got on board\")\n        else:\n            print(f\"{person.name} was too short :(\")\n\n    def __str__(self):\n        return f\"{self.name} ({self.visitors} visitors)\"

    The Attraction contains a method admit_visitor, which takes an object of type Person as an argument. If the visitor is tall enough, they are admitted on board and the number of visitors is increased. The classes can be tested as follows:

    rollercoaster = Attraction(\"Rollercoaster\", 120)\njared = Person(\"Jared\", 172)\nalice = Person(\"Alice\", 105)\n\nrollercoaster.admit_visitor(jared)\nrollercoaster.admit_visitor(alice)\n\nprint(rollercoaster)

    Jared got on board\nAlice was too short :(\nRollercoaster (1 visitors)

    The exercise template contains a class named Person and a skeleton implementation for the class BabyCentre. A BabyCentre object performs various actions on a Person object. It may, for example, weigh or feed the person. In this exercise you will implement the rest of the BabyCentre class. Please do not change the class definition of Person in any way.

    Weighing persons

    The BabyCentre class definition contains an outline for the function weigh:

    class BabyCentre:\n    def weigh(self, person: Person):\n        # return the weight of the person passed as an argument\n        return -1

    The method takes a Person object as its argument. It should return the weight of the person. You can access the weight of a person through the appropriate attribute defined in the Person class. Please fill in the rest of the implementation for the method weigh.

    Below is an example of a main function where a BabyCentre weighs two separate Person objects:

    baby_centre = BabyCentre()\n\neric = Person(\"Eric\", 1, 110, 7)\npeter = Person(\"Peter\", 33, 176, 85)\n\nprint(f\"{eric.name} weighs {baby_centre.weigh(eric)} kg\")\nprint(f\"{peter.name} weighs {baby_centre.weigh(peter)} kg\")

    Eric weighs 7 kg\nPeter weighs 85 kg

    Feeding

    It is possible to change the state of an object passed as an argument. Please implement the method feed(person: Person) which increases by one the weight of the person passed as an argument.

    In the following example two persons are weighed, and then one of them is fed three times. Then the persons are weighed again:

    baby_centre = BabyCentre()\n\neric = Person(\"Eric\", 1, 110, 7)\npeter = Person(\"Peter\", 33, 176, 85)\n\nprint(f\"{eric.name} weighs {baby_centre.weigh(eric)} kg\")\nprint(f\"{peter.name} weighs {baby_centre.weigh(peter)} kg\")\nprint() \n\nbaby_centre.feed(eric)\nbaby_centre.feed(eric)\nbaby_centre.feed(eric)\n\nprint(f\"{eric.name} weighs {baby_centre.weigh(eric)} kg\")\nprint(f\"{peter.name} weighs {baby_centre.weigh(peter)} kg\")

    The printout should reveal that Eric's weight has increased by three:

    Eric weighs 7 kg\nPeter weighs 85 kg

    Eric weighs 10 kg\nPeter weighs 85 kg

    Counting weigh-ins

    Please implement the method weigh_ins() which returns the total number of weigh-ins a BabyCentre object has performed. NB: you will need a new attribute for keeping track of the number of weigh-ins. You can use the following code to test your method:

    baby_centre = BabyCentre()\n\neric = Person(\"Eric\", 1, 110, 7)\npeter = Person(\"Peter\", 33, 176, 85)\n\nprint(f\"Total number of weigh-ins is {baby_centre.weigh_ins()}\")\n\nbaby_centre.weigh(eric)\nbaby_centre.weigh(eric)\n\nprint(f\"Total number of weigh-ins is {baby_centre.weigh_ins()}\")\n\nbaby_centre.weigh(eric)\nbaby_centre.weigh(eric)\nbaby_centre.weigh(eric)\nbaby_centre.weigh(eric)\n\nprint(f\"Total number of weigh-ins is {baby_centre.weigh_ins()}\")

    Total number of weigh-ins is 0\nTotal number of weigh-ins is 2\nTotal number of weigh-ins is 6

    In the previous part there was an exercise where you implemented the class LunchCard. The card had separate methods for eating a regular and a special lunch, along with a method for depositing money on the card.

    The LunchCard class, as you were asked to implement it, has some problems, however. The card itself had knowledge of the prices of the different lunch options, and knew to subtract the right amount of money from the balance based on these. But imagine the prices changed, or there were new items introduced to the system, but several cards were already registered in the system. This would mean all existing cards would need to be replaced by versions with knowledge of the new prices.

    A better solution would be to make the cards \"stupid\", ignorant of the prices of different products. The purpose of the card would be to simply keep track of the available balance. All more complicated features should be contained within another class: the payment terminal.

    A simpler LunchCard

    Let's first implement a simpler version of the LunchCard class. The card should contain functionality only for finding out the current balance, depositing money on the card, and subtracting from the balance. Please fill in the subtract_from_balance(amount) method in the exercise template according to the instructions in the comments:

    class LunchCard:\n    def __init__(self, balance: float):\n        self.balance = balance\n\n    def deposit_money(self, amount: float):\n        self.balance += amount\n\n    def subtract_from_balance(self, amount: float):\n        pass\n        # The amount should be subtracted from the balance only if\n        # there is enough money on the card.\n        # If the payment is successful, the method returns True. \n        # Otherwise it returns False.

    You may use the following code to test your function:

    if __name__ == \"__main__\":\n    card = LunchCard(10)\n    print(\"Balance\", card.balance)\n    result = card.subtract_from_balance(8)\n    print(\"Payment successful:\", result)\n    print(\"Balance\", card.balance)\n    result = card.subtract_from_balance(4)\n    print(\"Payment successful:\", result)\n    print(\"Balance\", card.balance)

    Balance 10\nPayment successful: True\nBalance 2\nPayment successful: False\nBalance 2

    The payment terminal and dealing with cash payments

    In the student cafeteria it is possible to pay with either cash or a LunchCard. A payment terminal is used to handle both cash and card transactions. Let's start with the cash transactions.

    Here we have a skeleton implementation for a PaymentTerminal class. Please implement the methods as described in the comments:

    class PaymentTerminal:\n    def __init__(self):\n        # Initially there is 1000 euros in cash available at the terminal\n        self.funds = 1000\n        self.lunches = 0\n        self.specials = 0\n\n    def eat_lunch(self, payment: float):\n        # A regular lunch costs 2.50 euros.\n        # Increase the value of the funds at the terminal by the \n        # price of the lunch, increase the number of lunches sold, \n        # and return the appropriate change.\n        # If the payment passed as an argument is not large enough to cover\n        # the price, the lunch is not sold, and the entire sum is returned.\n\n    def eat_special(self, payment: float):\n        # A special lunch costs 4.30 euros.\n        # Increase the value of the funds at the terminal by the \n        # price of the lunch, increase the number of lunches sold, \n        # and return the appropriate change.\n        # If the payment passed as an argument is not large enough to cover\n        # the price, the lunch is not sold, and the entire sum is returned.

    You may use the following code to test your class:

    exactum = PaymentTerminal()\n\nchange = exactum.eat_lunch(10)\nprint(\"The change returned was\", change)\n\nchange = exactum.eat_lunch(5)\nprint(\"The change returned was\", change)\n\nchange = exactum.eat_special(4.3)\nprint(\"The change returned was\", change)\n\nprint(\"Funds available at the terminal:\", exactum.funds)\nprint(\"Regular lunches sold:\", exactum.lunches)\nprint(\"Special lunches sold:\", exactum.specials)

    The change returned was 7.5\nThe change returned was 2.5\nThe change returned was 0.0\nFunds available at the terminal: 1009.3\nRegular lunches sold: 2\nSpecial lunches sold: 1

    Dealing with card transactions

    Now let's implement card transactions. We will need methods which take a LunchCard as an argument, and reduce the balance on the card by the price of the lunch. Below you will find the outlines of these functions. Please fill in the methods as described in the comments:

    class PaymentTerminal:\n    # ...\n\n    def eat_lunch_lunchcard(self, card: LunchCard):\n        # A regular lunch costs 2.50 euros.\n        # If there is enough money on the card, \n        # subtract the price of the lunch from the balance\n        # and return True. If not, return False.\n\n\n    def eat_special_lunchcard(self, card: LunchCard):\n        # A special lunch costs 4.30 euros.\n        # If there is enough money on the card, \n        # subtract the price of the lunch from the balance\n        # and return True. If not, return False.

    NB: when paying with a LunchCard the cash funds available at the terminal do not change. However, the lunches are still sold whenever there is the required balance available, so remember to increase the number of lunches sold appropriately.

    You may use the following code to test your class:

    exactum = PaymentTerminal()\n\nchange = exactum.eat_lunch(10)\nprint(\"The change returned was\", change)\n\ncard = LunchCard(7)\n\nresult = exactum.eat_special_lunchcard(card)\nprint(\"Payment successful:\", result)\nresult = exactum.eat_special_lunchcard(card)\nprint(\"Payment successful:\", result)\nresult = exactum.eat_lunch_lunchcard(card)\nprint(\"Payment successful:\", result)\n\nprint(\"Funds available at the terminal:\", exactum.funds)\nprint(\"Regular lunches sold:\", exactum.lunches)\nprint(\"Special lunches sold:\", exactum.specials)

    The change returned was 7.5\nPayment successful: True\nPayment successful: False\nPayment successful: True\nFunds available at the terminal: 1002.5\nRegular lunches sold: 2\nSpecial lunches sold: 1

    Depositing money on the card

    Finally, let's add a method which lets you deposit money on the card. The card owner pays this by cash, so the deposited sum is added to the funds available at the terminal. Here is an outline for the method:

    def deposit_money_on_card(self, card: LunchCard, amount: float):\n    pass

    You may use the following code to test your method:

    exactum = PaymentTerminal()\n\ncard = LunchCard(2)\nprint(f\"Card balance is {card.balance} euros\")\n\nresult = exactum.eat_special_lunchcard(card)\nprint(\"Payment successful:\", result)\n\nexactum.deposit_money_on_card(card, 100)\nprint(f\"Card balance is {card.balance} euros\")\n\nresult = exactum.eat_special_lunchcard(card)\nprint(\"Payment successful:\", result)\nprint(f\"Card balance is {card.balance} euros\")\n\nprint(\"Funds available at the terminal:\", exactum.funds)\nprint(\"Regular lunches sold:\", exactum.lunches)\nprint(\"Special lunches sold:\", exactum.specials)

    Card balance is 2 euros\nPayment successful: False\nCard balance is 102 euros\nPayment successful: True\nCard balance is 97.7 euros\nFunds available at the terminal: 1100\nRegular lunches sold: 0\nSpecial lunches sold: 1

    An instance of the same class as an argument to a method

    Below we have yet another version of the class Person:

    class Person:\n    def __init__(self, name: str, year_of_birth: int):\n        self.name = name\n        self.year_of_birth = year_of_birth

    Let's assume we want to write a program which compares the ages of objects of type Person. We could write a separate function for this purpose:

    def older_than(person1: Person, person2: Person):\n    if person1.year_of_birth < person2.year_of_birth:\n        return True\n    else:\n        return False\n\nmuhammad = Person(\"Muhammad ibn Musa al-Khwarizmi\", 780)\npascal = Person(\"Blaise Pascal\", 1623)\ngrace = Person(\"Grace Hopper\", 1906)\n\nif older_than(muhammad, pascal):\n    print(f\"{muhammad} is older than {pascal}\")\nelse:\n    print(f\"{muhammad} is not older than {pascal}\")\n\nif older_than(grace, pascal):\n    print(f\"{grace} is older than {pascal}\")\nelse:\n    print(f\"{grace} is not older than {pascal}\")

    Muhammad ibn Musa al-Khwarizmi is older than Blaise Pascal\nGrace Hopper is not older than Blaise Pascal

    One of the principles of object oriented programming is to include any functionality which handles objects of a certain type in the class definition, as methods. So instead of a function we could write a method which allows us to compare the age of a Person object to another Person object:

    class Person:\n    def __init__(self, name: str, year_of_birth: int):\n        self.name = name\n        self.year_of_birth = year_of_birth\n\n    # NB: type hints must be enclosed in quotation marks if the parameter\n    # is of the same type as the class itself!\n    def older_than(self, another: \"Person\"):\n        if self.year_of_birth < another.year_of_birth:\n            return True\n        else:\n            return False

    Here the object which the method is called on is referred to as self, while the other Person object is another.

    Remember, calling a method differs from calling a function. A method is attached to an object with the dot notation:

    muhammad = Person(\"Muhammad ibn Musa al-Khwarizmi\", 780)\npascal = Person(\"Blaise Pascal\", 1623)\ngrace = Person(\"Grace Hopper\", 1906)\n\nif muhammad.older_than(pascal):\n    print(f\"{muhammad.name} is older than {pascal.name}\")\nelse:\n    print(f\"{muhammad.name} is not older than {pascal.name}\")\n\nif grace.older_than(pascal):\n    print(f\"{grace.name} is older than {pascal.name}\")\nelse:\n    print(f\"{grace.name} is not older than {pascal.name}\")

    To the left of the dot is the object itself, which is referred to as self within the method definition. In parentheses is the argument to the method, which is the object referred to as another.

    The printout from the program is exactly the same as with the function implementation above.

    A rather cosmetic point to finish off: the if...else structure in the method older_than is by and large unneccessary. The value of the Boolean expression in the condition is already the exact same truth value which is returned. The method can thus be simplified:

    class Person:\n    def __init__(self, name: str, year_of_birth: int):\n        self.name = name\n        self.year_of_birth = year_of_birth\n\n    # NB: type hints must be enclosed in quotation marks if the parameter \n    # is of the same type as the class itself!\n    def older_than(self, another: \"Person\"):\n        return self.year_of_birth < another.year_of_birth:

    As stated in the comments in the examples above, if the parameter in a method definition is of the same type as the class itself, the type hint must be enclosed in quotation marks. Leaving the quotation marks out causes an error, which you will see if you try the following:

    class Person:\n    # ...\n\n    # this would cause an error, as Person must be enclosed in quotation marks\n    def older_than(self, another: Person):\n        return self.year_of_birth < another.year_of_birth:

    The database of a real estate agency keeps records of available properties with objects defined by the following class:

    class RealProperty:\n    def __init__(self, rooms: int, square_metres: int, price_per_sqm: int):\n        self.rooms = rooms\n        self.square_metres = square_metres\n        self.price_per_sqm = price_per_sqm

    Your task is to implement methods which allow for comparison between available properties.

    Is it bigger?

    Please write a method named bigger(self, compared_to) which returns True if the RealProperty object itself is bigger than the one it is compared to.

    An example of how the function should work:

    central_studio = RealProperty(1, 16, 5500)\ndowntown_two_bedroom = RealProperty(2, 38, 4200)\nsuburbs_three_bedroom = RealProperty(3, 78, 2500)\n\nprint(central_studio.bigger(downtown_two_bedroom))\nprint(suburbs_three_bedroom.bigger(downtown_two_bedroom))

    False\nTrue

    Price difference

    Please write a method named price_difference(self, compared_to) which returns the difference in price between the RealProperty object itself and the one it is compared to. The price difference is the absolute value of the difference between the total prices of the two properties. The total price of a property is its price per square metre multiplied by the amount of square metres in the property.

    An example of how the function should work:

    central_studio = RealProperty(1, 16, 5500)\ndowntown_two_bedroom = RealProperty(2, 38, 4200)\nsuburbs_three_bedroom = RealProperty(3, 78, 2500)\n\nprint(central_studio.price_difference(downtown_two_bedroom))\nprint(suburbs_three_bedroom.price_difference(downtown_two_bedroom))

    71600\n35400

    Is it more expensive?

    Please write a method named more_expensive(self, compared_to) which returns True if the RealProperty object itself is more expensive that the one it is compared to.

    An example of how the function should work:

    central_studio = RealProperty(1, 16, 5500)\ndowntown_two_bedroom = RealProperty(2, 38, 4200)\nsuburbs_three_bedroom = RealProperty(3, 78, 2500)\n\nprint(central_studio.more_expensive(downtown_two_bedroom))\nprint(suburbs_three_bedroom.more_expensive(downtown_two_bedroom))

    False\nTrue

    ","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-9/1-objects-and-references.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 able to use different data structures for handling objects"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how objects can be passed as arguments"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Every value in Python is an object. Any object you create based on a class you've defined yourself works exactly the same as any \"regular\" Python object. For example, objects can be stored in 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":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" datetime "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" date\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"CompletedCourse"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" course_name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" credits"},{"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":"text","value":" completion_date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" date"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" course_name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" credits\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"completion_date "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" completion_date\n\n\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 we create some completed courses and add these to a list "}]},{"type":"text","value":"\n completed "},{"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 maths1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CompletedCourse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mathematics 1\""}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"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":"3"}]},{"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":"11"}]},{"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 prog1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CompletedCourse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Programming 1\""}]},{"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":"6"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2019"}]},{"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":"12"}]},{"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":"17"}]},{"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 completed"},{"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":"maths1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n completed"},{"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":"prog1"},{"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":"# Let's add a couple more straight to the list"}]},{"type":"text","value":"\n completed"},{"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":"CompletedCourse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Physics 2\""}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2019"}]},{"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":"11"}]},{"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":"10"}]},{"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 completed"},{"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":"CompletedCourse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Programming 2\""}]},{"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":"5"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" date"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"2020"}]},{"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":"5"}]},{"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":"19"}]},{"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\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Go through all the completed courses, print out their names "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# and sum up the credits received"}]},{"type":"text","value":"\n credits "},{"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":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" course "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" completed"},{"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":"course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits\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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Total credits received:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" credits"},{"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":"Mathematics 1\nProgramming 1\nPhysics 2\nProgramming 2\nTotal credits received: 20"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The fastest car","tmcname":"part09-01_fastest_car"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Car"}]},{"type":"text","value":" which represents the features of a car through two attributes: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"make (str)"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"top_speed (int)"}]},{"type":"text","value":"."}]},{"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":"fastest_car(cars: list)"}]},{"type":"text","value":" which takes a list of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Car"}]},{"type":"text","value":" objects as its argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should return the make of the fastest car. You may assume there will always be a single car with the highest top speed. Do not change the list given as an argument, or make any changes to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Car"}]},{"type":"text","value":" class definition."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may use the following code to test your 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","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 car1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Car"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saab\""}]},{"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":"195"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n car2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Car"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lada\""}]},{"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":"110"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n car3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Car"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ferrari\""}]},{"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":"280"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n car4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Car"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Trabant\""}]},{"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":"85"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n cars "},{"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":"text","value":"car1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" car2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" car3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" car4"},{"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":"fastest_car"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"cars"},{"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":"Ferrari"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Passing submissions","tmcname":"part09-02_passing_submissions"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ExamSubmission"}]},{"type":"text","value":" which, as the name implies, models an examinee's submission in an exam. The class has two attributes defined: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"examinee (str)"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"points (int)"}]},{"type":"text","value":"."}]},{"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":"passed(submissions: list, lowest_passing: int)"}]},{"type":"text","value":" which takes a list of exam submissions and an integer number representing the lowest passing grade as its arguments."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The function should create and return a new list, which contains only the passed submissions from the original list. Please do not change the list given as an argument, or make any changes to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ExamSubmission"}]},{"type":"text","value":" class definition."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may use the following code to test your 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","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 s1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ExamSubmission"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"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":"12"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n s2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ExamSubmission"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pippa\""}]},{"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":"19"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n s3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ExamSubmission"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n s4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ExamSubmission"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Phoebe\""}]},{"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":"9"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n s5 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ExamSubmission"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Persephone\""}]},{"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":"17"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n passes "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" passed"},{"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":"s1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" s5"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"15"}]},{"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":" passing "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" passes"},{"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":"passing"},{"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":"ExamSubmission (examinee: Pippa, points: 19)\nExamSubmission (examinee: Paul, points: 15)\nExamSubmission (examinee: Persephone, points: 17)"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may remember that lists do not contain any objects themselves. They contain "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"references to objects"}]},{"type":"text","value":". The exact same object can appear multiple times in a single list, and it can be referred to multiple times within the list or outside it. Let's have a look at an example:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Product"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"text","value":" unit"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"unit "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" unit\n\n\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 shopping_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","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n milk "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Milk\""}]},{"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":"\"litre\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n shopping_list"},{"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":"milk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n shopping_list"},{"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":"milk"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n shopping_list"},{"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":"Product"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Cucumber\""}]},{"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":"\"piece\""}]},{"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":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 336px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/246fe44dfbe7194a2f4607b6fb229cee/d99f2/9_1_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: 60%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAMCAYAAABiDJ37AAAACXBIWXMAAAsTAAALEwEAmpwYAAABRUlEQVQoz32TCYuDQAyF/f8/TwrVWkF7Wax3T4ua5QsbdqplA8N4vLx5yct48hvTNOmeZZkURSFVVUlZllLXtRwOB3m/3wZVzOVyUYzhmqaRNE3FEycgbdtW5nG9Xj8Iv2GI2+32R2gK8zyX7XYr+/1eVquVdF23IAQThqEcj0ddcRxLFEX6vCA8n8+y2WwkCAItwRS5hGDW67Xi2DlwGAYtf0FIz+aBSpeQfhl+jlsQYsj9ftdl5VKiS4gh9Ov5fKp6sDyjfEH4er3k8XioUisLQhdDMhjIfN/Xd0g/THET5u6aq/x3MZQ+juNHjmcgdxkIFX3fqwKUu4EiDnQPYnnyT5DA+DC4SZJoG1i4aRNgim33SNjtdnI6nfQGkMDpzBT9oy80m++YYaRgv7XJ4yfO2jWjNGYKIjMDt0mkn3yfj45L+gMG96HyVCCmjQAAAABJRU5ErkJggg=='); 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":["/static/246fe44dfbe7194a2f4607b6fb229cee/a0b58/9_1_1.webp 230w","/static/246fe44dfbe7194a2f4607b6fb229cee/a6cd2/9_1_1.webp 336w"],"sizes":"(max-width: 336px) 100vw, 336px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/246fe44dfbe7194a2f4607b6fb229cee/81c8e/9_1_1.png 230w","/static/246fe44dfbe7194a2f4607b6fb229cee/d99f2/9_1_1.png 336w"],"sizes":"(max-width: 336px) 100vw, 336px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/246fe44dfbe7194a2f4607b6fb229cee/d99f2/9_1_1.png","alt":"9 1 1","title":"9 1 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":"p","properties":{},"children":[{"type":"text","value":"If there is more than one reference to the same object, it makes no difference which one of the references is used:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Dog"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name\n\ndogs "},{"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":"\nfluffy "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Dog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fluffy\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndogs"},{"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":"fluffy"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndogs"},{"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":"fluffy"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndogs"},{"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":"Dog"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Fluffy\""}]},{"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":"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":"\"Dogs initially:\""}]},{"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":" dog "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" dogs"},{"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":"dog"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The dog at index 0 is renamed:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndogs"},{"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":"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":"\"Pooch\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" dog "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" dogs"},{"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":"dog"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The dog at index 2 is renamed:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndogs"},{"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":"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":"\"Fifi\""}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" dog "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" dogs"},{"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":"dog"},{"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":"Dogs initially:\nFluffy\nFluffy\nFluffy\nThe dog at index 0 is renamed:\nPooch\nPooch\nFluffy\nThe dog at index 2 is renamed:\nPooch\nPooch\nFifi"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The references at indexes 0 and 1 in the list refer to the same object. Either one of the references can be used to access the object. The reference at index 2 refers to a different object, albeit with seemingly the same contents. Changing the contents of this latter object does not affect the other one."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" is used for checking if the two references refer to the exact same object, while the operator "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" will tell you if the contents of the objects are the same. The following example hopefully makes the difference clear:"}]},{"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":"list1 "},{"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":"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":" "},{"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":"\nlist2 "},{"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":"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":" "},{"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":"\nlist3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" list1\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":"list1 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" list2"},{"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":"list1 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" list3"},{"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":"list2 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" list3"},{"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":"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":"list1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" list2"},{"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":"list1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" list3"},{"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":"list2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" list3"},{"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":"False\nTrue\nFalse"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"True\nTrue\nTrue"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Any Python object can also be stored in a dictionary or any other data structure. This also applies to objects created based on a class you've defined yourself."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" cr"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"cr "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" cr\n\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":"# The key in this dictionary is the student number, "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# and the value is an object of type Student"}]},{"type":"text","value":"\n students "},{"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 students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345\""}]},{"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":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saul Student\""}]},{"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":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"54321\""}]},{"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":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sally Student\""}]},{"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":"67"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"a","properties":{"href":"http://www.pythontutor.com/visualize.html#mode=edit","target":"_blank","rel":["noopener","noreferrer"]},"children":[{"type":"text","value":"visualisation tool"}]},{"type":"text","value":" can help in making sense of the above example:"}]},{"type":"element","tagName":"span","properties":{"className":["gatsby-resp-image-wrapper"],"style":"position: relative; display: block; margin-left: auto; margin-right: auto; max-width: 920px; margin-bottom: 1rem;"},"children":[{"type":"text","value":"\n "},{"type":"element","tagName":"a","properties":{"className":["gatsby-resp-image-link"],"href":"/static/f80b81163a85d325ab28cff091c2f896/aa61c/9_1_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: 44.34782608695652%; position: relative; bottom: 0; left: 0; background-image: url('data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABQAAAAJCAYAAAAywQxIAAAACXBIWXMAAAsTAAALEwEAmpwYAAABeElEQVQoz2WSWW8TMRhF5///I3hD4rFvQANFLW2AJJNZ7fG+zEF2MhFtr2R5Fvvq3qOvWdeVnHNdRWJSnE8z3sX6Xv7fVs44a+uzMZ515Z2alBJqWRi6HikkUiyIacFaVy/HGIsr1kW+/zzw+NwS0srYPzGO92j3jIt7bPhFSD3NlsxozTxOaKXIOd3SbQox8+PxyMvfER9X1LJHih1SP7DoB4Ta4UJ7SWiNYewHFikR08wiS0KL0aZ+00pTzm1KuaDZMY537yvnK5fT4cjQD7V+MSl7RTALvPOvWFbTMKHNAakEzkpWHBBpUk7EEOjalr7rmcaZru0ZuoFpGK4YNHnNV8PXKM7nrxxPHxmXTyj3hcZZg9a61iuyJiKFI8bC9jIB/yd7uwvZMs9PWH8gpI7Ga411F1OlFM4Vs0CIAe99NSz8NoZvq0/DNw7HD8z6M9Lc0WgVkMLjXGToO/7sf1ee3bnD+3C7vE3Deul8MywTkVIgRl+D/AOPArp7NsH/1QAAAABJRU5ErkJggg=='); 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":["/static/f80b81163a85d325ab28cff091c2f896/a0b58/9_1_2.webp 230w","/static/f80b81163a85d325ab28cff091c2f896/bc10c/9_1_2.webp 460w","/static/f80b81163a85d325ab28cff091c2f896/966d8/9_1_2.webp 920w","/static/f80b81163a85d325ab28cff091c2f896/fa9eb/9_1_2.webp 1065w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/webp"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"source","properties":{"srcSet":["/static/f80b81163a85d325ab28cff091c2f896/81c8e/9_1_2.png 230w","/static/f80b81163a85d325ab28cff091c2f896/08a84/9_1_2.png 460w","/static/f80b81163a85d325ab28cff091c2f896/c0255/9_1_2.png 920w","/static/f80b81163a85d325ab28cff091c2f896/aa61c/9_1_2.png 1065w"],"sizes":"(max-width: 920px) 100vw, 920px","type":"image/png"},"children":[]},{"type":"text","value":"\n "},{"type":"element","tagName":"img","properties":{"className":["gatsby-resp-image-image"],"src":"/static/f80b81163a85d325ab28cff091c2f896/c0255/9_1_2.png","alt":"9 1 2","title":"9 1 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":"h2","properties":{"id":"self-or-no-self","style":"position:relative;"},"children":[{"type":"text","value":"Self or no self?"},{"type":"element","tagName":"a","properties":{"href":"#self-or-no-self","ariaLabel":"self or no self 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":"Thus far we've only touched upon the surface of using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" parameter name. Let's have a closer look at when it should or should not be used."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Below we have a simple class which lets us create a vocabulary object containing some words:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Vocabulary"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_word"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" word"},{"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":"str"}]},{"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":"if"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words"},{"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":"word"},{"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":"print_vocabulary"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"for"}]},{"type":"text","value":" word "},{"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":"sorted"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words"},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nvocab "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Vocabulary"},{"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":"\nvocab"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_word"},{"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":"\nvocab"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"object\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nvocab"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"object-oriented programming\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nvocab"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"object\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nvocab"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"nerd\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nvocab"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_vocabulary"},{"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":"nerd\nobject\nobject-oriented programming\npython"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The list of words is stored in an attribute named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.words"}]},{"type":"text","value":". In this case the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" parameter name is mandatory in both the constructor method of the class and in any other method accessing that variable. If "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" is left out, the different methods will not access the same list of words."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's add a new method to our class definition. The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"longest_word(self)"}]},{"type":"text","value":" returns (one of) the longest words in the vocabulary."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following is one way of completing this task, but we will soon see it is not a very good way:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Vocabulary"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words "},{"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","comment"]},"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":"longest_word"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# define two helper variables"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"longest "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length_of_longest "},{"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":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words"},{"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":"if"}]},{"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":"word"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length_of_longest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length_of_longest "},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"longest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" word\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"longest"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This method uses two helper variables which are declared with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" parameter name. Remember, the names of variables do not matter in the functional sense, so these variables could also be named more confusingly as, for example, "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"helper"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"helper2"}]},{"type":"text","value":". The code begins to look a bit cryptic:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Vocabulary"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words "},{"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","comment"]},"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":"longest_word"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# define two helper variables"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper2 "},{"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":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words"},{"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":"if"}]},{"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":"word"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper2 "},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" word\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When a variable is declared with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" parameter name, it becomes an attribute of the object. This means that the variable will exist for as long as the object exists. Specifically, the variable will continue existing also after the method declaring it has finished its execution. In the example above this is quite unnecessary, as the helper variables are meant to be used only within the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"longest_word(self)"}]},{"type":"text","value":". So, declaring helper variables with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" parameter name is not a very good idea here."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Besides causing variables to exist beyond their \"expiration date\", using "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" to create new attributes where they aren't necessary can cause difficult bugs in your code. Especially generically named attributes such as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self.helper"}]},{"type":"text","value":", which are then used in various different methods, can cause unexpected behaviour which is hard to trace."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"For example, if a helper variable is declared as an attribute and assigned an initial value in the constructor, but the variable is then used in an unrelated context in another method, the results are often unpredictable:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Vocabulary"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words "},{"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":"# define helper variables"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper2 "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper3 "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper4 "},{"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":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"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":"longest_word"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words"},{"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":"# above the helper variables were all assigned string values"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the following will not work because the type of helper2 is wrong"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"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":"word"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper2 "},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" word\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"helper"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You might think this would be solved by just declaring attributes where they are used, "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"outside"}]},{"type":"text","value":" the constructor, but this results in a situation where the attributes accessible through an object are dependent on "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"which methods have been executed"}]},{"type":"text","value":". In the previous part we saw that the advantage of declaring attributes in the constructor is that all instances of the class will then have the exact same attributes. If this is not the case, using different instances of the class can easily lead to errors."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In conclusion, if you need helper variables for use within a single method, the correct way to do it is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"without"}]},{"type":"text","value":" "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":". To make your code easier to understand, also use informative variable names:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Vocabulary"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words "},{"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","comment"]},"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":"longest_word"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# the correct way of declaring helper variables "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# for use within a single method"}]},{"type":"text","value":"\n longest "},{"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":"text","value":"\n length_of_longest "},{"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":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" word "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"words"},{"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":"if"}]},{"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":"word"},{"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":" length_of_longest"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n length_of_longest "},{"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":"word"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n longest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" word\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" longest"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the above implementation the helper variables are only accessible while the method is being executed. The values stored within cannot cause complications in other parts of the program."}]},{"type":"element","tagName":"h2","properties":{"id":"objects-as-arguments-to-functions","style":"position:relative;"},"children":[{"type":"text","value":"Objects as arguments to functions"},{"type":"element","tagName":"a","properties":{"href":"#objects-as-arguments-to-functions","ariaLabel":"objects as arguments to 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":"The objects created based on our own classes are usually mutable. You may remember that, for instance, Python lists are mutable: when passed as arguments to functions, their contents can change as a result of the execution."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at a simple example where a function receives a reference to an object of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" as its argument. The function then changes the name of the student. Both the function and the main function calling it access the same object, so the change is apparent in the main function as well."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" student_number"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"student_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" student_number\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"student_number"},{"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 type hint here uses the name of the class defined above"}]},{"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":"change_name"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Student"},{"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 student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"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":"\"Saul Student\""}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# create a Student object"}]},{"type":"text","value":"\nsteve "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"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 Student\""}]},{"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":"\"12345\""}]},{"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":"steve"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nchange_name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"steve"},{"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":"steve"},{"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":"Steve Student (12345)\nSaul Student (12345)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is also possible to create objects within functions. If a function returns a reference to the newly created object, it is also accessible within 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","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" choice\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" student_number"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"student_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" student_number\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"student_number"},{"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":"# This function creates and returns a new Student object."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# It randomly selects values for the name and the student number."}]},{"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":"new_student"}]},{"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 first_names "},{"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":"\"Mark\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mindy\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mary\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mike\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"]"}]},{"type":"text","value":"\n last_names "},{"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":"\"Javanese\""}]},{"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":"\"Rusty\""}]},{"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":"\"Scriptor\""}]},{"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":"\"Pythons\""}]},{"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":"# randomly determine the name"}]},{"type":"text","value":"\n name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" choice"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"first_names"},{"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":"\" \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" choice"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"last_names"},{"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":"# randomly determine the student number"}]},{"type":"text","value":"\n student_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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"randint"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"99999"}]},{"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","comment"]},"children":[{"type":"text","value":"# Create and return a Student object"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" student_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":"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":"# Call the function five times and store the results in a list"}]},{"type":"text","value":"\n students "},{"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","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":"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","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n students"},{"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":"new_student"},{"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\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Print out the results"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" student "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" students "},{"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":"student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Executing the above could result in the following printout (NB: as randomness is involved, if you try the code yourself, the results will likely be different)."}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Mary Rusty (78218)\nMindy Rusty (80068)\nMike Pythons (70396)\nMark Javanese (83307)\nMary Pythons (45149)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"objects-as-arguments-to-methods","style":"position:relative;"},"children":[{"type":"text","value":"Objects as arguments to methods"},{"type":"element","tagName":"a","properties":{"href":"#objects-as-arguments-to-methods","ariaLabel":"objects as arguments to methods 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":"Similarly, objects can act as arguments to methods. Let's have a look at an example from an amusement park:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" height"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" height\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Attraction"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" min_height"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"visitors "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"min_height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" min_height\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":"admit_visitor"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Person"},{"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":"if"}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"height "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":">="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"min_height"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"visitors "},{"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":"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":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" got on board\""}]}]},{"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":"else"}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" was too short :(\""}]}]},{"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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"visitors"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" visitors)\""}]}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The Attraction contains a method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"admit_visitor"}]},{"type":"text","value":", which takes an object of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" as an argument. If the visitor is tall enough, they are admitted on board and the number of visitors is increased. The classes can be tested as follows:"}]},{"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":"rollercoaster "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Attraction"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Rollercoaster\""}]},{"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":"120"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\njared "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Jared\""}]},{"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":"172"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nalice "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Alice\""}]},{"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":"105"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nrollercoaster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"admit_visitor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"jared"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nrollercoaster"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"admit_visitor"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"alice"},{"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":"rollercoaster"},{"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":"Jared got on board\nAlice was too short :(\nRollercoaster (1 visitors)"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Baby Centre","tmcname":"part09-03_baby_centre"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" and a skeleton implementation for the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BabyCentre"}]},{"type":"text","value":". A "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BabyCentre"}]},{"type":"text","value":" object performs various actions on a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" object. It may, for example, weigh or feed the person. In this exercise you will implement the rest of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BabyCentre"}]},{"type":"text","value":" class. Please do not change the class definition of "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" in any way."}]},{"type":"element","tagName":"h2","properties":{"id":"weighing-persons","style":"position:relative;"},"children":[{"type":"text","value":"Weighing persons"},{"type":"element","tagName":"a","properties":{"href":"#weighing-persons","ariaLabel":"weighing persons 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 "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BabyCentre"}]},{"type":"text","value":" class definition contains an outline for the function "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"weigh"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"BabyCentre"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"weigh"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Person"},{"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":"# return the weight of the person passed as an argument"}]},{"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","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The method takes a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" object as its argument. It should return the weight of the person. You can access the weight of a person through the appropriate attribute defined in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" class. Please fill in the rest of the implementation for the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"weigh"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Below is an example of a main function where a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BabyCentre"}]},{"type":"text","value":" weighs two separate "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" objects:"}]},{"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":"baby_centre "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BabyCentre"},{"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\neric "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"1"}]},{"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":"110"}]},{"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":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"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":"33"}]},{"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":"176"}]},{"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":"85"}]},{"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":"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":"eric"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" weighs "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"baby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"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":" kg\""}]}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" weighs "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"baby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peter"},{"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":" kg\""}]}]},{"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":"Eric weighs 7 kg\nPeter weighs 85 kg"}]}]},{"type":"element","tagName":"h2","properties":{"id":"feeding","style":"position:relative;"},"children":[{"type":"text","value":"Feeding"},{"type":"element","tagName":"a","properties":{"href":"#feeding","ariaLabel":"feeding 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":"It is possible to change the state of an object passed as an argument. Please implement the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"feed(person: Person)"}]},{"type":"text","value":" which increases by one the weight of the person passed as an argument."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example two persons are weighed, and then one of them is fed three times. Then the persons are weighed again:"}]},{"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":"baby_centre "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BabyCentre"},{"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\neric "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"1"}]},{"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":"110"}]},{"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":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"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":"33"}]},{"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":"176"}]},{"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":"85"}]},{"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":"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":"eric"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" weighs "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"baby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"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":" kg\""}]}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" weighs "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"baby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peter"},{"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":" kg\""}]}]},{"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\nbaby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"feed"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbaby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"feed"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbaby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"feed"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"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":"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":"eric"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" weighs "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"baby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"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":" kg\""}]}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"peter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" weighs "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"baby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peter"},{"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":" kg\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The printout should reveal that Eric's weight has increased by three:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eric weighs 7 kg\nPeter weighs 85 kg"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Eric weighs 10 kg\nPeter weighs 85 kg"}]}]},{"type":"element","tagName":"h2","properties":{"id":"counting-weigh-ins","style":"position:relative;"},"children":[{"type":"text","value":"Counting weigh-ins"},{"type":"element","tagName":"a","properties":{"href":"#counting-weigh-ins","ariaLabel":"counting weigh ins 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":"Please implement the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"weigh_ins()"}]},{"type":"text","value":" which returns the total number of weigh-ins a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BabyCentre"}]},{"type":"text","value":" object has performed. NB: you will need a new attribute for keeping track of the number of weigh-ins. You can use the following code to test your method:"}]},{"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":"baby_centre "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BabyCentre"},{"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\neric "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"1"}]},{"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":"110"}]},{"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":"7"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"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":"33"}]},{"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":"176"}]},{"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":"85"}]},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Total number of weigh-ins 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":"baby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh_ins"},{"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","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbaby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbaby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Total number of weigh-ins 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":"baby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh_ins"},{"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","string"]},"children":[{"type":"text","value":"\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbaby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbaby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbaby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbaby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"eric"},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Total number of weigh-ins 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":"baby_centre"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weigh_ins"},{"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","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":"Total number of weigh-ins is 0\nTotal number of weigh-ins is 2\nTotal number of weigh-ins is 6"}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"LunchCard and PaymentTerminal","tmcname":"part09-04_lunchcard_and_paymentterminal"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the previous part there was an "},{"type":"element","tagName":"a","properties":{"href":"/part-8/5-more-examples-of-classes#programming-exercise-lunchcard"},"children":[{"type":"text","value":"exercise"}]},{"type":"text","value":" where you implemented the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LunchCard"}]},{"type":"text","value":". The card had separate methods for eating a regular and a special lunch, along with a method for depositing money on the card."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LunchCard"}]},{"type":"text","value":" class, as you were asked to implement it, has some problems, however. The card itself had knowledge of the prices of the different lunch options, and knew to subtract the right amount of money from the balance based on these. But imagine the prices changed, or there were new items introduced to the system, but several cards were already registered in the system. This would mean all existing cards would need to be replaced by versions with knowledge of the new prices."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A better solution would be to make the cards \"stupid\", ignorant of the prices of different products. The purpose of the card would be to simply keep track of the available balance. All more complicated features should be contained within another class: the payment terminal."}]},{"type":"element","tagName":"h2","properties":{"id":"a-simpler-lunchcard","style":"position:relative;"},"children":[{"type":"text","value":"A simpler LunchCard"},{"type":"element","tagName":"a","properties":{"href":"#a-simpler-lunchcard","ariaLabel":"a simpler lunchcard 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":"Let's first implement a simpler version of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LunchCard"}]},{"type":"text","value":" class. The card should contain functionality only for finding out the current balance, depositing money on the card, and subtracting from the balance. Please fill in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"subtract_from_balance(amount)"}]},{"type":"text","value":" method in the exercise template according to the instructions in the comments:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"LunchCard"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\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":"deposit_money"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" amount"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" amount\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":"subtract_from_balance"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" amount"},{"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":"float"}]},{"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":"pass"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# The amount should be subtracted from the balance only if"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# there is enough money on the card."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If the payment is successful, the method returns True. "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Otherwise it returns False."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may use the following code to test your 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","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 card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LunchCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"\"Balance\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n result "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"subtract_from_balance"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Payment successful:\""}]},{"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":"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":"\"Balance\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n result "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"subtract_from_balance"},{"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":"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":"\"Payment successful:\""}]},{"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":"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":"\"Balance\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"Balance 10\nPayment successful: True\nBalance 2\nPayment successful: False\nBalance 2"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-payment-terminal-and-dealing-with-cash-payments","style":"position:relative;"},"children":[{"type":"text","value":"The payment terminal and dealing with cash payments"},{"type":"element","tagName":"a","properties":{"href":"#the-payment-terminal-and-dealing-with-cash-payments","ariaLabel":"the payment terminal and dealing with cash payments 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 the student cafeteria it is possible to pay with either cash or a LunchCard. A payment terminal is used to handle both cash and card transactions. Let's start with the cash transactions."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here we have a skeleton implementation for a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PaymentTerminal"}]},{"type":"text","value":" class. Please implement the methods as described in the comments:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PaymentTerminal"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# Initially there is 1000 euros in cash available at the terminal"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"funds "},{"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":"1000"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lunches "},{"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":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"specials "},{"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":"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":"eat_lunch"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" payment"},{"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":"float"}]},{"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":"# A regular lunch costs 2.50 euros."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Increase the value of the funds at the terminal by the "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# price of the lunch, increase the number of lunches sold, "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# and return the appropriate change."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If the payment passed as an argument is not large enough to cover"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the price, the lunch is not sold, and the entire sum is returned."}]},{"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":"eat_special"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" payment"},{"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":"float"}]},{"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":"# A special lunch costs 4.30 euros."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Increase the value of the funds at the terminal by the "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# price of the lunch, increase the number of lunches sold, "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# and return the appropriate change."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If the payment passed as an argument is not large enough to cover"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the price, the lunch is not sold, and the entire sum is returned."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may use the following code to test your class:"}]},{"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":"exactum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PaymentTerminal"},{"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\nchange "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_lunch"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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 change returned was\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" change"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nchange "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_lunch"},{"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"},{"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 change returned was\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" change"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nchange "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_special"},{"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.3"}]},{"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 change returned was\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" change"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Funds available at the terminal:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"funds"},{"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":"\"Regular lunches sold:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lunches"},{"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":"\"Special lunches sold:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"specials"},{"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":"The change returned was 7.5\nThe change returned was 2.5\nThe change returned was 0.0\nFunds available at the terminal: 1009.3\nRegular lunches sold: 2\nSpecial lunches sold: 1"}]}]},{"type":"element","tagName":"h2","properties":{"id":"dealing-with-card-transactions","style":"position:relative;"},"children":[{"type":"text","value":"Dealing with card transactions"},{"type":"element","tagName":"a","properties":{"href":"#dealing-with-card-transactions","ariaLabel":"dealing with card transactions 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":"Now let's implement card transactions. We will need methods which take a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"LunchCard"}]},{"type":"text","value":" as an argument, and reduce the balance on the card by the price of the lunch. Below you will find the outlines of these functions. Please fill in the methods as described in the comments:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PaymentTerminal"}]},{"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":"# ..."}]},{"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":"eat_lunch_lunchcard"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" LunchCard"},{"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":"# A regular lunch costs 2.50 euros."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If there is enough money on the card, "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# subtract the price of the lunch from the balance"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# and return True. If not, return False."}]},{"type":"text","value":"\n\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":"eat_special_lunchcard"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" LunchCard"},{"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":"# A special lunch costs 4.30 euros."}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# If there is enough money on the card, "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# subtract the price of the lunch from the balance"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# and return True. If not, return False."}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" when paying with a LunchCard the cash funds available at the terminal do not change. However, the lunches are still sold whenever there is the required balance available, so remember to increase the number of lunches sold appropriately."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may use the following code to test your class:"}]},{"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":"exactum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PaymentTerminal"},{"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\nchange "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_lunch"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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 change returned was\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" change"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ncard "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LunchCard"},{"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":"text","value":"\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_special_lunchcard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"card"},{"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":"\"Payment successful:\""}]},{"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":"text","value":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_special_lunchcard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"card"},{"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":"\"Payment successful:\""}]},{"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":"text","value":"\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_lunch_lunchcard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"card"},{"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":"\"Payment successful:\""}]},{"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":"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Funds available at the terminal:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"funds"},{"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":"\"Regular lunches sold:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lunches"},{"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":"\"Special lunches sold:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"specials"},{"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":"The change returned was 7.5\nPayment successful: True\nPayment successful: False\nPayment successful: True\nFunds available at the terminal: 1002.5\nRegular lunches sold: 2\nSpecial lunches sold: 1"}]}]},{"type":"element","tagName":"h2","properties":{"id":"depositing-money-on-the-card","style":"position:relative;"},"children":[{"type":"text","value":"Depositing money on the card"},{"type":"element","tagName":"a","properties":{"href":"#depositing-money-on-the-card","ariaLabel":"depositing money on the card 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":"Finally, let's add a method which lets you deposit money on the card. The card owner pays this by cash, so the deposited sum is added to the funds available at the terminal. Here is an outline for the method:"}]},{"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":"deposit_money_on_card"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" LunchCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" amount"},{"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":"float"}]},{"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":"pass"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may use the following code to test your method:"}]},{"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":"exactum "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PaymentTerminal"},{"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\ncard "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" LunchCard"},{"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":"element","tagName":"span","properties":{"className":["token","string-interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"f\"Card balance 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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euros\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_special_lunchcard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"card"},{"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":"\"Payment successful:\""}]},{"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":"text","value":"\n\nexactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"deposit_money_on_card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"card"},{"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":"100"}]},{"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\"Card balance 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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euros\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nresult "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"eat_special_lunchcard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"card"},{"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":"\"Payment successful:\""}]},{"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":"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\"Card balance 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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" euros\""}]}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Funds available at the terminal:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"funds"},{"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":"\"Regular lunches sold:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lunches"},{"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":"\"Special lunches sold:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" exactum"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"specials"},{"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":"Card balance is 2 euros\nPayment successful: False\nCard balance is 102 euros\nPayment successful: True\nCard balance is 97.7 euros\nFunds available at the terminal: 1100\nRegular lunches sold: 0\nSpecial lunches sold: 1"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"an-instance-of-the-same-class-as-an-argument-to-a-method","style":"position:relative;"},"children":[{"type":"text","value":"An instance of the same class as an argument to a method"},{"type":"element","tagName":"a","properties":{"href":"#an-instance-of-the-same-class-as-an-argument-to-a-method","ariaLabel":"an instance of the same class as an argument to a method 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":"Below we have yet another version of the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" year_of_birth"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" year_of_birth"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's assume we want to write a program which compares the ages of objects of type Person. We could write a separate function for this purpose:"}]},{"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":"older_than"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"person1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" person2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Person"},{"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":"if"}]},{"type":"text","value":" person1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" person2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth"},{"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","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\nmuhammad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Muhammad ibn Musa al-Khwarizmi\""}]},{"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":"780"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npascal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Blaise Pascal\""}]},{"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":"1623"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngrace "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Grace Hopper\""}]},{"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":"1906"}]},{"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":"if"}]},{"type":"text","value":" older_than"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pascal"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" is older than "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" is not older than "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" older_than"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" pascal"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" is older than "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" is not older than "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"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":"Muhammad ibn Musa al-Khwarizmi is older than Blaise Pascal\nGrace Hopper is not older than Blaise Pascal"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"One of the principles of object oriented programming is to include any functionality which handles objects of a certain type in the class definition, as methods. So instead of a function we could write a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"method"}]},{"type":"text","value":" which allows us to compare the age of a Person object to "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"another"}]},{"type":"text","value":" Person object:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" year_of_birth"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" year_of_birth\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# NB: type hints must be enclosed in quotation marks if the parameter"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# is of the same type as the class itself!"}]},{"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":"older_than"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" another"},{"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":"\"Person\""}]},{"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":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" another"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth"},{"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","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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","boolean"]},"children":[{"type":"text","value":"False"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here the object which the method is called on is referred to as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":", while the other Person object is "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"another"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Remember, calling a method differs from calling a function. A method is attached to an object with the dot notation:"}]},{"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":"muhammad "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Muhammad ibn Musa al-Khwarizmi\""}]},{"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":"780"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npascal "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Blaise Pascal\""}]},{"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":"1623"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ngrace "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Grace Hopper\""}]},{"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":"1906"}]},{"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":"if"}]},{"type":"text","value":" muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"older_than"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pascal"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" is older than "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"muhammad"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" is not older than "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"older_than"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"pascal"},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" is older than "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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\""}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"grace"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" is not older than "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"pascal"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"p","properties":{},"children":[{"type":"text","value":"To the left of the dot is the object itself, which is referred to as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" within the method definition. In parentheses is the argument to the method, which is the object referred to as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"another"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The printout from the program is exactly the same as with the function implementation above."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A rather cosmetic point to finish off: the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"if...else"}]},{"type":"text","value":" structure in the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"older_than"}]},{"type":"text","value":" is by and large unneccessary. The value of the Boolean expression in the condition is already the exact same truth value which is returned. The method can thus be simplified:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" year_of_birth"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" year_of_birth\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# NB: type hints must be enclosed in quotation marks if the parameter "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# is of the same type as the class itself!"}]},{"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":"older_than"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" another"},{"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":"\"Person\""}]},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" another"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As stated in the comments in the examples above, if the parameter in a method definition is of the same type as the class itself, the type hint must be enclosed in quotation marks. Leaving the quotation marks out causes an error, which you will see if you try the following:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Person"}]},{"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":"# ..."}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# this would cause an error, as Person must be enclosed in quotation marks"}]},{"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":"older_than"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" another"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Person"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<"}]},{"type":"text","value":" another"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"year_of_birth"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]}]}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Comparing properties","tmcname":"part09-05_comparing_properties"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The database of a real estate agency keeps records of available properties with objects defined by the following class:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"RealProperty"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" rooms"},{"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":"text","value":" square_metres"},{"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":"text","value":" price_per_sqm"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"rooms "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" rooms\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"square_metres "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" square_metres\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"price_per_sqm "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" price_per_sqm"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your task is to implement methods which allow for comparison between available properties."}]},{"type":"element","tagName":"h2","properties":{"id":"is-it-bigger","style":"position:relative;"},"children":[{"type":"text","value":"Is it bigger?"},{"type":"element","tagName":"a","properties":{"href":"#is-it-bigger","ariaLabel":"is it bigger 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":"Please write a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"bigger(self, compared_to)"}]},{"type":"text","value":" which returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" if the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"RealProperty"}]},{"type":"text","value":" object itself is bigger than the one it is compared to."}]},{"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":"central_studio "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"16"}]},{"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":"5500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndowntown_two_bedroom "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"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":"4200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuburbs_three_bedroom "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"78"}]},{"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":"2500"}]},{"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":"central_studio"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bigger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"downtown_two_bedroom"},{"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":"suburbs_three_bedroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"bigger"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"downtown_two_bedroom"},{"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":"False\nTrue"}]}]},{"type":"element","tagName":"h2","properties":{"id":"price-difference","style":"position:relative;"},"children":[{"type":"text","value":"Price difference"},{"type":"element","tagName":"a","properties":{"href":"#price-difference","ariaLabel":"price difference 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":"Please write a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"price_difference(self, compared_to)"}]},{"type":"text","value":" which returns the difference in price between the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"RealProperty"}]},{"type":"text","value":" object itself and the one it is compared to. The price difference is the absolute value of the difference between the total prices of the two properties. The total price of a property is its price per square metre multiplied by the amount of square metres in the property."}]},{"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":"central_studio "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"16"}]},{"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":"5500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndowntown_two_bedroom "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"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":"4200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuburbs_three_bedroom "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"78"}]},{"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":"2500"}]},{"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":"central_studio"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"price_difference"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"downtown_two_bedroom"},{"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":"suburbs_three_bedroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"price_difference"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"downtown_two_bedroom"},{"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":"71600\n35400"}]}]},{"type":"element","tagName":"h2","properties":{"id":"is-it-more-expensive","style":"position:relative;"},"children":[{"type":"text","value":"Is it more expensive?"},{"type":"element","tagName":"a","properties":{"href":"#is-it-more-expensive","ariaLabel":"is it more expensive 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":"Please write a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"more_expensive(self, compared_to)"}]},{"type":"text","value":" which returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" if the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"RealProperty"}]},{"type":"text","value":" object itself is more expensive that the one it is compared to."}]},{"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":"central_studio "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"16"}]},{"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":"5500"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndowntown_two_bedroom "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"38"}]},{"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":"4200"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuburbs_three_bedroom "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" RealProperty"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"78"}]},{"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":"2500"}]},{"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":"central_studio"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"more_expensive"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"downtown_two_bedroom"},{"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":"suburbs_three_bedroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"more_expensive"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"downtown_two_bedroom"},{"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":"False\nTrue"}]}]}]}]},"html":"

    After this section

      \n
    • You will be able to use different data structures for handling objects
    • \n
    • You will know how objects can be passed as arguments
    • \n

    Every value in Python is an object. Any object you create based on a class you've defined yourself works exactly the same as any \"regular\" Python object. For example, objects can be stored in a list:

    from datetime import date\n\nclass CompletedCourse:\n\n    def __init__(self, course_name: str, credits: int, completion_date: date):\n        self.name = course_name\n        self.credits = credits\n        self.completion_date = completion_date\n\n\nif __name__ == \"__main__\":\n    # Here we create some completed courses and add these to a list \n    completed = []\n\n    maths1 = CompletedCourse(\"Mathematics 1\", 5, date(2020, 3, 11))\n    prog1 = CompletedCourse(\"Programming 1\", 6, date(2019, 12, 17))\n\n    completed.append(maths1)\n    completed.append(prog1)\n\n    # Let's add a couple more straight to the list\n    completed.append(CompletedCourse(\"Physics 2\", 4, date(2019, 11, 10)))\n    completed.append(CompletedCourse(\"Programming 2\", 5, date(2020, 5, 19)))\n\n    # Go through all the completed courses, print out their names \n    # and sum up the credits received\n    credits = 0\n    for course in completed:\n        print(course.name)\n        credits += course.credits\n\n    print(\"Total credits received:\", credits)

    Mathematics 1\nProgramming 1\nPhysics 2\nProgramming 2\nTotal credits received: 20

    The exercise template contains a class named Car which represents the features of a car through two attributes: make (str) and top_speed (int).

    Please write a function named fastest_car(cars: list) which takes a list of Car objects as its argument.

    The function should return the make of the fastest car. You may assume there will always be a single car with the highest top speed. Do not change the list given as an argument, or make any changes to the Car class definition.

    You may use the following code to test your function:

    if __name__ == \"__main__\":\n    car1 = Car(\"Saab\", 195)\n    car2 = Car(\"Lada\", 110)\n    car3 = Car(\"Ferrari\", 280)\n    car4 = Car(\"Trabant\", 85)\n\n    cars = [car1, car2, car3, car4]\n    print(fastest_car(cars))

    Ferrari

    The exercise template contains a class named ExamSubmission which, as the name implies, models an examinee's submission in an exam. The class has two attributes defined: examinee (str) and points (int).

    Please write a function named passed(submissions: list, lowest_passing: int) which takes a list of exam submissions and an integer number representing the lowest passing grade as its arguments.

    The function should create and return a new list, which contains only the passed submissions from the original list. Please do not change the list given as an argument, or make any changes to the ExamSubmission class definition.

    You may use the following code to test your function:

    if __name__ == \"__main__\":\n    s1 = ExamSubmission(\"Peter\", 12)\n    s2 = ExamSubmission(\"Pippa\", 19)\n    s3 = ExamSubmission(\"Paul\", 15)\n    s4 = ExamSubmission(\"Phoebe\", 9)\n    s5 = ExamSubmission(\"Persephone\", 17)\n\n    passes = passed([s1, s2, s3, s4, s5], 15)\n    for passing in passes:\n        print(passing)

    ExamSubmission (examinee: Pippa, points: 19)\nExamSubmission (examinee: Paul, points: 15)\nExamSubmission (examinee: Persephone, points: 17)

    You may remember that lists do not contain any objects themselves. They contain references to objects. The exact same object can appear multiple times in a single list, and it can be referred to multiple times within the list or outside it. Let's have a look at an example:

    class Product:\n    def __init__(self, name: int, unit: str):\n        self.name = name\n        self.unit = unit\n\n\nif __name__ == \"__main__\":\n    shopping_list = []\n    milk = Product(\"Milk\", \"litre\")\n\n    shopping_list.append(milk)\n    shopping_list.append(milk)\n    shopping_list.append(Product(\"Cucumber\", \"piece\"))
    \n \n \n \n \n \n \"9\n \n \n

    If there is more than one reference to the same object, it makes no difference which one of the references is used:

    class Dog:\n    def __init__(self, name):\n        self.name = name\n\n    def __str__(self):\n        return self.name\n\ndogs = []\nfluffy = Dog(\"Fluffy\")\ndogs.append(fluffy)\ndogs.append(fluffy)\ndogs.append(Dog(\"Fluffy\"))\n\nprint(\"Dogs initially:\")\nfor dog in dogs:\n    print(dog)\n\nprint(\"The dog at index 0 is renamed:\")\ndogs[0].name = \"Pooch\"\nfor dog in dogs:\n    print(dog)\n\nprint(\"The dog at index 2 is renamed:\")\ndogs[2].name = \"Fifi\"\nfor dog in dogs:\n    print(dog)

    Dogs initially:\nFluffy\nFluffy\nFluffy\nThe dog at index 0 is renamed:\nPooch\nPooch\nFluffy\nThe dog at index 2 is renamed:\nPooch\nPooch\nFifi

    The references at indexes 0 and 1 in the list refer to the same object. Either one of the references can be used to access the object. The reference at index 2 refers to a different object, albeit with seemingly the same contents. Changing the contents of this latter object does not affect the other one.

    The operator is is used for checking if the two references refer to the exact same object, while the operator == will tell you if the contents of the objects are the same. The following example hopefully makes the difference clear:

    list1 = [1, 2, 3]\nlist2 = [1, 2, 3]\nlist3 = list1\n\nprint(list1 is list2)\nprint(list1 is list3)\nprint(list2 is list3)\n\nprint()\n\nprint(list1 == list2)\nprint(list1 == list3)\nprint(list2 == list3)

    False\nTrue\nFalse

    True\nTrue\nTrue

    Any Python object can also be stored in a dictionary or any other data structure. This also applies to objects created based on a class you've defined yourself.

    class Student:\n    def __init__(self, name: str, cr: int):\n        self.name = name\n        self.cr = cr\n\nif __name__ == \"__main__\":\n    # The key in this dictionary is the student number, \n    # and the value is an object of type Student\n    students = {}\n    students[\"12345\"] = Student(\"Saul Student\", 10)\n    students[\"54321\"] = Student(\"Sally Student\", 67)

    The visualisation tool can help in making sense of the above example:

    \n \n \n \n \n \n \"9\n \n \n

    Self or no self?

    Thus far we've only touched upon the surface of using the self parameter name. Let's have a closer look at when it should or should not be used.

    Below we have a simple class which lets us create a vocabulary object containing some words:

    class Vocabulary:\n    def __init__(self):\n        self.words = []\n\n    def add_word(self, word: str):\n        if not word in self.words:\n            self.words.append(word)\n\n    def print_vocabulary(self):\n        for word in sorted(self.words):\n            print(word)\n\nvocab = Vocabulary()\nvocab.add_word(\"python\")\nvocab.add_word(\"object\")\nvocab.add_word(\"object-oriented programming\")\nvocab.add_word(\"object\")\nvocab.add_word(\"nerd\")\n\nvocab.print_vocabulary()

    nerd\nobject\nobject-oriented programming\npython

    The list of words is stored in an attribute named self.words. In this case the self parameter name is mandatory in both the constructor method of the class and in any other method accessing that variable. If self is left out, the different methods will not access the same list of words.

    Let's add a new method to our class definition. The method longest_word(self) returns (one of) the longest words in the vocabulary.

    The following is one way of completing this task, but we will soon see it is not a very good way:

    class Vocabulary:\n    def __init__(self):\n        self.words = []\n\n    # ...\n\n    def longest_word(self):\n        # define two helper variables\n        self.longest = \"\"\n        self.length_of_longest = 0\n\n        for word in self.words:\n            if len(word) > self.length_of_longest:\n                self.length_of_longest = len(word)\n                self.longest = word\n\n        return self.longest

    This method uses two helper variables which are declared with the self parameter name. Remember, the names of variables do not matter in the functional sense, so these variables could also be named more confusingly as, for example, helper and helper2. The code begins to look a bit cryptic:

    class Vocabulary:\n    def __init__(self):\n        self.words = []\n\n    # ...\n\n    def longest_word(self):\n        # define two helper variables\n        self.helper = \"\"\n        self.helper2 = 0\n\n        for word in self.words:\n            if len(word) > self.helper2:\n                self.helper2 = len(word)\n                self.helper = word\n\n        return self.helper

    When a variable is declared with the self parameter name, it becomes an attribute of the object. This means that the variable will exist for as long as the object exists. Specifically, the variable will continue existing also after the method declaring it has finished its execution. In the example above this is quite unnecessary, as the helper variables are meant to be used only within the method longest_word(self). So, declaring helper variables with the self parameter name is not a very good idea here.

    Besides causing variables to exist beyond their \"expiration date\", using self to create new attributes where they aren't necessary can cause difficult bugs in your code. Especially generically named attributes such as self.helper, which are then used in various different methods, can cause unexpected behaviour which is hard to trace.

    For example, if a helper variable is declared as an attribute and assigned an initial value in the constructor, but the variable is then used in an unrelated context in another method, the results are often unpredictable:

    class Vocabulary:\n    def __init__(self):\n        self.words = []\n        # define helper variables\n        self.helper = \"\"\n        self.helper2 = \"\"\n        self.helper3 = \"\"\n        self.helper4 = \"\"\n\n    # ...\n\n    def longest_word(self):\n        for word in self.words:\n            # above the helper variables were all assigned string values\n            # the following will not work because the type of helper2 is wrong\n            if len(word) > self.helper2:\n                self.helper2 = len(word)\n                self.helper = word\n\n        return self.helper

    You might think this would be solved by just declaring attributes where they are used, outside the constructor, but this results in a situation where the attributes accessible through an object are dependent on which methods have been executed. In the previous part we saw that the advantage of declaring attributes in the constructor is that all instances of the class will then have the exact same attributes. If this is not the case, using different instances of the class can easily lead to errors.

    In conclusion, if you need helper variables for use within a single method, the correct way to do it is without self. To make your code easier to understand, also use informative variable names:

    class Vocabulary:\n    def __init__(self):\n        self.words = []\n\n    # ...\n\n    def longest_word(self):\n        # the correct way of declaring helper variables \n        # for use within a single method\n        longest = \"\"\n        length_of_longest = 0\n\n        for word in self.words:\n            if len(word) > length_of_longest:\n                length_of_longest = len(word)\n                longest = word\n\n        return longest

    In the above implementation the helper variables are only accessible while the method is being executed. The values stored within cannot cause complications in other parts of the program.

    Objects as arguments to functions

    The objects created based on our own classes are usually mutable. You may remember that, for instance, Python lists are mutable: when passed as arguments to functions, their contents can change as a result of the execution.

    Let's have a look at a simple example where a function receives a reference to an object of type Student as its argument. The function then changes the name of the student. Both the function and the main function calling it access the same object, so the change is apparent in the main function as well.

    class Student:\n    def __init__(self, name: str, student_number: str):\n        self.name = name\n        self.student_number = student_number\n\n    def __str__(self):\n        return f\"{self.name} ({self.student_number})\"\n\n# the type hint here uses the name of the class defined above\ndef change_name(student: Student):\n    student.name = \"Saul Student\"\n\n# create a Student object\nsteve = Student(\"Steve Student\", \"12345\")\n\nprint(steve)\nchange_name(steve)\nprint(steve)

    Steve Student (12345)\nSaul Student (12345)

    It is also possible to create objects within functions. If a function returns a reference to the newly created object, it is also accessible within the main function:

    from random import randint, choice\n\nclass Student:\n    def __init__(self, name: str, student_number: str):\n        self.name = name\n        self.student_number = student_number\n\n    def __str__(self):\n        return f\"{self.name} ({self.student_number})\"\n\n# This function creates and returns a new Student object.\n# It randomly selects values for the name and the student number.\ndef new_student():\n    first_names = [\"Mark\",\"Mindy\",\"Mary\",\"Mike\"]\n    last_names = [\"Javanese\", \"Rusty\", \"Scriptor\", \"Pythons\"]\n\n    # randomly determine the name\n    name = choice(first_names) + \" \" + choice(last_names)\n\n    # randomly determine the student number\n    student_number = str(randint(10000,99999))\n\n    # Create and return a Student object\n    return Student(name, student_number)\n\nif __name__ == \"__main__\":\n    # Call the function five times and store the results in a list\n    students = []\n    for i in range(5):\n        students.append(new_student())\n\n    # Print out the results\n    for student in students :\n        print(student)

    Executing the above could result in the following printout (NB: as randomness is involved, if you try the code yourself, the results will likely be different).

    Mary Rusty (78218)\nMindy Rusty (80068)\nMike Pythons (70396)\nMark Javanese (83307)\nMary Pythons (45149)

    Objects as arguments to methods

    Similarly, objects can act as arguments to methods. Let's have a look at an example from an amusement park:

    class Person:\n    def __init__(self, name: str, height: int):\n        self.name = name\n        self.height = height\n\nclass Attraction:\n    def __init__(self, name: str, min_height: int):\n        self.visitors = 0\n        self.name = name\n        self.min_height = min_height\n\n    def admit_visitor(self, person: Person):\n        if person.height >= self.min_height:\n            self.visitors += 1\n            print(f\"{person.name} got on board\")\n        else:\n            print(f\"{person.name} was too short :(\")\n\n    def __str__(self):\n        return f\"{self.name} ({self.visitors} visitors)\"

    The Attraction contains a method admit_visitor, which takes an object of type Person as an argument. If the visitor is tall enough, they are admitted on board and the number of visitors is increased. The classes can be tested as follows:

    rollercoaster = Attraction(\"Rollercoaster\", 120)\njared = Person(\"Jared\", 172)\nalice = Person(\"Alice\", 105)\n\nrollercoaster.admit_visitor(jared)\nrollercoaster.admit_visitor(alice)\n\nprint(rollercoaster)

    Jared got on board\nAlice was too short :(\nRollercoaster (1 visitors)

    The exercise template contains a class named Person and a skeleton implementation for the class BabyCentre. A BabyCentre object performs various actions on a Person object. It may, for example, weigh or feed the person. In this exercise you will implement the rest of the BabyCentre class. Please do not change the class definition of Person in any way.

    Weighing persons

    The BabyCentre class definition contains an outline for the function weigh:

    class BabyCentre:\n    def weigh(self, person: Person):\n        # return the weight of the person passed as an argument\n        return -1

    The method takes a Person object as its argument. It should return the weight of the person. You can access the weight of a person through the appropriate attribute defined in the Person class. Please fill in the rest of the implementation for the method weigh.

    Below is an example of a main function where a BabyCentre weighs two separate Person objects:

    baby_centre = BabyCentre()\n\neric = Person(\"Eric\", 1, 110, 7)\npeter = Person(\"Peter\", 33, 176, 85)\n\nprint(f\"{eric.name} weighs {baby_centre.weigh(eric)} kg\")\nprint(f\"{peter.name} weighs {baby_centre.weigh(peter)} kg\")

    Eric weighs 7 kg\nPeter weighs 85 kg

    Feeding

    It is possible to change the state of an object passed as an argument. Please implement the method feed(person: Person) which increases by one the weight of the person passed as an argument.

    In the following example two persons are weighed, and then one of them is fed three times. Then the persons are weighed again:

    baby_centre = BabyCentre()\n\neric = Person(\"Eric\", 1, 110, 7)\npeter = Person(\"Peter\", 33, 176, 85)\n\nprint(f\"{eric.name} weighs {baby_centre.weigh(eric)} kg\")\nprint(f\"{peter.name} weighs {baby_centre.weigh(peter)} kg\")\nprint() \n\nbaby_centre.feed(eric)\nbaby_centre.feed(eric)\nbaby_centre.feed(eric)\n\nprint(f\"{eric.name} weighs {baby_centre.weigh(eric)} kg\")\nprint(f\"{peter.name} weighs {baby_centre.weigh(peter)} kg\")

    The printout should reveal that Eric's weight has increased by three:

    Eric weighs 7 kg\nPeter weighs 85 kg

    Eric weighs 10 kg\nPeter weighs 85 kg

    Counting weigh-ins

    Please implement the method weigh_ins() which returns the total number of weigh-ins a BabyCentre object has performed. NB: you will need a new attribute for keeping track of the number of weigh-ins. You can use the following code to test your method:

    baby_centre = BabyCentre()\n\neric = Person(\"Eric\", 1, 110, 7)\npeter = Person(\"Peter\", 33, 176, 85)\n\nprint(f\"Total number of weigh-ins is {baby_centre.weigh_ins()}\")\n\nbaby_centre.weigh(eric)\nbaby_centre.weigh(eric)\n\nprint(f\"Total number of weigh-ins is {baby_centre.weigh_ins()}\")\n\nbaby_centre.weigh(eric)\nbaby_centre.weigh(eric)\nbaby_centre.weigh(eric)\nbaby_centre.weigh(eric)\n\nprint(f\"Total number of weigh-ins is {baby_centre.weigh_ins()}\")

    Total number of weigh-ins is 0\nTotal number of weigh-ins is 2\nTotal number of weigh-ins is 6

    In the previous part there was an exercise where you implemented the class LunchCard. The card had separate methods for eating a regular and a special lunch, along with a method for depositing money on the card.

    The LunchCard class, as you were asked to implement it, has some problems, however. The card itself had knowledge of the prices of the different lunch options, and knew to subtract the right amount of money from the balance based on these. But imagine the prices changed, or there were new items introduced to the system, but several cards were already registered in the system. This would mean all existing cards would need to be replaced by versions with knowledge of the new prices.

    A better solution would be to make the cards \"stupid\", ignorant of the prices of different products. The purpose of the card would be to simply keep track of the available balance. All more complicated features should be contained within another class: the payment terminal.

    A simpler LunchCard

    Let's first implement a simpler version of the LunchCard class. The card should contain functionality only for finding out the current balance, depositing money on the card, and subtracting from the balance. Please fill in the subtract_from_balance(amount) method in the exercise template according to the instructions in the comments:

    class LunchCard:\n    def __init__(self, balance: float):\n        self.balance = balance\n\n    def deposit_money(self, amount: float):\n        self.balance += amount\n\n    def subtract_from_balance(self, amount: float):\n        pass\n        # The amount should be subtracted from the balance only if\n        # there is enough money on the card.\n        # If the payment is successful, the method returns True. \n        # Otherwise it returns False.

    You may use the following code to test your function:

    if __name__ == \"__main__\":\n    card = LunchCard(10)\n    print(\"Balance\", card.balance)\n    result = card.subtract_from_balance(8)\n    print(\"Payment successful:\", result)\n    print(\"Balance\", card.balance)\n    result = card.subtract_from_balance(4)\n    print(\"Payment successful:\", result)\n    print(\"Balance\", card.balance)

    Balance 10\nPayment successful: True\nBalance 2\nPayment successful: False\nBalance 2

    The payment terminal and dealing with cash payments

    In the student cafeteria it is possible to pay with either cash or a LunchCard. A payment terminal is used to handle both cash and card transactions. Let's start with the cash transactions.

    Here we have a skeleton implementation for a PaymentTerminal class. Please implement the methods as described in the comments:

    class PaymentTerminal:\n    def __init__(self):\n        # Initially there is 1000 euros in cash available at the terminal\n        self.funds = 1000\n        self.lunches = 0\n        self.specials = 0\n\n    def eat_lunch(self, payment: float):\n        # A regular lunch costs 2.50 euros.\n        # Increase the value of the funds at the terminal by the \n        # price of the lunch, increase the number of lunches sold, \n        # and return the appropriate change.\n        # If the payment passed as an argument is not large enough to cover\n        # the price, the lunch is not sold, and the entire sum is returned.\n\n    def eat_special(self, payment: float):\n        # A special lunch costs 4.30 euros.\n        # Increase the value of the funds at the terminal by the \n        # price of the lunch, increase the number of lunches sold, \n        # and return the appropriate change.\n        # If the payment passed as an argument is not large enough to cover\n        # the price, the lunch is not sold, and the entire sum is returned.

    You may use the following code to test your class:

    exactum = PaymentTerminal()\n\nchange = exactum.eat_lunch(10)\nprint(\"The change returned was\", change)\n\nchange = exactum.eat_lunch(5)\nprint(\"The change returned was\", change)\n\nchange = exactum.eat_special(4.3)\nprint(\"The change returned was\", change)\n\nprint(\"Funds available at the terminal:\", exactum.funds)\nprint(\"Regular lunches sold:\", exactum.lunches)\nprint(\"Special lunches sold:\", exactum.specials)

    The change returned was 7.5\nThe change returned was 2.5\nThe change returned was 0.0\nFunds available at the terminal: 1009.3\nRegular lunches sold: 2\nSpecial lunches sold: 1

    Dealing with card transactions

    Now let's implement card transactions. We will need methods which take a LunchCard as an argument, and reduce the balance on the card by the price of the lunch. Below you will find the outlines of these functions. Please fill in the methods as described in the comments:

    class PaymentTerminal:\n    # ...\n\n    def eat_lunch_lunchcard(self, card: LunchCard):\n        # A regular lunch costs 2.50 euros.\n        # If there is enough money on the card, \n        # subtract the price of the lunch from the balance\n        # and return True. If not, return False.\n\n\n    def eat_special_lunchcard(self, card: LunchCard):\n        # A special lunch costs 4.30 euros.\n        # If there is enough money on the card, \n        # subtract the price of the lunch from the balance\n        # and return True. If not, return False.

    NB: when paying with a LunchCard the cash funds available at the terminal do not change. However, the lunches are still sold whenever there is the required balance available, so remember to increase the number of lunches sold appropriately.

    You may use the following code to test your class:

    exactum = PaymentTerminal()\n\nchange = exactum.eat_lunch(10)\nprint(\"The change returned was\", change)\n\ncard = LunchCard(7)\n\nresult = exactum.eat_special_lunchcard(card)\nprint(\"Payment successful:\", result)\nresult = exactum.eat_special_lunchcard(card)\nprint(\"Payment successful:\", result)\nresult = exactum.eat_lunch_lunchcard(card)\nprint(\"Payment successful:\", result)\n\nprint(\"Funds available at the terminal:\", exactum.funds)\nprint(\"Regular lunches sold:\", exactum.lunches)\nprint(\"Special lunches sold:\", exactum.specials)

    The change returned was 7.5\nPayment successful: True\nPayment successful: False\nPayment successful: True\nFunds available at the terminal: 1002.5\nRegular lunches sold: 2\nSpecial lunches sold: 1

    Depositing money on the card

    Finally, let's add a method which lets you deposit money on the card. The card owner pays this by cash, so the deposited sum is added to the funds available at the terminal. Here is an outline for the method:

    def deposit_money_on_card(self, card: LunchCard, amount: float):\n    pass

    You may use the following code to test your method:

    exactum = PaymentTerminal()\n\ncard = LunchCard(2)\nprint(f\"Card balance is {card.balance} euros\")\n\nresult = exactum.eat_special_lunchcard(card)\nprint(\"Payment successful:\", result)\n\nexactum.deposit_money_on_card(card, 100)\nprint(f\"Card balance is {card.balance} euros\")\n\nresult = exactum.eat_special_lunchcard(card)\nprint(\"Payment successful:\", result)\nprint(f\"Card balance is {card.balance} euros\")\n\nprint(\"Funds available at the terminal:\", exactum.funds)\nprint(\"Regular lunches sold:\", exactum.lunches)\nprint(\"Special lunches sold:\", exactum.specials)

    Card balance is 2 euros\nPayment successful: False\nCard balance is 102 euros\nPayment successful: True\nCard balance is 97.7 euros\nFunds available at the terminal: 1100\nRegular lunches sold: 0\nSpecial lunches sold: 1

    An instance of the same class as an argument to a method

    Below we have yet another version of the class Person:

    class Person:\n    def __init__(self, name: str, year_of_birth: int):\n        self.name = name\n        self.year_of_birth = year_of_birth

    Let's assume we want to write a program which compares the ages of objects of type Person. We could write a separate function for this purpose:

    def older_than(person1: Person, person2: Person):\n    if person1.year_of_birth < person2.year_of_birth:\n        return True\n    else:\n        return False\n\nmuhammad = Person(\"Muhammad ibn Musa al-Khwarizmi\", 780)\npascal = Person(\"Blaise Pascal\", 1623)\ngrace = Person(\"Grace Hopper\", 1906)\n\nif older_than(muhammad, pascal):\n    print(f\"{muhammad} is older than {pascal}\")\nelse:\n    print(f\"{muhammad} is not older than {pascal}\")\n\nif older_than(grace, pascal):\n    print(f\"{grace} is older than {pascal}\")\nelse:\n    print(f\"{grace} is not older than {pascal}\")

    Muhammad ibn Musa al-Khwarizmi is older than Blaise Pascal\nGrace Hopper is not older than Blaise Pascal

    One of the principles of object oriented programming is to include any functionality which handles objects of a certain type in the class definition, as methods. So instead of a function we could write a method which allows us to compare the age of a Person object to another Person object:

    class Person:\n    def __init__(self, name: str, year_of_birth: int):\n        self.name = name\n        self.year_of_birth = year_of_birth\n\n    # NB: type hints must be enclosed in quotation marks if the parameter\n    # is of the same type as the class itself!\n    def older_than(self, another: \"Person\"):\n        if self.year_of_birth < another.year_of_birth:\n            return True\n        else:\n            return False

    Here the object which the method is called on is referred to as self, while the other Person object is another.

    Remember, calling a method differs from calling a function. A method is attached to an object with the dot notation:

    muhammad = Person(\"Muhammad ibn Musa al-Khwarizmi\", 780)\npascal = Person(\"Blaise Pascal\", 1623)\ngrace = Person(\"Grace Hopper\", 1906)\n\nif muhammad.older_than(pascal):\n    print(f\"{muhammad.name} is older than {pascal.name}\")\nelse:\n    print(f\"{muhammad.name} is not older than {pascal.name}\")\n\nif grace.older_than(pascal):\n    print(f\"{grace.name} is older than {pascal.name}\")\nelse:\n    print(f\"{grace.name} is not older than {pascal.name}\")

    To the left of the dot is the object itself, which is referred to as self within the method definition. In parentheses is the argument to the method, which is the object referred to as another.

    The printout from the program is exactly the same as with the function implementation above.

    A rather cosmetic point to finish off: the if...else structure in the method older_than is by and large unneccessary. The value of the Boolean expression in the condition is already the exact same truth value which is returned. The method can thus be simplified:

    class Person:\n    def __init__(self, name: str, year_of_birth: int):\n        self.name = name\n        self.year_of_birth = year_of_birth\n\n    # NB: type hints must be enclosed in quotation marks if the parameter \n    # is of the same type as the class itself!\n    def older_than(self, another: \"Person\"):\n        return self.year_of_birth < another.year_of_birth:

    As stated in the comments in the examples above, if the parameter in a method definition is of the same type as the class itself, the type hint must be enclosed in quotation marks. Leaving the quotation marks out causes an error, which you will see if you try the following:

    class Person:\n    # ...\n\n    # this would cause an error, as Person must be enclosed in quotation marks\n    def older_than(self, another: Person):\n        return self.year_of_birth < another.year_of_birth:

    The database of a real estate agency keeps records of available properties with objects defined by the following class:

    class RealProperty:\n    def __init__(self, rooms: int, square_metres: int, price_per_sqm: int):\n        self.rooms = rooms\n        self.square_metres = square_metres\n        self.price_per_sqm = price_per_sqm

    Your task is to implement methods which allow for comparison between available properties.

    Is it bigger?

    Please write a method named bigger(self, compared_to) which returns True if the RealProperty object itself is bigger than the one it is compared to.

    An example of how the function should work:

    central_studio = RealProperty(1, 16, 5500)\ndowntown_two_bedroom = RealProperty(2, 38, 4200)\nsuburbs_three_bedroom = RealProperty(3, 78, 2500)\n\nprint(central_studio.bigger(downtown_two_bedroom))\nprint(suburbs_three_bedroom.bigger(downtown_two_bedroom))

    False\nTrue

    Price difference

    Please write a method named price_difference(self, compared_to) which returns the difference in price between the RealProperty object itself and the one it is compared to. The price difference is the absolute value of the difference between the total prices of the two properties. The total price of a property is its price per square metre multiplied by the amount of square metres in the property.

    An example of how the function should work:

    central_studio = RealProperty(1, 16, 5500)\ndowntown_two_bedroom = RealProperty(2, 38, 4200)\nsuburbs_three_bedroom = RealProperty(3, 78, 2500)\n\nprint(central_studio.price_difference(downtown_two_bedroom))\nprint(suburbs_three_bedroom.price_difference(downtown_two_bedroom))

    71600\n35400

    Is it more expensive?

    Please write a method named more_expensive(self, compared_to) which returns True if the RealProperty object itself is more expensive that the one it is compared to.

    An example of how the function should work:

    central_studio = RealProperty(1, 16, 5500)\ndowntown_two_bedroom = RealProperty(2, 38, 4200)\nsuburbs_three_bedroom = RealProperty(3, 78, 2500)\n\nprint(central_studio.more_expensive(downtown_two_bedroom))\nprint(suburbs_three_bedroom.more_expensive(downtown_two_bedroom))

    False\nTrue

    ","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-9/1-objects-and-references.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-9/2-objects-as-attributes/page-data.json b/page-data/part-9/2-objects-as-attributes/page-data.json index 67eb196d5..6d5042b42 100644 --- a/page-data/part-9/2-objects-as-attributes/page-data.json +++ b/page-data/part-9/2-objects-as-attributes/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-9/2-objects-as-attributes", - "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 know how to use objects as attributes in other objects"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be familiar with the keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We have already seen examples of classes which have lists as attributes. As there is thus nothing stopping us from including mutable objects as attributes in our classes, we might as well use instances of our own classes as attributes in other classes we've defined ourselves. In the following examples we will define the classes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Course"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CompletedCourse"}]},{"type":"text","value":". A completed course makes use of the first two classes. The class definitions are very short and simple in order to better concentrate on the technique of using instances of our own classes as attributes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We will assume each class is defined in a separate file."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First we define the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Course"}]},{"type":"text","value":" in a file named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"course.py"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Course"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" code"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" credits"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"code "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" code\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" credits"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Next, the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" in a file named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"student.py"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" student_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" credits"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"student_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" student_number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" credits"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Finally, the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CompletedCourse"}]},{"type":"text","value":" is defined in a file named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"completedcourse.py"}]},{"type":"text","value":". As it uses the other two classes, they have to be imported before they can be used:"}]},{"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":"from"}]},{"type":"text","value":" course "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Course\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" student "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Student\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"CompletedCourse"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" grade"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"student "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" student\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"course "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" course\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"grade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" grade"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here is an example of a main function which adds some completed courses to 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":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" completedcourse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" CompletedCourse\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" course "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Course\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" student "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Student\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Create a list of students"}]},{"type":"text","value":"\nstudents "},{"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":"\nstudents"},{"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":"Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ollie\""}]},{"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":"\"1234\""}]},{"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":"10"}]},{"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":"\nstudents"},{"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":"Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"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":"\"3210\""}]},{"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":"23"}]},{"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":"\nstudents"},{"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":"Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lena\""}]},{"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":"\"9999\""}]},{"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":"43"}]},{"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":"\nstudents"},{"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":"Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tina\""}]},{"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":"\"3333\""}]},{"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":"8"}]},{"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","comment"]},"children":[{"type":"text","value":"# Create a course named Introduction to Programming"}]},{"type":"text","value":"\nitp "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduction to Programming\""}]},{"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":"\"itp1\""}]},{"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":"5"}]},{"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":"# Add completed courses for each student, with grade 3 for all"}]},{"type":"text","value":"\ncompleted "},{"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","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" student "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n completed"},{"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":"CompletedCourse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" itp"},{"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":"3"}]},{"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","comment"]},"children":[{"type":"text","value":"# Print out the name of the student for each completed course"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" course "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" completed"},{"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":"course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"Ollie\nPeter\nLena\nTina"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What exactly is happening with all the dots on the line "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print(course.student.name)"}]},{"type":"text","value":"?"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"course"}]},{"type":"text","value":" is an instance of the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CompletedCourse"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"student"}]},{"type":"text","value":" refers to an attribute of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CompletedCourse"}]},{"type":"text","value":" object, which is an object of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the attribute "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" object contains the name of the student"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"h2","properties":{"id":"when-is-an-import-necessary","style":"position:relative;"},"children":[{"type":"text","value":"When is an import necessary?"},{"type":"element","tagName":"a","properties":{"href":"#when-is-an-import-necessary","ariaLabel":"when is an import necessary 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 the examples above an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement appeared quite a few times:"}]},{"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":"from"}]},{"type":"text","value":" completedcourse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" CompletedCourse\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" course "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Course\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" student "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Student\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# rest of the main function"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement is only necessary when using code which is defined somewhere outside the current file (or Python interpreter session). This includes situations where we want to use something defined in the Python standard library. For example, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" module contains some mathematical operations:"}]},{"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":"import"}]},{"type":"text","value":" math\n\nx "},{"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":"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\"the square root of "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" 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":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the example above we assumed the three classes were each defined in a separate file, and the main function was run from yet another file. This is why the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statements were necessary."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If all program code is written in the same file, as most of the exercises on this course advise you to do, "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"you will not need"}]},{"type":"text","value":" "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statements to use the classes you have defined."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you find yourself writing something along the lines of"}]},{"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":"from"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Person\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# more code goes here"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"it is likely you have gotten something wrong. If you need a refresher, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement was first introduced in "},{"type":"element","tagName":"a","properties":{"href":"/part-7/1-modules"},"children":[{"type":"text","value":"part 7"}]},{"type":"text","value":" of this course material."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Pets","tmcname":"part09-06_pets"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the outlines of two classes: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Pet"}]},{"type":"text","value":". Each person has one pet. Please change the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method in the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" so that it also prints out information about the person's pet as shown in the example below."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The string returned by the method "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"must follow the format specified below exactly"}]},{"type":"text","value":"."}]},{"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":"hulda "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Pet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hulda\""}]},{"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":"\"mixed-breed dog\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlevi "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Levi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" hulda"},{"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":"levi"},{"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":"Levi, whose pal is Hulda, a mixed-breed dog"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" all class definitions are in the same text file. You should not need to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" anything."}]}]},{"type":"element","tagName":"h2","properties":{"id":"a-list-of-objects-as-an-attribute-of-an-object","style":"position:relative;"},"children":[{"type":"text","value":"A list of objects as an attribute of an object"},{"type":"element","tagName":"a","properties":{"href":"#a-list-of-objects-as-an-attribute-of-an-object","ariaLabel":"a list of objects as an attribute of an object 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 the examples above we used single instances of other classes as attributes: a Person has a single Pet as an attribute, and a CompletedCourse has one Student and one Course as its attributes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In object oriented programming it is often the case that we want to have a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"collection"}]},{"type":"text","value":" of objects as an attribute. For example, the relationship between a sports team and its players follows this pattern:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Player"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" goals"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"goals "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" goals\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"goals"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" goals)\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Team"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"players "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_player"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Player"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"players"},{"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":"player"},{"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":"summary"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 goals "},{"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","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" player "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"players"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n goals"},{"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":"player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"goals"},{"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":"\"Team:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"\"Players:\""}]},{"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":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"players"},{"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":"\"Goals scored by each player:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" goals"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of our class in action:"}]},{"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":"ca "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Team"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Campus Allstars\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"10"}]},{"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":"\nca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"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":"22"}]},{"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":"\nca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Andy\""}]},{"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":"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":"text","value":"\nca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"summary"},{"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":"Team: Campus Allstars\nPlayers: 3\nGoals scored by each player: [10, 22, 1]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"A box of presents","tmcname":"part09-07_box_of_presents"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will practice wrapping presents. You will write two classes: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Present"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Box"}]},{"type":"text","value":". A present has a name and a weight, and a box contains presents."}]},{"type":"element","tagName":"h2","properties":{"id":"the-present-class","style":"position:relative;"},"children":[{"type":"text","value":"The Present class"},{"type":"element","tagName":"a","properties":{"href":"#the-present-class","ariaLabel":"the present class 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":"Please define the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Present"}]},{"type":"text","value":" which can be used to represent different kinds of presents. The class definition should contain attributes for the name and the weight (in kg) of the present. Instances of the class should work as follows:"}]},{"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":"book "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Present"},{"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 Book\""}]},{"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":"2"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The name of the present:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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 weight of the present:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weight"},{"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":"\"Present:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This should print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The name of the present: ABC Book\nThe weight of the present: 2\nPresent: ABC Book (2 kg)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-box-class","style":"position:relative;"},"children":[{"type":"text","value":"The Box class"},{"type":"element","tagName":"a","properties":{"href":"#the-box-class","ariaLabel":"the box class 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":"Please define the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Box"}]},{"type":"text","value":". You should be able to add presents to the box, and the box should keep track of the combined weight of the presents within. The class definition should contain these methods:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_present(self, present: Present)"}]},{"type":"text","value":" which adds the present given as an argument to the box. The method has no return value."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"total_weight(self)"}]},{"type":"text","value":" which returns the combined weight of the presents in the box."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may use the following code to test your class:"}]},{"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":"book "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Present"},{"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 Book\""}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbox "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Box"},{"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":"\nbox"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_present"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"book"},{"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":"box"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"total_weight"},{"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\ncd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Present"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pink Floyd: Dark Side of the Moon\""}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbox"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_present"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"cd"},{"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":"box"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"total_weight"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n3"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"none-a-reference-to-nothing","style":"position:relative;"},"children":[{"type":"text","value":"None: a reference to nothing"},{"type":"element","tagName":"a","properties":{"href":"#none-a-reference-to-nothing","ariaLabel":"none a reference to nothing 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 Python programming all initialised variables refer to an object. There are, however, inevitably situations where we need to refer to something which does not exist, without causing errors. The keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":" represents exactly such an \"empty\" object."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Continuing from the Team and Player example above, let's assume we want to add a method for searching for players on the team by the name of the player. If no such player is found, it might make sense to return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Player"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" goals"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"goals "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" goals\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"goals"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" goals)\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Team"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"players "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_player"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Player"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"players"},{"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":"player"},{"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":"find_player"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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":"for"}]},{"type":"text","value":" player "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"players"},{"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":"if"}]},{"type":"text","value":" player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" name"},{"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":" player\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's test our 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":"text","value":"ca "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Team"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Campus Allstars\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"10"}]},{"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":"\nca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Amily\""}]},{"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":"22"}]},{"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":"\nca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Andy\""}]},{"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":"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":"text","value":"\n\nplayer1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Andy\""}]},{"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":"player1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nplayer2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Charlie\""}]},{"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":"player2"},{"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":"Andy (1 goals)\nNone"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Be careful with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":", though. It can sometimes cause more trouble than it solves. It is a common programming error to try to access a method or an attribute through a reference which evaluates to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":":"}]},{"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":"ca "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Team"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Campus Allstars\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"10"}]},{"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\nplayer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Charlie\""}]},{"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\"Goals by Charlie: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"goals"},{"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":"p","properties":{},"children":[{"type":"text","value":"Executing the above would cause an error:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"Traceback (most recent call last):\nFile \""},{"type":"element","tagName":"stdin","properties":{},"children":[{"type":"text","value":"\", line 1, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nAttributeError: 'NoneType' object has no attribute 'goals'"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is a good idea to check for "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":" before trying to access any attributes or methods of return 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":"ca "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Team"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Campus Allstars\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"10"}]},{"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\nplayer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Charlie\""}]},{"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":"if"}]},{"type":"text","value":" player "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"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\"Goals by Charlie: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"goals"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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\"Charlie doesn't play in Campus Allstars :(\""}]}]},{"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":"Charlie doesn't play in Campus Allstars :("}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The shortest person in the room","tmcname":"part09-08_shortest_in_room"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":". A person has a name and a height. In this exercise you will implement the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Room"}]},{"type":"text","value":". You may add any number of persons to a room, and you may also search for and remove the shortest person in the room."}]},{"type":"element","tagName":"h2","properties":{"id":"room","style":"position:relative;"},"children":[{"type":"text","value":"Room"},{"type":"element","tagName":"a","properties":{"href":"#room","ariaLabel":"room 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":"Please define the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Room"}]},{"type":"text","value":". It should have a list of persons as an attribute, and also contain the following methods:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add(person: Person)"}]},{"type":"text","value":" adds the person given as an argument to the room."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"is_empty()"}]},{"type":"text","value":" returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" depending on whether the room is empty."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print_contents()"}]},{"type":"text","value":" prints out the contents of the list of persons in the room."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please have a look at the following usage example:"}]},{"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":"room "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Room"},{"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":"\"Is the room empty?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" room"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"is_empty"},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lea\""}]},{"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":"183"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kenya\""}]},{"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":"172"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ally\""}]},{"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":"166"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nina\""}]},{"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":"162"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dorothy\""}]},{"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":"155"}]},{"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":"\"Is the room empty?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" room"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"is_empty"},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_contents"},{"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":"Is the room empty? True\nIs the room empty? False\nThere are 5 persons in the room, and their combined height is 838 cm\nLea (183 cm)\nKenya (172 cm)\nAlly (166 cm)\nNina (162 cm)\nDorothy (155 cm)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-shortest-person","style":"position:relative;"},"children":[{"type":"text","value":"The shortest person"},{"type":"element","tagName":"a","properties":{"href":"#the-shortest-person","ariaLabel":"the shortest person 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":"Please define the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"shortest()"}]},{"type":"text","value":" within the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Room"}]},{"type":"text","value":" class definition. The method should return the shortest person in the room it is called on. If the room is empty, the method should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":". The method should "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" remove the person fron the room."}]},{"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":"room "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Room"},{"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":"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":"\"Is the room empty?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" room"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"is_empty"},{"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"},{"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":"\"Shortest:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" room"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"shortest"},{"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\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lea\""}]},{"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":"183"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kenya\""}]},{"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":"172"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nina\""}]},{"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":"162"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ally\""}]},{"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":"166"}]},{"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":"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\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":"\"Is the room empty?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" room"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"is_empty"},{"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"},{"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":"\"Shortest:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" room"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"shortest"},{"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\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\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_contents"},{"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":"Is the room empty? True\nShortest: None"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Is the room empty? False\nShortest: Nina"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are 4 persons in the room, and their combined height is 683 cm\nLea (183 cm)\nKenya (172 cm)\nNina (162 cm)\nAlly (166 cm)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"removing-a-person-from-the-room","style":"position:relative;"},"children":[{"type":"text","value":"Removing a person from the room"},{"type":"element","tagName":"a","properties":{"href":"#removing-a-person-from-the-room","ariaLabel":"removing a person from the room 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":"Please define the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"remove_shortest()"}]},{"type":"text","value":" within the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Room"}]},{"type":"text","value":" class definition. The method should remove the shortest "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" object from the room and return the reference to the object. If the room is empty, the method should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":"."}]},{"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":"room "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Room"},{"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\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lea\""}]},{"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":"183"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kenya\""}]},{"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":"172"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nina\""}]},{"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":"162"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ally\""}]},{"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":"166"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_contents"},{"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":"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\nremoved "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" room"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove_shortest"},{"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\"Removed from room: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"removed"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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\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\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_contents"},{"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":"There are 4 persons in the room, and their combined height is 683 cm\nLea (183 cm)\nKenya (172 cm)\nNina (162 cm)\nAlly (166 cm)"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Removed from room: Nina"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are 3 persons in the room, and their combined height is 521 cm\nLea (183 cm)\nKenya (172 cm)\nAlly (166 cm)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint"}]},{"type":"text","value":": in "},{"type":"element","tagName":"a","properties":{"href":"/part-4/3-lists#removing-items-from-a-list"},"children":[{"type":"text","value":"part 4"}]},{"type":"text","value":" you will find instructions for removing items from a list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint2"}]},{"type":"text","value":": it is always possible to call another method of the same class from within a method. The following should work just fine:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Room"}]},{"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":"# ..."}]},{"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":"shortest"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 code goes here"}]},{"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":"remove_shortest"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 shortest_person "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"shortest"},{"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":"# ..."}]}]}]}]}]}]},"html":"

    After this section

      \n
    • You will know how to use objects as attributes in other objects
    • \n
    • You will be familiar with the keyword None
    • \n

    We have already seen examples of classes which have lists as attributes. As there is thus nothing stopping us from including mutable objects as attributes in our classes, we might as well use instances of our own classes as attributes in other classes we've defined ourselves. In the following examples we will define the classes Course, Student and CompletedCourse. A completed course makes use of the first two classes. The class definitions are very short and simple in order to better concentrate on the technique of using instances of our own classes as attributes.

    We will assume each class is defined in a separate file.

    First we define the class Course in a file named course.py:

    class Course:\n    def __init__(self, name: str, code: str, credits: int):\n        self.name = name\n        self.code = code\n        self.credits = credits

    Next, the class Student in a file named student.py:

    class Student:\n    def __init__(self, name: str, student_number: str, credits: int):\n        self.name = name\n        self.student_number = student_number\n        self.credits = credits

    Finally, the class CompletedCourse is defined in a file named completedcourse.py. As it uses the other two classes, they have to be imported before they can be used:

    from course import Course\nfrom student import Student\n\nclass CompletedCourse:\n    def __init__(self, student: Student, course: Course, grade: int):\n        self.student = student\n        self.course = course\n        self.grade = grade

    Here is an example of a main function which adds some completed courses to a list:

    from completedcourse import CompletedCourse\nfrom course import Course\nfrom student import Student\n\n# Create a list of students\nstudents = []\nstudents.append(Student(\"Ollie\", \"1234\", 10))\nstudents.append(Student(\"Peter\", \"3210\", 23))\nstudents.append(Student(\"Lena\", \"9999\", 43))\nstudents.append(Student(\"Tina\", \"3333\", 8))\n\n# Create a course named Introduction to Programming\nitp = Course(\"Introduction to Programming\", \"itp1\", 5)\n\n# Add completed courses for each student, with grade 3 for all\ncompleted = []\nfor student in students:\n    completed.append(CompletedCourse(student, itp, 3))\n\n# Print out the name of the student for each completed course\nfor course in completed:\n    print(course.student.name)

    Ollie\nPeter\nLena\nTina

    What exactly is happening with all the dots on the line print(course.student.name)?

      \n
    • course is an instance of the class CompletedCourse
    • \n
    • student refers to an attribute of the CompletedCourse object, which is an object of type Student
    • \n
    • the attribute name in the Student object contains the name of the student
    • \n

    When is an import necessary?

    In the examples above an import statement appeared quite a few times:

    from completedcourse import CompletedCourse\nfrom course import Course\nfrom student import Student\n\n# rest of the main function

    An import statement is only necessary when using code which is defined somewhere outside the current file (or Python interpreter session). This includes situations where we want to use something defined in the Python standard library. For example, the math module contains some mathematical operations:

    import math\n\nx = 10\nprint(f\"the square root of {x} is {math.sqrt(x)}\")

    In the example above we assumed the three classes were each defined in a separate file, and the main function was run from yet another file. This is why the import statements were necessary.

    If all program code is written in the same file, as most of the exercises on this course advise you to do, you will not need import statements to use the classes you have defined.

    If you find yourself writing something along the lines of

    from person import Person\n\n# more code goes here

    it is likely you have gotten something wrong. If you need a refresher, the import statement was first introduced in part 7 of this course material.

    The exercise template contains the outlines of two classes: Person and Pet. Each person has one pet. Please change the __str__ method in the class Person so that it also prints out information about the person's pet as shown in the example below.

    The string returned by the method must follow the format specified below exactly.

    hulda = Pet(\"Hulda\", \"mixed-breed dog\")\nlevi = Person(\"Levi\", hulda)\n\nprint(levi)

    Levi, whose pal is Hulda, a mixed-breed dog

    NB: all class definitions are in the same text file. You should not need to import anything.

    A list of objects as an attribute of an object

    In the examples above we used single instances of other classes as attributes: a Person has a single Pet as an attribute, and a CompletedCourse has one Student and one Course as its attributes.

    In object oriented programming it is often the case that we want to have a collection of objects as an attribute. For example, the relationship between a sports team and its players follows this pattern:

    class Player:\n    def __init__(self, name: str, goals: int):\n        self.name = name\n        self.goals = goals\n\n    def __str__(self):\n        return f\"{self.name} ({self.goals} goals)\"\n\nclass Team:\n    def __init__(self, name: str):\n        self.name = name\n        self.players = []\n\n    def add_player(self, player: Player):\n        self.players.append(player)\n\n    def summary(self):\n        goals = []\n        for player in self.players:\n            goals.append(player.goals)\n        print(\"Team:\", self.name)\n        print(\"Players:\", len(self.players))\n        print(\"Goals scored by each player:\", goals)

    An example of our class in action:

    ca = Team(\"Campus Allstars\")\nca.add_player(Player(\"Eric\", 10))\nca.add_player(Player(\"Emily\", 22))\nca.add_player(Player(\"Andy\", 1))\nca.summary()

    Team: Campus Allstars\nPlayers: 3\nGoals scored by each player: [10, 22, 1]

    In this exercise you will practice wrapping presents. You will write two classes: Present and Box. A present has a name and a weight, and a box contains presents.

    The Present class

    Please define the class Present which can be used to represent different kinds of presents. The class definition should contain attributes for the name and the weight (in kg) of the present. Instances of the class should work as follows:

    book = Present(\"ABC Book\", 2)\n\nprint(\"The name of the present:\", book.name)\nprint(\"The weight of the present:\", book.weight)\nprint(\"Present:\", book)

    This should print out

    The name of the present: ABC Book\nThe weight of the present: 2\nPresent: ABC Book (2 kg)

    The Box class

    Please define the class Box. You should be able to add presents to the box, and the box should keep track of the combined weight of the presents within. The class definition should contain these methods:

      \n
    • add_present(self, present: Present) which adds the present given as an argument to the box. The method has no return value.
    • \n
    • total_weight(self) which returns the combined weight of the presents in the box.
    • \n

    You may use the following code to test your class:

    book = Present(\"ABC Book\", 2)\n\nbox = Box()\nbox.add_present(book)\nprint(box.total_weight())\n\ncd = Present(\"Pink Floyd: Dark Side of the Moon\", 1)\nbox.add_present(cd)\nprint(box.total_weight())

    2\n3

    None: a reference to nothing

    In Python programming all initialised variables refer to an object. There are, however, inevitably situations where we need to refer to something which does not exist, without causing errors. The keyword None represents exactly such an \"empty\" object.

    Continuing from the Team and Player example above, let's assume we want to add a method for searching for players on the team by the name of the player. If no such player is found, it might make sense to return None:

    class Player:\n    def __init__(self, name: str, goals: int):\n        self.name = name\n        self.goals = goals\n\n    def __str__(self):\n        return f\"{self.name} ({self.goals} goals)\"\n\nclass Team:\n    def __init__(self, name: str):\n        self.name = name\n        self.players = []\n\n    def add_player(self, player: Player):\n        self.players.append(player)\n\n    def find_player(self, name: str):\n        for player in self.players:\n            if player.name == name:\n                return player\n        return None

    Let's test our function:

    ca = Team(\"Campus Allstars\")\nca.add_player(Player(\"Eric\", 10))\nca.add_player(Player(\"Amily\", 22))\nca.add_player(Player(\"Andy\", 1))\n\nplayer1 = ca.find_player(\"Andy\")\nprint(player1)\nplayer2 = ca.find_player(\"Charlie\")\nprint(player2)

    Andy (1 goals)\nNone

    Be careful with None, though. It can sometimes cause more trouble than it solves. It is a common programming error to try to access a method or an attribute through a reference which evaluates to None:

    ca = Team(\"Campus Allstars\")\nca.add_player(Player(\"Eric\", 10))\n\nplayer = ca.find_player(\"Charlie\")\nprint(f\"Goals by Charlie: {player.goals}\")

    Executing the above would cause an error:

    Traceback (most recent call last):\nFile \"\", line 1, in \nAttributeError: 'NoneType' object has no attribute 'goals'

    It is a good idea to check for None before trying to access any attributes or methods of return values:

    ca = Team(\"Campus Allstars\")\nca.add_player(Player(\"Eric\", 10))\n\nplayer = ca.find_player(\"Charlie\")\nif player is not None:\n    print(f\"Goals by Charlie: {player.goals}\")\nelse:\n    print(f\"Charlie doesn't play in Campus Allstars :(\")

    Charlie doesn't play in Campus Allstars :(

    The exercise template contains the class Person. A person has a name and a height. In this exercise you will implement the class Room. You may add any number of persons to a room, and you may also search for and remove the shortest person in the room.

    Room

    Please define the class Room. It should have a list of persons as an attribute, and also contain the following methods:

      \n
    • add(person: Person) adds the person given as an argument to the room.
    • \n
    • is_empty() returns True or False depending on whether the room is empty.
    • \n
    • print_contents() prints out the contents of the list of persons in the room.
    • \n

    Please have a look at the following usage example:

    room = Room()\nprint(\"Is the room empty?\", room.is_empty())\nroom.add(Person(\"Lea\", 183))\nroom.add(Person(\"Kenya\", 172))\nroom.add(Person(\"Ally\", 166))\nroom.add(Person(\"Nina\", 162))\nroom.add(Person(\"Dorothy\", 155))\nprint(\"Is the room empty?\", room.is_empty())\nroom.print_contents()

    Is the room empty? True\nIs the room empty? False\nThere are 5 persons in the room, and their combined height is 838 cm\nLea (183 cm)\nKenya (172 cm)\nAlly (166 cm)\nNina (162 cm)\nDorothy (155 cm)

    The shortest person

    Please define the method shortest() within the Room class definition. The method should return the shortest person in the room it is called on. If the room is empty, the method should return None. The method should not remove the person fron the room.

    room = Room()\n\nprint(\"Is the room empty?\", room.is_empty())\nprint(\"Shortest:\", room.shortest())\n\nroom.add(Person(\"Lea\", 183))\nroom.add(Person(\"Kenya\", 172))\nroom.add(Person(\"Nina\", 162))\nroom.add(Person(\"Ally\", 166))\n\nprint()\n\nprint(\"Is the room empty?\", room.is_empty())\nprint(\"Shortest:\", room.shortest())\n\nprint()\n\nroom.print_contents()

    Is the room empty? True\nShortest: None

    Is the room empty? False\nShortest: Nina

    There are 4 persons in the room, and their combined height is 683 cm\nLea (183 cm)\nKenya (172 cm)\nNina (162 cm)\nAlly (166 cm)

    Removing a person from the room

    Please define the method remove_shortest() within the Room class definition. The method should remove the shortest Person object from the room and return the reference to the object. If the room is empty, the method should return None.

    room = Room()\n\nroom.add(Person(\"Lea\", 183))\nroom.add(Person(\"Kenya\", 172))\nroom.add(Person(\"Nina\", 162))\nroom.add(Person(\"Ally\", 166))\nroom.print_contents()\n\nprint()\n\nremoved = room.remove_shortest()\nprint(f\"Removed from room: {removed.name}\")\n\nprint()\n\nroom.print_contents()

    There are 4 persons in the room, and their combined height is 683 cm\nLea (183 cm)\nKenya (172 cm)\nNina (162 cm)\nAlly (166 cm)

    Removed from room: Nina

    There are 3 persons in the room, and their combined height is 521 cm\nLea (183 cm)\nKenya (172 cm)\nAlly (166 cm)

    Hint: in part 4 you will find instructions for removing items from a list.

    Hint2: it is always possible to call another method of the same class from within a method. The following should work just fine:

    class Room:\n    # ...\n    def shortest(self):\n        # your code goes here\n\n    def remove_shortest(self):\n        shortest_person = self.shortest()\n        # ...
    ","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-9/2-objects-as-attributes.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know how to use objects as attributes in other objects"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be familiar with the keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We have already seen examples of classes which have lists as attributes. As there is thus nothing stopping us from including mutable objects as attributes in our classes, we might as well use instances of our own classes as attributes in other classes we've defined ourselves. In the following examples we will define the classes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Course"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CompletedCourse"}]},{"type":"text","value":". A completed course makes use of the first two classes. The class definitions are very short and simple in order to better concentrate on the technique of using instances of our own classes as attributes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We will assume each class is defined in a separate file."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First we define the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Course"}]},{"type":"text","value":" in a file named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"course.py"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Course"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" code"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" credits"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"code "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" code\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" credits"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Next, the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" in a file named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"student.py"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" student_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" credits"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"student_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" student_number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" credits"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Finally, the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CompletedCourse"}]},{"type":"text","value":" is defined in a file named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"completedcourse.py"}]},{"type":"text","value":". As it uses the other two classes, they have to be imported before they can be used:"}]},{"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":"from"}]},{"type":"text","value":" course "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Course\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" student "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Student\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"CompletedCourse"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" grade"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"student "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" student\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"course "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" course\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"grade "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" grade"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here is an example of a main function which adds some completed courses to 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":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" completedcourse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" CompletedCourse\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" course "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Course\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" student "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Student\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Create a list of students"}]},{"type":"text","value":"\nstudents "},{"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":"\nstudents"},{"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":"Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ollie\""}]},{"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":"\"1234\""}]},{"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":"10"}]},{"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":"\nstudents"},{"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":"Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"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":"\"3210\""}]},{"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":"23"}]},{"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":"\nstudents"},{"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":"Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lena\""}]},{"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":"\"9999\""}]},{"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":"43"}]},{"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":"\nstudents"},{"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":"Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Tina\""}]},{"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":"\"3333\""}]},{"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":"8"}]},{"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","comment"]},"children":[{"type":"text","value":"# Create a course named Introduction to Programming"}]},{"type":"text","value":"\nitp "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Introduction to Programming\""}]},{"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":"\"itp1\""}]},{"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":"5"}]},{"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":"# Add completed courses for each student, with grade 3 for all"}]},{"type":"text","value":"\ncompleted "},{"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","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" student "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" students"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n completed"},{"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":"CompletedCourse"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" itp"},{"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":"3"}]},{"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","comment"]},"children":[{"type":"text","value":"# Print out the name of the student for each completed course"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" course "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" completed"},{"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":"course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"Ollie\nPeter\nLena\nTina"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"What exactly is happening with all the dots on the line "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print(course.student.name)"}]},{"type":"text","value":"?"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"course"}]},{"type":"text","value":" is an instance of the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CompletedCourse"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"student"}]},{"type":"text","value":" refers to an attribute of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CompletedCourse"}]},{"type":"text","value":" object, which is an object of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the attribute "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" in the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" object contains the name of the student"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"h2","properties":{"id":"when-is-an-import-necessary","style":"position:relative;"},"children":[{"type":"text","value":"When is an import necessary?"},{"type":"element","tagName":"a","properties":{"href":"#when-is-an-import-necessary","ariaLabel":"when is an import necessary 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 the examples above an "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement appeared quite a few times:"}]},{"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":"from"}]},{"type":"text","value":" completedcourse "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" CompletedCourse\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" course "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Course\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"from"}]},{"type":"text","value":" student "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Student\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# rest of the main function"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement is only necessary when using code which is defined somewhere outside the current file (or Python interpreter session). This includes situations where we want to use something defined in the Python standard library. For example, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"math"}]},{"type":"text","value":" module contains some mathematical operations:"}]},{"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":"import"}]},{"type":"text","value":" math\n\nx "},{"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":"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\"the square root of "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" 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":"math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the example above we assumed the three classes were each defined in a separate file, and the main function was run from yet another file. This is why the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statements were necessary."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If all program code is written in the same file, as most of the exercises on this course advise you to do, "},{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"you will not need"}]},{"type":"text","value":" "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statements to use the classes you have defined."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you find yourself writing something along the lines of"}]},{"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":"from"}]},{"type":"text","value":" person "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" Person\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# more code goes here"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"it is likely you have gotten something wrong. If you need a refresher, the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" statement was first introduced in "},{"type":"element","tagName":"a","properties":{"href":"/part-7/1-modules"},"children":[{"type":"text","value":"part 7"}]},{"type":"text","value":" of this course material."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Pets","tmcname":"part09-06_pets"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the outlines of two classes: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Pet"}]},{"type":"text","value":". Each person has one pet. Please change the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method in the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" so that it also prints out information about the person's pet as shown in the example below."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The string returned by the method "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"must follow the format specified below exactly"}]},{"type":"text","value":"."}]},{"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":"hulda "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Pet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Hulda\""}]},{"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":"\"mixed-breed dog\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nlevi "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Levi\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" hulda"},{"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":"levi"},{"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":"Levi, whose pal is Hulda, a mixed-breed dog"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" all class definitions are in the same text file. You should not need to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" anything."}]}]},{"type":"element","tagName":"h2","properties":{"id":"a-list-of-objects-as-an-attribute-of-an-object","style":"position:relative;"},"children":[{"type":"text","value":"A list of objects as an attribute of an object"},{"type":"element","tagName":"a","properties":{"href":"#a-list-of-objects-as-an-attribute-of-an-object","ariaLabel":"a list of objects as an attribute of an object 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 the examples above we used single instances of other classes as attributes: a Person has a single Pet as an attribute, and a CompletedCourse has one Student and one Course as its attributes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In object oriented programming it is often the case that we want to have a "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"collection"}]},{"type":"text","value":" of objects as an attribute. For example, the relationship between a sports team and its players follows this pattern:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Player"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" goals"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"goals "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" goals\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"goals"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" goals)\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Team"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"players "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_player"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Player"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"players"},{"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":"player"},{"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":"summary"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 goals "},{"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","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" player "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"players"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n goals"},{"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":"player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"goals"},{"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":"\"Team:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"\"Players:\""}]},{"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":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"players"},{"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":"\"Goals scored by each player:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" goals"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of our class in action:"}]},{"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":"ca "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Team"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Campus Allstars\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"10"}]},{"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":"\nca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Emily\""}]},{"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":"22"}]},{"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":"\nca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Andy\""}]},{"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":"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":"text","value":"\nca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"summary"},{"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":"Team: Campus Allstars\nPlayers: 3\nGoals scored by each player: [10, 22, 1]"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"A box of presents","tmcname":"part09-07_box_of_presents"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this exercise you will practice wrapping presents. You will write two classes: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Present"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Box"}]},{"type":"text","value":". A present has a name and a weight, and a box contains presents."}]},{"type":"element","tagName":"h2","properties":{"id":"the-present-class","style":"position:relative;"},"children":[{"type":"text","value":"The Present class"},{"type":"element","tagName":"a","properties":{"href":"#the-present-class","ariaLabel":"the present class 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":"Please define the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Present"}]},{"type":"text","value":" which can be used to represent different kinds of presents. The class definition should contain attributes for the name and the weight (in kg) of the present. Instances of the class should work as follows:"}]},{"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":"book "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Present"},{"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 Book\""}]},{"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":"2"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The name of the present:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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 weight of the present:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weight"},{"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":"\"Present:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"This should print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The name of the present: ABC Book\nThe weight of the present: 2\nPresent: ABC Book (2 kg)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-box-class","style":"position:relative;"},"children":[{"type":"text","value":"The Box class"},{"type":"element","tagName":"a","properties":{"href":"#the-box-class","ariaLabel":"the box class 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":"Please define the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Box"}]},{"type":"text","value":". You should be able to add presents to the box, and the box should keep track of the combined weight of the presents within. The class definition should contain these methods:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_present(self, present: Present)"}]},{"type":"text","value":" which adds the present given as an argument to the box. The method has no return value."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"total_weight(self)"}]},{"type":"text","value":" which returns the combined weight of the presents in the box."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may use the following code to test your class:"}]},{"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":"book "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Present"},{"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 Book\""}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbox "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Box"},{"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":"\nbox"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_present"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"book"},{"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":"box"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"total_weight"},{"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\ncd "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Present"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Pink Floyd: Dark Side of the Moon\""}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbox"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_present"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"cd"},{"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":"box"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"total_weight"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"2\n3"}]}]}]},{"type":"element","tagName":"h2","properties":{"id":"none-a-reference-to-nothing","style":"position:relative;"},"children":[{"type":"text","value":"None: a reference to nothing"},{"type":"element","tagName":"a","properties":{"href":"#none-a-reference-to-nothing","ariaLabel":"none a reference to nothing 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 Python programming all initialised variables refer to an object. There are, however, inevitably situations where we need to refer to something which does not exist, without causing errors. The keyword "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":" represents exactly such an \"empty\" object."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Continuing from the Team and Player example above, let's assume we want to add a method for searching for players on the team by the name of the player. If no such player is found, it might make sense to return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Player"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" goals"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"goals "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" goals\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"goals"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" goals)\""}]}]},{"type":"text","value":"\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Team"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"players "},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"add_player"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Player"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"players"},{"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":"player"},{"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":"find_player"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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":"for"}]},{"type":"text","value":" player "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"players"},{"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":"if"}]},{"type":"text","value":" player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"=="}]},{"type":"text","value":" name"},{"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":" player\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's test our 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":"text","value":"ca "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Team"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Campus Allstars\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"10"}]},{"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":"\nca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Amily\""}]},{"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":"22"}]},{"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":"\nca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Andy\""}]},{"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":"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":"text","value":"\n\nplayer1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Andy\""}]},{"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":"player1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nplayer2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Charlie\""}]},{"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":"player2"},{"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":"Andy (1 goals)\nNone"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Be careful with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":", though. It can sometimes cause more trouble than it solves. It is a common programming error to try to access a method or an attribute through a reference which evaluates to "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":":"}]},{"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":"ca "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Team"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Campus Allstars\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"10"}]},{"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\nplayer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Charlie\""}]},{"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\"Goals by Charlie: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"goals"},{"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":"p","properties":{},"children":[{"type":"text","value":"Executing the above would cause an error:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"text","value":"Traceback (most recent call last):\nFile \""},{"type":"element","tagName":"stdin","properties":{},"children":[{"type":"text","value":"\", line 1, in "},{"type":"element","tagName":"module","properties":{},"children":[{"type":"text","value":"\nAttributeError: 'NoneType' object has no attribute 'goals'"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It is a good idea to check for "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":" before trying to access any attributes or methods of return 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":"ca "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Team"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Campus Allstars\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Eric\""}]},{"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":"10"}]},{"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\nplayer "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" ca"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"find_player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Charlie\""}]},{"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":"if"}]},{"type":"text","value":" player "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"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\"Goals by Charlie: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"goals"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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\"Charlie doesn't play in Campus Allstars :(\""}]}]},{"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":"Charlie doesn't play in Campus Allstars :("}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"The shortest person in the room","tmcname":"part09-08_shortest_in_room"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":". A person has a name and a height. In this exercise you will implement the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Room"}]},{"type":"text","value":". You may add any number of persons to a room, and you may also search for and remove the shortest person in the room."}]},{"type":"element","tagName":"h2","properties":{"id":"room","style":"position:relative;"},"children":[{"type":"text","value":"Room"},{"type":"element","tagName":"a","properties":{"href":"#room","ariaLabel":"room 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":"Please define the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Room"}]},{"type":"text","value":". It should have a list of persons as an attribute, and also contain the following methods:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add(person: Person)"}]},{"type":"text","value":" adds the person given as an argument to the room."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"is_empty()"}]},{"type":"text","value":" returns "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"True"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":" depending on whether the room is empty."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print_contents()"}]},{"type":"text","value":" prints out the contents of the list of persons in the room."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please have a look at the following usage example:"}]},{"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":"room "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Room"},{"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":"\"Is the room empty?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" room"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"is_empty"},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lea\""}]},{"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":"183"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kenya\""}]},{"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":"172"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ally\""}]},{"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":"166"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nina\""}]},{"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":"162"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Dorothy\""}]},{"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":"155"}]},{"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":"\"Is the room empty?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" room"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"is_empty"},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_contents"},{"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":"Is the room empty? True\nIs the room empty? False\nThere are 5 persons in the room, and their combined height is 838 cm\nLea (183 cm)\nKenya (172 cm)\nAlly (166 cm)\nNina (162 cm)\nDorothy (155 cm)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-shortest-person","style":"position:relative;"},"children":[{"type":"text","value":"The shortest person"},{"type":"element","tagName":"a","properties":{"href":"#the-shortest-person","ariaLabel":"the shortest person 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":"Please define the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"shortest()"}]},{"type":"text","value":" within the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Room"}]},{"type":"text","value":" class definition. The method should return the shortest person in the room it is called on. If the room is empty, the method should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":". The method should "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" remove the person fron the room."}]},{"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":"room "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Room"},{"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":"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":"\"Is the room empty?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" room"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"is_empty"},{"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"},{"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":"\"Shortest:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" room"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"shortest"},{"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\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lea\""}]},{"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":"183"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kenya\""}]},{"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":"172"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nina\""}]},{"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":"162"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ally\""}]},{"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":"166"}]},{"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":"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\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":"\"Is the room empty?\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" room"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"is_empty"},{"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"},{"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":"\"Shortest:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" room"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"shortest"},{"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\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\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_contents"},{"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":"Is the room empty? True\nShortest: None"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Is the room empty? False\nShortest: Nina"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are 4 persons in the room, and their combined height is 683 cm\nLea (183 cm)\nKenya (172 cm)\nNina (162 cm)\nAlly (166 cm)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"removing-a-person-from-the-room","style":"position:relative;"},"children":[{"type":"text","value":"Removing a person from the room"},{"type":"element","tagName":"a","properties":{"href":"#removing-a-person-from-the-room","ariaLabel":"removing a person from the room 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":"Please define the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"remove_shortest()"}]},{"type":"text","value":" within the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Room"}]},{"type":"text","value":" class definition. The method should remove the shortest "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Person"}]},{"type":"text","value":" object from the room and return the reference to the object. If the room is empty, the method should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":"."}]},{"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":"room "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Room"},{"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\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Lea\""}]},{"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":"183"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Kenya\""}]},{"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":"172"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nina\""}]},{"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":"162"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"Person"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Ally\""}]},{"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":"166"}]},{"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":"\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_contents"},{"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":"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\nremoved "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" room"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"remove_shortest"},{"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\"Removed from room: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"removed"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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\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\nroom"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_contents"},{"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":"There are 4 persons in the room, and their combined height is 683 cm\nLea (183 cm)\nKenya (172 cm)\nNina (162 cm)\nAlly (166 cm)"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Removed from room: Nina"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There are 3 persons in the room, and their combined height is 521 cm\nLea (183 cm)\nKenya (172 cm)\nAlly (166 cm)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint"}]},{"type":"text","value":": in "},{"type":"element","tagName":"a","properties":{"href":"/part-4/3-lists#removing-items-from-a-list"},"children":[{"type":"text","value":"part 4"}]},{"type":"text","value":" you will find instructions for removing items from a list."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"Hint2"}]},{"type":"text","value":": it is always possible to call another method of the same class from within a method. The following should work just fine:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Room"}]},{"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":"# ..."}]},{"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":"shortest"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 code goes here"}]},{"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":"remove_shortest"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 shortest_person "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"shortest"},{"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":"# ..."}]}]}]}]}]}]},"html":"

    After this section

      \n
    • You will know how to use objects as attributes in other objects
    • \n
    • You will be familiar with the keyword None
    • \n

    We have already seen examples of classes which have lists as attributes. As there is thus nothing stopping us from including mutable objects as attributes in our classes, we might as well use instances of our own classes as attributes in other classes we've defined ourselves. In the following examples we will define the classes Course, Student and CompletedCourse. A completed course makes use of the first two classes. The class definitions are very short and simple in order to better concentrate on the technique of using instances of our own classes as attributes.

    We will assume each class is defined in a separate file.

    First we define the class Course in a file named course.py:

    class Course:\n    def __init__(self, name: str, code: str, credits: int):\n        self.name = name\n        self.code = code\n        self.credits = credits

    Next, the class Student in a file named student.py:

    class Student:\n    def __init__(self, name: str, student_number: str, credits: int):\n        self.name = name\n        self.student_number = student_number\n        self.credits = credits

    Finally, the class CompletedCourse is defined in a file named completedcourse.py. As it uses the other two classes, they have to be imported before they can be used:

    from course import Course\nfrom student import Student\n\nclass CompletedCourse:\n    def __init__(self, student: Student, course: Course, grade: int):\n        self.student = student\n        self.course = course\n        self.grade = grade

    Here is an example of a main function which adds some completed courses to a list:

    from completedcourse import CompletedCourse\nfrom course import Course\nfrom student import Student\n\n# Create a list of students\nstudents = []\nstudents.append(Student(\"Ollie\", \"1234\", 10))\nstudents.append(Student(\"Peter\", \"3210\", 23))\nstudents.append(Student(\"Lena\", \"9999\", 43))\nstudents.append(Student(\"Tina\", \"3333\", 8))\n\n# Create a course named Introduction to Programming\nitp = Course(\"Introduction to Programming\", \"itp1\", 5)\n\n# Add completed courses for each student, with grade 3 for all\ncompleted = []\nfor student in students:\n    completed.append(CompletedCourse(student, itp, 3))\n\n# Print out the name of the student for each completed course\nfor course in completed:\n    print(course.student.name)

    Ollie\nPeter\nLena\nTina

    What exactly is happening with all the dots on the line print(course.student.name)?

      \n
    • course is an instance of the class CompletedCourse
    • \n
    • student refers to an attribute of the CompletedCourse object, which is an object of type Student
    • \n
    • the attribute name in the Student object contains the name of the student
    • \n

    When is an import necessary?

    In the examples above an import statement appeared quite a few times:

    from completedcourse import CompletedCourse\nfrom course import Course\nfrom student import Student\n\n# rest of the main function

    An import statement is only necessary when using code which is defined somewhere outside the current file (or Python interpreter session). This includes situations where we want to use something defined in the Python standard library. For example, the math module contains some mathematical operations:

    import math\n\nx = 10\nprint(f\"the square root of {x} is {math.sqrt(x)}\")

    In the example above we assumed the three classes were each defined in a separate file, and the main function was run from yet another file. This is why the import statements were necessary.

    If all program code is written in the same file, as most of the exercises on this course advise you to do, you will not need import statements to use the classes you have defined.

    If you find yourself writing something along the lines of

    from person import Person\n\n# more code goes here

    it is likely you have gotten something wrong. If you need a refresher, the import statement was first introduced in part 7 of this course material.

    The exercise template contains the outlines of two classes: Person and Pet. Each person has one pet. Please change the __str__ method in the class Person so that it also prints out information about the person's pet as shown in the example below.

    The string returned by the method must follow the format specified below exactly.

    hulda = Pet(\"Hulda\", \"mixed-breed dog\")\nlevi = Person(\"Levi\", hulda)\n\nprint(levi)

    Levi, whose pal is Hulda, a mixed-breed dog

    NB: all class definitions are in the same text file. You should not need to import anything.

    A list of objects as an attribute of an object

    In the examples above we used single instances of other classes as attributes: a Person has a single Pet as an attribute, and a CompletedCourse has one Student and one Course as its attributes.

    In object oriented programming it is often the case that we want to have a collection of objects as an attribute. For example, the relationship between a sports team and its players follows this pattern:

    class Player:\n    def __init__(self, name: str, goals: int):\n        self.name = name\n        self.goals = goals\n\n    def __str__(self):\n        return f\"{self.name} ({self.goals} goals)\"\n\nclass Team:\n    def __init__(self, name: str):\n        self.name = name\n        self.players = []\n\n    def add_player(self, player: Player):\n        self.players.append(player)\n\n    def summary(self):\n        goals = []\n        for player in self.players:\n            goals.append(player.goals)\n        print(\"Team:\", self.name)\n        print(\"Players:\", len(self.players))\n        print(\"Goals scored by each player:\", goals)

    An example of our class in action:

    ca = Team(\"Campus Allstars\")\nca.add_player(Player(\"Eric\", 10))\nca.add_player(Player(\"Emily\", 22))\nca.add_player(Player(\"Andy\", 1))\nca.summary()

    Team: Campus Allstars\nPlayers: 3\nGoals scored by each player: [10, 22, 1]

    In this exercise you will practice wrapping presents. You will write two classes: Present and Box. A present has a name and a weight, and a box contains presents.

    The Present class

    Please define the class Present which can be used to represent different kinds of presents. The class definition should contain attributes for the name and the weight (in kg) of the present. Instances of the class should work as follows:

    book = Present(\"ABC Book\", 2)\n\nprint(\"The name of the present:\", book.name)\nprint(\"The weight of the present:\", book.weight)\nprint(\"Present:\", book)

    This should print out

    The name of the present: ABC Book\nThe weight of the present: 2\nPresent: ABC Book (2 kg)

    The Box class

    Please define the class Box. You should be able to add presents to the box, and the box should keep track of the combined weight of the presents within. The class definition should contain these methods:

      \n
    • add_present(self, present: Present) which adds the present given as an argument to the box. The method has no return value.
    • \n
    • total_weight(self) which returns the combined weight of the presents in the box.
    • \n

    You may use the following code to test your class:

    book = Present(\"ABC Book\", 2)\n\nbox = Box()\nbox.add_present(book)\nprint(box.total_weight())\n\ncd = Present(\"Pink Floyd: Dark Side of the Moon\", 1)\nbox.add_present(cd)\nprint(box.total_weight())

    2\n3

    None: a reference to nothing

    In Python programming all initialised variables refer to an object. There are, however, inevitably situations where we need to refer to something which does not exist, without causing errors. The keyword None represents exactly such an \"empty\" object.

    Continuing from the Team and Player example above, let's assume we want to add a method for searching for players on the team by the name of the player. If no such player is found, it might make sense to return None:

    class Player:\n    def __init__(self, name: str, goals: int):\n        self.name = name\n        self.goals = goals\n\n    def __str__(self):\n        return f\"{self.name} ({self.goals} goals)\"\n\nclass Team:\n    def __init__(self, name: str):\n        self.name = name\n        self.players = []\n\n    def add_player(self, player: Player):\n        self.players.append(player)\n\n    def find_player(self, name: str):\n        for player in self.players:\n            if player.name == name:\n                return player\n        return None

    Let's test our function:

    ca = Team(\"Campus Allstars\")\nca.add_player(Player(\"Eric\", 10))\nca.add_player(Player(\"Amily\", 22))\nca.add_player(Player(\"Andy\", 1))\n\nplayer1 = ca.find_player(\"Andy\")\nprint(player1)\nplayer2 = ca.find_player(\"Charlie\")\nprint(player2)

    Andy (1 goals)\nNone

    Be careful with None, though. It can sometimes cause more trouble than it solves. It is a common programming error to try to access a method or an attribute through a reference which evaluates to None:

    ca = Team(\"Campus Allstars\")\nca.add_player(Player(\"Eric\", 10))\n\nplayer = ca.find_player(\"Charlie\")\nprint(f\"Goals by Charlie: {player.goals}\")

    Executing the above would cause an error:

    Traceback (most recent call last):\nFile \"\", line 1, in \nAttributeError: 'NoneType' object has no attribute 'goals'

    It is a good idea to check for None before trying to access any attributes or methods of return values:

    ca = Team(\"Campus Allstars\")\nca.add_player(Player(\"Eric\", 10))\n\nplayer = ca.find_player(\"Charlie\")\nif player is not None:\n    print(f\"Goals by Charlie: {player.goals}\")\nelse:\n    print(f\"Charlie doesn't play in Campus Allstars :(\")

    Charlie doesn't play in Campus Allstars :(

    The exercise template contains the class Person. A person has a name and a height. In this exercise you will implement the class Room. You may add any number of persons to a room, and you may also search for and remove the shortest person in the room.

    Room

    Please define the class Room. It should have a list of persons as an attribute, and also contain the following methods:

      \n
    • add(person: Person) adds the person given as an argument to the room.
    • \n
    • is_empty() returns True or False depending on whether the room is empty.
    • \n
    • print_contents() prints out the contents of the list of persons in the room.
    • \n

    Please have a look at the following usage example:

    room = Room()\nprint(\"Is the room empty?\", room.is_empty())\nroom.add(Person(\"Lea\", 183))\nroom.add(Person(\"Kenya\", 172))\nroom.add(Person(\"Ally\", 166))\nroom.add(Person(\"Nina\", 162))\nroom.add(Person(\"Dorothy\", 155))\nprint(\"Is the room empty?\", room.is_empty())\nroom.print_contents()

    Is the room empty? True\nIs the room empty? False\nThere are 5 persons in the room, and their combined height is 838 cm\nLea (183 cm)\nKenya (172 cm)\nAlly (166 cm)\nNina (162 cm)\nDorothy (155 cm)

    The shortest person

    Please define the method shortest() within the Room class definition. The method should return the shortest person in the room it is called on. If the room is empty, the method should return None. The method should not remove the person fron the room.

    room = Room()\n\nprint(\"Is the room empty?\", room.is_empty())\nprint(\"Shortest:\", room.shortest())\n\nroom.add(Person(\"Lea\", 183))\nroom.add(Person(\"Kenya\", 172))\nroom.add(Person(\"Nina\", 162))\nroom.add(Person(\"Ally\", 166))\n\nprint()\n\nprint(\"Is the room empty?\", room.is_empty())\nprint(\"Shortest:\", room.shortest())\n\nprint()\n\nroom.print_contents()

    Is the room empty? True\nShortest: None

    Is the room empty? False\nShortest: Nina

    There are 4 persons in the room, and their combined height is 683 cm\nLea (183 cm)\nKenya (172 cm)\nNina (162 cm)\nAlly (166 cm)

    Removing a person from the room

    Please define the method remove_shortest() within the Room class definition. The method should remove the shortest Person object from the room and return the reference to the object. If the room is empty, the method should return None.

    room = Room()\n\nroom.add(Person(\"Lea\", 183))\nroom.add(Person(\"Kenya\", 172))\nroom.add(Person(\"Nina\", 162))\nroom.add(Person(\"Ally\", 166))\nroom.print_contents()\n\nprint()\n\nremoved = room.remove_shortest()\nprint(f\"Removed from room: {removed.name}\")\n\nprint()\n\nroom.print_contents()

    There are 4 persons in the room, and their combined height is 683 cm\nLea (183 cm)\nKenya (172 cm)\nNina (162 cm)\nAlly (166 cm)

    Removed from room: Nina

    There are 3 persons in the room, and their combined height is 521 cm\nLea (183 cm)\nKenya (172 cm)\nAlly (166 cm)

    Hint: in part 4 you will find instructions for removing items from a list.

    Hint2: it is always possible to call another method of the same class from within a method. The following should work just fine:

    class Room:\n    # ...\n    def shortest(self):\n        # your code goes here\n\n    def remove_shortest(self):\n        shortest_person = self.shortest()\n        # ...
    ","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-9/2-objects-as-attributes.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-9/3-encapsulation/page-data.json b/page-data/part-9/3-encapsulation/page-data.json index 5a9ac47ee..d8b74abcc 100644 --- a/page-data/part-9/3-encapsulation/page-data.json +++ b/page-data/part-9/3-encapsulation/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-9/3-encapsulation", - "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 know what encapsulation means"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to create private attributes"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to create getters and setters for your attributes"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In object oriented programming the term "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"client"}]},{"type":"text","value":" refers to a program which uses a class, or instances of a class. A class offers the client "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"services"}]},{"type":"text","value":" through which the client can access the objects created based on the class. The goals here are that"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the use of a class and/or objects is as simple as possible from the client's point of view"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"integrity"}]},{"type":"text","value":" of any object is preserved at all times"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The integrity of an object means that the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"state"}]},{"type":"text","value":" of an object always remains acceptable. In practice this means that the values of the object's attributes are always acceptable. For example, an object representing a date should never have 13 as the value of the month, an object modelling a student should never have a negative number as the value of study credits attained, and so forth."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's take a look at a class named Student:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" student_number"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"student_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" student_number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"study_credits "},{"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":"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":"add_credits"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" study_credits"},{"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":"if"}]},{"type":"text","value":" study_credits "},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"study_credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" study_credits"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" object offers its clients the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_credits"}]},{"type":"text","value":", which allows the client to add a specified number of credits to the student's total. The method ensures the value passed as the argument is above zero. The following code adds study credits on three occasions:"}]},{"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":"sally "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sally Student\""}]},{"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":"\"12345\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsally"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_credits"},{"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":"\nsally"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_credits"},{"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":"\nsally"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_credits"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"\"Study credits:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sally"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"study_credits"},{"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":"Study credits: 20"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Despite the method definition it is still possible to access the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"study_credits"}]},{"type":"text","value":" attribute directly. This could result in an erroneous state where the integrity of the object is lost:"}]},{"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":"sally "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sally Student\""}]},{"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":"\"12345\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsally"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"study_credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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":"100"}]},{"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":"\"Study credits:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sally"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"study_credits"},{"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":"Study credits: -100"}]}]},{"type":"element","tagName":"h2","properties":{"id":"encapsulation","style":"position:relative;"},"children":[{"type":"text","value":"Encapsulation"},{"type":"element","tagName":"a","properties":{"href":"#encapsulation","ariaLabel":"encapsulation 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":"A common feature in object oriented programming languages is that classes can usually hide their attributes from any prospective clients. Hidden attributes are usually called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"private"}]},{"type":"text","value":". In Python this privacy is achieved by adding two underscores "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__"}]},{"type":"text","value":" to the beginning of the attribute name:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"CreditCard"}]},{"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":"# the attribute number is private, while the attribute name is accessible"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A private attribute is not directly visible to the client. Trying to refer to it causes an error. In the above example the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" attribute can be easily accessed and 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":"card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CreditCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Randy Riches\""}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"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":"\"Charlie Churchmouse\""}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"Randy Riches\nCharlie Churchmouse"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Trying to print out the card number, however, causes and error:"}]},{"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":"card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CreditCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Randy Riches\""}]},{"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":"card"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"AttributeError: 'CreditCard' object has no attribute '__number'"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hiding attributes from clients is called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"encapsulation"}]},{"type":"text","value":". As the name implies, the attribute is \"enclosed in a capsule\". The client is then offered a suitable interface for accessing and processing the data stored in the object."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's add another encapsulated attribute: the balance on the credit card. This time we'll also add publicly visible methods which allow the client to access and change the balance:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"CreditCard"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\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":"deposit_money"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" amount"},{"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":"float"}]},{"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":"if"}]},{"type":"text","value":" amount "},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" amount\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":"withdraw_money"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" amount"},{"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":"float"}]},{"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":"if"}]},{"type":"text","value":" amount "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" amount "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" amount\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":"retrieve_balance"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance"}]}]}]},{"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":"card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CreditCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123456\""}]},{"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":"\"Randy Riches\""}]},{"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":"5000"}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"retrieve_balance"},{"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":"\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"deposit_money"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"retrieve_balance"},{"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":"\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"withdraw_money"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"500"}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"retrieve_balance"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# The following will not work because the balance is not sufficient"}]},{"type":"text","value":"\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"withdraw_money"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10000"}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"retrieve_balance"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5000\n5100\n4600\n4600"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The balance cannot be changed directly because the attribute is private, but we've included the methods "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"deposit_money"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"withdraw_money"}]},{"type":"text","value":" for changing the value. The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"retrieve_balance"}]},{"type":"text","value":" returns the value stored in balance. The methods include some rudimentary checks for retaining the integrity of the object: for instance, the card cannot be overdrawn."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Car","tmcname":"part09-09_car"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Car"}]},{"type":"text","value":" which has two private, "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"encapsulated"}]},{"type":"text","value":" variables: the amount of petrol in the tank (0 to 60 litres) and odometer reading (in kilometres). The car consumes one litre of petrol per kilometre."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The class should also contain the following methods:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fill_up()"}]},{"type":"text","value":" which fills up the tank"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"drive(km:int)"}]},{"type":"text","value":" which drives the car for the distance indicated, or for however long the petrol in the tank allows"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" which returns a string representation of the car as per the examples below"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how the class is used:"}]},{"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":"car "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Car"},{"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":"car"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill_up"},{"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":"car"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"drive"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"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":"car"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"drive"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"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":"car"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"drive"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"car"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill_up"},{"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":"\ncar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill_up"},{"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":"car"},{"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":"Car: odometer reading 0 km, petrol remaining 0 litres\nCar: odometer reading 0 km, petrol remaining 60 litres\nCar: odometer reading 20 km, petrol remaining 40 litres\nCar: odometer reading 60 km, petrol remaining 0 litres\nCar: odometer reading 60 km, petrol remaining 0 litres\nCar: odometer reading 60 km, petrol remaining 60 litres"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" you are asked to encapsulate the amount of petrol left and the odometer reading. It should not be possible to access them directly from outside the class's own methods."}]}]},{"type":"element","tagName":"h2","properties":{"id":"a-brief-note-on-private-attributes-python-and-object-oriented-programming","style":"position:relative;"},"children":[{"type":"text","value":"A brief note on private attributes, Python and object oriented programming"},{"type":"element","tagName":"a","properties":{"href":"#a-brief-note-on-private-attributes-python-and-object-oriented-programming","ariaLabel":"a brief note on private attributes python and object oriented programming 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":"There are ways around the underscore "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__"}]},{"type":"text","value":" notation for hiding attributes, which you may come across if you search for resources online. No Python attribute is truly private, and this is intentional on the part of the creators of Python. On the other hand, a Python programmer is generally expected to respect the visibility guidelines set in classes, and it takes a special effort to get around these. In other object oriented programming languages, such as Java, private variables are often truly hidden, and it is best if you think of private Python variables as such as well."}]},{"type":"element","tagName":"h2","properties":{"id":"getters-and-setters","style":"position:relative;"},"children":[{"type":"text","value":"Getters and setters"},{"type":"element","tagName":"a","properties":{"href":"#getters-and-setters","ariaLabel":"getters and setters 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 object oriented programming, methods which are dedicated to accessing and changing attributes are usually called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"getters"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"setters"}]},{"type":"text","value":". Not all Python programmers use the terms \"getter\" and \"setter\", but the concept of "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"properties"}]},{"type":"text","value":" outlined below is very similar, which is why we will use the generally accepted object oriented programming terminology here."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, above we created some public methods for accessing private attributes, but there is a more straightforward, \"pythonic\" way of accessing attributes. Let's have a look at a simple class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Wallet"}]},{"type":"text","value":" with a single, private attribute "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"money"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Wallet"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__money "},{"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":"p","properties":{},"children":[{"type":"text","value":"We can add getter and setter methods for accessing the private attribute using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":" decorator:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Wallet"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__money "},{"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":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# A getter method"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"money"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__money\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# A setter method"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@money"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"money"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" money"},{"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":"if"}]},{"type":"text","value":" money "},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__money "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" money"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First, we define a getter method, which returns the amount of money currently in the wallet. Then we define a setter method, which sets a new value for the money attribute while making sure the new value is not negative."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The new methods can be used like so:"}]},{"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":"wallet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Wallet"},{"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":"wallet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"money"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nwallet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"money "},{"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":"50"}]},{"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":"wallet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"money"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nwallet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"money "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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":"30"}]},{"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":"wallet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"money"},{"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\n50\n50"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As far as the client is concerned, using these new methods is no different from directly accessing an attribute. Parentheses are not necessary; instead it is perfectly acceptable to state "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"wallet.money = 50"}]},{"type":"text","value":", as if we were simply assigning a value to a variable. Indeed, the purpose was to hide (i.e. encapsulate) the internal implementation of the attribute while offering an easy way of accessing and modifying the data stored in the object."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The previous example has a small problem: the client is not notified of the failure to set a negative value for the money attribute. When a value supplied is clearly wrong, it is usually a good idea to raise an exception and thus let the client know. In this case the exception should probably be of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" to signify that the value supplied was unacceptable."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here we have an improved version of the class, along with some code for testing it:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Wallet"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__money "},{"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":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# A getter method"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"money"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__money\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# A setter method"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@money"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"money"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" money"},{"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":"if"}]},{"type":"text","value":" money "},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__money "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" money\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 amount must not be below zero\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"wallet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"money "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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":"30"}]},{"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":"wallet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"money"},{"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":"ValueError: The amount must not be below zero"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" the getter method, i.e. the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":" decorator, must be introduced before the setter method, or there will be an error when the class is executed. This is because the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":" decorator defines the name of the \"attribute\" offerred to the client. The setter method, added with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":".setter"}]},{"type":"text","value":", simply adds a new functionality to it."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Recording","tmcname":"part09-10_recording"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Recording"}]},{"type":"text","value":" which models a single recording. The class should have one private variable: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__length"}]},{"type":"text","value":" of type integer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement the following:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a constructor which takes the length as an argument"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a getter method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"length"}]},{"type":"text","value":" which returns the length of the recording"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a setter method which sets the length of the recording"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It should be possible to make use of the class as follows:"}]},{"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":"the_wall "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Recording"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"43"}]},{"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":"the_wall"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nthe_wall"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length "},{"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":"44"}]},{"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":"the_wall"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length"},{"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":"43\n44"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the argument for either the constructor or the setter method is below zero, this should raise a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you need a refresher on raising exceptions, please see "},{"type":"element","tagName":"a","properties":{"href":"/part-6/3-errors#raising-exceptions"},"children":[{"type":"text","value":"part 6"}]},{"type":"text","value":" of the course materials."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following example has a class with two private attributes, along with getters and setters for both. Please try the program out with different values passed as arguments:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Player"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player_number"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__player_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" player_number\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"name"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"name"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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":"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 name may not be an empty string\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"player_number"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__player_number\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@player_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"player_number"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player_number"},{"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":"if"}]},{"type":"text","value":" player_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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__player_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" player_number\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 player number must be a positive integer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"player "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betty Ballmer\""}]},{"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":"10"}]},{"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":"player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"player_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nplayer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"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":"\"Buster Ballmer\""}]},{"type":"text","value":"\nplayer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"player_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":"11"}]},{"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":"player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"player_number"},{"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":"Betty Ballmer\n10\nBuster Ballmer\n11"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To finish off this section, let's take a look at a class which models a simple diary. All attributes are private, but they are handled through differing interfaces: the owner of the diary has getter and setter methods, but the diary entries are processed with \"traditional\" methods. In this case it makes sense to deny the client all access to the internal data structure of the diary. Only the public methods are directly visible to the client."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Encapsulation also ensures that the internal implementation of the class can be changed at will, provided that the public interface stays intact. The client does not have to know or care whether the internal data structure is based on lists, dictionaries, or something completely 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":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Diary"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" owner"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__owner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" owner\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__entries "},{"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","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"owner"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__owner\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@owner"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"owner"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" owner"},{"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":"if"}]},{"type":"text","value":" owner "},{"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":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__owner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" owner\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 owner may not be an empty string\""}]},{"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":"add_entry"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" entry"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__entries"},{"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":"entry"},{"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":"print_entries"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"\"A total of\""}]},{"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":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__entries"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"entries\""}]},{"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":" entry "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__entries"},{"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":"\"- \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" entry"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"diary "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Diary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndiary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_entry"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Today I ate porridge\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndiary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_entry"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Today I learned object oriented programming\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndiary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_entry"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Today I went to bed early\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndiary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_entries"},{"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":"A total of 3 entries"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Today I ate porridge"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Today I learned object oriented programming"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Today I went to bed early"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Weather station","tmcname":"part09-11_weather_station"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"WeatherStation"}]},{"type":"text","value":" which is used to store observations about the weather. The class should have the following public attributes:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a constructor which takes the name of the station as its argument"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_observation(observation: str)"}]},{"type":"text","value":" which adds an observation as the last entry in a list"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"latest_observation()"}]},{"type":"text","value":" which returns the latest observation added to the list. If there are no observations yet, the method should return an "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"empty string"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number_of_observations()"}]},{"type":"text","value":" which returns the total number of observations added"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method which returns the name of the station and the total number of observations added as per the example below."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"All attributes should be encapsulated, so they can't be directly accessed. It is up to you how you implement the class, as long as the public interface is exactly as described above."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how the class is used:"}]},{"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":"station "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" WeatherStation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Houston\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_observation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Rain 10mm\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_observation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sunny\""}]},{"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":"station"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"latest_observation"},{"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\nstation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_observation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Thunderstorm\""}]},{"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":"station"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"latest_observation"},{"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\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":"station"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_observations"},{"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"},{"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":"station"},{"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":"Sunny\nThunderstorm\n3\nHouston, 3 observations"}]}]}]}]},"html":"

    After this section

      \n
    • You will know what encapsulation means
    • \n
    • You will be able to create private attributes
    • \n
    • You will know how to create getters and setters for your attributes
    • \n

    In object oriented programming the term client refers to a program which uses a class, or instances of a class. A class offers the client services through which the client can access the objects created based on the class. The goals here are that

      \n
    1. the use of a class and/or objects is as simple as possible from the client's point of view
    2. \n
    3. the integrity of any object is preserved at all times
    4. \n

    The integrity of an object means that the state of an object always remains acceptable. In practice this means that the values of the object's attributes are always acceptable. For example, an object representing a date should never have 13 as the value of the month, an object modelling a student should never have a negative number as the value of study credits attained, and so forth.

    Let's take a look at a class named Student:

    class Student:\n    def __init__(self, name: str, student_number: str):\n        self.name = name\n        self.student_number = student_number\n        self.study_credits = 0\n\n    def add_credits(self, study_credits):\n        if study_credits > 0:\n            self.study_credits += study_credits

    The Student object offers its clients the method add_credits, which allows the client to add a specified number of credits to the student's total. The method ensures the value passed as the argument is above zero. The following code adds study credits on three occasions:

    sally = Student(\"Sally Student\", \"12345\")\nsally.add_credits(5)\nsally.add_credits(5)\nsally.add_credits(10)\nprint(\"Study credits:\", sally.study_credits)

    Study credits: 20

    Despite the method definition it is still possible to access the study_credits attribute directly. This could result in an erroneous state where the integrity of the object is lost:

    sally = Student(\"Sally Student\", \"12345\")\nsally.study_credits = -100\nprint(\"Study credits:\", sally.study_credits)

    Study credits: -100

    Encapsulation

    A common feature in object oriented programming languages is that classes can usually hide their attributes from any prospective clients. Hidden attributes are usually called private. In Python this privacy is achieved by adding two underscores __ to the beginning of the attribute name:

    class CreditCard:\n    # the attribute number is private, while the attribute name is accessible\n    def __init__(self, number: str, name: str):\n        self.__number = number\n        self.name = name

    A private attribute is not directly visible to the client. Trying to refer to it causes an error. In the above example the name attribute can be easily accessed and changed:

    card = CreditCard(\"123456\",\"Randy Riches\")\nprint(card.name)\ncard.name = \"Charlie Churchmouse\"\nprint(card.name)

    Randy Riches\nCharlie Churchmouse

    Trying to print out the card number, however, causes and error:

    card = CreditCard(\"123456\",\"Randy Riches\")\nprint(card.__number)

    AttributeError: 'CreditCard' object has no attribute '__number'

    Hiding attributes from clients is called encapsulation. As the name implies, the attribute is \"enclosed in a capsule\". The client is then offered a suitable interface for accessing and processing the data stored in the object.

    Let's add another encapsulated attribute: the balance on the credit card. This time we'll also add publicly visible methods which allow the client to access and change the balance:

    class CreditCard:\n    def __init__(self, number: str, name: str, balance: float):\n        self.__number = number\n        self.name = name\n        self.__balance = balance\n\n    def deposit_money(self, amount: float):\n        if amount > 0:\n            self.__balance += amount\n\n    def withdraw_money(self, amount: float):\n        if amount > 0 and amount <= self.__balance:\n            self.__balance -= amount\n\n    def retrieve_balance(self):\n        return self.__balance
    card = CreditCard(\"123456\", \"Randy Riches\", 5000)\nprint(card.retrieve_balance())\ncard.deposit_money(100)\nprint(card.retrieve_balance())\ncard.withdraw_money(500)\nprint(card.retrieve_balance())\n# The following will not work because the balance is not sufficient\ncard.withdraw_money(10000)\nprint(card.retrieve_balance())

    5000\n5100\n4600\n4600

    The balance cannot be changed directly because the attribute is private, but we've included the methods deposit_money and withdraw_money for changing the value. The method retrieve_balance returns the value stored in balance. The methods include some rudimentary checks for retaining the integrity of the object: for instance, the card cannot be overdrawn.

    Please implement a class named Car which has two private, encapsulated variables: the amount of petrol in the tank (0 to 60 litres) and odometer reading (in kilometres). The car consumes one litre of petrol per kilometre.

    The class should also contain the following methods:

      \n
    • fill_up() which fills up the tank
    • \n
    • drive(km:int) which drives the car for the distance indicated, or for however long the petrol in the tank allows
    • \n
    • __str__ which returns a string representation of the car as per the examples below
    • \n

    An example of how the class is used:

    car = Car()\nprint(car)\ncar.fill_up()\nprint(car)\ncar.drive(20)\nprint(car)\ncar.drive(50)\nprint(car)\ncar.drive(10)\nprint(car)\ncar.fill_up()\ncar.fill_up()\nprint(car)

    Car: odometer reading 0 km, petrol remaining 0 litres\nCar: odometer reading 0 km, petrol remaining 60 litres\nCar: odometer reading 20 km, petrol remaining 40 litres\nCar: odometer reading 60 km, petrol remaining 0 litres\nCar: odometer reading 60 km, petrol remaining 0 litres\nCar: odometer reading 60 km, petrol remaining 60 litres

    NB: you are asked to encapsulate the amount of petrol left and the odometer reading. It should not be possible to access them directly from outside the class's own methods.

    A brief note on private attributes, Python and object oriented programming

    There are ways around the underscore __ notation for hiding attributes, which you may come across if you search for resources online. No Python attribute is truly private, and this is intentional on the part of the creators of Python. On the other hand, a Python programmer is generally expected to respect the visibility guidelines set in classes, and it takes a special effort to get around these. In other object oriented programming languages, such as Java, private variables are often truly hidden, and it is best if you think of private Python variables as such as well.

    Getters and setters

    In object oriented programming, methods which are dedicated to accessing and changing attributes are usually called getters and setters. Not all Python programmers use the terms \"getter\" and \"setter\", but the concept of properties outlined below is very similar, which is why we will use the generally accepted object oriented programming terminology here.

    So, above we created some public methods for accessing private attributes, but there is a more straightforward, \"pythonic\" way of accessing attributes. Let's have a look at a simple class named Wallet with a single, private attribute money:

    class Wallet:\n    def __init__(self):\n        self.__money = 0

    We can add getter and setter methods for accessing the private attribute using the @property decorator:

    class Wallet:\n    def __init__(self):\n        self.__money = 0\n\n    # A getter method\n    @property\n    def money(self):\n        return self.__money\n\n    # A setter method\n    @money.setter\n    def money(self, money):\n        if money >= 0:\n            self.__money = money

    First, we define a getter method, which returns the amount of money currently in the wallet. Then we define a setter method, which sets a new value for the money attribute while making sure the new value is not negative.

    The new methods can be used like so:

    wallet = Wallet()\nprint(wallet.money)\n\nwallet.money = 50\nprint(wallet.money)\n\nwallet.money = -30\nprint(wallet.money)

    0\n50\n50

    As far as the client is concerned, using these new methods is no different from directly accessing an attribute. Parentheses are not necessary; instead it is perfectly acceptable to state wallet.money = 50, as if we were simply assigning a value to a variable. Indeed, the purpose was to hide (i.e. encapsulate) the internal implementation of the attribute while offering an easy way of accessing and modifying the data stored in the object.

    The previous example has a small problem: the client is not notified of the failure to set a negative value for the money attribute. When a value supplied is clearly wrong, it is usually a good idea to raise an exception and thus let the client know. In this case the exception should probably be of type ValueError to signify that the value supplied was unacceptable.

    Here we have an improved version of the class, along with some code for testing it:

    class Wallet:\n    def __init__(self):\n        self.__money = 0\n\n    # A getter method\n    @property\n    def money(self):\n        return self.__money\n\n    # A setter method\n    @money.setter\n    def money(self, money):\n        if money >= 0:\n            self.__money = money\n        else:\n            raise ValueError(\"The amount must not be below zero\")
    wallet.money = -30\nprint(wallet.money)

    ValueError: The amount must not be below zero

    NB: the getter method, i.e. the @property decorator, must be introduced before the setter method, or there will be an error when the class is executed. This is because the @property decorator defines the name of the \"attribute\" offerred to the client. The setter method, added with .setter, simply adds a new functionality to it.

    Please create a class named Recording which models a single recording. The class should have one private variable: __length of type integer.

    Please implement the following:

      \n
    • a constructor which takes the length as an argument
    • \n
    • a getter method length which returns the length of the recording
    • \n
    • a setter method which sets the length of the recording
    • \n

    It should be possible to make use of the class as follows:

    the_wall = Recording(43)\nprint(the_wall.length)\nthe_wall.length = 44\nprint(the_wall.length)

    43\n44

    If the argument for either the constructor or the setter method is below zero, this should raise a ValueError.

    If you need a refresher on raising exceptions, please see part 6 of the course materials.

    The following example has a class with two private attributes, along with getters and setters for both. Please try the program out with different values passed as arguments:

    class Player:\n    def __init__(self, name: str, player_number: int):\n        self.__name = name\n        self.__player_number = player_number\n\n    @property\n    def name(self):\n        return self.__name\n\n    @name.setter\n    def name(self, name: str):\n        if name != \"\":\n            self.__name = name\n        else:\n            raise ValueError(\"The name may not be an empty string\")\n\n    @property\n    def player_number(self):\n        return self.__player_number\n\n    @player_number.setter\n    def player_number(self, player_number: int):\n        if player_number > 0:\n            self.__player_number = player_number\n        else:\n            raise ValueError(\"The player number must be a positive integer\")
    player = Player(\"Betty Ballmer\", 10)\nprint(player.name)\nprint(player.player_number)\n\nplayer.name = \"Buster Ballmer\"\nplayer.player_number = 11\nprint(player.name)\nprint(player.player_number)

    Betty Ballmer\n10\nBuster Ballmer\n11

    To finish off this section, let's take a look at a class which models a simple diary. All attributes are private, but they are handled through differing interfaces: the owner of the diary has getter and setter methods, but the diary entries are processed with \"traditional\" methods. In this case it makes sense to deny the client all access to the internal data structure of the diary. Only the public methods are directly visible to the client.

    Encapsulation also ensures that the internal implementation of the class can be changed at will, provided that the public interface stays intact. The client does not have to know or care whether the internal data structure is based on lists, dictionaries, or something completely different.

    class Diary:\n    def __init__(self, owner: str):\n        self.__owner = owner\n        self.__entries = []\n\n    @property\n    def owner(self):\n        return self.__owner\n\n    @owner.setter\n    def owner(self, owner):\n        if owner != \"\":\n            self.__owner = owner\n        else:\n            raise ValueError(\"The owner may not be an empty string\")\n\n    def add_entry(self, entry: str):\n        self.__entries.append(entry)\n\n    def print_entries(self):\n        print(\"A total of\", len(self.__entries), \"entries\")\n        for entry in self.__entries:\n            print(\"- \" + entry)
    diary = Diary(\"Peter\")\ndiary.add_entry(\"Today I ate porridge\")\ndiary.add_entry(\"Today I learned object oriented programming\")\ndiary.add_entry(\"Today I went to bed early\")\ndiary.print_entries()

    A total of 3 entries

      \n
    • Today I ate porridge
    • \n
    • Today I learned object oriented programming
    • \n
    • Today I went to bed early
    • \n

    Please create a class named WeatherStation which is used to store observations about the weather. The class should have the following public attributes:

      \n
    • a constructor which takes the name of the station as its argument
    • \n
    • a method named add_observation(observation: str) which adds an observation as the last entry in a list
    • \n
    • a method named latest_observation() which returns the latest observation added to the list. If there are no observations yet, the method should return an empty string.
    • \n
    • a method named number_of_observations() which returns the total number of observations added
    • \n
    • a __str__ method which returns the name of the station and the total number of observations added as per the example below.
    • \n

    All attributes should be encapsulated, so they can't be directly accessed. It is up to you how you implement the class, as long as the public interface is exactly as described above.

    An example of how the class is used:

    station = WeatherStation(\"Houston\")\nstation.add_observation(\"Rain 10mm\")\nstation.add_observation(\"Sunny\")\nprint(station.latest_observation())\n\nstation.add_observation(\"Thunderstorm\")\nprint(station.latest_observation())\n\nprint(station.number_of_observations())\nprint(station)

    Sunny\nThunderstorm\n3\nHouston, 3 observations

    ","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-9/3-encapsulation.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know what encapsulation means"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to create private attributes"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how to create getters and setters for your attributes"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In object oriented programming the term "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"client"}]},{"type":"text","value":" refers to a program which uses a class, or instances of a class. A class offers the client "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"services"}]},{"type":"text","value":" through which the client can access the objects created based on the class. The goals here are that"}]},{"type":"element","tagName":"ol","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the use of a class and/or objects is as simple as possible from the client's point of view"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"integrity"}]},{"type":"text","value":" of any object is preserved at all times"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The integrity of an object means that the "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"state"}]},{"type":"text","value":" of an object always remains acceptable. In practice this means that the values of the object's attributes are always acceptable. For example, an object representing a date should never have 13 as the value of the month, an object modelling a student should never have a negative number as the value of study credits attained, and so forth."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's take a look at a class named Student:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" student_number"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"student_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" student_number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"study_credits "},{"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":"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":"add_credits"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" study_credits"},{"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":"if"}]},{"type":"text","value":" study_credits "},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"study_credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" study_credits"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" object offers its clients the method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_credits"}]},{"type":"text","value":", which allows the client to add a specified number of credits to the student's total. The method ensures the value passed as the argument is above zero. The following code adds study credits on three occasions:"}]},{"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":"sally "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sally Student\""}]},{"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":"\"12345\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsally"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_credits"},{"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":"\nsally"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_credits"},{"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":"\nsally"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_credits"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"\"Study credits:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sally"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"study_credits"},{"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":"Study credits: 20"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Despite the method definition it is still possible to access the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"study_credits"}]},{"type":"text","value":" attribute directly. This could result in an erroneous state where the integrity of the object is lost:"}]},{"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":"sally "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sally Student\""}]},{"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":"\"12345\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsally"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"study_credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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":"100"}]},{"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":"\"Study credits:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" sally"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"study_credits"},{"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":"Study credits: -100"}]}]},{"type":"element","tagName":"h2","properties":{"id":"encapsulation","style":"position:relative;"},"children":[{"type":"text","value":"Encapsulation"},{"type":"element","tagName":"a","properties":{"href":"#encapsulation","ariaLabel":"encapsulation 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":"A common feature in object oriented programming languages is that classes can usually hide their attributes from any prospective clients. Hidden attributes are usually called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"private"}]},{"type":"text","value":". In Python this privacy is achieved by adding two underscores "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__"}]},{"type":"text","value":" to the beginning of the attribute name:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"CreditCard"}]},{"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":"# the attribute number is private, while the attribute name is accessible"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A private attribute is not directly visible to the client. Trying to refer to it causes an error. In the above example the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":" attribute can be easily accessed and 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":"card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CreditCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Randy Riches\""}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"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":"\"Charlie Churchmouse\""}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"Randy Riches\nCharlie Churchmouse"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Trying to print out the card number, however, causes and error:"}]},{"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":"card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CreditCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123456\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Randy Riches\""}]},{"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":"card"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"AttributeError: 'CreditCard' object has no attribute '__number'"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Hiding attributes from clients is called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"encapsulation"}]},{"type":"text","value":". As the name implies, the attribute is \"enclosed in a capsule\". The client is then offered a suitable interface for accessing and processing the data stored in the object."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's add another encapsulated attribute: the balance on the credit card. This time we'll also add publicly visible methods which allow the client to access and change the balance:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"CreditCard"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","builtin"]},"children":[{"type":"text","value":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\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":"deposit_money"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" amount"},{"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":"float"}]},{"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":"if"}]},{"type":"text","value":" amount "},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" amount\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":"withdraw_money"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" amount"},{"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":"float"}]},{"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":"if"}]},{"type":"text","value":" amount "},{"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":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" amount "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"<="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-="}]},{"type":"text","value":" amount\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":"retrieve_balance"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance"}]}]}]},{"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":"card "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CreditCard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"123456\""}]},{"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":"\"Randy Riches\""}]},{"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":"5000"}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"retrieve_balance"},{"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":"\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"deposit_money"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"retrieve_balance"},{"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":"\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"withdraw_money"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"500"}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"retrieve_balance"},{"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"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# The following will not work because the balance is not sufficient"}]},{"type":"text","value":"\ncard"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"withdraw_money"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10000"}]},{"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":"card"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"retrieve_balance"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5000\n5100\n4600\n4600"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The balance cannot be changed directly because the attribute is private, but we've included the methods "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"deposit_money"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"withdraw_money"}]},{"type":"text","value":" for changing the value. The method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"retrieve_balance"}]},{"type":"text","value":" returns the value stored in balance. The methods include some rudimentary checks for retaining the integrity of the object: for instance, the card cannot be overdrawn."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Car","tmcname":"part09-09_car"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Car"}]},{"type":"text","value":" which has two private, "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"encapsulated"}]},{"type":"text","value":" variables: the amount of petrol in the tank (0 to 60 litres) and odometer reading (in kilometres). The car consumes one litre of petrol per kilometre."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The class should also contain the following methods:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"fill_up()"}]},{"type":"text","value":" which fills up the tank"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"drive(km:int)"}]},{"type":"text","value":" which drives the car for the distance indicated, or for however long the petrol in the tank allows"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" which returns a string representation of the car as per the examples below"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how the class is used:"}]},{"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":"car "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Car"},{"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":"car"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill_up"},{"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":"car"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"drive"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"20"}]},{"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":"car"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"drive"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"50"}]},{"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":"car"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"drive"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"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":"car"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill_up"},{"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":"\ncar"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"fill_up"},{"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":"car"},{"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":"Car: odometer reading 0 km, petrol remaining 0 litres\nCar: odometer reading 0 km, petrol remaining 60 litres\nCar: odometer reading 20 km, petrol remaining 40 litres\nCar: odometer reading 60 km, petrol remaining 0 litres\nCar: odometer reading 60 km, petrol remaining 0 litres\nCar: odometer reading 60 km, petrol remaining 60 litres"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" you are asked to encapsulate the amount of petrol left and the odometer reading. It should not be possible to access them directly from outside the class's own methods."}]}]},{"type":"element","tagName":"h2","properties":{"id":"a-brief-note-on-private-attributes-python-and-object-oriented-programming","style":"position:relative;"},"children":[{"type":"text","value":"A brief note on private attributes, Python and object oriented programming"},{"type":"element","tagName":"a","properties":{"href":"#a-brief-note-on-private-attributes-python-and-object-oriented-programming","ariaLabel":"a brief note on private attributes python and object oriented programming 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":"There are ways around the underscore "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__"}]},{"type":"text","value":" notation for hiding attributes, which you may come across if you search for resources online. No Python attribute is truly private, and this is intentional on the part of the creators of Python. On the other hand, a Python programmer is generally expected to respect the visibility guidelines set in classes, and it takes a special effort to get around these. In other object oriented programming languages, such as Java, private variables are often truly hidden, and it is best if you think of private Python variables as such as well."}]},{"type":"element","tagName":"h2","properties":{"id":"getters-and-setters","style":"position:relative;"},"children":[{"type":"text","value":"Getters and setters"},{"type":"element","tagName":"a","properties":{"href":"#getters-and-setters","ariaLabel":"getters and setters 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 object oriented programming, methods which are dedicated to accessing and changing attributes are usually called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"getters"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"setters"}]},{"type":"text","value":". Not all Python programmers use the terms \"getter\" and \"setter\", but the concept of "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"properties"}]},{"type":"text","value":" outlined below is very similar, which is why we will use the generally accepted object oriented programming terminology here."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, above we created some public methods for accessing private attributes, but there is a more straightforward, \"pythonic\" way of accessing attributes. Let's have a look at a simple class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Wallet"}]},{"type":"text","value":" with a single, private attribute "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"money"}]},{"type":"text","value":":"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Wallet"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__money "},{"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":"p","properties":{},"children":[{"type":"text","value":"We can add getter and setter methods for accessing the private attribute using the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":" decorator:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Wallet"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__money "},{"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":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# A getter method"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"money"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__money\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# A setter method"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@money"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"money"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" money"},{"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":"if"}]},{"type":"text","value":" money "},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__money "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" money"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"First, we define a getter method, which returns the amount of money currently in the wallet. Then we define a setter method, which sets a new value for the money attribute while making sure the new value is not negative."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The new methods can be used like so:"}]},{"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":"wallet "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Wallet"},{"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":"wallet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"money"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nwallet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"money "},{"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":"50"}]},{"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":"wallet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"money"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nwallet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"money "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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":"30"}]},{"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":"wallet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"money"},{"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\n50\n50"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As far as the client is concerned, using these new methods is no different from directly accessing an attribute. Parentheses are not necessary; instead it is perfectly acceptable to state "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"wallet.money = 50"}]},{"type":"text","value":", as if we were simply assigning a value to a variable. Indeed, the purpose was to hide (i.e. encapsulate) the internal implementation of the attribute while offering an easy way of accessing and modifying the data stored in the object."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The previous example has a small problem: the client is not notified of the failure to set a negative value for the money attribute. When a value supplied is clearly wrong, it is usually a good idea to raise an exception and thus let the client know. In this case the exception should probably be of type "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":" to signify that the value supplied was unacceptable."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Here we have an improved version of the class, along with some code for testing it:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Wallet"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__money "},{"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":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# A getter method"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"money"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__money\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# A setter method"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@money"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"money"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" money"},{"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":"if"}]},{"type":"text","value":" money "},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__money "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" money\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 amount must not be below zero\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"wallet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"money "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"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":"30"}]},{"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":"wallet"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"money"},{"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":"ValueError: The amount must not be below zero"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"element","tagName":"strong","properties":{},"children":[{"type":"text","value":"NB:"}]},{"type":"text","value":" the getter method, i.e. the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":" decorator, must be introduced before the setter method, or there will be an error when the class is executed. This is because the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":" decorator defines the name of the \"attribute\" offerred to the client. The setter method, added with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":".setter"}]},{"type":"text","value":", simply adds a new functionality to it."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Recording","tmcname":"part09-10_recording"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Recording"}]},{"type":"text","value":" which models a single recording. The class should have one private variable: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__length"}]},{"type":"text","value":" of type integer."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please implement the following:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a constructor which takes the length as an argument"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a getter method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"length"}]},{"type":"text","value":" which returns the length of the recording"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a setter method which sets the length of the recording"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It should be possible to make use of the class as follows:"}]},{"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":"the_wall "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Recording"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"43"}]},{"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":"the_wall"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nthe_wall"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length "},{"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":"44"}]},{"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":"the_wall"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length"},{"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":"43\n44"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If the argument for either the constructor or the setter method is below zero, this should raise a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ValueError"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you need a refresher on raising exceptions, please see "},{"type":"element","tagName":"a","properties":{"href":"/part-6/3-errors#raising-exceptions"},"children":[{"type":"text","value":"part 6"}]},{"type":"text","value":" of the course materials."}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following example has a class with two private attributes, along with getters and setters for both. Please try the program out with different values passed as arguments:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Player"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player_number"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__player_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" player_number\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"name"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"name"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"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":"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 name may not be an empty string\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"player_number"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__player_number\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@player_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"player_number"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" player_number"},{"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":"if"}]},{"type":"text","value":" player_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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__player_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" player_number\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 player number must be a positive integer\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"player "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Betty Ballmer\""}]},{"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":"10"}]},{"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":"player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"player_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nplayer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"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":"\"Buster Ballmer\""}]},{"type":"text","value":"\nplayer"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"player_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":"11"}]},{"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":"player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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":"player"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"player_number"},{"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":"Betty Ballmer\n10\nBuster Ballmer\n11"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"To finish off this section, let's take a look at a class which models a simple diary. All attributes are private, but they are handled through differing interfaces: the owner of the diary has getter and setter methods, but the diary entries are processed with \"traditional\" methods. In this case it makes sense to deny the client all access to the internal data structure of the diary. Only the public methods are directly visible to the client."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Encapsulation also ensures that the internal implementation of the class can be changed at will, provided that the public interface stays intact. The client does not have to know or care whether the internal data structure is based on lists, dictionaries, or something completely 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":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Diary"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" owner"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__owner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" owner\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__entries "},{"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","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"owner"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__owner\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@owner"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"owner"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" owner"},{"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":"if"}]},{"type":"text","value":" owner "},{"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":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__owner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" owner\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 owner may not be an empty string\""}]},{"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":"add_entry"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" entry"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__entries"},{"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":"entry"},{"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":"print_entries"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"\"A total of\""}]},{"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":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__entries"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"entries\""}]},{"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":" entry "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__entries"},{"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":"\"- \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" entry"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"diary "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Diary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndiary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_entry"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Today I ate porridge\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndiary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_entry"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Today I learned object oriented programming\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndiary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_entry"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Today I went to bed early\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ndiary"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_entries"},{"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":"A total of 3 entries"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Today I ate porridge"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Today I learned object oriented programming"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Today I went to bed early"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Weather station","tmcname":"part09-11_weather_station"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"WeatherStation"}]},{"type":"text","value":" which is used to store observations about the weather. The class should have the following public attributes:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a constructor which takes the name of the station as its argument"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_observation(observation: str)"}]},{"type":"text","value":" which adds an observation as the last entry in a list"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"latest_observation()"}]},{"type":"text","value":" which returns the latest observation added to the list. If there are no observations yet, the method should return an "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"empty string"}]},{"type":"text","value":"."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"number_of_observations()"}]},{"type":"text","value":" which returns the total number of observations added"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method which returns the name of the station and the total number of observations added as per the example below."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"All attributes should be encapsulated, so they can't be directly accessed. It is up to you how you implement the class, as long as the public interface is exactly as described above."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An example of how the class is used:"}]},{"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":"station "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" WeatherStation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Houston\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_observation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Rain 10mm\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_observation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sunny\""}]},{"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":"station"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"latest_observation"},{"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\nstation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_observation"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Thunderstorm\""}]},{"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":"station"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"latest_observation"},{"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\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":"station"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"number_of_observations"},{"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"},{"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":"station"},{"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":"Sunny\nThunderstorm\n3\nHouston, 3 observations"}]}]}]}]},"html":"

    After this section

      \n
    • You will know what encapsulation means
    • \n
    • You will be able to create private attributes
    • \n
    • You will know how to create getters and setters for your attributes
    • \n

    In object oriented programming the term client refers to a program which uses a class, or instances of a class. A class offers the client services through which the client can access the objects created based on the class. The goals here are that

      \n
    1. the use of a class and/or objects is as simple as possible from the client's point of view
    2. \n
    3. the integrity of any object is preserved at all times
    4. \n

    The integrity of an object means that the state of an object always remains acceptable. In practice this means that the values of the object's attributes are always acceptable. For example, an object representing a date should never have 13 as the value of the month, an object modelling a student should never have a negative number as the value of study credits attained, and so forth.

    Let's take a look at a class named Student:

    class Student:\n    def __init__(self, name: str, student_number: str):\n        self.name = name\n        self.student_number = student_number\n        self.study_credits = 0\n\n    def add_credits(self, study_credits):\n        if study_credits > 0:\n            self.study_credits += study_credits

    The Student object offers its clients the method add_credits, which allows the client to add a specified number of credits to the student's total. The method ensures the value passed as the argument is above zero. The following code adds study credits on three occasions:

    sally = Student(\"Sally Student\", \"12345\")\nsally.add_credits(5)\nsally.add_credits(5)\nsally.add_credits(10)\nprint(\"Study credits:\", sally.study_credits)

    Study credits: 20

    Despite the method definition it is still possible to access the study_credits attribute directly. This could result in an erroneous state where the integrity of the object is lost:

    sally = Student(\"Sally Student\", \"12345\")\nsally.study_credits = -100\nprint(\"Study credits:\", sally.study_credits)

    Study credits: -100

    Encapsulation

    A common feature in object oriented programming languages is that classes can usually hide their attributes from any prospective clients. Hidden attributes are usually called private. In Python this privacy is achieved by adding two underscores __ to the beginning of the attribute name:

    class CreditCard:\n    # the attribute number is private, while the attribute name is accessible\n    def __init__(self, number: str, name: str):\n        self.__number = number\n        self.name = name

    A private attribute is not directly visible to the client. Trying to refer to it causes an error. In the above example the name attribute can be easily accessed and changed:

    card = CreditCard(\"123456\",\"Randy Riches\")\nprint(card.name)\ncard.name = \"Charlie Churchmouse\"\nprint(card.name)

    Randy Riches\nCharlie Churchmouse

    Trying to print out the card number, however, causes and error:

    card = CreditCard(\"123456\",\"Randy Riches\")\nprint(card.__number)

    AttributeError: 'CreditCard' object has no attribute '__number'

    Hiding attributes from clients is called encapsulation. As the name implies, the attribute is \"enclosed in a capsule\". The client is then offered a suitable interface for accessing and processing the data stored in the object.

    Let's add another encapsulated attribute: the balance on the credit card. This time we'll also add publicly visible methods which allow the client to access and change the balance:

    class CreditCard:\n    def __init__(self, number: str, name: str, balance: float):\n        self.__number = number\n        self.name = name\n        self.__balance = balance\n\n    def deposit_money(self, amount: float):\n        if amount > 0:\n            self.__balance += amount\n\n    def withdraw_money(self, amount: float):\n        if amount > 0 and amount <= self.__balance:\n            self.__balance -= amount\n\n    def retrieve_balance(self):\n        return self.__balance
    card = CreditCard(\"123456\", \"Randy Riches\", 5000)\nprint(card.retrieve_balance())\ncard.deposit_money(100)\nprint(card.retrieve_balance())\ncard.withdraw_money(500)\nprint(card.retrieve_balance())\n# The following will not work because the balance is not sufficient\ncard.withdraw_money(10000)\nprint(card.retrieve_balance())

    5000\n5100\n4600\n4600

    The balance cannot be changed directly because the attribute is private, but we've included the methods deposit_money and withdraw_money for changing the value. The method retrieve_balance returns the value stored in balance. The methods include some rudimentary checks for retaining the integrity of the object: for instance, the card cannot be overdrawn.

    Please implement a class named Car which has two private, encapsulated variables: the amount of petrol in the tank (0 to 60 litres) and odometer reading (in kilometres). The car consumes one litre of petrol per kilometre.

    The class should also contain the following methods:

      \n
    • fill_up() which fills up the tank
    • \n
    • drive(km:int) which drives the car for the distance indicated, or for however long the petrol in the tank allows
    • \n
    • __str__ which returns a string representation of the car as per the examples below
    • \n

    An example of how the class is used:

    car = Car()\nprint(car)\ncar.fill_up()\nprint(car)\ncar.drive(20)\nprint(car)\ncar.drive(50)\nprint(car)\ncar.drive(10)\nprint(car)\ncar.fill_up()\ncar.fill_up()\nprint(car)

    Car: odometer reading 0 km, petrol remaining 0 litres\nCar: odometer reading 0 km, petrol remaining 60 litres\nCar: odometer reading 20 km, petrol remaining 40 litres\nCar: odometer reading 60 km, petrol remaining 0 litres\nCar: odometer reading 60 km, petrol remaining 0 litres\nCar: odometer reading 60 km, petrol remaining 60 litres

    NB: you are asked to encapsulate the amount of petrol left and the odometer reading. It should not be possible to access them directly from outside the class's own methods.

    A brief note on private attributes, Python and object oriented programming

    There are ways around the underscore __ notation for hiding attributes, which you may come across if you search for resources online. No Python attribute is truly private, and this is intentional on the part of the creators of Python. On the other hand, a Python programmer is generally expected to respect the visibility guidelines set in classes, and it takes a special effort to get around these. In other object oriented programming languages, such as Java, private variables are often truly hidden, and it is best if you think of private Python variables as such as well.

    Getters and setters

    In object oriented programming, methods which are dedicated to accessing and changing attributes are usually called getters and setters. Not all Python programmers use the terms \"getter\" and \"setter\", but the concept of properties outlined below is very similar, which is why we will use the generally accepted object oriented programming terminology here.

    So, above we created some public methods for accessing private attributes, but there is a more straightforward, \"pythonic\" way of accessing attributes. Let's have a look at a simple class named Wallet with a single, private attribute money:

    class Wallet:\n    def __init__(self):\n        self.__money = 0

    We can add getter and setter methods for accessing the private attribute using the @property decorator:

    class Wallet:\n    def __init__(self):\n        self.__money = 0\n\n    # A getter method\n    @property\n    def money(self):\n        return self.__money\n\n    # A setter method\n    @money.setter\n    def money(self, money):\n        if money >= 0:\n            self.__money = money

    First, we define a getter method, which returns the amount of money currently in the wallet. Then we define a setter method, which sets a new value for the money attribute while making sure the new value is not negative.

    The new methods can be used like so:

    wallet = Wallet()\nprint(wallet.money)\n\nwallet.money = 50\nprint(wallet.money)\n\nwallet.money = -30\nprint(wallet.money)

    0\n50\n50

    As far as the client is concerned, using these new methods is no different from directly accessing an attribute. Parentheses are not necessary; instead it is perfectly acceptable to state wallet.money = 50, as if we were simply assigning a value to a variable. Indeed, the purpose was to hide (i.e. encapsulate) the internal implementation of the attribute while offering an easy way of accessing and modifying the data stored in the object.

    The previous example has a small problem: the client is not notified of the failure to set a negative value for the money attribute. When a value supplied is clearly wrong, it is usually a good idea to raise an exception and thus let the client know. In this case the exception should probably be of type ValueError to signify that the value supplied was unacceptable.

    Here we have an improved version of the class, along with some code for testing it:

    class Wallet:\n    def __init__(self):\n        self.__money = 0\n\n    # A getter method\n    @property\n    def money(self):\n        return self.__money\n\n    # A setter method\n    @money.setter\n    def money(self, money):\n        if money >= 0:\n            self.__money = money\n        else:\n            raise ValueError(\"The amount must not be below zero\")
    wallet.money = -30\nprint(wallet.money)

    ValueError: The amount must not be below zero

    NB: the getter method, i.e. the @property decorator, must be introduced before the setter method, or there will be an error when the class is executed. This is because the @property decorator defines the name of the \"attribute\" offerred to the client. The setter method, added with .setter, simply adds a new functionality to it.

    Please create a class named Recording which models a single recording. The class should have one private variable: __length of type integer.

    Please implement the following:

      \n
    • a constructor which takes the length as an argument
    • \n
    • a getter method length which returns the length of the recording
    • \n
    • a setter method which sets the length of the recording
    • \n

    It should be possible to make use of the class as follows:

    the_wall = Recording(43)\nprint(the_wall.length)\nthe_wall.length = 44\nprint(the_wall.length)

    43\n44

    If the argument for either the constructor or the setter method is below zero, this should raise a ValueError.

    If you need a refresher on raising exceptions, please see part 6 of the course materials.

    The following example has a class with two private attributes, along with getters and setters for both. Please try the program out with different values passed as arguments:

    class Player:\n    def __init__(self, name: str, player_number: int):\n        self.__name = name\n        self.__player_number = player_number\n\n    @property\n    def name(self):\n        return self.__name\n\n    @name.setter\n    def name(self, name: str):\n        if name != \"\":\n            self.__name = name\n        else:\n            raise ValueError(\"The name may not be an empty string\")\n\n    @property\n    def player_number(self):\n        return self.__player_number\n\n    @player_number.setter\n    def player_number(self, player_number: int):\n        if player_number > 0:\n            self.__player_number = player_number\n        else:\n            raise ValueError(\"The player number must be a positive integer\")
    player = Player(\"Betty Ballmer\", 10)\nprint(player.name)\nprint(player.player_number)\n\nplayer.name = \"Buster Ballmer\"\nplayer.player_number = 11\nprint(player.name)\nprint(player.player_number)

    Betty Ballmer\n10\nBuster Ballmer\n11

    To finish off this section, let's take a look at a class which models a simple diary. All attributes are private, but they are handled through differing interfaces: the owner of the diary has getter and setter methods, but the diary entries are processed with \"traditional\" methods. In this case it makes sense to deny the client all access to the internal data structure of the diary. Only the public methods are directly visible to the client.

    Encapsulation also ensures that the internal implementation of the class can be changed at will, provided that the public interface stays intact. The client does not have to know or care whether the internal data structure is based on lists, dictionaries, or something completely different.

    class Diary:\n    def __init__(self, owner: str):\n        self.__owner = owner\n        self.__entries = []\n\n    @property\n    def owner(self):\n        return self.__owner\n\n    @owner.setter\n    def owner(self, owner):\n        if owner != \"\":\n            self.__owner = owner\n        else:\n            raise ValueError(\"The owner may not be an empty string\")\n\n    def add_entry(self, entry: str):\n        self.__entries.append(entry)\n\n    def print_entries(self):\n        print(\"A total of\", len(self.__entries), \"entries\")\n        for entry in self.__entries:\n            print(\"- \" + entry)
    diary = Diary(\"Peter\")\ndiary.add_entry(\"Today I ate porridge\")\ndiary.add_entry(\"Today I learned object oriented programming\")\ndiary.add_entry(\"Today I went to bed early\")\ndiary.print_entries()

    A total of 3 entries

      \n
    • Today I ate porridge
    • \n
    • Today I learned object oriented programming
    • \n
    • Today I went to bed early
    • \n

    Please create a class named WeatherStation which is used to store observations about the weather. The class should have the following public attributes:

      \n
    • a constructor which takes the name of the station as its argument
    • \n
    • a method named add_observation(observation: str) which adds an observation as the last entry in a list
    • \n
    • a method named latest_observation() which returns the latest observation added to the list. If there are no observations yet, the method should return an empty string.
    • \n
    • a method named number_of_observations() which returns the total number of observations added
    • \n
    • a __str__ method which returns the name of the station and the total number of observations added as per the example below.
    • \n

    All attributes should be encapsulated, so they can't be directly accessed. It is up to you how you implement the class, as long as the public interface is exactly as described above.

    An example of how the class is used:

    station = WeatherStation(\"Houston\")\nstation.add_observation(\"Rain 10mm\")\nstation.add_observation(\"Sunny\")\nprint(station.latest_observation())\n\nstation.add_observation(\"Thunderstorm\")\nprint(station.latest_observation())\n\nprint(station.number_of_observations())\nprint(station)

    Sunny\nThunderstorm\n3\nHouston, 3 observations

    ","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-9/3-encapsulation.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-9/4-scope-of-methods/page-data.json b/page-data/part-9/4-scope-of-methods/page-data.json index efd5ef3ca..686c45432 100644 --- a/page-data/part-9/4-scope-of-methods/page-data.json +++ b/page-data/part-9/4-scope-of-methods/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-9/4-scope-of-methods", - "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 know how you can limit the visibility of a method in Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to write private methods"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The methods defined within a class can be hidden in exactly the same way as the attributes were in the previous section. If the method begins with two underscores "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__"}]},{"type":"text","value":", it is not directly accessible by the client."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, the technique is the same for both methods and attributes, but the use cases are usually a little different. Private attributes often come paired with getter and setter methods for controlling access to them. Private methods, on the other hand, are usually intended for internal use only, as helper methods for processes which the client does not need to know about."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A private method can be used within the class just like any other method, of course remembering to include the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" prefix. The following is a simple class representing the recipient of email letters. It includes a private helper method for checking the email address is in a valid format:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Recipient"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__check_email"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"email"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" email\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 email address is not valid\""}]},{"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":"__check_email"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"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":"# A simple check: the address must be over 5 characters long "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# and contain a dot and an @ character"}]},{"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","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"email"},{"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":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" email "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"@\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" email"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Attempting to call the private method directly causes an error:"}]},{"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":"peter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Recipient"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Emailer\""}]},{"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":"\"peter@example.com\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__check_email"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"someone@example.com\""}]},{"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":"AttributeError: 'Recipient' object has no attribute '__check_email'"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Within the class the method can be accessed normally, and it makes sense to use it also for setting a new value for the address. Let's add getter and setter methods for the email address:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Recipient"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__check_email"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"email"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" email\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 email address is not valid\""}]},{"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":"__check_email"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"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":"# A simple check: the address must be over 5 characters long "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# and contain a dot and an @ character"}]},{"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","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"email"},{"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":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" email "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"@\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" email\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"email"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__email\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@email"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"email"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"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":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__check_email"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"email"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" email\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 email address is not valid\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"info","name":"Python scope and namespace"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We already came across the term "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"scope"}]},{"type":"text","value":" in "},{"type":"element","tagName":"a","properties":{"href":"/part-6/4-scope-of-variables"},"children":[{"type":"text","value":"part 6"}]},{"type":"text","value":" of this material, and defined it as the sections of a program where a (variable) name is visible. Looking at the term from another direction, it also refers to what is visible from a specific point in program code. Another related term is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"namespace"}]},{"type":"text","value":", which refers to the names specifically available within a defined Python unit, such as a class or a function definition."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The scope within a method is different from the scope within a class, which is again different from the scope at the client code which creates an instance of the class. A method has access to its local variables, but also to the attributes and other methods in the class it is a part of, even if they are private. The class also has access to these, its own members, but it cannot directly access the local variables within its methods. The client code has access to only the public methods and attributes defined in the class, but of course also some other names in the environment in which it exists."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It may seem counter-intuitive that a class would not have access to all its contents, but it is essential for ensuring integrity. For example, it might make sense to use the same local variable name in various different methods within the same class, if they perform somehow similar functionalities. If the class had direct access to all of the local variables within the methods, they would have to be named differently, or else it would not be clear which version of the variable was meant where. We have already seen with attributes declared with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" that helper variables should not be made accessible outside a method, so adding the variables as attributes or global variables should not be an option. There has to be a way to keep names in different parts of the program separate, and this is what namespaces are for."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The idea of a namespace helps with understanding how the same name can happily coexist in different functions, classes or modules at the same time. If a name is specific to a namespace, such as a method definition, it is not directly accessible outside it, and so there is no reason why another namespace could not use the same name. Mastering namespaces and scopes is essential in becoming a proficient programmer, and you will get much practice on this course."}]}]},{"type":"element","tagName":"h2","properties":{"id":"do-i-need-a-private-method","style":"position:relative;"},"children":[{"type":"text","value":"Do I need a private method?"},{"type":"element","tagName":"a","properties":{"href":"#do-i-need-a-private-method","ariaLabel":"do i need a private method 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 the following example the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"DeckOfCards"}]},{"type":"text","value":" is a model for a deck of 52 cards. It contains the helper method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__reset_deck"}]},{"type":"text","value":", which creates a new shuffled deck of cards. The private method is at the moment only called in the constructor method, so the implementation could arguably be placed directly in the constructor. However, using a separate method makes the code easier to read and also makes it possible to access the functionality later in other methods if necessary."}]},{"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":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" shuffle\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"DeckOfCards"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__reset_deck"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__reset_deck"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__deck "},{"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":"# Add all 52 cards to the deck"}]},{"type":"text","value":"\n suits "},{"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":"\"spades\""}]},{"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":"\"hearts\""}]},{"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":"\"clubs\""}]},{"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":"\"diamonds\""}]},{"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":" suit "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" suits"},{"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":" "},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__deck"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"suit"},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Shuffle the deck"}]},{"type":"text","value":"\n shuffle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__deck"},{"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":"deal"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" number_of_cards"},{"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 hand "},{"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":"# Move the top cards in the deck to the hand"}]},{"type":"text","value":"\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":"number_of_cards"},{"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 hand"},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__deck"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" hand"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's test the class:"}]},{"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":"deck "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" DeckOfCards"},{"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":"\nhand1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" deck"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"deal"},{"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"},{"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":"hand1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhand2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" deck"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"deal"},{"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"},{"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":"hand2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As the hands are randomly generated, the following is only an example of what could be printed out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[('spades', 7), ('spades', 11), ('hearts', 7), ('diamonds', 3), ('spades', 4)]\n[('clubs', 8), ('spades', 12), ('diamonds', 13), ('clubs', 11), ('spades', 10)]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Private methods are generally less common than private attributes. As a rule of thumb, a method should be hidden whenever the client has no need to directly access it. This is especially the case when it is possible that the client could adversely affect the integrity of the object by calling the method."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Service charge","tmcname":"part09-12_service_charge"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" which models a bank account. The class should contain"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a constructor which takes the name of the owner (str), account number (str) and balance (float) as arguments"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"deposit(amount: float)"}]},{"type":"text","value":" for depositing money to the account"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"withdraw(amount: float)"}]},{"type":"text","value":" for withdrawing money from the account"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a getter method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balance"}]},{"type":"text","value":" which returns the balance of the account"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The class should also contain the private method"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__service_charge()"}]},{"type":"text","value":", which decreases the balance on the account by one percent. Whenever either of the methods "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"deposit"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"withdraw"}]},{"type":"text","value":" is called, this method should also be called. The service charge is calculated and subtracted only after the actual operation is completed (that is, after the amount specified has been added to or subtracted from the balance)."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"All data attributes within the class definition should be private."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may use the following code for testing your class:"}]},{"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":"account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Randy Riches\""}]},{"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":"\"12345-6789\""}]},{"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":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\naccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"withdraw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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":"account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\naccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"deposit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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":"account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"891.0\n981.09"}]}]}]}]},"html":"

    After this section

      \n
    • You will know how you can limit the visibility of a method in Python
    • \n
    • You will be able to write private methods
    • \n

    The methods defined within a class can be hidden in exactly the same way as the attributes were in the previous section. If the method begins with two underscores __, it is not directly accessible by the client.

    So, the technique is the same for both methods and attributes, but the use cases are usually a little different. Private attributes often come paired with getter and setter methods for controlling access to them. Private methods, on the other hand, are usually intended for internal use only, as helper methods for processes which the client does not need to know about.

    A private method can be used within the class just like any other method, of course remembering to include the self prefix. The following is a simple class representing the recipient of email letters. It includes a private helper method for checking the email address is in a valid format:

    class Recipient:\n    def __init__(self, name: str, email: str):\n        self.__name = name\n        if self.__check_email(email):\n            self.__email = email\n        else:\n            raise ValueError(\"The email address is not valid\")\n\n    def __check_email(self, email: str):\n        # A simple check: the address must be over 5 characters long \n        # and contain a dot and an @ character\n        return len(email) > 5 and \".\" in email and \"@\" in email

    Attempting to call the private method directly causes an error:

    peter = Recipient(\"Peter Emailer\", \"peter@example.com\")\npeter.__check_email(\"someone@example.com\")

    AttributeError: 'Recipient' object has no attribute '__check_email'

    Within the class the method can be accessed normally, and it makes sense to use it also for setting a new value for the address. Let's add getter and setter methods for the email address:

    class Recipient:\n    def __init__(self, name: str, email: str):\n        self.__name = name\n        if self.__check_email(email):\n            self.__email = email\n        else:\n            raise ValueError(\"The email address is not valid\")\n\n    def __check_email(self, email: str):\n        # A simple check: the address must be over 5 characters long \n        # and contain a dot and an @ character\n        return len(email) > 5 and \".\" in email and \"@\" in email\n\n    @property\n    def email(self):\n        return self.__email\n\n    @email.setter\n    def email(self, email: str):\n        if self.__check_email(email):\n            self.__email = email\n        else:\n            raise ValueError(\"The email address is not valid\")

    We already came across the term scope in part 6 of this material, and defined it as the sections of a program where a (variable) name is visible. Looking at the term from another direction, it also refers to what is visible from a specific point in program code. Another related term is namespace, which refers to the names specifically available within a defined Python unit, such as a class or a function definition.

    The scope within a method is different from the scope within a class, which is again different from the scope at the client code which creates an instance of the class. A method has access to its local variables, but also to the attributes and other methods in the class it is a part of, even if they are private. The class also has access to these, its own members, but it cannot directly access the local variables within its methods. The client code has access to only the public methods and attributes defined in the class, but of course also some other names in the environment in which it exists.

    It may seem counter-intuitive that a class would not have access to all its contents, but it is essential for ensuring integrity. For example, it might make sense to use the same local variable name in various different methods within the same class, if they perform somehow similar functionalities. If the class had direct access to all of the local variables within the methods, they would have to be named differently, or else it would not be clear which version of the variable was meant where. We have already seen with attributes declared with self that helper variables should not be made accessible outside a method, so adding the variables as attributes or global variables should not be an option. There has to be a way to keep names in different parts of the program separate, and this is what namespaces are for.

    The idea of a namespace helps with understanding how the same name can happily coexist in different functions, classes or modules at the same time. If a name is specific to a namespace, such as a method definition, it is not directly accessible outside it, and so there is no reason why another namespace could not use the same name. Mastering namespaces and scopes is essential in becoming a proficient programmer, and you will get much practice on this course.

    Do I need a private method?

    In the following example the class DeckOfCards is a model for a deck of 52 cards. It contains the helper method __reset_deck, which creates a new shuffled deck of cards. The private method is at the moment only called in the constructor method, so the implementation could arguably be placed directly in the constructor. However, using a separate method makes the code easier to read and also makes it possible to access the functionality later in other methods if necessary.

    from random import shuffle\n\nclass DeckOfCards:\n    def __init__(self):\n        self.__reset_deck()\n\n    def __reset_deck(self):\n        self.__deck = []\n        # Add all 52 cards to the deck\n        suits = [\"spades\", \"hearts\", \"clubs\", \"diamonds\"]\n        for suit in suits:\n            for number in range(1, 14):\n                self.__deck.append((suit, number))\n        # Shuffle the deck\n        shuffle(self.__deck)\n\n    def deal(self, number_of_cards: int):\n        hand = []\n        # Move the top cards in the deck to the hand\n        for i in range(number_of_cards):\n            hand.append(self.__deck.pop())\n        return hand

    Let's test the class:

    deck = DeckOfCards()\nhand1 = deck.deal(5)\nprint(hand1)\nhand2 = deck.deal(5)\nprint(hand2)

    As the hands are randomly generated, the following is only an example of what could be printed out:

    [('spades', 7), ('spades', 11), ('hearts', 7), ('diamonds', 3), ('spades', 4)]\n[('clubs', 8), ('spades', 12), ('diamonds', 13), ('clubs', 11), ('spades', 10)]

    Private methods are generally less common than private attributes. As a rule of thumb, a method should be hidden whenever the client has no need to directly access it. This is especially the case when it is possible that the client could adversely affect the integrity of the object by calling the method.

    Please create a class named BankAccount which models a bank account. The class should contain

      \n
    • a constructor which takes the name of the owner (str), account number (str) and balance (float) as arguments
    • \n
    • a method deposit(amount: float) for depositing money to the account
    • \n
    • a method withdraw(amount: float) for withdrawing money from the account
    • \n
    • a getter method balance which returns the balance of the account
    • \n

    The class should also contain the private method

      \n
    • __service_charge(), which decreases the balance on the account by one percent. Whenever either of the methods deposit or withdraw is called, this method should also be called. The service charge is calculated and subtracted only after the actual operation is completed (that is, after the amount specified has been added to or subtracted from the balance).
    • \n

    All data attributes within the class definition should be private.

    You may use the following code for testing your class:

    account = BankAccount(\"Randy Riches\", \"12345-6789\", 1000)\naccount.withdraw(100)\nprint(account.balance)\naccount.deposit(100)\nprint(account.balance)\n

    891.0\n981.09

    ","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-9/4-scope-of-methods.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 know how you can limit the visibility of a method in Python"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to write private methods"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The methods defined within a class can be hidden in exactly the same way as the attributes were in the previous section. If the method begins with two underscores "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__"}]},{"type":"text","value":", it is not directly accessible by the client."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, the technique is the same for both methods and attributes, but the use cases are usually a little different. Private attributes often come paired with getter and setter methods for controlling access to them. Private methods, on the other hand, are usually intended for internal use only, as helper methods for processes which the client does not need to know about."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A private method can be used within the class just like any other method, of course remembering to include the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" prefix. The following is a simple class representing the recipient of email letters. It includes a private helper method for checking the email address is in a valid format:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Recipient"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__check_email"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"email"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" email\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 email address is not valid\""}]},{"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":"__check_email"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"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":"# A simple check: the address must be over 5 characters long "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# and contain a dot and an @ character"}]},{"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","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"email"},{"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":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" email "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"@\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" email"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Attempting to call the private method directly causes an error:"}]},{"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":"peter "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Recipient"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Emailer\""}]},{"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":"\"peter@example.com\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeter"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__check_email"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"someone@example.com\""}]},{"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":"AttributeError: 'Recipient' object has no attribute '__check_email'"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Within the class the method can be accessed normally, and it makes sense to use it also for setting a new value for the address. Let's add getter and setter methods for the email address:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Recipient"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__check_email"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"email"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" email\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 email address is not valid\""}]},{"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":"__check_email"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"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":"# A simple check: the address must be over 5 characters long "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# and contain a dot and an @ character"}]},{"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","builtin"]},"children":[{"type":"text","value":"len"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"email"},{"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":"5"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\".\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" email "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"and"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"@\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" email\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"email"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__email\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@email"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"email"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" email"},{"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":"str"}]},{"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":"if"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__check_email"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"email"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__email "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" email\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 email address is not valid\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"text-box","properties":{"variant":"info","name":"Python scope and namespace"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"We already came across the term "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"scope"}]},{"type":"text","value":" in "},{"type":"element","tagName":"a","properties":{"href":"/part-6/4-scope-of-variables"},"children":[{"type":"text","value":"part 6"}]},{"type":"text","value":" of this material, and defined it as the sections of a program where a (variable) name is visible. Looking at the term from another direction, it also refers to what is visible from a specific point in program code. Another related term is "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"namespace"}]},{"type":"text","value":", which refers to the names specifically available within a defined Python unit, such as a class or a function definition."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The scope within a method is different from the scope within a class, which is again different from the scope at the client code which creates an instance of the class. A method has access to its local variables, but also to the attributes and other methods in the class it is a part of, even if they are private. The class also has access to these, its own members, but it cannot directly access the local variables within its methods. The client code has access to only the public methods and attributes defined in the class, but of course also some other names in the environment in which it exists."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It may seem counter-intuitive that a class would not have access to all its contents, but it is essential for ensuring integrity. For example, it might make sense to use the same local variable name in various different methods within the same class, if they perform somehow similar functionalities. If the class had direct access to all of the local variables within the methods, they would have to be named differently, or else it would not be clear which version of the variable was meant where. We have already seen with attributes declared with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" that helper variables should not be made accessible outside a method, so adding the variables as attributes or global variables should not be an option. There has to be a way to keep names in different parts of the program separate, and this is what namespaces are for."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The idea of a namespace helps with understanding how the same name can happily coexist in different functions, classes or modules at the same time. If a name is specific to a namespace, such as a method definition, it is not directly accessible outside it, and so there is no reason why another namespace could not use the same name. Mastering namespaces and scopes is essential in becoming a proficient programmer, and you will get much practice on this course."}]}]},{"type":"element","tagName":"h2","properties":{"id":"do-i-need-a-private-method","style":"position:relative;"},"children":[{"type":"text","value":"Do I need a private method?"},{"type":"element","tagName":"a","properties":{"href":"#do-i-need-a-private-method","ariaLabel":"do i need a private method 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 the following example the class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"DeckOfCards"}]},{"type":"text","value":" is a model for a deck of 52 cards. It contains the helper method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__reset_deck"}]},{"type":"text","value":", which creates a new shuffled deck of cards. The private method is at the moment only called in the constructor method, so the implementation could arguably be placed directly in the constructor. However, using a separate method makes the code easier to read and also makes it possible to access the functionality later in other methods if necessary."}]},{"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":"from"}]},{"type":"text","value":" random "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" shuffle\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"DeckOfCards"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__reset_deck"},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__reset_deck"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__deck "},{"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":"# Add all 52 cards to the deck"}]},{"type":"text","value":"\n suits "},{"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":"\"spades\""}]},{"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":"\"hearts\""}]},{"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":"\"clubs\""}]},{"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":"\"diamonds\""}]},{"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":" suit "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" suits"},{"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":" "},{"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":"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"14"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__deck"},{"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":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"suit"},{"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Shuffle the deck"}]},{"type":"text","value":"\n shuffle"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__deck"},{"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":"deal"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" number_of_cards"},{"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 hand "},{"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":"# Move the top cards in the deck to the hand"}]},{"type":"text","value":"\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":"number_of_cards"},{"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 hand"},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__deck"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"pop"},{"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 "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" hand"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's test the class:"}]},{"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":"deck "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" DeckOfCards"},{"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":"\nhand1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" deck"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"deal"},{"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"},{"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":"hand1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nhand2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" deck"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"deal"},{"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"},{"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":"hand2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As the hands are randomly generated, the following is only an example of what could be printed out:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"[('spades', 7), ('spades', 11), ('hearts', 7), ('diamonds', 3), ('spades', 4)]\n[('clubs', 8), ('spades', 12), ('diamonds', 13), ('clubs', 11), ('spades', 10)]"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Private methods are generally less common than private attributes. As a rule of thumb, a method should be hidden whenever the client has no need to directly access it. This is especially the case when it is possible that the client could adversely affect the integrity of the object by calling the method."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Service charge","tmcname":"part09-12_service_charge"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"BankAccount"}]},{"type":"text","value":" which models a bank account. The class should contain"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a constructor which takes the name of the owner (str), account number (str) and balance (float) as arguments"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"deposit(amount: float)"}]},{"type":"text","value":" for depositing money to the account"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"withdraw(amount: float)"}]},{"type":"text","value":" for withdrawing money from the account"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a getter method "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"balance"}]},{"type":"text","value":" which returns the balance of the account"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The class should also contain the private method"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__service_charge()"}]},{"type":"text","value":", which decreases the balance on the account by one percent. Whenever either of the methods "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"deposit"}]},{"type":"text","value":" or "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"withdraw"}]},{"type":"text","value":" is called, this method should also be called. The service charge is calculated and subtracted only after the actual operation is completed (that is, after the amount specified has been added to or subtracted from the balance)."}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"All data attributes within the class definition should be private."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You may use the following code for testing your class:"}]},{"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":"account "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" BankAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Randy Riches\""}]},{"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":"\"12345-6789\""}]},{"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":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\naccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"withdraw"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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":"account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\naccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"deposit"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"100"}]},{"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":"account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n"}]}]}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"891.0\n981.09"}]}]}]}]},"html":"

    After this section

      \n
    • You will know how you can limit the visibility of a method in Python
    • \n
    • You will be able to write private methods
    • \n

    The methods defined within a class can be hidden in exactly the same way as the attributes were in the previous section. If the method begins with two underscores __, it is not directly accessible by the client.

    So, the technique is the same for both methods and attributes, but the use cases are usually a little different. Private attributes often come paired with getter and setter methods for controlling access to them. Private methods, on the other hand, are usually intended for internal use only, as helper methods for processes which the client does not need to know about.

    A private method can be used within the class just like any other method, of course remembering to include the self prefix. The following is a simple class representing the recipient of email letters. It includes a private helper method for checking the email address is in a valid format:

    class Recipient:\n    def __init__(self, name: str, email: str):\n        self.__name = name\n        if self.__check_email(email):\n            self.__email = email\n        else:\n            raise ValueError(\"The email address is not valid\")\n\n    def __check_email(self, email: str):\n        # A simple check: the address must be over 5 characters long \n        # and contain a dot and an @ character\n        return len(email) > 5 and \".\" in email and \"@\" in email

    Attempting to call the private method directly causes an error:

    peter = Recipient(\"Peter Emailer\", \"peter@example.com\")\npeter.__check_email(\"someone@example.com\")

    AttributeError: 'Recipient' object has no attribute '__check_email'

    Within the class the method can be accessed normally, and it makes sense to use it also for setting a new value for the address. Let's add getter and setter methods for the email address:

    class Recipient:\n    def __init__(self, name: str, email: str):\n        self.__name = name\n        if self.__check_email(email):\n            self.__email = email\n        else:\n            raise ValueError(\"The email address is not valid\")\n\n    def __check_email(self, email: str):\n        # A simple check: the address must be over 5 characters long \n        # and contain a dot and an @ character\n        return len(email) > 5 and \".\" in email and \"@\" in email\n\n    @property\n    def email(self):\n        return self.__email\n\n    @email.setter\n    def email(self, email: str):\n        if self.__check_email(email):\n            self.__email = email\n        else:\n            raise ValueError(\"The email address is not valid\")

    We already came across the term scope in part 6 of this material, and defined it as the sections of a program where a (variable) name is visible. Looking at the term from another direction, it also refers to what is visible from a specific point in program code. Another related term is namespace, which refers to the names specifically available within a defined Python unit, such as a class or a function definition.

    The scope within a method is different from the scope within a class, which is again different from the scope at the client code which creates an instance of the class. A method has access to its local variables, but also to the attributes and other methods in the class it is a part of, even if they are private. The class also has access to these, its own members, but it cannot directly access the local variables within its methods. The client code has access to only the public methods and attributes defined in the class, but of course also some other names in the environment in which it exists.

    It may seem counter-intuitive that a class would not have access to all its contents, but it is essential for ensuring integrity. For example, it might make sense to use the same local variable name in various different methods within the same class, if they perform somehow similar functionalities. If the class had direct access to all of the local variables within the methods, they would have to be named differently, or else it would not be clear which version of the variable was meant where. We have already seen with attributes declared with self that helper variables should not be made accessible outside a method, so adding the variables as attributes or global variables should not be an option. There has to be a way to keep names in different parts of the program separate, and this is what namespaces are for.

    The idea of a namespace helps with understanding how the same name can happily coexist in different functions, classes or modules at the same time. If a name is specific to a namespace, such as a method definition, it is not directly accessible outside it, and so there is no reason why another namespace could not use the same name. Mastering namespaces and scopes is essential in becoming a proficient programmer, and you will get much practice on this course.

    Do I need a private method?

    In the following example the class DeckOfCards is a model for a deck of 52 cards. It contains the helper method __reset_deck, which creates a new shuffled deck of cards. The private method is at the moment only called in the constructor method, so the implementation could arguably be placed directly in the constructor. However, using a separate method makes the code easier to read and also makes it possible to access the functionality later in other methods if necessary.

    from random import shuffle\n\nclass DeckOfCards:\n    def __init__(self):\n        self.__reset_deck()\n\n    def __reset_deck(self):\n        self.__deck = []\n        # Add all 52 cards to the deck\n        suits = [\"spades\", \"hearts\", \"clubs\", \"diamonds\"]\n        for suit in suits:\n            for number in range(1, 14):\n                self.__deck.append((suit, number))\n        # Shuffle the deck\n        shuffle(self.__deck)\n\n    def deal(self, number_of_cards: int):\n        hand = []\n        # Move the top cards in the deck to the hand\n        for i in range(number_of_cards):\n            hand.append(self.__deck.pop())\n        return hand

    Let's test the class:

    deck = DeckOfCards()\nhand1 = deck.deal(5)\nprint(hand1)\nhand2 = deck.deal(5)\nprint(hand2)

    As the hands are randomly generated, the following is only an example of what could be printed out:

    [('spades', 7), ('spades', 11), ('hearts', 7), ('diamonds', 3), ('spades', 4)]\n[('clubs', 8), ('spades', 12), ('diamonds', 13), ('clubs', 11), ('spades', 10)]

    Private methods are generally less common than private attributes. As a rule of thumb, a method should be hidden whenever the client has no need to directly access it. This is especially the case when it is possible that the client could adversely affect the integrity of the object by calling the method.

    Please create a class named BankAccount which models a bank account. The class should contain

      \n
    • a constructor which takes the name of the owner (str), account number (str) and balance (float) as arguments
    • \n
    • a method deposit(amount: float) for depositing money to the account
    • \n
    • a method withdraw(amount: float) for withdrawing money from the account
    • \n
    • a getter method balance which returns the balance of the account
    • \n

    The class should also contain the private method

      \n
    • __service_charge(), which decreases the balance on the account by one percent. Whenever either of the methods deposit or withdraw is called, this method should also be called. The service charge is calculated and subtracted only after the actual operation is completed (that is, after the amount specified has been added to or subtracted from the balance).
    • \n

    All data attributes within the class definition should be private.

    You may use the following code for testing your class:

    account = BankAccount(\"Randy Riches\", \"12345-6789\", 1000)\naccount.withdraw(100)\nprint(account.balance)\naccount.deposit(100)\nprint(account.balance)\n

    891.0\n981.09

    ","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-9/4-scope-of-methods.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-9/5-class-attributes/page-data.json b/page-data/part-9/5-class-attributes/page-data.json index fa464ae88..6ba6752c7 100644 --- a/page-data/part-9/5-class-attributes/page-data.json +++ b/page-data/part-9/5-class-attributes/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-9/5-class-attributes", - "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 the terms class variable and class method"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how static traits are different from the traits of instances"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to add static traits to your own classes"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"traits"}]},{"type":"text","value":" of objects are a central concept in object oriented programming. The term encompasses the methods and variables defined in the class definition. Again, not all Python programmers use the term \"traits\", some preferring "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"attributes"}]},{"type":"text","value":", "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"features"}]},{"type":"text","value":" or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"members"}]},{"type":"text","value":". Traits is quite accepted in the wider object oriented programming world, so that is the term we use here."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Thus far we have dealt mostly with "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"traits of objects"}]},{"type":"text","value":". These include the methods and attributes accessible in any instance of a class. In fact, classes "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"themselves"}]},{"type":"text","value":" can also have traits, which are sometimes called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"static traits"}]},{"type":"text","value":", or more specifically "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"class variables"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"class methods"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"class-variables","style":"position:relative;"},"children":[{"type":"text","value":"Class variables"},{"type":"element","tagName":"a","properties":{"href":"#class-variables","ariaLabel":"class variables 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":"Each instance of a class has its own specific values for each attribute defined in the class, as we've seen in the examples in the previous sections. But what if we wanted to have some data that is shared by the different instances? Enter "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"class variables"}]},{"type":"text","value":", also known as static variables. A class variable is a variable which is accessed through the class itself, not through the instances created based on the class. At any given time during the execution of the program a class variable has one single value, no matter how many instances of the class are created."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A class variable is declared without the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" prefix, and usually outside any method definition as it should be accessible from anywhere within the class, or even from outside the class."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"SavingsAccount"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n general_rate "},{"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.03"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" account_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" interest_rate"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__account_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" account_number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__interest_rate "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" interest_rate\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":"add_interest"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# The total interest rate equals "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the general rate + the interest rate of the account"}]},{"type":"text","value":"\n total_interest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SavingsAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"general_rate "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__interest_rate\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" total_interest\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"balance"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"general_rate"}]},{"type":"text","value":" is defined within the class but outside any method definitions, and it does not use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" prefix, it is a class variable."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A class variable is accessed through the name of the class, for example 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","comment"]},"children":[{"type":"text","value":"# The general rate exists independently of any object instances"}]},{"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 general interest rate is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" SavingsAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"general_rate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\naccount "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SavingsAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345\""}]},{"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":"1000"}]},{"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":"0.05"}]},{"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":"# Add the total interest accrued to the balance on the account"}]},{"type":"text","value":"\naccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_interest"},{"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":"account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"The general interest rate is 0.03\n1080.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, the class variables are accessed through the name of the class, for instance with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SavingsAccount.general_rate"}]},{"type":"text","value":", while instance variables are accessed through the name of the object variable, such as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"account.balance"}]},{"type":"text","value":". An instance variable naturally only exists when an instance of the class has been created, but a class variable is available everywhere and at any point in time where the class itself is available."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Class variables are useful when there is need for values which are shared by all instances of the class. In the example above we assumed the total interest rate of all savings accounts is formed from two components: the general rate of interest is shared by all accounts, but each account also has it's own interest rate in an instance variable. The general rate may also change, but the change will then affect all instances of the class equally."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"SavingsAccount"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n general_rate "},{"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.03"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" account_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" interest_rate"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__account_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" account_number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__interest_rate "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" interest_rate\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":"add_interest"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# The total interest rate equals "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the general rate + the interest rate of the account"}]},{"type":"text","value":"\n total_interest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SavingsAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"general_rate "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__interest_rate\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" total_interest\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"balance"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"total_interest"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__interest_rate "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" SavingsAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"general_rate"}]}]}]},{"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":"account1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SavingsAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345\""}]},{"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":"100"}]},{"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":"0.03"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\naccount2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SavingsAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"54321\""}]},{"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":"200"}]},{"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":"0.06"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"General interest rate:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" SavingsAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"general_rate"},{"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":"account1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"total_interest"},{"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":"account2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"total_interest"},{"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":"# The general rate of interest is now 10 percent"}]},{"type":"text","value":"\nSavingsAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"general_rate "},{"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.10"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"General interest rate:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" SavingsAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"general_rate"},{"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":"account1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"total_interest"},{"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":"account2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"total_interest"},{"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":"General interest rate: 0.03\n0.06\n0.09\nGeneral interest rate: 0.1\n0.13\n0.16"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the general rate of interest changes, the total interest rate for all instances of the class changes. As you can see above, it is possible to add a getter method with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":" decorator even though there isn't an attribute of the same name in the class. This method returns the sum of the general rate of interest and the account specific interest rate."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at another example. The class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneNumber"}]},{"type":"text","value":" is used to define a single phone number, but it also contains some country codes in a dictionary. This dictionary is a class variable, and as such is shared by all the instances of the class, because the country code for phone numbers from a single country is always the same."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneNumber"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n country_codes "},{"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":"\"Finland\""}]},{"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":"\"+358\""}]},{"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":"\"Sweden\""}]},{"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":"\"+46\""}]},{"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":"\"United States\""}]},{"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":"\"+1\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" phone_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" country"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phone_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" phone_number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__country "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" country\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"phone_number"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# When the country code prefix is added "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the initial zero is removed from the phone number"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" PhoneNumber"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"country_codes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__country"},{"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":"\" \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phone_number"},{"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":"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":"paulas_no "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneNumber"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Paula Pythons\""}]},{"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":"\"050 1234 567\""}]},{"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":"\"Finland\""}]},{"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":"paulas_no"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"phone_number"},{"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":"+358 50 1234 567"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each PhoneNumber object contains the name of the owner, the number itself, and the country of the phone number. When the attribute containing the phone number is accessed with the getter method, the appropriate country code is retrieved from the class variable dictionary based on the country attribute, and the result is prefixed to the number."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The example implementation above is not yet very functional otherwise. In the following example we've added getter and setters for all attributes:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneNumber"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n country_codes "},{"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":"\"Finland\""}]},{"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":"\"+358\""}]},{"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":"\"Sweden\""}]},{"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":"\"+46\""}]},{"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":"\"United States\""}]},{"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":"\"+1\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" phone_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" country"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This is a call to the phone_number.setter method"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"phone_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" phone_number\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This is a call to the country.setter method"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"country "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" country\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the getter method for phone_number combines the country code "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# and the attribute phone_number"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"phone_number"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# the initial zero is removed as the country code is prefixed"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" PhoneNumber"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"country_codes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__country"},{"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":"\" \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phone_number"},{"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":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@phone_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"phone_number"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Making sure the number contains only numbers and space characters"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" number"},{"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":"if"}]},{"type":"text","value":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234567890 \""}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"A phone number can only contain numbers and spaces\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phone_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" number\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# a getter for only the number itself without the country code"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"local_number"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phone_number\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"country"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__country\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"country"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" country"},{"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":"# Making sure the country is a key in the dictionary of country codes"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" country "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" PhoneNumber"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"country_codes"},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This country is not on the list.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__country "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" country\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"name"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"name"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"phone_number"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name"},{"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":"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":"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 pn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneNumber"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Pythons\""}]},{"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":"\"040 111 1111\""}]},{"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":"\"Sweden\""}]},{"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":"pn"},{"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":"pn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"phone_number"},{"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":"pn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"local_number"},{"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":"+46 40 111 1111 (Peter Pythons)\n+46 40 111 1111\n040 111 1111"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Postcodes","tmcname":"part09-13_postcodes"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the class definition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"City"}]},{"type":"text","value":" which is a model for a single city."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please add a class variable named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"postcodes"}]},{"type":"text","value":" which refers to a dictionary. The keys of the dictionary are names of cities, and the values attached are the postcodes for those cities. Both are strings."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The dictionary should contain (at least) the following postcodes:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Helsinki 00100"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Turku 20100"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Tampere 33100"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Rovaniemi 96100"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Oulu 90100"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You do not need to implement any other functionality."}]}]},{"type":"element","tagName":"h2","properties":{"id":"class-methods","style":"position:relative;"},"children":[{"type":"text","value":"Class methods"},{"type":"element","tagName":"a","properties":{"href":"#class-methods","ariaLabel":"class methods 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":"A class method, also called a static method, is a method which is not attached to any single instance of the class. A class method can be called without creating any instances of the class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Class methods are usually tools which have something to do with the purpose of the class, but which are detached in the sense that it should not be necessary to create instances of the class in order to be able to call them. Class methods are usually public, so that they can be called both from outside the class and from within the class, including from within instances of the class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A class method is defined with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@classmethod"}]},{"type":"text","value":" annotation. The first parameter is always "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cls"}]},{"type":"text","value":". The variable name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cls"}]},{"type":"text","value":" is similar to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" parameter. The difference is that "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cls"}]},{"type":"text","value":" points to the class while "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" point to an instance of the class. Neither parameter is included in the argument list when the function is called; Python fills in the appropriate value automatically."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example we have a class modelling vehicle registrations. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Registration"}]},{"type":"text","value":" class contains a static method for checking whether a license plate is valid. The method is a static class method because it is useful to be able to check if a license plate is valid even before a single Registration object is created:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Registration"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" owner"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" make"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" year"},{"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":"text","value":" license_plate"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__owner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" owner\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__make "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" make\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__year "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" year\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Call the license_plate.setter method"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"license_plate "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" license_plate\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"license_plate"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__license_plate\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@license_plate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"license_plate"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" plate"},{"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":"if"}]},{"type":"text","value":" Registration"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"license_plate_valid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"plate"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__license_plate "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" plate\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 license plate is not valid\""}]},{"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":"# A class method for validating the license plate"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@classmethod"}]},{"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":"license_plate_valid"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"cls"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" plate"},{"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":"str"}]},{"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":"if"}]},{"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":"plate"},{"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":"3"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"or"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"-\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" plate"},{"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","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Check the beginning and end sections of the plate separately"}]},{"type":"text","value":"\n letters"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" plate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the beginning section can have only letters"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" letters"},{"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":"if"}]},{"type":"text","value":" character"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lower"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"abcdefghijklmnopqrstuvwxyzåäö\""}]},{"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","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the end section can have only numbers"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" character "},{"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":"if"}]},{"type":"text","value":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234567890\""}]},{"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","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]}]}]}]},{"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":"registration "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Registration"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mary Motorist\""}]},{"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":"\"Volvo\""}]},{"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":"\"1992\""}]},{"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":"\"abc-123\""}]},{"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":"if"}]},{"type":"text","value":" Registration"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"license_plate_valid"},{"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-789\""}]},{"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":"\"This is a valid license plate!\""}]},{"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":"This is a valid license plate!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The validity of a license plate can be checked even without creating a single instance of the class, for example with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Registration.license_plate_valid(\"xyz-789\")"}]},{"type":"text","value":". The same method is called within the constructor of the class. NB: even within the constructor this method is accessed through the name of the class, not "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":"!"}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"List helper","tmcname":"part09-14_list_helper"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ListHelper"}]},{"type":"text","value":" which contains the following two class methods."}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"greatest_frequency(my_list: list)"}]},{"type":"text","value":" returns the most common item on the list"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"doubles(my_list: list)"}]},{"type":"text","value":" returns the number of unique items which appear at least twice on the list"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It should be possible to use these methods without creating an instance of the class. An example of how the methods could be used:"}]},{"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":"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"1"}]},{"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":"2"}]},{"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":"1"}]},{"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":"3"}]},{"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":"3"}]},{"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":"4"}]},{"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":"5"}]},{"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":"5"}]},{"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":"5"}]},{"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":"6"}]},{"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":"5"}]},{"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":"5"}]},{"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":"5"}]},{"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":"ListHelper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"greatest_frequency"},{"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":"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":"ListHelper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"doubles"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5\n3"}]}]}]}]},"html":"

    After this section

      \n
    • You will be familiar with the terms class variable and class method
    • \n
    • You will know how static traits are different from the traits of instances
    • \n
    • You will be able to add static traits to your own classes
    • \n

    The traits of objects are a central concept in object oriented programming. The term encompasses the methods and variables defined in the class definition. Again, not all Python programmers use the term \"traits\", some preferring attributes, features or members. Traits is quite accepted in the wider object oriented programming world, so that is the term we use here.

    Thus far we have dealt mostly with traits of objects. These include the methods and attributes accessible in any instance of a class. In fact, classes themselves can also have traits, which are sometimes called static traits, or more specifically class variables and class methods.

    Class variables

    Each instance of a class has its own specific values for each attribute defined in the class, as we've seen in the examples in the previous sections. But what if we wanted to have some data that is shared by the different instances? Enter class variables, also known as static variables. A class variable is a variable which is accessed through the class itself, not through the instances created based on the class. At any given time during the execution of the program a class variable has one single value, no matter how many instances of the class are created.

    A class variable is declared without the self prefix, and usually outside any method definition as it should be accessible from anywhere within the class, or even from outside the class.

    class SavingsAccount:\n    general_rate = 0.03\n\n    def __init__(self, account_number: str, balance: float, interest_rate: float):\n        self.__account_number = account_number\n        self.__balance = balance\n        self.__interest_rate = interest_rate\n\n    def add_interest(self):\n        # The total interest rate equals \n        # the general rate + the interest rate of the account\n        total_interest = SavingsAccount.general_rate + self.__interest_rate\n        self.__balance += self.__balance * total_interest\n\n    @property\n    def balance(self):\n        return self.__balance

    As the variable general_rate is defined within the class but outside any method definitions, and it does not use the self prefix, it is a class variable.

    A class variable is accessed through the name of the class, for example like this:

    # The general rate exists independently of any object instances\nprint(\"The general interest rate is\", SavingsAccount.general_rate)\n\naccount = SavingsAccount(\"12345\", 1000, 0.05)\n# Add the total interest accrued to the balance on the account\naccount.add_interest()\nprint(account.balance)

    The general interest rate is 0.03\n1080.0

    So, the class variables are accessed through the name of the class, for instance with SavingsAccount.general_rate, while instance variables are accessed through the name of the object variable, such as account.balance. An instance variable naturally only exists when an instance of the class has been created, but a class variable is available everywhere and at any point in time where the class itself is available.

    Class variables are useful when there is need for values which are shared by all instances of the class. In the example above we assumed the total interest rate of all savings accounts is formed from two components: the general rate of interest is shared by all accounts, but each account also has it's own interest rate in an instance variable. The general rate may also change, but the change will then affect all instances of the class equally.

    class SavingsAccount:\n    general_rate = 0.03\n\n    def __init__(self, account_number: str, balance: float, interest_rate: float):\n        self.__account_number = account_number\n        self.__balance = balance\n        self.__interest_rate = interest_rate\n\n    def add_interest(self):\n        # The total interest rate equals \n        # the general rate + the interest rate of the account\n        total_interest = SavingsAccount.general_rate + self.__interest_rate\n        self.__balance += self.__balance * total_interest\n\n    @property\n    def balance(self):\n        return self.__balance\n\n    @property\n    def total_interest(self):\n        return self.__interest_rate + SavingsAccount.general_rate
    account1 = SavingsAccount(\"12345\", 100, 0.03)\naccount2 = SavingsAccount(\"54321\", 200, 0.06)\n\nprint(\"General interest rate:\", SavingsAccount.general_rate)\nprint(account1.total_interest)\nprint(account2.total_interest)\n\n# The general rate of interest is now 10 percent\nSavingsAccount.general_rate = 0.10\n\nprint(\"General interest rate:\", SavingsAccount.general_rate)\nprint(account1.total_interest)\nprint(account2.total_interest)

    General interest rate: 0.03\n0.06\n0.09\nGeneral interest rate: 0.1\n0.13\n0.16

    When the general rate of interest changes, the total interest rate for all instances of the class changes. As you can see above, it is possible to add a getter method with the @property decorator even though there isn't an attribute of the same name in the class. This method returns the sum of the general rate of interest and the account specific interest rate.

    Let's have a look at another example. The class PhoneNumber is used to define a single phone number, but it also contains some country codes in a dictionary. This dictionary is a class variable, and as such is shared by all the instances of the class, because the country code for phone numbers from a single country is always the same.

    class PhoneNumber:\n    country_codes = {\"Finland\": \"+358\", \"Sweden\": \"+46\", \"United States\": \"+1\"}\n\n    def __init__(self, name: str, phone_number: str, country: str):\n        self.__name = name\n        self.__phone_number = phone_number\n        self.__country = country\n\n    @property\n    def phone_number(self):\n        # When the country code prefix is added \n        # the initial zero is removed from the phone number\n        return PhoneNumber.country_codes[self.__country] + \" \" + self.__phone_number[1:]
    paulas_no = PhoneNumber(\"Paula Pythons\", \"050 1234 567\", \"Finland\")\nprint(paulas_no.phone_number)

    +358 50 1234 567

    Each PhoneNumber object contains the name of the owner, the number itself, and the country of the phone number. When the attribute containing the phone number is accessed with the getter method, the appropriate country code is retrieved from the class variable dictionary based on the country attribute, and the result is prefixed to the number.

    The example implementation above is not yet very functional otherwise. In the following example we've added getter and setters for all attributes:

    class PhoneNumber:\n    country_codes = {\"Finland\": \"+358\", \"Sweden\": \"+46\", \"United States\": \"+1\"}\n\n    def __init__(self, name: str, phone_number: str, country: str):\n        self.__name = name\n        # This is a call to the phone_number.setter method\n        self.phone_number = phone_number\n        # This is a call to the country.setter method\n        self.country = country\n\n    # the getter method for phone_number combines the country code \n    # and the attribute phone_number\n    @property\n    def phone_number(self):\n        # the initial zero is removed as the country code is prefixed\n        return PhoneNumber.country_codes[self.__country] + \" \" + self.__phone_number[1:]\n\n    @phone_number.setter\n    def phone_number(self, number):\n        # Making sure the number contains only numbers and space characters\n        for character in number:\n            if character not in \"1234567890 \":\n                raise ValueError(\"A phone number can only contain numbers and spaces\")\n        self.__phone_number = number\n\n    # a getter for only the number itself without the country code\n    @property\n    def local_number(self):\n        return self.__phone_number\n\n    @property\n    def country(self):\n        return self.__country\n\n    @country.setter\n    def country(self, country):\n        # Making sure the country is a key in the dictionary of country codes\n        if country not in PhoneNumber.country_codes:\n            raise ValueError(\"This country is not on the list.\")\n        self.__country = country\n\n    @property\n    def name(self):\n        return self.__name\n\n    @name.setter\n    def name(self, name):\n        self.__name = name\n\n    def __str__(self):\n        return f\"{self.phone_number} ({self.__name})\"
    if __name__ == \"__main__\":\n    pn = PhoneNumber(\"Peter Pythons\", \"040 111 1111\", \"Sweden\")\n    print(pn)\n    print(pn.phone_number)\n    print(pn.local_number)

    +46 40 111 1111 (Peter Pythons)\n+46 40 111 1111\n040 111 1111

    The exercise template contains the class definition City which is a model for a single city.

    Please add a class variable named postcodes which refers to a dictionary. The keys of the dictionary are names of cities, and the values attached are the postcodes for those cities. Both are strings.

    The dictionary should contain (at least) the following postcodes:

      \n
    • Helsinki 00100
    • \n
    • Turku 20100
    • \n
    • Tampere 33100
    • \n
    • Rovaniemi 96100
    • \n
    • Oulu 90100
    • \n

    You do not need to implement any other functionality.

    Class methods

    A class method, also called a static method, is a method which is not attached to any single instance of the class. A class method can be called without creating any instances of the class.

    Class methods are usually tools which have something to do with the purpose of the class, but which are detached in the sense that it should not be necessary to create instances of the class in order to be able to call them. Class methods are usually public, so that they can be called both from outside the class and from within the class, including from within instances of the class.

    A class method is defined with the @classmethod annotation. The first parameter is always cls. The variable name cls is similar to the self parameter. The difference is that cls points to the class while self point to an instance of the class. Neither parameter is included in the argument list when the function is called; Python fills in the appropriate value automatically.

    In the following example we have a class modelling vehicle registrations. The Registration class contains a static method for checking whether a license plate is valid. The method is a static class method because it is useful to be able to check if a license plate is valid even before a single Registration object is created:

    class Registration:\n    def __init__(self, owner: str, make: str, year: int, license_plate: str):\n        self.__owner = owner\n        self.__make = make\n        self.__year = year\n\n        # Call the license_plate.setter method\n        self.license_plate = license_plate\n\n    @property\n    def license_plate(self):\n        return self.__license_plate\n\n    @license_plate.setter\n    def license_plate(self, plate):\n        if Registration.license_plate_valid(plate):\n            self.__license_plate = plate\n        else:\n            raise ValueError(\"The license plate is not valid\")\n\n    # A class method for validating the license plate\n    @classmethod\n    def license_plate_valid(cls, plate: str):\n        if len(plate) < 3 or \"-\" not in plate:\n            return False\n\n        # Check the beginning and end sections of the plate separately\n        letters, numbers = plate.split(\"-\")\n\n        # the beginning section can have only letters\n        for character in letters:\n            if character.lower() not in \"abcdefghijklmnopqrstuvwxyzåäö\":\n                return False\n\n        # the end section can have only numbers\n        for character in numbers:\n            if character not in \"1234567890\":\n                return False\n\n        return True
    registration = Registration(\"Mary Motorist\", \"Volvo\", \"1992\", \"abc-123\")\n\nif Registration.license_plate_valid(\"xyz-789\"):\n    print(\"This is a valid license plate!\")

    This is a valid license plate!

    The validity of a license plate can be checked even without creating a single instance of the class, for example with Registration.license_plate_valid(\"xyz-789\"). The same method is called within the constructor of the class. NB: even within the constructor this method is accessed through the name of the class, not self!

    Please create a class named ListHelper which contains the following two class methods.

      \n
    • greatest_frequency(my_list: list) returns the most common item on the list
    • \n
    • doubles(my_list: list) returns the number of unique items which appear at least twice on the list
    • \n

    It should be possible to use these methods without creating an instance of the class. An example of how the methods could be used:

    numbers = [1, 1, 2, 1, 3, 3, 4, 5, 5, 5, 6, 5, 5, 5]\nprint(ListHelper.greatest_frequency(numbers))\nprint(ListHelper.doubles(numbers))

    5\n3

    ","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-9/5-class-attributes.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 the terms class variable and class method"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will know how static traits are different from the traits of instances"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to add static traits to your own classes"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"traits"}]},{"type":"text","value":" of objects are a central concept in object oriented programming. The term encompasses the methods and variables defined in the class definition. Again, not all Python programmers use the term \"traits\", some preferring "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"attributes"}]},{"type":"text","value":", "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"features"}]},{"type":"text","value":" or "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"members"}]},{"type":"text","value":". Traits is quite accepted in the wider object oriented programming world, so that is the term we use here."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Thus far we have dealt mostly with "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"traits of objects"}]},{"type":"text","value":". These include the methods and attributes accessible in any instance of a class. In fact, classes "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"themselves"}]},{"type":"text","value":" can also have traits, which are sometimes called "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"static traits"}]},{"type":"text","value":", or more specifically "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"class variables"}]},{"type":"text","value":" and "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"class methods"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h2","properties":{"id":"class-variables","style":"position:relative;"},"children":[{"type":"text","value":"Class variables"},{"type":"element","tagName":"a","properties":{"href":"#class-variables","ariaLabel":"class variables 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":"Each instance of a class has its own specific values for each attribute defined in the class, as we've seen in the examples in the previous sections. But what if we wanted to have some data that is shared by the different instances? Enter "},{"type":"element","tagName":"em","properties":{},"children":[{"type":"text","value":"class variables"}]},{"type":"text","value":", also known as static variables. A class variable is a variable which is accessed through the class itself, not through the instances created based on the class. At any given time during the execution of the program a class variable has one single value, no matter how many instances of the class are created."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A class variable is declared without the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" prefix, and usually outside any method definition as it should be accessible from anywhere within the class, or even from outside the class."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"SavingsAccount"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n general_rate "},{"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.03"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" account_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" interest_rate"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__account_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" account_number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__interest_rate "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" interest_rate\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":"add_interest"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# The total interest rate equals "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the general rate + the interest rate of the account"}]},{"type":"text","value":"\n total_interest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SavingsAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"general_rate "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__interest_rate\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" total_interest\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"balance"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"As the variable "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"general_rate"}]},{"type":"text","value":" is defined within the class but outside any method definitions, and it does not use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" prefix, it is a class variable."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A class variable is accessed through the name of the class, for example 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","comment"]},"children":[{"type":"text","value":"# The general rate exists independently of any object instances"}]},{"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 general interest rate is\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" SavingsAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"general_rate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\naccount "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SavingsAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345\""}]},{"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":"1000"}]},{"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":"0.05"}]},{"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":"# Add the total interest accrued to the balance on the account"}]},{"type":"text","value":"\naccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_interest"},{"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":"account"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"balance"},{"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":"The general interest rate is 0.03\n1080.0"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"So, the class variables are accessed through the name of the class, for instance with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"SavingsAccount.general_rate"}]},{"type":"text","value":", while instance variables are accessed through the name of the object variable, such as "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"account.balance"}]},{"type":"text","value":". An instance variable naturally only exists when an instance of the class has been created, but a class variable is available everywhere and at any point in time where the class itself is available."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Class variables are useful when there is need for values which are shared by all instances of the class. In the example above we assumed the total interest rate of all savings accounts is formed from two components: the general rate of interest is shared by all accounts, but each account also has it's own interest rate in an instance variable. The general rate may also change, but the change will then affect all instances of the class equally."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"SavingsAccount"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n general_rate "},{"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.03"}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" account_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" balance"},{"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":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" interest_rate"},{"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":"float"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__account_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" account_number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" balance\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__interest_rate "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" interest_rate\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":"add_interest"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# The total interest rate equals "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the general rate + the interest rate of the account"}]},{"type":"text","value":"\n total_interest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SavingsAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"general_rate "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__interest_rate\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+="}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"*"}]},{"type":"text","value":" total_interest\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"balance"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__balance\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"total_interest"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__interest_rate "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" SavingsAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"general_rate"}]}]}]},{"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":"account1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SavingsAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"12345\""}]},{"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":"100"}]},{"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":"0.03"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\naccount2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" SavingsAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"54321\""}]},{"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":"200"}]},{"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":"0.06"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"General interest rate:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" SavingsAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"general_rate"},{"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":"account1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"total_interest"},{"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":"account2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"total_interest"},{"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":"# The general rate of interest is now 10 percent"}]},{"type":"text","value":"\nSavingsAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"general_rate "},{"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.10"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"General interest rate:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" SavingsAccount"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"general_rate"},{"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":"account1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"total_interest"},{"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":"account2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"total_interest"},{"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":"General interest rate: 0.03\n0.06\n0.09\nGeneral interest rate: 0.1\n0.13\n0.16"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"When the general rate of interest changes, the total interest rate for all instances of the class changes. As you can see above, it is possible to add a getter method with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":" decorator even though there isn't an attribute of the same name in the class. This method returns the sum of the general rate of interest and the account specific interest rate."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at another example. The class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"PhoneNumber"}]},{"type":"text","value":" is used to define a single phone number, but it also contains some country codes in a dictionary. This dictionary is a class variable, and as such is shared by all the instances of the class, because the country code for phone numbers from a single country is always the same."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneNumber"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n country_codes "},{"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":"\"Finland\""}]},{"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":"\"+358\""}]},{"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":"\"Sweden\""}]},{"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":"\"+46\""}]},{"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":"\"United States\""}]},{"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":"\"+1\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" phone_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" country"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phone_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" phone_number\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__country "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" country\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"phone_number"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# When the country code prefix is added "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the initial zero is removed from the phone number"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" PhoneNumber"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"country_codes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__country"},{"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":"\" \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phone_number"},{"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":"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":"paulas_no "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneNumber"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Paula Pythons\""}]},{"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":"\"050 1234 567\""}]},{"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":"\"Finland\""}]},{"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":"paulas_no"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"phone_number"},{"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":"+358 50 1234 567"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Each PhoneNumber object contains the name of the owner, the number itself, and the country of the phone number. When the attribute containing the phone number is accessed with the getter method, the appropriate country code is retrieved from the class variable dictionary based on the country attribute, and the result is prefixed to the number."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The example implementation above is not yet very functional otherwise. In the following example we've added getter and setters for all attributes:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"PhoneNumber"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n country_codes "},{"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":"\"Finland\""}]},{"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":"\"+358\""}]},{"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":"\"Sweden\""}]},{"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":"\"+46\""}]},{"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":"\"United States\""}]},{"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":"\"+1\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" phone_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" country"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This is a call to the phone_number.setter method"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"phone_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" phone_number\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This is a call to the country.setter method"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"country "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" country\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the getter method for phone_number combines the country code "}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# and the attribute phone_number"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"phone_number"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":"# the initial zero is removed as the country code is prefixed"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" PhoneNumber"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"country_codes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"["}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__country"},{"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":"\" \""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phone_number"},{"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":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@phone_number"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"phone_number"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Making sure the number contains only numbers and space characters"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" number"},{"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":"if"}]},{"type":"text","value":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234567890 \""}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"A phone number can only contain numbers and spaces\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phone_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" number\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# a getter for only the number itself without the country code"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"local_number"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__phone_number\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"country"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__country\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@country"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"country"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" country"},{"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":"# Making sure the country is a key in the dictionary of country codes"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" country "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" PhoneNumber"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"country_codes"},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"This country is not on the list.\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__country "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" country\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"name"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"name"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"phone_number"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name"},{"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":"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":"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 pn "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" PhoneNumber"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Peter Pythons\""}]},{"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":"\"040 111 1111\""}]},{"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":"\"Sweden\""}]},{"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":"pn"},{"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":"pn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"phone_number"},{"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":"pn"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"local_number"},{"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":"+46 40 111 1111 (Peter Pythons)\n+46 40 111 1111\n040 111 1111"}]}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Postcodes","tmcname":"part09-13_postcodes"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The exercise template contains the class definition "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"City"}]},{"type":"text","value":" which is a model for a single city."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please add a class variable named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"postcodes"}]},{"type":"text","value":" which refers to a dictionary. The keys of the dictionary are names of cities, and the values attached are the postcodes for those cities. Both are strings."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The dictionary should contain (at least) the following postcodes:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Helsinki 00100"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Turku 20100"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Tampere 33100"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Rovaniemi 96100"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"Oulu 90100"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You do not need to implement any other functionality."}]}]},{"type":"element","tagName":"h2","properties":{"id":"class-methods","style":"position:relative;"},"children":[{"type":"text","value":"Class methods"},{"type":"element","tagName":"a","properties":{"href":"#class-methods","ariaLabel":"class methods 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":"A class method, also called a static method, is a method which is not attached to any single instance of the class. A class method can be called without creating any instances of the class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Class methods are usually tools which have something to do with the purpose of the class, but which are detached in the sense that it should not be necessary to create instances of the class in order to be able to call them. Class methods are usually public, so that they can be called both from outside the class and from within the class, including from within instances of the class."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"A class method is defined with the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@classmethod"}]},{"type":"text","value":" annotation. The first parameter is always "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cls"}]},{"type":"text","value":". The variable name "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cls"}]},{"type":"text","value":" is similar to the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" parameter. The difference is that "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"cls"}]},{"type":"text","value":" points to the class while "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":" point to an instance of the class. Neither parameter is included in the argument list when the function is called; Python fills in the appropriate value automatically."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In the following example we have a class modelling vehicle registrations. The "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Registration"}]},{"type":"text","value":" class contains a static method for checking whether a license plate is valid. The method is a static class method because it is useful to be able to check if a license plate is valid even before a single Registration object is created:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Registration"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" owner"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" make"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" year"},{"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":"text","value":" license_plate"},{"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":"str"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__owner "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" owner\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__make "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" make\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__year "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" year\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Call the license_plate.setter method"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"license_plate "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" license_plate\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"license_plate"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__license_plate\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@license_plate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"license_plate"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" plate"},{"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":"if"}]},{"type":"text","value":" Registration"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"license_plate_valid"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"plate"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__license_plate "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" plate\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 license plate is not valid\""}]},{"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":"# A class method for validating the license plate"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@classmethod"}]},{"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":"license_plate_valid"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"cls"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" plate"},{"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":"str"}]},{"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":"if"}]},{"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":"plate"},{"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":"3"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"or"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"-\""}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" plate"},{"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","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# Check the beginning and end sections of the plate separately"}]},{"type":"text","value":"\n letters"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" numbers "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" plate"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"split"},{"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\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the beginning section can have only letters"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" letters"},{"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":"if"}]},{"type":"text","value":" character"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"lower"},{"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":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"abcdefghijklmnopqrstuvwxyzåäö\""}]},{"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","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# the end section can have only numbers"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"for"}]},{"type":"text","value":" character "},{"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":"if"}]},{"type":"text","value":" character "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"not"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"in"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"1234567890\""}]},{"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","boolean"]},"children":[{"type":"text","value":"False"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]}]}]}]},{"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":"registration "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Registration"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Mary Motorist\""}]},{"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":"\"Volvo\""}]},{"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":"\"1992\""}]},{"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":"\"abc-123\""}]},{"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":"if"}]},{"type":"text","value":" Registration"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"license_plate_valid"},{"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-789\""}]},{"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":"\"This is a valid license plate!\""}]},{"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":"This is a valid license plate!"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The validity of a license plate can be checked even without creating a single instance of the class, for example with "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Registration.license_plate_valid(\"xyz-789\")"}]},{"type":"text","value":". The same method is called within the constructor of the class. NB: even within the constructor this method is accessed through the name of the class, not "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"self"}]},{"type":"text","value":"!"}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"List helper","tmcname":"part09-14_list_helper"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please create a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"ListHelper"}]},{"type":"text","value":" which contains the following two class methods."}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"greatest_frequency(my_list: list)"}]},{"type":"text","value":" returns the most common item on the list"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"doubles(my_list: list)"}]},{"type":"text","value":" returns the number of unique items which appear at least twice on the list"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"It should be possible to use these methods without creating an instance of the class. An example of how the methods could be used:"}]},{"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":"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","number"]},"children":[{"type":"text","value":"1"}]},{"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":"1"}]},{"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":"2"}]},{"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":"1"}]},{"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":"3"}]},{"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":"3"}]},{"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":"4"}]},{"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":"5"}]},{"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":"5"}]},{"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":"5"}]},{"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":"6"}]},{"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":"5"}]},{"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":"5"}]},{"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":"5"}]},{"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":"ListHelper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"greatest_frequency"},{"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":"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":"ListHelper"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"doubles"},{"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":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"5\n3"}]}]}]}]},"html":"

    After this section

      \n
    • You will be familiar with the terms class variable and class method
    • \n
    • You will know how static traits are different from the traits of instances
    • \n
    • You will be able to add static traits to your own classes
    • \n

    The traits of objects are a central concept in object oriented programming. The term encompasses the methods and variables defined in the class definition. Again, not all Python programmers use the term \"traits\", some preferring attributes, features or members. Traits is quite accepted in the wider object oriented programming world, so that is the term we use here.

    Thus far we have dealt mostly with traits of objects. These include the methods and attributes accessible in any instance of a class. In fact, classes themselves can also have traits, which are sometimes called static traits, or more specifically class variables and class methods.

    Class variables

    Each instance of a class has its own specific values for each attribute defined in the class, as we've seen in the examples in the previous sections. But what if we wanted to have some data that is shared by the different instances? Enter class variables, also known as static variables. A class variable is a variable which is accessed through the class itself, not through the instances created based on the class. At any given time during the execution of the program a class variable has one single value, no matter how many instances of the class are created.

    A class variable is declared without the self prefix, and usually outside any method definition as it should be accessible from anywhere within the class, or even from outside the class.

    class SavingsAccount:\n    general_rate = 0.03\n\n    def __init__(self, account_number: str, balance: float, interest_rate: float):\n        self.__account_number = account_number\n        self.__balance = balance\n        self.__interest_rate = interest_rate\n\n    def add_interest(self):\n        # The total interest rate equals \n        # the general rate + the interest rate of the account\n        total_interest = SavingsAccount.general_rate + self.__interest_rate\n        self.__balance += self.__balance * total_interest\n\n    @property\n    def balance(self):\n        return self.__balance

    As the variable general_rate is defined within the class but outside any method definitions, and it does not use the self prefix, it is a class variable.

    A class variable is accessed through the name of the class, for example like this:

    # The general rate exists independently of any object instances\nprint(\"The general interest rate is\", SavingsAccount.general_rate)\n\naccount = SavingsAccount(\"12345\", 1000, 0.05)\n# Add the total interest accrued to the balance on the account\naccount.add_interest()\nprint(account.balance)

    The general interest rate is 0.03\n1080.0

    So, the class variables are accessed through the name of the class, for instance with SavingsAccount.general_rate, while instance variables are accessed through the name of the object variable, such as account.balance. An instance variable naturally only exists when an instance of the class has been created, but a class variable is available everywhere and at any point in time where the class itself is available.

    Class variables are useful when there is need for values which are shared by all instances of the class. In the example above we assumed the total interest rate of all savings accounts is formed from two components: the general rate of interest is shared by all accounts, but each account also has it's own interest rate in an instance variable. The general rate may also change, but the change will then affect all instances of the class equally.

    class SavingsAccount:\n    general_rate = 0.03\n\n    def __init__(self, account_number: str, balance: float, interest_rate: float):\n        self.__account_number = account_number\n        self.__balance = balance\n        self.__interest_rate = interest_rate\n\n    def add_interest(self):\n        # The total interest rate equals \n        # the general rate + the interest rate of the account\n        total_interest = SavingsAccount.general_rate + self.__interest_rate\n        self.__balance += self.__balance * total_interest\n\n    @property\n    def balance(self):\n        return self.__balance\n\n    @property\n    def total_interest(self):\n        return self.__interest_rate + SavingsAccount.general_rate
    account1 = SavingsAccount(\"12345\", 100, 0.03)\naccount2 = SavingsAccount(\"54321\", 200, 0.06)\n\nprint(\"General interest rate:\", SavingsAccount.general_rate)\nprint(account1.total_interest)\nprint(account2.total_interest)\n\n# The general rate of interest is now 10 percent\nSavingsAccount.general_rate = 0.10\n\nprint(\"General interest rate:\", SavingsAccount.general_rate)\nprint(account1.total_interest)\nprint(account2.total_interest)

    General interest rate: 0.03\n0.06\n0.09\nGeneral interest rate: 0.1\n0.13\n0.16

    When the general rate of interest changes, the total interest rate for all instances of the class changes. As you can see above, it is possible to add a getter method with the @property decorator even though there isn't an attribute of the same name in the class. This method returns the sum of the general rate of interest and the account specific interest rate.

    Let's have a look at another example. The class PhoneNumber is used to define a single phone number, but it also contains some country codes in a dictionary. This dictionary is a class variable, and as such is shared by all the instances of the class, because the country code for phone numbers from a single country is always the same.

    class PhoneNumber:\n    country_codes = {\"Finland\": \"+358\", \"Sweden\": \"+46\", \"United States\": \"+1\"}\n\n    def __init__(self, name: str, phone_number: str, country: str):\n        self.__name = name\n        self.__phone_number = phone_number\n        self.__country = country\n\n    @property\n    def phone_number(self):\n        # When the country code prefix is added \n        # the initial zero is removed from the phone number\n        return PhoneNumber.country_codes[self.__country] + \" \" + self.__phone_number[1:]
    paulas_no = PhoneNumber(\"Paula Pythons\", \"050 1234 567\", \"Finland\")\nprint(paulas_no.phone_number)

    +358 50 1234 567

    Each PhoneNumber object contains the name of the owner, the number itself, and the country of the phone number. When the attribute containing the phone number is accessed with the getter method, the appropriate country code is retrieved from the class variable dictionary based on the country attribute, and the result is prefixed to the number.

    The example implementation above is not yet very functional otherwise. In the following example we've added getter and setters for all attributes:

    class PhoneNumber:\n    country_codes = {\"Finland\": \"+358\", \"Sweden\": \"+46\", \"United States\": \"+1\"}\n\n    def __init__(self, name: str, phone_number: str, country: str):\n        self.__name = name\n        # This is a call to the phone_number.setter method\n        self.phone_number = phone_number\n        # This is a call to the country.setter method\n        self.country = country\n\n    # the getter method for phone_number combines the country code \n    # and the attribute phone_number\n    @property\n    def phone_number(self):\n        # the initial zero is removed as the country code is prefixed\n        return PhoneNumber.country_codes[self.__country] + \" \" + self.__phone_number[1:]\n\n    @phone_number.setter\n    def phone_number(self, number):\n        # Making sure the number contains only numbers and space characters\n        for character in number:\n            if character not in \"1234567890 \":\n                raise ValueError(\"A phone number can only contain numbers and spaces\")\n        self.__phone_number = number\n\n    # a getter for only the number itself without the country code\n    @property\n    def local_number(self):\n        return self.__phone_number\n\n    @property\n    def country(self):\n        return self.__country\n\n    @country.setter\n    def country(self, country):\n        # Making sure the country is a key in the dictionary of country codes\n        if country not in PhoneNumber.country_codes:\n            raise ValueError(\"This country is not on the list.\")\n        self.__country = country\n\n    @property\n    def name(self):\n        return self.__name\n\n    @name.setter\n    def name(self, name):\n        self.__name = name\n\n    def __str__(self):\n        return f\"{self.phone_number} ({self.__name})\"
    if __name__ == \"__main__\":\n    pn = PhoneNumber(\"Peter Pythons\", \"040 111 1111\", \"Sweden\")\n    print(pn)\n    print(pn.phone_number)\n    print(pn.local_number)

    +46 40 111 1111 (Peter Pythons)\n+46 40 111 1111\n040 111 1111

    The exercise template contains the class definition City which is a model for a single city.

    Please add a class variable named postcodes which refers to a dictionary. The keys of the dictionary are names of cities, and the values attached are the postcodes for those cities. Both are strings.

    The dictionary should contain (at least) the following postcodes:

      \n
    • Helsinki 00100
    • \n
    • Turku 20100
    • \n
    • Tampere 33100
    • \n
    • Rovaniemi 96100
    • \n
    • Oulu 90100
    • \n

    You do not need to implement any other functionality.

    Class methods

    A class method, also called a static method, is a method which is not attached to any single instance of the class. A class method can be called without creating any instances of the class.

    Class methods are usually tools which have something to do with the purpose of the class, but which are detached in the sense that it should not be necessary to create instances of the class in order to be able to call them. Class methods are usually public, so that they can be called both from outside the class and from within the class, including from within instances of the class.

    A class method is defined with the @classmethod annotation. The first parameter is always cls. The variable name cls is similar to the self parameter. The difference is that cls points to the class while self point to an instance of the class. Neither parameter is included in the argument list when the function is called; Python fills in the appropriate value automatically.

    In the following example we have a class modelling vehicle registrations. The Registration class contains a static method for checking whether a license plate is valid. The method is a static class method because it is useful to be able to check if a license plate is valid even before a single Registration object is created:

    class Registration:\n    def __init__(self, owner: str, make: str, year: int, license_plate: str):\n        self.__owner = owner\n        self.__make = make\n        self.__year = year\n\n        # Call the license_plate.setter method\n        self.license_plate = license_plate\n\n    @property\n    def license_plate(self):\n        return self.__license_plate\n\n    @license_plate.setter\n    def license_plate(self, plate):\n        if Registration.license_plate_valid(plate):\n            self.__license_plate = plate\n        else:\n            raise ValueError(\"The license plate is not valid\")\n\n    # A class method for validating the license plate\n    @classmethod\n    def license_plate_valid(cls, plate: str):\n        if len(plate) < 3 or \"-\" not in plate:\n            return False\n\n        # Check the beginning and end sections of the plate separately\n        letters, numbers = plate.split(\"-\")\n\n        # the beginning section can have only letters\n        for character in letters:\n            if character.lower() not in \"abcdefghijklmnopqrstuvwxyzåäö\":\n                return False\n\n        # the end section can have only numbers\n        for character in numbers:\n            if character not in \"1234567890\":\n                return False\n\n        return True
    registration = Registration(\"Mary Motorist\", \"Volvo\", \"1992\", \"abc-123\")\n\nif Registration.license_plate_valid(\"xyz-789\"):\n    print(\"This is a valid license plate!\")

    This is a valid license plate!

    The validity of a license plate can be checked even without creating a single instance of the class, for example with Registration.license_plate_valid(\"xyz-789\"). The same method is called within the constructor of the class. NB: even within the constructor this method is accessed through the name of the class, not self!

    Please create a class named ListHelper which contains the following two class methods.

      \n
    • greatest_frequency(my_list: list) returns the most common item on the list
    • \n
    • doubles(my_list: list) returns the number of unique items which appear at least twice on the list
    • \n

    It should be possible to use these methods without creating an instance of the class. An example of how the methods could be used:

    numbers = [1, 1, 2, 1, 3, 3, 4, 5, 5, 5, 6, 5, 5, 5]\nprint(ListHelper.greatest_frequency(numbers))\nprint(ListHelper.doubles(numbers))

    5\n3

    ","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-9/5-class-attributes.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-9/6-more-examples-with-classes/page-data.json b/page-data/part-9/6-more-examples-with-classes/page-data.json index 9caea915f..cb2a75ceb 100644 --- a/page-data/part-9/6-more-examples-with-classes/page-data.json +++ b/page-data/part-9/6-more-examples-with-classes/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-content-template-js", "path": "/part-9/6-more-examples-with-classes", - "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 some more examples of classes and objects"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use default values for parameters in your methods"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following example consists of two classes. The class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Point"}]},{"type":"text","value":" is a model for a point in two-dimensional space. The class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Line"}]},{"type":"text","value":" is a model for a line segment between two points. The code below is commented; please read the comments in order to understand how the classes 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":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" math\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Point"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The class represents a point in two-dimensional space \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" x"},{"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":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"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":"float"}]},{"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":"# These attributes are public because any value is acceptable for x and y"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" x\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" y\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This class method returns a new Point at origo (0, 0)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# It is possible to return a new instance of the class from within the class"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@classmethod"}]},{"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":"origo"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"cls"},{"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":" Point"},{"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","number"]},"children":[{"type":"text","value":"0"}]},{"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":"# This class method creates a new Point based on an existing Point"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# The original Point can be mirrored on either or both of the x and y axes"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# For example, the Point (1, 3) mirrored on the x-axis is (1, -3)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@classmethod"}]},{"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":"mirrored"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"cls"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" point"},{"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":"\"Point\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mirror_x"},{"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":"bool"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mirror_y"},{"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":"bool"}]},{"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 x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" point"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x\n y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" point"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" mirror_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"y\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" mirror_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"x\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" Point"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y"},{"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Line"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The class represents a line segment in two-dimensional space \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" beginning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Point"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Point"},{"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":"# These attributes are public because any two Points are acceptable"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"beginning "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" beginning\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"end "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" end\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This method uses the Pythagorean theorem to calculate the length of the line segment"}]},{"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":"length"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 sum_of_squares "},{"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":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"end"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"beginning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x"},{"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":"2"}]},{"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","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"end"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"beginning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y"},{"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":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sum_of_squares"},{"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":"# This method returns the Point in the middle of the line segment"}]},{"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":"centre_point"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 centre_x "},{"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":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"beginning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"end"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x"},{"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":"2"}]},{"type":"text","value":"\n centre_y "},{"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":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"beginning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"end"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y"},{"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":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" Point"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"centre_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" centre_y"},{"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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"beginning"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"end"},{"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":"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":"point "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Point"},{"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":"3"}]},{"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":"point"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\norigo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Point"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"origo"},{"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":"origo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\npoint2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Point"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mirrored"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"point"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"point2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nline "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"point"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" point2"},{"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":"line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length"},{"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"},{"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":"line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"centre_point"},{"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"},{"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":"line"},{"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":"(1, 3)\n(0, 0)\n(-1, -3)\n6.324555320336759\n(0.0, 0.0)\n(1, 3) ... (-1, -3)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"default-values-of-parameters","style":"position:relative;"},"children":[{"type":"text","value":"Default values of parameters"},{"type":"element","tagName":"a","properties":{"href":"#default-values-of-parameters","ariaLabel":"default values of parameters 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 Python programming you can generally set a default value for any parameter. Default values can be used in both functions and methods."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If a parameter has a default value, you do not have to include a value as an argument when calling the function. If an argument is given, the default value is ignored. If not, the default value is used."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Default values are often used in constructors. If it can be expected that not all information is available when an object is created, it is better to include a default value in the definition of the constructor method than to force the client to take care of the issue. This makes using the class easier from the client's point of view, but it also ensures the integrity of the object. For instance, with a set default value we can be sure that an \"empty\" value is always the same, unless the client specifically wants to supply something different. If a default value is not set, it is up to the client to provide an \"empty\" value. This could be, for example, an empty string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":", the special empty object "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":", or the string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"not set\""}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at yet another class representing a student. When creating a new Student object the client must provide a name and a student number. The student number is private and should not be changed later. Additionally, a Student object has attributes for study credits and notes, which have default values set in the constructor. New values can be passed as arguments to the constructor, but they can also be left out so that the default values are used instead. Please have a look at the comments in the code to better understand what each method does."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" This class models a student \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" student_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" credits"},{"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":"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" notes"},{"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":"str"}]},{"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":"\"\""}]},{"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":"# calling the setter method for the name attribute"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"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":"student_number"},{"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":"5"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"A student number should have at least five characters\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__student_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" student_number\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# calling the setter method for the credits attribute"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" credits\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__notes "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" notes\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"name"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"name"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 name cannot be an empty string\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"student_number"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__student_number\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"credits"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__credits\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@credits"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"credits"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" op"},{"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":"if"}]},{"type":"text","value":" op "},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" op\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 number of study credits cannot be below zero\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"notes"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__notes\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@notes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"notes"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" notes"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__notes "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" notes\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":"summary"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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\"Student "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"student_number"},{"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 "},{"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\"- credits: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__credits"},{"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 "},{"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\"- notes: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"notes"},{"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":"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":"# Passing only the name and the student number as arguments to the constructor"}]},{"type":"text","value":"\nstudent1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sally Student\""}]},{"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":"\"12345\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstudent1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"summary"},{"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","comment"]},"children":[{"type":"text","value":"# Passing the name, the student number and the number of study credits"}]},{"type":"text","value":"\nstudent2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sassy Student\""}]},{"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":"\"54321\""}]},{"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":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstudent2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"summary"},{"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","comment"]},"children":[{"type":"text","value":"# Passing values for all the parameters"}]},{"type":"text","value":"\nstudent3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saul Student\""}]},{"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":"\"99999\""}]},{"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":"140"}]},{"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":"\"extra time in exam\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstudent3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"summary"},{"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","comment"]},"children":[{"type":"text","value":"# Passing a value for notes, but not for study credits"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# NB: the parameter must be named now that the arguments are not in order"}]},{"type":"text","value":"\nstudent4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sandy Student\""}]},{"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":"\"98765\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" notes"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"absent in academic year 20-21\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstudent4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"summary"},{"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":"Student Sally Student (12345):"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"credits: 0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"notes:"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Student Sassy Student (54321):"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"credits: 25"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"notes:"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Student Saul Student (99999):"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"credits: 140"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"notes: extra time in exam"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Student Sandy Student (98765):"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"credits: 0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"notes: absent in academic year 20-21"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: there is no setter method for the attribute "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"student_number"}]},{"type":"text","value":" as the student number is not supposed to change."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There is one rather significant snag when using default values for parameters. The following example modelling yet another kind of student will shed more light on 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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" completed_courses"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"completed_courses "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" completed_courses\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":"add_course"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" course"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"completed_courses"},{"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":"course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"student1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sally Student\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstudent2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sassy Student\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nstudent1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ItP\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstudent1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ACiP\""}]},{"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":"student1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"completed_courses"},{"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":"student2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"completed_courses"},{"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":"['ItP', 'ACiP']\n['ItP', 'ACiP']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Adding completed courses to Sally's list also adds those courses to Sassy's list. In fact, these two are the exact same list, as Python reuses the reference stored in the default value. Creating the two new Student objects in the above example is equivalent to the following:"}]},{"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":"courses "},{"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":"\nstudent1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sally Student\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" courses"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstudent2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sassy Student\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" courses"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The default values of parameters should never be instances of more complicated, mutable data structures, such as lists. The problem can be circumvented by making the following changes to the constructor of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" class:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" completed_courses"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" completed_courses "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"completed_courses "},{"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","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"completed_courses "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" completed_courses\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":"add_course"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" course"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"completed_courses"},{"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":"course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"student1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sally Student\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstudent2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sassy Student\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nstudent1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ItP\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstudent1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ACiP\""}]},{"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":"student1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"completed_courses"},{"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":"student2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"completed_courses"},{"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":"['ItP', 'ACiP']\n[]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-grand-finale","style":"position:relative;"},"children":[{"type":"text","value":"The Grand Finale"},{"type":"element","tagName":"a","properties":{"href":"#the-grand-finale","ariaLabel":"the grand finale 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":"Even though the following exercise finishes off this part of the material, the techniques required to solve it were all covered already in the section named "},{"type":"element","tagName":"a","properties":{"href":"/part-9/2-objects-as-attributes"},"children":[{"type":"text","value":"objects as attributes"}]},{"type":"text","value":". Specifically, you are not required to use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":" decorator or default values for parameters in this exercise. This exercise is very similar to the exercises "},{"type":"element","tagName":"a","properties":{"href":"/part-9/2-objects-as-attributes#programming-exercise-a-box-of-presents"},"children":[{"type":"text","value":"a box of presents"}]},{"type":"text","value":" and "},{"type":"element","tagName":"a","properties":{"href":"/part-9/2-objects-as-attributes#programming-exercise-the-shortest-person-in-the-room"},"children":[{"type":"text","value":"the shortest person in the room"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h4","properties":{"id":"important-information-regarding-the-next-exercise","style":"position:relative;"},"children":[{"type":"text","value":"Important information regarding the next exercise"},{"type":"element","tagName":"a","properties":{"href":"#important-information-regarding-the-next-exercise","ariaLabel":"important information regarding the next exercise 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":"Please note that there is an issue resulting from an update in Python, which conflicts with the inbuilt library and the original file name for this exercise. If you experience any issues, we recommend redownloading the exercise folder. Once you have obtained the new local test files, you can use either \"code.py\" or \"code_1.py\" as the file name. While using Visual Studio Code, you may receive notifications about problems in the test file. However, these notifications can be safely ignored, as they are caused by the test's inability to import from either the \"code.py\" or \"code_1.py\" files."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Item, Suitcase and Cargo hold","tmcname":"part09-15_item_suitcase_hold"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this series of exercises you will create the classes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Item"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Suitcase"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Cargo Hold"}]},{"type":"text","value":", which will let you further practice working on objects which contain references to other objects."}]},{"type":"element","tagName":"h2","properties":{"id":"item","style":"position:relative;"},"children":[{"type":"text","value":"Item"},{"type":"element","tagName":"a","properties":{"href":"#item","ariaLabel":"item 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":"Please create a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Item"}]},{"type":"text","value":" which is used to create items of different kinds. Each item has a name and a weight (in kilograms)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can use the following code to test your class:"}]},{"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":"book "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"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 Book\""}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nphone "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"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":"1"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Name of the book:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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"},{"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":"\"Weight of the book:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weight"},{"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\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":"\"Book:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"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":"\"Phone:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" phone"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your program should print out this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Name of the book: ABC Book\nWeight of the book: 2\nBook: ABC Book (2 kg)\nPhone: Nokia 3210 (1 kg)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Item"}]},{"type":"text","value":" should provide the methods "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"weight"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":", which return the values stored in those attributes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The name and weight should be encapsulated within the class. The following code should not 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":"book "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"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 Book\""}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weight "},{"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":"h2","properties":{"id":"suitcase","style":"position:relative;"},"children":[{"type":"text","value":"Suitcase"},{"type":"element","tagName":"a","properties":{"href":"#suitcase","ariaLabel":"suitcase 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":"Please write a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Suitcase"}]},{"type":"text","value":". You should be able to pack items into a suitcase. A suitcase also has a maximum combined weight for the items stored within."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your class should contains the following members:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a constructor which takes the maximum weight as an argument"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_item"}]},{"type":"text","value":" which adds the item given as an argument to the suitcase. The method has no return value."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method which returns a string in the format \"x items (y kg)\""}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The class should make sure that the combined weight of the items stored within any "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Suitcase"}]},{"type":"text","value":" does not exceed the maximum weight set for that instance. If the maximum weight would be exceeded when the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_item"}]},{"type":"text","value":" method is called, the new item should not be added to the suitcase."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your class should work as follows:"}]},{"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":"book "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"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 Book\""}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nphone "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbrick "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Brick\""}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsuitcase "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Suitcase"},{"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"},{"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":"suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsuitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"book"},{"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":"suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsuitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"phone"},{"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":"suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsuitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"brick"},{"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":"suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Executing the above should print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0 items (0 kg)\n1 items (2 kg)\n2 items (3 kg)\n2 items (3 kg)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"mind-your-language","style":"position:relative;"},"children":[{"type":"text","value":"Mind your language"},{"type":"element","tagName":"a","properties":{"href":"#mind-your-language","ariaLabel":"mind your language 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 notification \"1 items\" is not very grammatical. Instead, it should say \"1 item\". Please make the required changes to your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The previous example should now print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0 items (0 kg)\n1 item (2 kg)\n2 items (3 kg)\n2 items (3 kg)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"all-the-items","style":"position:relative;"},"children":[{"type":"text","value":"All the items"},{"type":"element","tagName":"a","properties":{"href":"#all-the-items","ariaLabel":"all the items 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":"Please add the following methods to your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Suitcase"}]},{"type":"text","value":" class definition:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print_items"}]},{"type":"text","value":" prints out all the items stored in the suitcase"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"weight"}]},{"type":"text","value":" returns an integer number representing the combined weight of all the items stored in the suitcase"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your class should now work with the following program:"}]},{"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":"book "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"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 Book\""}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nphone "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbrick "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Brick\""}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsuitcase "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"phone"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"brick"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The suitcase contains the following items:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"\ncombined_weight "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weight"},{"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\"Combined weight: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"combined_weight"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" kg\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Executing the above program should print out this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The suitcase contains the following items:\nABC Book (2 kg)\nNokia 3210 (1 kg)\nBrick (4 kg)\nCombined weight: 7 kg"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you have implemented your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Suitcase"}]},{"type":"text","value":" class with more than two instance variables, please make the required changes so that each instance has only two data attributes: the maximum weight and a list of items within."}]},{"type":"element","tagName":"h2","properties":{"id":"the-heaviest-item","style":"position:relative;"},"children":[{"type":"text","value":"The heaviest item"},{"type":"element","tagName":"a","properties":{"href":"#the-heaviest-item","ariaLabel":"the heaviest item 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":"Please add a new method to your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Suitcase"}]},{"type":"text","value":" class: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"heaviest_item"}]},{"type":"text","value":" should return the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Item"}]},{"type":"text","value":" which is the heaviest. If there are two or more items with the same, heaviest weight, the method may return any one of these. The method should return a reference to an object. If the suitcase is empty, the method should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your class should now work with the following program:"}]},{"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":"book "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"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 Book\""}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nphone "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbrick "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Brick\""}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsuitcase "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"phone"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"brick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nheaviest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"heaviest_item"},{"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\"The heaviest item: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"heaviest"},{"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":"p","properties":{},"children":[{"type":"text","value":"Executing the above program should print out this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The heaviest item: Brick (4 kg)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"cargo-hold","style":"position:relative;"},"children":[{"type":"text","value":"Cargo hold"},{"type":"element","tagName":"a","properties":{"href":"#cargo-hold","ariaLabel":"cargo hold 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":"Please write a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CargoHold"}]},{"type":"text","value":" with the following methods:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a constructor which takes the maximum weight as an argument"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_suitcase"}]},{"type":"text","value":" which adds the suitcase given as an argument to the cargo hold"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method which returns a string in the format \"x suitcases, space for y kg\""}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The class should make sure that the combined weight of the items stored within any "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CargoHold"}]},{"type":"text","value":" does not exceed the maximum weight set for that instance. If the maximum weight would be exceeded when the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_suitcase"}]},{"type":"text","value":" method is called, the new suitcase should not be added to the cargo hold."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your class should now work with the following program:"}]},{"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":"cargo_hold "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CargoHold"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"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":"cargo_hold"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"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 Book\""}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nphone "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbrick "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Brick\""}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nadas_suitcase "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadas_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadas_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"phone"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\npeters_suitcase "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeters_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"brick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ncargo_hold"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"adas_suitcase"},{"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":"cargo_hold"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ncargo_hold"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_suitcase"},{"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":"cargo_hold"},{"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 suitcases, space for 1000 kg\n1 suitcase, space for 997 kg\n2 suitcases, space for 993 kg"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-contents-of-the-cargo-hold","style":"position:relative;"},"children":[{"type":"text","value":"The contents of the cargo hold"},{"type":"element","tagName":"a","properties":{"href":"#the-contents-of-the-cargo-hold","ariaLabel":"the contents of the cargo hold 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":"Please add a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print_items"}]},{"type":"text","value":" to your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CargoHold"}]},{"type":"text","value":" class. It should print out all the items in all the suitcases within the cargo hold."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your class should now work with the following program:"}]},{"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":"book "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"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 Book\""}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nphone "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbrick "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Brick\""}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nadas_suitcase "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadas_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadas_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"phone"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\npeters_suitcase "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeters_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"brick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ncargo_hold "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CargoHold"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncargo_hold"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"adas_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncargo_hold"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_suitcase"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The suitcases in the cargo hold contain the following items:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncargo_hold"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Executing the above program should print out this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The suitcases in the cargo hold contain the following items:\nABC Book (2 kg)\nNokia 3210 (1 kg)\nBrick (4 kg)"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this week's materials."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will be familiar with some more examples of classes and objects
    • \n
    • You will be able to use default values for parameters in your methods
    • \n

    The following example consists of two classes. The class Point is a model for a point in two-dimensional space. The class Line is a model for a line segment between two points. The code below is commented; please read the comments in order to understand how the classes work.

    import math\n\nclass Point:\n    \"\"\" The class represents a point in two-dimensional space \"\"\"\n\n    def __init__(self, x: float, y: float):\n        # These attributes are public because any value is acceptable for x and y\n        self.x = x\n        self.y = y\n\n    # This class method returns a new Point at origo (0, 0)\n    # It is possible to return a new instance of the class from within the class\n    @classmethod\n    def origo(cls):\n        return Point(0, 0)\n\n    # This class method creates a new Point based on an existing Point\n    # The original Point can be mirrored on either or both of the x and y axes\n    # For example, the Point (1, 3) mirrored on the x-axis is (1, -3)\n    @classmethod\n    def mirrored(cls, point: \"Point\", mirror_x: bool, mirror_y: bool):\n        x = point.x\n        y = point.y\n        if mirror_x:\n            y = -y\n        if mirror_y:\n            x = -x\n\n        return Point(x, y)\n\n    def __str__(self):\n        return f\"({self.x}, {self.y})\"\n\n\nclass Line:\n    \"\"\" The class represents a line segment in two-dimensional space \"\"\"\n\n    def __init__(self, beginning: Point, end: Point):\n        # These attributes are public because any two Points are acceptable\n        self.beginning = beginning\n        self.end = end\n\n    # This method uses the Pythagorean theorem to calculate the length of the line segment\n    def length(self):\n        sum_of_squares = (self.end.x - self.beginning.x) ** 2 + (self.end.y - self.beginning.y) ** 2\n        return math.sqrt(sum_of_squares)\n\n    # This method returns the Point in the middle of the line segment\n    def centre_point(self):\n        centre_x = (self.beginning.x + self.end.x) / 2\n        centre_y = (self.beginning.y + self.end.y) / 2\n        return Point(centre_x, centre_y)\n\n    def __str__(self):\n        return f\"{self.beginning} ... {self.end}\"
    point = Point(1,3)\nprint(point)\n\norigo = Point.origo()\nprint(origo)\n\npoint2 = Point.mirrored(point, True, True)\nprint(point2)\n\nline = Line(point, point2)\nprint(line.length())\nprint(line.centre_point())\nprint(line)

    (1, 3)\n(0, 0)\n(-1, -3)\n6.324555320336759\n(0.0, 0.0)\n(1, 3) ... (-1, -3)

    Default values of parameters

    In Python programming you can generally set a default value for any parameter. Default values can be used in both functions and methods.

    If a parameter has a default value, you do not have to include a value as an argument when calling the function. If an argument is given, the default value is ignored. If not, the default value is used.

    Default values are often used in constructors. If it can be expected that not all information is available when an object is created, it is better to include a default value in the definition of the constructor method than to force the client to take care of the issue. This makes using the class easier from the client's point of view, but it also ensures the integrity of the object. For instance, with a set default value we can be sure that an \"empty\" value is always the same, unless the client specifically wants to supply something different. If a default value is not set, it is up to the client to provide an \"empty\" value. This could be, for example, an empty string \"\", the special empty object None, or the string \"not set\".

    Let's have a look at yet another class representing a student. When creating a new Student object the client must provide a name and a student number. The student number is private and should not be changed later. Additionally, a Student object has attributes for study credits and notes, which have default values set in the constructor. New values can be passed as arguments to the constructor, but they can also be left out so that the default values are used instead. Please have a look at the comments in the code to better understand what each method does.

    class Student:\n    \"\"\" This class models a student \"\"\"\n\n    def __init__(self, name: str, student_number: str, credits: int = 0, notes: str = \"\"):\n        # calling the setter method for the name attribute\n        self.name = name\n\n        if len(student_number) < 5:\n            raise ValueError(\"A student number should have at least five characters\")\n\n        self.__student_number = student_number\n\n        # calling the setter method for the credits attribute\n        self.credits = credits\n\n        self.__notes = notes\n\n    @property\n    def name(self):\n        return self.__name\n\n    @name.setter\n    def name(self, name):\n        if name != \"\":\n            self.__name = name\n        else:\n            raise ValueError(\"The name cannot be an empty string\")\n\n    @property\n    def student_number(self):\n        return self.__student_number\n\n    @property\n    def credits(self):\n        return self.__credits\n\n    @credits.setter\n    def credits(self, op):\n        if op >= 0:\n            self.__credits = op\n        else:\n            raise ValueError(\"The number of study credits cannot be below zero\")\n\n    @property\n    def notes(self):\n        return self.__notes\n\n    @notes.setter\n    def notes(self, notes):\n        self.__notes = notes\n\n    def summary(self):\n        print(f\"Student {self.__name} ({self.student_number}):\")\n        print(f\"- credits: {self.__credits}\")\n        print(f\"- notes: {self.notes}\")
    # Passing only the name and the student number as arguments to the constructor\nstudent1 = Student(\"Sally Student\", \"12345\")\nstudent1.summary()\n\n# Passing the name, the student number and the number of study credits\nstudent2 = Student(\"Sassy Student\", \"54321\", 25)\nstudent2.summary()\n\n# Passing values for all the parameters\nstudent3 = Student(\"Saul Student\", \"99999\", 140, \"extra time in exam\")\nstudent3.summary()\n\n# Passing a value for notes, but not for study credits\n# NB: the parameter must be named now that the arguments are not in order\nstudent4 = Student(\"Sandy Student\", \"98765\", notes=\"absent in academic year 20-21\")\nstudent4.summary()

    Student Sally Student (12345):

      \n
    • credits: 0
    • \n
    • notes:
    • \n

    Student Sassy Student (54321):

      \n
    • credits: 25
    • \n
    • notes:
    • \n

    Student Saul Student (99999):

      \n
    • credits: 140
    • \n
    • notes: extra time in exam
    • \n

    Student Sandy Student (98765):

      \n
    • credits: 0
    • \n
    • notes: absent in academic year 20-21
    • \n

    NB: there is no setter method for the attribute student_number as the student number is not supposed to change.

    There is one rather significant snag when using default values for parameters. The following example modelling yet another kind of student will shed more light on this:

    class Student:\n    def __init__(self, name, completed_courses=[]):\n        self.name = name\n        self.completed_courses = completed_courses\n\n    def add_course(self, course):\n        self.completed_courses.append(course)
    student1 = Student(\"Sally Student\")\nstudent2 = Student(\"Sassy Student\")\n\nstudent1.add_course(\"ItP\")\nstudent1.add_course(\"ACiP\")\n\nprint(student1.completed_courses)\nprint(student2.completed_courses)

    ['ItP', 'ACiP']\n['ItP', 'ACiP']

    Adding completed courses to Sally's list also adds those courses to Sassy's list. In fact, these two are the exact same list, as Python reuses the reference stored in the default value. Creating the two new Student objects in the above example is equivalent to the following:

    courses = []\nstudent1 = Student(\"Sally Student\", courses)\nstudent2 = Student(\"Sassy Student\", courses)

    The default values of parameters should never be instances of more complicated, mutable data structures, such as lists. The problem can be circumvented by making the following changes to the constructor of the Student class:

    class Student:\n    def __init__(self, name, completed_courses=None):\n        self.name = name\n        if completed_courses is None:\n            self.completed_courses = []\n        else:\n            self.completed_courses = completed_courses\n\n    def add_course(self, course):\n        self.completed_courses.append(course)
    student1 = Student(\"Sally Student\")\nstudent2 = Student(\"Sassy Student\")\n\nstudent1.add_course(\"ItP\")\nstudent1.add_course(\"ACiP\")\n\nprint(student1.completed_courses)\nprint(student2.completed_courses)

    ['ItP', 'ACiP']\n[]

    The Grand Finale

    Even though the following exercise finishes off this part of the material, the techniques required to solve it were all covered already in the section named objects as attributes. Specifically, you are not required to use the @property decorator or default values for parameters in this exercise. This exercise is very similar to the exercises a box of presents and the shortest person in the room.

    Important information regarding the next exercise

    Please note that there is an issue resulting from an update in Python, which conflicts with the inbuilt library and the original file name for this exercise. If you experience any issues, we recommend redownloading the exercise folder. Once you have obtained the new local test files, you can use either \"code.py\" or \"code_1.py\" as the file name. While using Visual Studio Code, you may receive notifications about problems in the test file. However, these notifications can be safely ignored, as they are caused by the test's inability to import from either the \"code.py\" or \"code_1.py\" files.

    In this series of exercises you will create the classes Item, Suitcase and Cargo Hold, which will let you further practice working on objects which contain references to other objects.

    Item

    Please create a class named Item which is used to create items of different kinds. Each item has a name and a weight (in kilograms).

    You can use the following code to test your class:

    book = Item(\"ABC Book\", 2)\nphone = Item(\"Nokia 3210\", 1)\n\nprint(\"Name of the book:\", book.name())\nprint(\"Weight of the book:\", book.weight())\n\nprint(\"Book:\", book)\nprint(\"Phone:\", phone)

    Your program should print out this:

    Name of the book: ABC Book\nWeight of the book: 2\nBook: ABC Book (2 kg)\nPhone: Nokia 3210 (1 kg)

    An Item should provide the methods weight and name, which return the values stored in those attributes.

    The name and weight should be encapsulated within the class. The following code should not work:

    book = Item(\"ABC Book\", 2)\nbook.weight = 10

    Suitcase

    Please write a class named Suitcase. You should be able to pack items into a suitcase. A suitcase also has a maximum combined weight for the items stored within.

    Your class should contains the following members:

      \n
    • a constructor which takes the maximum weight as an argument
    • \n
    • a method named add_item which adds the item given as an argument to the suitcase. The method has no return value.
    • \n
    • a __str__ method which returns a string in the format \"x items (y kg)\"
    • \n

    The class should make sure that the combined weight of the items stored within any Suitcase does not exceed the maximum weight set for that instance. If the maximum weight would be exceeded when the add_item method is called, the new item should not be added to the suitcase.

    Your class should work as follows:

    book = Item(\"ABC Book\", 2)\nphone = Item(\"Nokia 3210\", 1)\nbrick = Item(\"Brick\", 4)\n\nsuitcase = Suitcase(5)\nprint(suitcase)\n\nsuitcase.add_item(book)\nprint(suitcase)\n\nsuitcase.add_item(phone)\nprint(suitcase)\n\nsuitcase.add_item(brick)\nprint(suitcase)

    Executing the above should print out

    0 items (0 kg)\n1 items (2 kg)\n2 items (3 kg)\n2 items (3 kg)

    Mind your language

    The notification \"1 items\" is not very grammatical. Instead, it should say \"1 item\". Please make the required changes to your __str__ method.

    The previous example should now print out

    0 items (0 kg)\n1 item (2 kg)\n2 items (3 kg)\n2 items (3 kg)

    All the items

    Please add the following methods to your Suitcase class definition:

      \n
    • print_items prints out all the items stored in the suitcase
    • \n
    • weight returns an integer number representing the combined weight of all the items stored in the suitcase
    • \n

    Your class should now work with the following program:

    book = Item(\"ABC Book\", 2)\nphone = Item(\"Nokia 3210\", 1)\nbrick = Item(\"Brick\", 4)\n\nsuitcase = Suitcase(10)\nsuitcase.add_item(book)\nsuitcase.add_item(phone)\nsuitcase.add_item(brick)\n\nprint(\"The suitcase contains the following items:\")\nsuitcase.print_items()\ncombined_weight = suitcase.weight()\nprint(f\"Combined weight: {combined_weight} kg\")

    Executing the above program should print out this:

    The suitcase contains the following items:\nABC Book (2 kg)\nNokia 3210 (1 kg)\nBrick (4 kg)\nCombined weight: 7 kg

    If you have implemented your Suitcase class with more than two instance variables, please make the required changes so that each instance has only two data attributes: the maximum weight and a list of items within.

    The heaviest item

    Please add a new method to your Suitcase class: heaviest_item should return the Item which is the heaviest. If there are two or more items with the same, heaviest weight, the method may return any one of these. The method should return a reference to an object. If the suitcase is empty, the method should return None.

    Your class should now work with the following program:

    book = Item(\"ABC Book\", 2)\nphone = Item(\"Nokia 3210\", 1)\nbrick = Item(\"Brick\", 4)\n\nsuitcase = Suitcase(10)\nsuitcase.add_item(book)\nsuitcase.add_item(phone)\nsuitcase.add_item(brick)\n\nheaviest = suitcase.heaviest_item()\nprint(f\"The heaviest item: {heaviest}\")

    Executing the above program should print out this:

    The heaviest item: Brick (4 kg)

    Cargo hold

    Please write a class named CargoHold with the following methods:

      \n
    • a constructor which takes the maximum weight as an argument
    • \n
    • a method named add_suitcase which adds the suitcase given as an argument to the cargo hold
    • \n
    • a __str__ method which returns a string in the format \"x suitcases, space for y kg\"
    • \n

    The class should make sure that the combined weight of the items stored within any CargoHold does not exceed the maximum weight set for that instance. If the maximum weight would be exceeded when the add_suitcase method is called, the new suitcase should not be added to the cargo hold.

    Your class should now work with the following program:

    cargo_hold = CargoHold(1000)\nprint(cargo_hold)\n\nbook = Item(\"ABC Book\", 2)\nphone = Item(\"Nokia 3210\", 1)\nbrick = Item(\"Brick\", 4)\n\nadas_suitcase = Suitcase(10)\nadas_suitcase.add_item(book)\nadas_suitcase.add_item(phone)\n\npeters_suitcase = Suitcase(10)\npeters_suitcase.add_item(brick)\n\ncargo_hold.add_suitcase(adas_suitcase)\nprint(cargo_hold)\n\ncargo_hold.add_suitcase(peters_suitcase)\nprint(cargo_hold)

    0 suitcases, space for 1000 kg\n1 suitcase, space for 997 kg\n2 suitcases, space for 993 kg

    The contents of the cargo hold

    Please add a method named print_items to your CargoHold class. It should print out all the items in all the suitcases within the cargo hold.

    Your class should now work with the following program:

    book = Item(\"ABC Book\", 2)\nphone = Item(\"Nokia 3210\", 1)\nbrick = Item(\"Brick\", 4)\n\nadas_suitcase = Suitcase(10)\nadas_suitcase.add_item(book)\nadas_suitcase.add_item(phone)\n\npeters_suitcase = Suitcase(10)\npeters_suitcase.add_item(brick)\n\ncargo_hold = CargoHold(1000)\ncargo_hold.add_suitcase(adas_suitcase)\ncargo_hold.add_suitcase(peters_suitcase)\n\nprint(\"The suitcases in the cargo hold contain the following items:\")\ncargo_hold.print_items()

    Executing the above program should print out this:

    The suitcases in the cargo hold contain the following items:\nABC Book (2 kg)\nNokia 3210 (1 kg)\nBrick (4 kg)

    Please respond to a quick questionnaire on this week's materials.

    ","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-9/6-more-examples-with-classes.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}}]}},"pageContext":{}}, + "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 some more examples of classes and objects"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"You will be able to use default values for parameters in your methods"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The following example consists of two classes. The class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Point"}]},{"type":"text","value":" is a model for a point in two-dimensional space. The class "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Line"}]},{"type":"text","value":" is a model for a line segment between two points. The code below is commented; please read the comments in order to understand how the classes 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":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"import"}]},{"type":"text","value":" math\n\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Point"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The class represents a point in two-dimensional space \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" x"},{"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":"float"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"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":"float"}]},{"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":"# These attributes are public because any value is acceptable for x and y"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" x\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" y\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This class method returns a new Point at origo (0, 0)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# It is possible to return a new instance of the class from within the class"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@classmethod"}]},{"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":"origo"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"cls"},{"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":" Point"},{"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","number"]},"children":[{"type":"text","value":"0"}]},{"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":"# This class method creates a new Point based on an existing Point"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# The original Point can be mirrored on either or both of the x and y axes"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# For example, the Point (1, 3) mirrored on the x-axis is (1, -3)"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@classmethod"}]},{"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":"mirrored"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"cls"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" point"},{"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":"\"Point\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mirror_x"},{"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":"bool"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" mirror_y"},{"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":"bool"}]},{"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 x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" point"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x\n y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" point"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" mirror_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"y\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" mirror_y"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":"x\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" Point"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" y"},{"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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y"},{"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\n"},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Line"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" The class represents a line segment in two-dimensional space \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" beginning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Point"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" end"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":" Point"},{"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":"# These attributes are public because any two Points are acceptable"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"beginning "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" beginning\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"end "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" end\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# This method uses the Pythagorean theorem to calculate the length of the line segment"}]},{"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":"length"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 sum_of_squares "},{"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":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"end"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"beginning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x"},{"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":"2"}]},{"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","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"end"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"-"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"beginning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y"},{"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":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" math"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"sqrt"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"sum_of_squares"},{"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":"# This method returns the Point in the middle of the line segment"}]},{"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":"centre_point"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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 centre_x "},{"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":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"beginning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"end"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"x"},{"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":"2"}]},{"type":"text","value":"\n centre_y "},{"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":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"beginning"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"+"}]},{"type":"text","value":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"end"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"y"},{"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":"2"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"return"}]},{"type":"text","value":" Point"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"centre_x"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" centre_y"},{"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":"__str__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" "},{"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":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"beginning"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"end"},{"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":"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":"point "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Point"},{"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":"3"}]},{"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":"point"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\norigo "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Point"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"origo"},{"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":"origo"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\npoint2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Point"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"mirrored"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"point"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"True"}]},{"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":"point2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nline "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"point"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" point2"},{"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":"line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"length"},{"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"},{"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":"line"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"centre_point"},{"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"},{"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":"line"},{"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":"(1, 3)\n(0, 0)\n(-1, -3)\n6.324555320336759\n(0.0, 0.0)\n(1, 3) ... (-1, -3)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"default-values-of-parameters","style":"position:relative;"},"children":[{"type":"text","value":"Default values of parameters"},{"type":"element","tagName":"a","properties":{"href":"#default-values-of-parameters","ariaLabel":"default values of parameters 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 Python programming you can generally set a default value for any parameter. Default values can be used in both functions and methods."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If a parameter has a default value, you do not have to include a value as an argument when calling the function. If an argument is given, the default value is ignored. If not, the default value is used."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Default values are often used in constructors. If it can be expected that not all information is available when an object is created, it is better to include a default value in the definition of the constructor method than to force the client to take care of the issue. This makes using the class easier from the client's point of view, but it also ensures the integrity of the object. For instance, with a set default value we can be sure that an \"empty\" value is always the same, unless the client specifically wants to supply something different. If a default value is not set, it is up to the client to provide an \"empty\" value. This could be, for example, an empty string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"\""}]},{"type":"text","value":", the special empty object "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":", or the string "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"\"not set\""}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Let's have a look at yet another class representing a student. When creating a new Student object the client must provide a name and a student number. The student number is private and should not be changed later. Additionally, a Student object has attributes for study credits and notes, which have default values set in the constructor. New values can be passed as arguments to the constructor, but they can also be left out so that the default values are used instead. Please have a look at the comments in the code to better understand what each method does."}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n "},{"type":"element","tagName":"span","properties":{"className":["token","triple-quoted-string","string"]},"children":[{"type":"text","value":"\"\"\" This class models a student \"\"\""}]},{"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":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" student_number"},{"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":"str"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" credits"},{"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":"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":"0"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" notes"},{"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":"str"}]},{"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":"\"\""}]},{"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":"# calling the setter method for the name attribute"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"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":"student_number"},{"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":"5"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"A student number should have at least five characters\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__student_number "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" student_number\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# calling the setter method for the credits attribute"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" credits\n\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__notes "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" notes\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"name"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"name"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"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":"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":"\"\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 name cannot be an empty string\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"student_number"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__student_number\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"credits"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__credits\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@credits"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"credits"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" op"},{"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":"if"}]},{"type":"text","value":" op "},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__credits "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" op\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"else"}]},{"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":"raise"}]},{"type":"text","value":" ValueError"},{"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 number of study credits cannot be below zero\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@property"}]},{"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":"notes"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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":" self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__notes\n\n "},{"type":"element","tagName":"span","properties":{"className":["token","decorator","annotation","punctuation"]},"children":[{"type":"text","value":"@notes"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"setter"}]},{"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":"notes"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" notes"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__notes "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" notes\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":"summary"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"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\"Student "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__name"},{"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","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"student_number"},{"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 "},{"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\"- credits: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"__credits"},{"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 "},{"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\"- notes: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"notes"},{"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":"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":"# Passing only the name and the student number as arguments to the constructor"}]},{"type":"text","value":"\nstudent1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sally Student\""}]},{"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":"\"12345\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstudent1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"summary"},{"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","comment"]},"children":[{"type":"text","value":"# Passing the name, the student number and the number of study credits"}]},{"type":"text","value":"\nstudent2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sassy Student\""}]},{"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":"\"54321\""}]},{"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":"25"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstudent2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"summary"},{"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","comment"]},"children":[{"type":"text","value":"# Passing values for all the parameters"}]},{"type":"text","value":"\nstudent3 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Saul Student\""}]},{"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":"\"99999\""}]},{"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":"140"}]},{"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":"\"extra time in exam\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstudent3"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"summary"},{"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","comment"]},"children":[{"type":"text","value":"# Passing a value for notes, but not for study credits"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"span","properties":{"className":["token","comment"]},"children":[{"type":"text","value":"# NB: the parameter must be named now that the arguments are not in order"}]},{"type":"text","value":"\nstudent4 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sandy Student\""}]},{"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":"\"98765\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" notes"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"absent in academic year 20-21\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstudent4"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"summary"},{"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":"Student Sally Student (12345):"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"credits: 0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"notes:"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Student Sassy Student (54321):"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"credits: 25"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"notes:"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Student Saul Student (99999):"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"credits: 140"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"notes: extra time in exam"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Student Sandy Student (98765):"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"credits: 0"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"notes: absent in academic year 20-21"}]},{"type":"text","value":"\n"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"NB: there is no setter method for the attribute "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"student_number"}]},{"type":"text","value":" as the student number is not supposed to change."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"There is one rather significant snag when using default values for parameters. The following example modelling yet another kind of student will shed more light on 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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" completed_courses"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"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","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"completed_courses "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" completed_courses\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":"add_course"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" course"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"completed_courses"},{"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":"course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"student1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sally Student\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstudent2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sassy Student\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nstudent1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ItP\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstudent1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ACiP\""}]},{"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":"student1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"completed_courses"},{"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":"student2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"completed_courses"},{"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":"['ItP', 'ACiP']\n['ItP', 'ACiP']"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Adding completed courses to Sally's list also adds those courses to Sassy's list. In fact, these two are the exact same list, as Python reuses the reference stored in the default value. Creating the two new Student objects in the above example is equivalent to the following:"}]},{"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":"courses "},{"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":"\nstudent1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sally Student\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" courses"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstudent2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sassy Student\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" courses"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The default values of parameters should never be instances of more complicated, mutable data structures, such as lists. The problem can be circumvented by making the following changes to the constructor of the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Student"}]},{"type":"text","value":" class:"}]},{"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":"class"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","class-name"]},"children":[{"type":"text","value":"Student"}]},{"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":"def"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","function"]},"children":[{"type":"text","value":"__init__"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" name"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" completed_courses"},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" name\n "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"if"}]},{"type":"text","value":" completed_courses "},{"type":"element","tagName":"span","properties":{"className":["token","keyword"]},"children":[{"type":"text","value":"is"}]},{"type":"text","value":" "},{"type":"element","tagName":"span","properties":{"className":["token","boolean"]},"children":[{"type":"text","value":"None"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"completed_courses "},{"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","keyword"]},"children":[{"type":"text","value":"else"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":":"}]},{"type":"text","value":"\n self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"completed_courses "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" completed_courses\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":"add_course"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" course"},{"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 self"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"completed_courses"},{"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":"course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"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":"student1 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sally Student\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstudent2 "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Student"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Sassy Student\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nstudent1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ItP\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nstudent1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_course"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"ACiP\""}]},{"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":"student1"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"completed_courses"},{"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":"student2"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"completed_courses"},{"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":"['ItP', 'ACiP']\n[]"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-grand-finale","style":"position:relative;"},"children":[{"type":"text","value":"The Grand Finale"},{"type":"element","tagName":"a","properties":{"href":"#the-grand-finale","ariaLabel":"the grand finale 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":"Even though the following exercise finishes off this part of the material, the techniques required to solve it were all covered already in the section named "},{"type":"element","tagName":"a","properties":{"href":"/part-9/2-objects-as-attributes"},"children":[{"type":"text","value":"objects as attributes"}]},{"type":"text","value":". Specifically, you are not required to use the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"@property"}]},{"type":"text","value":" decorator or default values for parameters in this exercise. This exercise is very similar to the exercises "},{"type":"element","tagName":"a","properties":{"href":"/part-9/2-objects-as-attributes#programming-exercise-a-box-of-presents"},"children":[{"type":"text","value":"a box of presents"}]},{"type":"text","value":" and "},{"type":"element","tagName":"a","properties":{"href":"/part-9/2-objects-as-attributes#programming-exercise-the-shortest-person-in-the-room"},"children":[{"type":"text","value":"the shortest person in the room"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"h4","properties":{"id":"important-information-regarding-the-next-exercise","style":"position:relative;"},"children":[{"type":"text","value":"Important information regarding the next exercise"},{"type":"element","tagName":"a","properties":{"href":"#important-information-regarding-the-next-exercise","ariaLabel":"important information regarding the next exercise 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":"Please note that there is an issue resulting from an update in Python, which conflicts with the inbuilt library and the original file name for this exercise. If you experience any issues, we recommend redownloading the exercise folder. Once you have obtained the new local test files, you can use either \"code.py\" or \"code_1.py\" as the file name. While using Visual Studio Code, you may receive notifications about problems in the test file. However, these notifications can be safely ignored, as they are caused by the test's inability to import from either the \"code.py\" or \"code_1.py\" files."}]},{"type":"element","tagName":"programming-exercise","properties":{"name":"Item, Suitcase and Cargo hold","tmcname":"part09-15_item_suitcase_hold"},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"In this series of exercises you will create the classes "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Item"}]},{"type":"text","value":", "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Suitcase"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Cargo Hold"}]},{"type":"text","value":", which will let you further practice working on objects which contain references to other objects."}]},{"type":"element","tagName":"h2","properties":{"id":"item","style":"position:relative;"},"children":[{"type":"text","value":"Item"},{"type":"element","tagName":"a","properties":{"href":"#item","ariaLabel":"item 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":"Please create a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Item"}]},{"type":"text","value":" which is used to create items of different kinds. Each item has a name and a weight (in kilograms)."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"You can use the following code to test your class:"}]},{"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":"book "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"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 Book\""}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nphone "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"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":"1"}]},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Name of the book:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"name"},{"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"},{"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":"\"Weight of the book:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weight"},{"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\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":"\"Book:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" book"},{"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":"\"Phone:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":","}]},{"type":"text","value":" phone"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your program should print out this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Name of the book: ABC Book\nWeight of the book: 2\nBook: ABC Book (2 kg)\nPhone: Nokia 3210 (1 kg)"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"An "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Item"}]},{"type":"text","value":" should provide the methods "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"weight"}]},{"type":"text","value":" and "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"name"}]},{"type":"text","value":", which return the values stored in those attributes."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The name and weight should be encapsulated within the class. The following code should not 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":"book "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"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 Book\""}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbook"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weight "},{"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":"h2","properties":{"id":"suitcase","style":"position:relative;"},"children":[{"type":"text","value":"Suitcase"},{"type":"element","tagName":"a","properties":{"href":"#suitcase","ariaLabel":"suitcase 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":"Please write a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Suitcase"}]},{"type":"text","value":". You should be able to pack items into a suitcase. A suitcase also has a maximum combined weight for the items stored within."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your class should contains the following members:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a constructor which takes the maximum weight as an argument"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_item"}]},{"type":"text","value":" which adds the item given as an argument to the suitcase. The method has no return value."}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method which returns a string in the format \"x items (y kg)\""}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The class should make sure that the combined weight of the items stored within any "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Suitcase"}]},{"type":"text","value":" does not exceed the maximum weight set for that instance. If the maximum weight would be exceeded when the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_item"}]},{"type":"text","value":" method is called, the new item should not be added to the suitcase."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your class should work as follows:"}]},{"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":"book "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"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 Book\""}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nphone "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbrick "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Brick\""}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsuitcase "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Suitcase"},{"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"},{"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":"suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsuitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"book"},{"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":"suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsuitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"phone"},{"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":"suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsuitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"brick"},{"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":"suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Executing the above should print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0 items (0 kg)\n1 items (2 kg)\n2 items (3 kg)\n2 items (3 kg)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"mind-your-language","style":"position:relative;"},"children":[{"type":"text","value":"Mind your language"},{"type":"element","tagName":"a","properties":{"href":"#mind-your-language","ariaLabel":"mind your language 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 notification \"1 items\" is not very grammatical. Instead, it should say \"1 item\". Please make the required changes to your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The previous example should now print out"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"0 items (0 kg)\n1 item (2 kg)\n2 items (3 kg)\n2 items (3 kg)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"all-the-items","style":"position:relative;"},"children":[{"type":"text","value":"All the items"},{"type":"element","tagName":"a","properties":{"href":"#all-the-items","ariaLabel":"all the items 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":"Please add the following methods to your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Suitcase"}]},{"type":"text","value":" class definition:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print_items"}]},{"type":"text","value":" prints out all the items stored in the suitcase"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"weight"}]},{"type":"text","value":" returns an integer number representing the combined weight of all the items stored in the suitcase"}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your class should now work with the following program:"}]},{"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":"book "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"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 Book\""}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nphone "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbrick "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Brick\""}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsuitcase "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"phone"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"brick"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The suitcase contains the following items:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"\ncombined_weight "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"weight"},{"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\"Combined weight: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"combined_weight"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"}"}]}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":" kg\""}]}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Executing the above program should print out this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The suitcase contains the following items:\nABC Book (2 kg)\nNokia 3210 (1 kg)\nBrick (4 kg)\nCombined weight: 7 kg"}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"If you have implemented your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Suitcase"}]},{"type":"text","value":" class with more than two instance variables, please make the required changes so that each instance has only two data attributes: the maximum weight and a list of items within."}]},{"type":"element","tagName":"h2","properties":{"id":"the-heaviest-item","style":"position:relative;"},"children":[{"type":"text","value":"The heaviest item"},{"type":"element","tagName":"a","properties":{"href":"#the-heaviest-item","ariaLabel":"the heaviest item 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":"Please add a new method to your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Suitcase"}]},{"type":"text","value":" class: "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"heaviest_item"}]},{"type":"text","value":" should return the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"Item"}]},{"type":"text","value":" which is the heaviest. If there are two or more items with the same, heaviest weight, the method may return any one of these. The method should return a reference to an object. If the suitcase is empty, the method should return "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"None"}]},{"type":"text","value":"."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your class should now work with the following program:"}]},{"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":"book "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"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 Book\""}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nphone "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbrick "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Brick\""}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nsuitcase "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"phone"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nsuitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"brick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nheaviest "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"heaviest_item"},{"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\"The heaviest item: "}]},{"type":"element","tagName":"span","properties":{"className":["token","interpolation"]},"children":[{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"{"}]},{"type":"text","value":"heaviest"},{"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":"p","properties":{},"children":[{"type":"text","value":"Executing the above program should print out this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The heaviest item: Brick (4 kg)"}]}]},{"type":"element","tagName":"h2","properties":{"id":"cargo-hold","style":"position:relative;"},"children":[{"type":"text","value":"Cargo hold"},{"type":"element","tagName":"a","properties":{"href":"#cargo-hold","ariaLabel":"cargo hold 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":"Please write a class named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CargoHold"}]},{"type":"text","value":" with the following methods:"}]},{"type":"element","tagName":"ul","properties":{},"children":[{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a constructor which takes the maximum weight as an argument"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_suitcase"}]},{"type":"text","value":" which adds the suitcase given as an argument to the cargo hold"}]},{"type":"text","value":"\n"},{"type":"element","tagName":"li","properties":{},"children":[{"type":"text","value":"a "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"__str__"}]},{"type":"text","value":" method which returns a string in the format \"x suitcases, space for y kg\""}]},{"type":"text","value":"\n"}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The class should make sure that the combined weight of the items stored within any "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CargoHold"}]},{"type":"text","value":" does not exceed the maximum weight set for that instance. If the maximum weight would be exceeded when the "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"add_suitcase"}]},{"type":"text","value":" method is called, the new suitcase should not be added to the cargo hold."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your class should now work with the following program:"}]},{"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":"cargo_hold "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CargoHold"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"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":"cargo_hold"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nbook "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"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 Book\""}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nphone "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbrick "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Brick\""}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nadas_suitcase "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadas_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadas_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"phone"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\npeters_suitcase "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeters_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"brick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ncargo_hold"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"adas_suitcase"},{"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":"cargo_hold"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ncargo_hold"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_suitcase"},{"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":"cargo_hold"},{"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 suitcases, space for 1000 kg\n1 suitcase, space for 997 kg\n2 suitcases, space for 993 kg"}]}]},{"type":"element","tagName":"h2","properties":{"id":"the-contents-of-the-cargo-hold","style":"position:relative;"},"children":[{"type":"text","value":"The contents of the cargo hold"},{"type":"element","tagName":"a","properties":{"href":"#the-contents-of-the-cargo-hold","ariaLabel":"the contents of the cargo hold 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":"Please add a method named "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"print_items"}]},{"type":"text","value":" to your "},{"type":"element","tagName":"code","properties":{"className":["language-text"]},"children":[{"type":"text","value":"CargoHold"}]},{"type":"text","value":" class. It should print out all the items in all the suitcases within the cargo hold."}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Your class should now work with the following program:"}]},{"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":"book "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"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 Book\""}]},{"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":"2"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nphone "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Nokia 3210\""}]},{"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":"1"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nbrick "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"Brick\""}]},{"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":"4"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\nadas_suitcase "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadas_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"book"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\nadas_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"phone"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\npeters_suitcase "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" Suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"10"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\npeters_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_item"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"brick"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\n\ncargo_hold "},{"type":"element","tagName":"span","properties":{"className":["token","operator"]},"children":[{"type":"text","value":"="}]},{"type":"text","value":" CargoHold"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"element","tagName":"span","properties":{"className":["token","number"]},"children":[{"type":"text","value":"1000"}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncargo_hold"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"adas_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncargo_hold"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"add_suitcase"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"("}]},{"type":"text","value":"peters_suitcase"},{"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":"element","tagName":"span","properties":{"className":["token","string"]},"children":[{"type":"text","value":"\"The suitcases in the cargo hold contain the following items:\""}]},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":")"}]},{"type":"text","value":"\ncargo_hold"},{"type":"element","tagName":"span","properties":{"className":["token","punctuation"]},"children":[{"type":"text","value":"."}]},{"type":"text","value":"print_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":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Executing the above program should print out this:"}]},{"type":"element","tagName":"sample-output","properties":{},"children":[{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"The suitcases in the cargo hold contain the following items:\nABC Book (2 kg)\nNokia 3210 (1 kg)\nBrick (4 kg)"}]}]}]},{"type":"element","tagName":"p","properties":{},"children":[{"type":"text","value":"Please respond to a quick questionnaire on this week's materials."}]},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"quiz","properties":{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b"},"children":[]}]}]},"html":"

    After this section

      \n
    • You will be familiar with some more examples of classes and objects
    • \n
    • You will be able to use default values for parameters in your methods
    • \n

    The following example consists of two classes. The class Point is a model for a point in two-dimensional space. The class Line is a model for a line segment between two points. The code below is commented; please read the comments in order to understand how the classes work.

    import math\n\nclass Point:\n    \"\"\" The class represents a point in two-dimensional space \"\"\"\n\n    def __init__(self, x: float, y: float):\n        # These attributes are public because any value is acceptable for x and y\n        self.x = x\n        self.y = y\n\n    # This class method returns a new Point at origo (0, 0)\n    # It is possible to return a new instance of the class from within the class\n    @classmethod\n    def origo(cls):\n        return Point(0, 0)\n\n    # This class method creates a new Point based on an existing Point\n    # The original Point can be mirrored on either or both of the x and y axes\n    # For example, the Point (1, 3) mirrored on the x-axis is (1, -3)\n    @classmethod\n    def mirrored(cls, point: \"Point\", mirror_x: bool, mirror_y: bool):\n        x = point.x\n        y = point.y\n        if mirror_x:\n            y = -y\n        if mirror_y:\n            x = -x\n\n        return Point(x, y)\n\n    def __str__(self):\n        return f\"({self.x}, {self.y})\"\n\n\nclass Line:\n    \"\"\" The class represents a line segment in two-dimensional space \"\"\"\n\n    def __init__(self, beginning: Point, end: Point):\n        # These attributes are public because any two Points are acceptable\n        self.beginning = beginning\n        self.end = end\n\n    # This method uses the Pythagorean theorem to calculate the length of the line segment\n    def length(self):\n        sum_of_squares = (self.end.x - self.beginning.x) ** 2 + (self.end.y - self.beginning.y) ** 2\n        return math.sqrt(sum_of_squares)\n\n    # This method returns the Point in the middle of the line segment\n    def centre_point(self):\n        centre_x = (self.beginning.x + self.end.x) / 2\n        centre_y = (self.beginning.y + self.end.y) / 2\n        return Point(centre_x, centre_y)\n\n    def __str__(self):\n        return f\"{self.beginning} ... {self.end}\"
    point = Point(1,3)\nprint(point)\n\norigo = Point.origo()\nprint(origo)\n\npoint2 = Point.mirrored(point, True, True)\nprint(point2)\n\nline = Line(point, point2)\nprint(line.length())\nprint(line.centre_point())\nprint(line)

    (1, 3)\n(0, 0)\n(-1, -3)\n6.324555320336759\n(0.0, 0.0)\n(1, 3) ... (-1, -3)

    Default values of parameters

    In Python programming you can generally set a default value for any parameter. Default values can be used in both functions and methods.

    If a parameter has a default value, you do not have to include a value as an argument when calling the function. If an argument is given, the default value is ignored. If not, the default value is used.

    Default values are often used in constructors. If it can be expected that not all information is available when an object is created, it is better to include a default value in the definition of the constructor method than to force the client to take care of the issue. This makes using the class easier from the client's point of view, but it also ensures the integrity of the object. For instance, with a set default value we can be sure that an \"empty\" value is always the same, unless the client specifically wants to supply something different. If a default value is not set, it is up to the client to provide an \"empty\" value. This could be, for example, an empty string \"\", the special empty object None, or the string \"not set\".

    Let's have a look at yet another class representing a student. When creating a new Student object the client must provide a name and a student number. The student number is private and should not be changed later. Additionally, a Student object has attributes for study credits and notes, which have default values set in the constructor. New values can be passed as arguments to the constructor, but they can also be left out so that the default values are used instead. Please have a look at the comments in the code to better understand what each method does.

    class Student:\n    \"\"\" This class models a student \"\"\"\n\n    def __init__(self, name: str, student_number: str, credits: int = 0, notes: str = \"\"):\n        # calling the setter method for the name attribute\n        self.name = name\n\n        if len(student_number) < 5:\n            raise ValueError(\"A student number should have at least five characters\")\n\n        self.__student_number = student_number\n\n        # calling the setter method for the credits attribute\n        self.credits = credits\n\n        self.__notes = notes\n\n    @property\n    def name(self):\n        return self.__name\n\n    @name.setter\n    def name(self, name):\n        if name != \"\":\n            self.__name = name\n        else:\n            raise ValueError(\"The name cannot be an empty string\")\n\n    @property\n    def student_number(self):\n        return self.__student_number\n\n    @property\n    def credits(self):\n        return self.__credits\n\n    @credits.setter\n    def credits(self, op):\n        if op >= 0:\n            self.__credits = op\n        else:\n            raise ValueError(\"The number of study credits cannot be below zero\")\n\n    @property\n    def notes(self):\n        return self.__notes\n\n    @notes.setter\n    def notes(self, notes):\n        self.__notes = notes\n\n    def summary(self):\n        print(f\"Student {self.__name} ({self.student_number}):\")\n        print(f\"- credits: {self.__credits}\")\n        print(f\"- notes: {self.notes}\")
    # Passing only the name and the student number as arguments to the constructor\nstudent1 = Student(\"Sally Student\", \"12345\")\nstudent1.summary()\n\n# Passing the name, the student number and the number of study credits\nstudent2 = Student(\"Sassy Student\", \"54321\", 25)\nstudent2.summary()\n\n# Passing values for all the parameters\nstudent3 = Student(\"Saul Student\", \"99999\", 140, \"extra time in exam\")\nstudent3.summary()\n\n# Passing a value for notes, but not for study credits\n# NB: the parameter must be named now that the arguments are not in order\nstudent4 = Student(\"Sandy Student\", \"98765\", notes=\"absent in academic year 20-21\")\nstudent4.summary()

    Student Sally Student (12345):

      \n
    • credits: 0
    • \n
    • notes:
    • \n

    Student Sassy Student (54321):

      \n
    • credits: 25
    • \n
    • notes:
    • \n

    Student Saul Student (99999):

      \n
    • credits: 140
    • \n
    • notes: extra time in exam
    • \n

    Student Sandy Student (98765):

      \n
    • credits: 0
    • \n
    • notes: absent in academic year 20-21
    • \n

    NB: there is no setter method for the attribute student_number as the student number is not supposed to change.

    There is one rather significant snag when using default values for parameters. The following example modelling yet another kind of student will shed more light on this:

    class Student:\n    def __init__(self, name, completed_courses=[]):\n        self.name = name\n        self.completed_courses = completed_courses\n\n    def add_course(self, course):\n        self.completed_courses.append(course)
    student1 = Student(\"Sally Student\")\nstudent2 = Student(\"Sassy Student\")\n\nstudent1.add_course(\"ItP\")\nstudent1.add_course(\"ACiP\")\n\nprint(student1.completed_courses)\nprint(student2.completed_courses)

    ['ItP', 'ACiP']\n['ItP', 'ACiP']

    Adding completed courses to Sally's list also adds those courses to Sassy's list. In fact, these two are the exact same list, as Python reuses the reference stored in the default value. Creating the two new Student objects in the above example is equivalent to the following:

    courses = []\nstudent1 = Student(\"Sally Student\", courses)\nstudent2 = Student(\"Sassy Student\", courses)

    The default values of parameters should never be instances of more complicated, mutable data structures, such as lists. The problem can be circumvented by making the following changes to the constructor of the Student class:

    class Student:\n    def __init__(self, name, completed_courses=None):\n        self.name = name\n        if completed_courses is None:\n            self.completed_courses = []\n        else:\n            self.completed_courses = completed_courses\n\n    def add_course(self, course):\n        self.completed_courses.append(course)
    student1 = Student(\"Sally Student\")\nstudent2 = Student(\"Sassy Student\")\n\nstudent1.add_course(\"ItP\")\nstudent1.add_course(\"ACiP\")\n\nprint(student1.completed_courses)\nprint(student2.completed_courses)

    ['ItP', 'ACiP']\n[]

    The Grand Finale

    Even though the following exercise finishes off this part of the material, the techniques required to solve it were all covered already in the section named objects as attributes. Specifically, you are not required to use the @property decorator or default values for parameters in this exercise. This exercise is very similar to the exercises a box of presents and the shortest person in the room.

    Important information regarding the next exercise

    Please note that there is an issue resulting from an update in Python, which conflicts with the inbuilt library and the original file name for this exercise. If you experience any issues, we recommend redownloading the exercise folder. Once you have obtained the new local test files, you can use either \"code.py\" or \"code_1.py\" as the file name. While using Visual Studio Code, you may receive notifications about problems in the test file. However, these notifications can be safely ignored, as they are caused by the test's inability to import from either the \"code.py\" or \"code_1.py\" files.

    In this series of exercises you will create the classes Item, Suitcase and Cargo Hold, which will let you further practice working on objects which contain references to other objects.

    Item

    Please create a class named Item which is used to create items of different kinds. Each item has a name and a weight (in kilograms).

    You can use the following code to test your class:

    book = Item(\"ABC Book\", 2)\nphone = Item(\"Nokia 3210\", 1)\n\nprint(\"Name of the book:\", book.name())\nprint(\"Weight of the book:\", book.weight())\n\nprint(\"Book:\", book)\nprint(\"Phone:\", phone)

    Your program should print out this:

    Name of the book: ABC Book\nWeight of the book: 2\nBook: ABC Book (2 kg)\nPhone: Nokia 3210 (1 kg)

    An Item should provide the methods weight and name, which return the values stored in those attributes.

    The name and weight should be encapsulated within the class. The following code should not work:

    book = Item(\"ABC Book\", 2)\nbook.weight = 10

    Suitcase

    Please write a class named Suitcase. You should be able to pack items into a suitcase. A suitcase also has a maximum combined weight for the items stored within.

    Your class should contains the following members:

      \n
    • a constructor which takes the maximum weight as an argument
    • \n
    • a method named add_item which adds the item given as an argument to the suitcase. The method has no return value.
    • \n
    • a __str__ method which returns a string in the format \"x items (y kg)\"
    • \n

    The class should make sure that the combined weight of the items stored within any Suitcase does not exceed the maximum weight set for that instance. If the maximum weight would be exceeded when the add_item method is called, the new item should not be added to the suitcase.

    Your class should work as follows:

    book = Item(\"ABC Book\", 2)\nphone = Item(\"Nokia 3210\", 1)\nbrick = Item(\"Brick\", 4)\n\nsuitcase = Suitcase(5)\nprint(suitcase)\n\nsuitcase.add_item(book)\nprint(suitcase)\n\nsuitcase.add_item(phone)\nprint(suitcase)\n\nsuitcase.add_item(brick)\nprint(suitcase)

    Executing the above should print out

    0 items (0 kg)\n1 items (2 kg)\n2 items (3 kg)\n2 items (3 kg)

    Mind your language

    The notification \"1 items\" is not very grammatical. Instead, it should say \"1 item\". Please make the required changes to your __str__ method.

    The previous example should now print out

    0 items (0 kg)\n1 item (2 kg)\n2 items (3 kg)\n2 items (3 kg)

    All the items

    Please add the following methods to your Suitcase class definition:

      \n
    • print_items prints out all the items stored in the suitcase
    • \n
    • weight returns an integer number representing the combined weight of all the items stored in the suitcase
    • \n

    Your class should now work with the following program:

    book = Item(\"ABC Book\", 2)\nphone = Item(\"Nokia 3210\", 1)\nbrick = Item(\"Brick\", 4)\n\nsuitcase = Suitcase(10)\nsuitcase.add_item(book)\nsuitcase.add_item(phone)\nsuitcase.add_item(brick)\n\nprint(\"The suitcase contains the following items:\")\nsuitcase.print_items()\ncombined_weight = suitcase.weight()\nprint(f\"Combined weight: {combined_weight} kg\")

    Executing the above program should print out this:

    The suitcase contains the following items:\nABC Book (2 kg)\nNokia 3210 (1 kg)\nBrick (4 kg)\nCombined weight: 7 kg

    If you have implemented your Suitcase class with more than two instance variables, please make the required changes so that each instance has only two data attributes: the maximum weight and a list of items within.

    The heaviest item

    Please add a new method to your Suitcase class: heaviest_item should return the Item which is the heaviest. If there are two or more items with the same, heaviest weight, the method may return any one of these. The method should return a reference to an object. If the suitcase is empty, the method should return None.

    Your class should now work with the following program:

    book = Item(\"ABC Book\", 2)\nphone = Item(\"Nokia 3210\", 1)\nbrick = Item(\"Brick\", 4)\n\nsuitcase = Suitcase(10)\nsuitcase.add_item(book)\nsuitcase.add_item(phone)\nsuitcase.add_item(brick)\n\nheaviest = suitcase.heaviest_item()\nprint(f\"The heaviest item: {heaviest}\")

    Executing the above program should print out this:

    The heaviest item: Brick (4 kg)

    Cargo hold

    Please write a class named CargoHold with the following methods:

      \n
    • a constructor which takes the maximum weight as an argument
    • \n
    • a method named add_suitcase which adds the suitcase given as an argument to the cargo hold
    • \n
    • a __str__ method which returns a string in the format \"x suitcases, space for y kg\"
    • \n

    The class should make sure that the combined weight of the items stored within any CargoHold does not exceed the maximum weight set for that instance. If the maximum weight would be exceeded when the add_suitcase method is called, the new suitcase should not be added to the cargo hold.

    Your class should now work with the following program:

    cargo_hold = CargoHold(1000)\nprint(cargo_hold)\n\nbook = Item(\"ABC Book\", 2)\nphone = Item(\"Nokia 3210\", 1)\nbrick = Item(\"Brick\", 4)\n\nadas_suitcase = Suitcase(10)\nadas_suitcase.add_item(book)\nadas_suitcase.add_item(phone)\n\npeters_suitcase = Suitcase(10)\npeters_suitcase.add_item(brick)\n\ncargo_hold.add_suitcase(adas_suitcase)\nprint(cargo_hold)\n\ncargo_hold.add_suitcase(peters_suitcase)\nprint(cargo_hold)

    0 suitcases, space for 1000 kg\n1 suitcase, space for 997 kg\n2 suitcases, space for 993 kg

    The contents of the cargo hold

    Please add a method named print_items to your CargoHold class. It should print out all the items in all the suitcases within the cargo hold.

    Your class should now work with the following program:

    book = Item(\"ABC Book\", 2)\nphone = Item(\"Nokia 3210\", 1)\nbrick = Item(\"Brick\", 4)\n\nadas_suitcase = Suitcase(10)\nadas_suitcase.add_item(book)\nadas_suitcase.add_item(phone)\n\npeters_suitcase = Suitcase(10)\npeters_suitcase.add_item(brick)\n\ncargo_hold = CargoHold(1000)\ncargo_hold.add_suitcase(adas_suitcase)\ncargo_hold.add_suitcase(peters_suitcase)\n\nprint(\"The suitcases in the cargo hold contain the following items:\")\ncargo_hold.print_items()

    Executing the above program should print out this:

    The suitcases in the cargo hold contain the following items:\nABC Book (2 kg)\nNokia 3210 (1 kg)\nBrick (4 kg)

    Please respond to a quick questionnaire on this week's materials.

    ","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-9/6-more-examples-with-classes.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."}}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."}}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"}}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."}}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"}}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"}}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"}}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"}}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"}}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"}}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"}}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"}}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"}}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"}}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"}}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"}}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"}}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"}}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"}}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"}}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"}}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"}}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"}}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"}}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"}}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"}}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"}}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"}}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"}}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"}}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"}}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"}}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"}}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"}}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"}}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"}}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"}}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"}}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"}}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"}}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"}}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"}}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"}}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"}}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"}}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"}}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"}}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"}}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"}}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"}}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"}}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"}}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"}}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"}}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"}}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"}}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"}}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"}}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"}}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"}}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"}}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"}}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"}}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"}}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"}}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"}}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"}}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"}}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"}}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"}}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"}}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"}}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"}}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"}}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"}}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"}}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"}}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"}}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"}}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"}}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"}}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"}}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"}}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"}}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"}}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"}}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"}}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"}}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"}}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/page-data/part-9/page-data.json b/page-data/part-9/page-data.json index f4a69bac9..c7ff5ce71 100644 --- a/page-data/part-9/page-data.json +++ b/page-data/part-9/page-data.json @@ -1,5 +1,5 @@ { "componentChunkName": "component---src-templates-course-part-overview-template-js", "path": "/part-9", - "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-9","title":"Part 9"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-9/index.md"},"allPages":{"edges":[{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}}]}},"pageContext":{}}, + "result": {"data":{"page":{"htmlAst":{"type":"root","children":[{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"pages-in-this-section","properties":{},"children":[]}]},{"type":"text","value":"\n"},{"type":"element","tagName":"div","properties":{},"children":[{"type":"element","tagName":"exercises-in-this-section","properties":{},"children":[]}]}],"data":{"quirksMode":false}},"html":"
    \n
    ","frontmatter":{"path":"/part-9","title":"Part 9"},"fileAbsolutePath":"/home/runner/work/programming-24/programming-24/data/part-9/index.md"},"allPages":{"edges":[{"node":{"id":"2b3a28b7-2804-51c9-befc-cb86cfbec18d","frontmatter":{"path":"/exam-adv","title":"Advanced Course in Programming exam 17.08."},"moocfiExercises":[]}},{"node":{"id":"cfca7c67-7aba-5fa4-a03a-1335423354d3","frontmatter":{"path":"/exam","title":"Exam 4.6."},"moocfiExercises":[]}},{"node":{"id":"3dfc2b44-73c3-578f-a6fa-6d29310fef48","frontmatter":{"path":"/faq","title":"Frequently asked questions"},"moocfiExercises":[]}},{"node":{"id":"bed28270-2cc0-5ff7-af9a-f51d1182f910","frontmatter":{"path":"/exam-intro","title":"Introduction to Programming exam 07.09."},"moocfiExercises":[]}},{"node":{"id":"9129bc97-249b-5255-ac9e-b03ad188af70","frontmatter":{"path":"/frontmatter-guide","title":"Frontmatter-guide"},"moocfiExercises":[]}},{"node":{"id":"39dccb2b-8962-52c7-9895-46106358eed3","frontmatter":{"path":"/grading-and-exams","title":"Grading and exams"},"moocfiExercises":[]}},{"node":{"id":"21c290d1-7cb1-5b68-ad33-84fc70f110e7","frontmatter":{"path":"/","title":"About this course"},"moocfiExercises":[]}},{"node":{"id":"349aa8ae-7b24-5dc9-b63b-6d660eb5db14","frontmatter":{"path":"/support-and-assistance","title":"Support and assistance"},"moocfiExercises":[]}},{"node":{"id":"83c88d3d-c84c-5f91-a82e-10588a4dfaa0","frontmatter":{"path":"/glossary","title":"Glossary"},"moocfiExercises":[]}},{"node":{"id":"9869df0e-efdd-5306-9146-b2efab4a71c6","frontmatter":{"path":"/all-exercises","title":"All exercises"},"moocfiExercises":[]}},{"node":{"id":"a6686aff-fa9a-58c9-8b0e-9fe4317c0c3b","frontmatter":{"path":"/error_messages","title":"Common error messages"},"moocfiExercises":[]}},{"node":{"id":"bee94002-6c2a-5acf-9c51-23ab9cc99018","frontmatter":{"path":"/part-1/1-getting-started","title":"Getting started"},"moocfiExercises":[{"id":"Emoticon","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Fix the code: Seven Brothers","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Row, Row, Row Your Boat","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Minutes in a year","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"},{"id":"Print some code","type":"programming-exercise","parentPagePath":"/part-1/1-getting-started"}]}},{"node":{"id":"3f849c89-a016-549a-be55-bd151671e7e3","frontmatter":{"path":"/part-1/3-more-about-variables","title":"More about variables"},"moocfiExercises":[{"id":"5f930565-c71e-50cc-89f2-eb410d7b8a9d","type":"quiz","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Extra space","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Arithmetics","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"},{"id":"Fix the code: Print a single line","type":"programming-exercise","parentPagePath":"/part-1/3-more-about-variables"}]}},{"node":{"id":"be630913-7008-5e73-8c65-c47430c3d432","frontmatter":{"path":"/part-1/4-arithmetic-operations","title":"Arithmetic operations"},"moocfiExercises":[{"id":"Times five","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Name and age","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Seconds in a day","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Fix the code: Product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and product","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Sum and mean","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Food expenditure","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"},{"id":"Students in groups","type":"programming-exercise","parentPagePath":"/part-1/4-arithmetic-operations"}]}},{"node":{"id":"4700c869-70a0-5b06-b2aa-1104c56749bc","frontmatter":{"path":"/part-1","title":"Part 1"},"moocfiExercises":[]}},{"node":{"id":"4b8da962-bf4a-593d-9c3f-df16ca7964f3","frontmatter":{"path":"/part-1/5-conditional-statements","title":"Conditional statements"},"moocfiExercises":[{"id":"Orwell","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Absolute value","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Soup or no soup","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Order of magnitude","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Calculator","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Temperatures","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Daily wages","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Loyalty bonus","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"What to wear tomorrow","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"Solving a quadratic equation","type":"programming-exercise","parentPagePath":"/part-1/5-conditional-statements"},{"id":"bcfae104-8cad-5fcd-ac64-d65ae689ac5f","type":"quiz","parentPagePath":"/part-1/5-conditional-statements"}]}},{"node":{"id":"79025b60-13e0-51b9-a3c7-277f0c945efb","frontmatter":{"path":"/part-10/1-class-hierarchies","title":"Class hierarchies"},"moocfiExercises":[{"id":"Laptop computer","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Game Museum","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Areas","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"},{"id":"Word game","type":"programming-exercise","parentPagePath":"/part-10/1-class-hierarchies"}]}},{"node":{"id":"4fd54847-9a70-59cb-b4c7-7079f7be5db1","frontmatter":{"path":"/part-1/2-information-from-the-user","title":"Information from the user"},"moocfiExercises":[{"id":"Name twice","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and exclamation marks","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Name and address","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Fix the code: Utterances","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-1/2-information-from-the-user"}]}},{"node":{"id":"6ad9d858-08fe-56d8-a95e-a5a538f03f3e","frontmatter":{"path":"/part-10/2-access-modifiers","title":"Access modifiers"},"moocfiExercises":[{"id":"Supergroup","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"},{"id":"Secret magic potion","type":"programming-exercise","parentPagePath":"/part-10/2-access-modifiers"}]}},{"node":{"id":"0a408de1-2982-53f6-a9b8-8f7fe0256695","frontmatter":{"path":"/part-10","title":"Part 10"},"moocfiExercises":[]}},{"node":{"id":"be92f428-773f-5104-8cf5-2010e8b2b8d4","frontmatter":{"path":"/part-10/3-oo-programming-techniques","title":"Object oriented programming techniques"},"moocfiExercises":[{"id":"Money","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"Simple date","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"},{"id":"An iterable shopping list","type":"programming-exercise","parentPagePath":"/part-10/3-oo-programming-techniques"}]}},{"node":{"id":"0b087cf9-3b7e-5bd9-813c-bbcc401a167c","frontmatter":{"path":"/part-11/1-list-comprehensions","title":"List comprehensions"},"moocfiExercises":[{"id":"Square roots","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Rows of stars","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Best exam result","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lengths","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Remove smaller than","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Begin with a vowel","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"},{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-11/1-list-comprehensions"}]}},{"node":{"id":"6d7a975e-67fe-571d-a54e-024448cfbfe6","frontmatter":{"path":"/part-11/2-more-comprehensions","title":"More comprehensions"},"moocfiExercises":[{"id":"Filter forbidden","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Products in shopping list","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Price difference of cheaper properties","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Lengths of strings","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"},{"id":"Most common words","type":"programming-exercise","parentPagePath":"/part-11/2-more-comprehensions"}]}},{"node":{"id":"4e068727-c2bf-5c5b-b9e5-ab543f05f948","frontmatter":{"path":"/part-11","title":"Part 11"},"moocfiExercises":[]}},{"node":{"id":"b715f296-a3fd-5c54-b159-09188ac0d82d","frontmatter":{"path":"/part-11/4-more-recursion-examples","title":"More recursion examples"},"moocfiExercises":[{"id":"Greatest node","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Bosses and subordinates","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"OrderBook","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"Order book application","type":"programming-exercise","parentPagePath":"/part-11/4-more-recursion-examples"},{"id":"de061758-95fc-5a58-9c40-eb68a5629972","type":"quiz","parentPagePath":"/part-11/4-more-recursion-examples"}]}},{"node":{"id":"33ee9e98-4cf7-5b2e-94d4-7c114c7e2e55","frontmatter":{"path":"/part-12/1-functions-as-arguments","title":"Functions as arguments"},"moocfiExercises":[{"id":"Sort by remaining stock","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by number of seasons","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Sort by ratings","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"ClimbingRoute","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Climbing areas","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"BallPlayers","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"},{"id":"Product search","type":"programming-exercise","parentPagePath":"/part-12/1-functions-as-arguments"}]}},{"node":{"id":"598f81b9-1492-57ae-8245-7348e7f25bea","frontmatter":{"path":"/part-12/2-generators","title":"Generators"},"moocfiExercises":[{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Prime numbers","type":"programming-exercise","parentPagePath":"/part-12/2-generators"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-12/2-generators"}]}},{"node":{"id":"0035c79a-6b7c-50c6-a72b-8cd1467bfe7b","frontmatter":{"path":"/part-12/4-regular-expressions","title":"Regular expressions"},"moocfiExercises":[{"id":"Regular expressions","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"Hockey statistics","type":"programming-exercise","parentPagePath":"/part-12/4-regular-expressions"},{"id":"6a606b63-c674-5231-9e79-55d7372e582a","type":"quiz","parentPagePath":"/part-12/4-regular-expressions"}]}},{"node":{"id":"299e480f-76d9-54d0-a727-aab3ca7f25b4","frontmatter":{"path":"/part-12","title":"Part 12"},"moocfiExercises":[]}},{"node":{"id":"1136b7c2-9b13-5788-9d10-20ae7e5bd93a","frontmatter":{"path":"/part-12/3-functional-programming","title":"Functional programming"},"moocfiExercises":[{"id":"Attempted courses","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Filtering attempts","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"},{"id":"Study credits","type":"programming-exercise","parentPagePath":"/part-12/3-functional-programming"}]}},{"node":{"id":"4473b76e-b871-5ae2-bb5d-d201576e067e","frontmatter":{"path":"/part-13/1-pygame","title":"Pygame"},"moocfiExercises":[{"id":"Four robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Robots in a row","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"A hundred robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"},{"id":"Random robots","type":"programming-exercise","parentPagePath":"/part-13/1-pygame"}]}},{"node":{"id":"073daf15-ceb9-5d0d-94e0-b96d4b89a8b2","frontmatter":{"path":"/part-11/3-recursion","title":"Recursion"},"moocfiExercises":[{"id":"Add numbers to a list","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Recursive sum","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"},{"id":"Balance all the brackets","type":"programming-exercise","parentPagePath":"/part-11/3-recursion"}]}},{"node":{"id":"2a7ab4d9-7a90-53cd-93ca-d11d23e3f57c","frontmatter":{"path":"/part-13/2-animation","title":"Animation"},"moocfiExercises":[{"id":"Vertical movement","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Round the perimeter","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Two robots","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robots in a circle","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Bouncing ball","type":"programming-exercise","parentPagePath":"/part-13/2-animation"},{"id":"Robot invasion","type":"programming-exercise","parentPagePath":"/part-13/2-animation"}]}},{"node":{"id":"bb2a50b0-5a0e-5bba-b874-321fbd6fdf5a","frontmatter":{"path":"/part-13/3-events","title":"Events"},"moocfiExercises":[{"id":"Four directions","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Four walls","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Two players","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"Robot and mouse","type":"programming-exercise","parentPagePath":"/part-13/3-events"},{"id":"The location of the robot","type":"programming-exercise","parentPagePath":"/part-13/3-events"}]}},{"node":{"id":"2df0dbea-3fd1-50fb-85aa-9809215d9822","frontmatter":{"path":"/part-13","title":"Part 13"},"moocfiExercises":[]}},{"node":{"id":"f63e2524-70cc-5f58-8eb3-03961e49d0ae","frontmatter":{"path":"/part-13/4-more-pygame-techniques","title":"More pygame techniques"},"moocfiExercises":[{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"Asteroids","type":"programming-exercise","parentPagePath":"/part-13/4-more-pygame-techniques"},{"id":"d8865dda-83f4-5d1b-b186-c2e6b90e0336","type":"quiz","parentPagePath":"/part-13/4-more-pygame-techniques"}]}},{"node":{"id":"811f3271-ff05-58ca-b6eb-22d4fbde18e9","frontmatter":{"path":"/part-14/1-game-project","title":"Game project"},"moocfiExercises":[]}},{"node":{"id":"3c2d2b5a-26fd-54d2-8e62-eab21285795b","frontmatter":{"path":"/part-14/2-robot-and-boxes","title":"Robot and boxes"},"moocfiExercises":[]}},{"node":{"id":"e12eccdf-2be5-5489-9dc1-c39eaa3513e2","frontmatter":{"path":"/part-14/3-finishing-the-game","title":"Finishing the game"},"moocfiExercises":[]}},{"node":{"id":"f9bbcf9e-aa0b-5168-88fe-90c3686dbfcd","frontmatter":{"path":"/part-14/4-your-own-game","title":"Your own game"},"moocfiExercises":[{"id":"d7de8698-17f0-5e9e-9f7e-ce808001a050","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"ad48f7b4-dbf9-5aa3-a68e-a7c114a03ac7","type":"quiz","parentPagePath":"/part-14/4-your-own-game"},{"id":"dc0405b3-392a-5811-9138-709848a26667","type":"quiz","parentPagePath":"/part-14/4-your-own-game"}]}},{"node":{"id":"f0655b3e-50cb-5f22-aa83-f68a516acdba","frontmatter":{"path":"/part-14","title":"Part 14"},"moocfiExercises":[]}},{"node":{"id":"aa860f81-ccef-5550-8a5b-96880d1c22ab","frontmatter":{"path":"/part-2/1-programming-terminology","title":"Programming terminology"},"moocfiExercises":[{"id":"Fix the syntax","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Number of characters","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"},{"id":"Typecasting","type":"programming-exercise","parentPagePath":"/part-2/1-programming-terminology"}]}},{"node":{"id":"70f00bba-3943-5d65-8faa-1a64fe393b38","frontmatter":{"path":"/part-2/2-else-elif","title":"More conditionals"},"moocfiExercises":[{"id":"Age of maturity","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Greater than or equal to","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"The elder","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"},{"id":"Alphabetically last","type":"programming-exercise","parentPagePath":"/part-2/2-else-elif"}]}},{"node":{"id":"869d3d3e-d3af-5bce-86a5-7f9b207132de","frontmatter":{"path":"/part-2/3-combining-conditions","title":"Combining conditions"},"moocfiExercises":[{"id":"Age check","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Nephews","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Grades and points","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"FizzBuzz","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Leap year","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Alphabetically in the middle","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"},{"id":"Gift tax calculator","type":"programming-exercise","parentPagePath":"/part-2/3-combining-conditions"}]}},{"node":{"id":"e5311581-750d-5356-a0c2-880e0255939e","frontmatter":{"path":"/part-2","title":"Part 2"},"moocfiExercises":[]}},{"node":{"id":"ce6b05b9-f819-5d09-8e71-234e5797528d","frontmatter":{"path":"/part-2/4-simple-loops","title":"Simple loops"},"moocfiExercises":[{"id":"Shall we continue?","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Input validation","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Repeat password","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"PIN and number of attempts","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"The next leap year","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Story","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"Working with numbers","type":"programming-exercise","parentPagePath":"/part-2/4-simple-loops"},{"id":"1346f801-2677-50d3-a0d4-a3cfd6e09ccf","type":"quiz","parentPagePath":"/part-2/4-simple-loops"}]}},{"node":{"id":"7a55df12-9060-5d5f-a79b-e3209d4c6563","frontmatter":{"path":"/part-3/2-working-with-strings","title":"Working with strings"},"moocfiExercises":[{"id":"String multiplied","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The longer string","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"End to beginning","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Second and second to last characters","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A line of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A rectangle of hashes","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Underlining","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Right-aligned","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"A framed word","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 1","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Substrings, part 2","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Does it contain vowels","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find the first substring","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"Find all the substrings","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"},{"id":"The second occurrence","type":"programming-exercise","parentPagePath":"/part-3/2-working-with-strings"}]}},{"node":{"id":"03e3f3b0-067a-52ce-8c25-85125f20c6ed","frontmatter":{"path":"/part-3/1-loops-with-conditions","title":"Loops with conditions"},"moocfiExercises":[{"id":"Print numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Fix the code: Countdown","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Numbers","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of two","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"Powers of base n","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 1","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"},{"id":"The sum of consecutive numbers, version 2","type":"programming-exercise","parentPagePath":"/part-3/1-loops-with-conditions"}]}},{"node":{"id":"3c2a69ea-8c77-5a81-9136-f4bf5e5811c3","frontmatter":{"path":"/part-3/3-more-loops","title":"More loops"},"moocfiExercises":[{"id":"Multiplication","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"First letters of words","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Factorial","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Flip the pairs","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"},{"id":"Taking turns","type":"programming-exercise","parentPagePath":"/part-3/3-more-loops"}]}},{"node":{"id":"a67dbaa9-a8eb-5af2-babc-abf6e46605db","frontmatter":{"path":"/part-3/4-defining-functions","title":"Defining functions"},"moocfiExercises":[{"id":"Seven Brothers","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"The first character","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Mean","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Print many times","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"Chessboard","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"A word squared","type":"programming-exercise","parentPagePath":"/part-3/4-defining-functions"},{"id":"80016b6a-28f8-5f64-a551-63b06f7f63a6","type":"quiz","parentPagePath":"/part-3/4-defining-functions"}]}},{"node":{"id":"1fb56170-dbc4-5b53-8d27-75710397c572","frontmatter":{"path":"/part-3","title":"Part 3"},"moocfiExercises":[]}},{"node":{"id":"edfce29d-2694-545c-9fff-c34c3b068f19","frontmatter":{"path":"/part-4/1-vscode","title":"The Visual Studio Code editor, Python interpreter and built-in debugging tool"},"moocfiExercises":[{"id":"Hello Visual Studio Code","type":"programming-exercise","parentPagePath":"/part-4/1-vscode"}]}},{"node":{"id":"b7829f4a-e645-55e9-b380-f92b3691051b","frontmatter":{"path":"/part-4/4-definite-iteration","title":"Definite iteration"},"moocfiExercises":[{"id":"Star-studded","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"From negative to positive","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"List of stars","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Anagrams","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Palindromes","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of positive numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Even numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The sum of lists","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"Distinct numbers","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The length of the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"The shortest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"},{"id":"All the longest in the list","type":"programming-exercise","parentPagePath":"/part-4/4-definite-iteration"}]}},{"node":{"id":"7b7fc08c-eeec-5ae1-a3e9-a8ce8f20aa53","frontmatter":{"path":"/part-4/2-more-functions","title":"More functions"},"moocfiExercises":[{"id":"Line","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A box of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square of hashes","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A square","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A triangle","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A shape","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"A spruce","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"The greatest number","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"Same characters","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"},{"id":"First, second and last words","type":"programming-exercise","parentPagePath":"/part-4/2-more-functions"}]}},{"node":{"id":"7a50c565-09d6-5f7b-b620-3c5c92e3fb6e","frontmatter":{"path":"/part-4/3-lists","title":"Lists"},"moocfiExercises":[{"id":"Change the value of an item","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Add items to a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Addition and removal","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Same word twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"List twice","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The length of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"Arithmetic mean","type":"programming-exercise","parentPagePath":"/part-4/3-lists"},{"id":"The range of a list","type":"programming-exercise","parentPagePath":"/part-4/3-lists"}]}},{"node":{"id":"e7a085e0-a756-5154-96f0-80c325fc5a80","frontmatter":{"path":"/part-4/5-print-statement-formatting","title":"Print statement formatting"},"moocfiExercises":[{"id":"Integers to strings","type":"programming-exercise","parentPagePath":"/part-4/5-print-statement-formatting"}]}},{"node":{"id":"ab18d050-388c-5481-b99b-aae5818d16b1","frontmatter":{"path":"/part-4/6-strings-and-lists","title":"More strings and lists"},"moocfiExercises":[{"id":"Everything reversed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Most common character","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No vowels allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"No shouting allowed","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Neighbours in a list","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"Grade statistics","type":"programming-exercise","parentPagePath":"/part-4/6-strings-and-lists"},{"id":"25acd630-8857-5eb3-9271-ff44ceda207f","type":"quiz","parentPagePath":"/part-4/6-strings-and-lists"}]}},{"node":{"id":"c093c8cd-7905-58b0-8f69-15ca7364d6ba","frontmatter":{"path":"/part-4","title":"Part 4"},"moocfiExercises":[]}},{"node":{"id":"15954f40-7ae5-5827-8283-a26471bc96d0","frontmatter":{"path":"/part-5/1-more-lists","title":"More lists"},"moocfiExercises":[{"id":"The longest string","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Number of matching elements","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Go","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check row","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check column","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check block","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"},{"id":"Sudoku: check grid","type":"programming-exercise","parentPagePath":"/part-5/1-more-lists"}]}},{"node":{"id":"3f88aa87-a3ff-51d7-a3de-ef43f6d82de3","frontmatter":{"path":"/part-5/2-references","title":"References"},"moocfiExercises":[{"id":"Items multiplied by two","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Remove the smallest","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: print out the grid and add a number","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Sudoku: add number to a copy of the grid","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Tic-Tac-Toe","type":"programming-exercise","parentPagePath":"/part-5/2-references"},{"id":"Transpose a matrix","type":"programming-exercise","parentPagePath":"/part-5/2-references"}]}},{"node":{"id":"369f1497-db93-5aec-a3b2-3de7ebbbec34","frontmatter":{"path":"/part-5/3-dictionary","title":"Dictionary"},"moocfiExercises":[{"id":"Times ten","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Factorials","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Histogram","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 1","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Phone book, version 2","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Invert a dictionary","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Numbers spelled out","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Movie database","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"},{"id":"Find movies","type":"programming-exercise","parentPagePath":"/part-5/3-dictionary"}]}},{"node":{"id":"f0047dbd-a87e-5422-be9c-73993e48e199","frontmatter":{"path":"/part-5/4-tuple","title":"Tuple"},"moocfiExercises":[{"id":"Create a tuple","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"The oldest person","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Older people","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"Student database","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"A square of letters","type":"programming-exercise","parentPagePath":"/part-5/4-tuple"},{"id":"28af7349-2997-5bb7-bc45-a9defb93914e","type":"quiz","parentPagePath":"/part-5/4-tuple"}]}},{"node":{"id":"1a360911-5670-59da-ace6-a5a1c2b3aa7d","frontmatter":{"path":"/part-5","title":"Part 5"},"moocfiExercises":[]}},{"node":{"id":"ed269b5a-2620-5b84-a570-e89a87d95414","frontmatter":{"path":"/part-6/1-reading-files","title":"Reading files"},"moocfiExercises":[{"id":"Largest number","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Fruit market","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Matrix","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 1","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 2","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Course grading, part 3","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Spell checker","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"Recipe search","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"},{"id":"City bikes","type":"programming-exercise","parentPagePath":"/part-6/1-reading-files"}]}},{"node":{"id":"66a907e2-3e82-568a-bc06-8dfc7bcf0e2f","frontmatter":{"path":"/part-6/2-writing-files","title":"Writing files"},"moocfiExercises":[{"id":"Inscription","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Diary","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Filtering the contents of a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Store personal data","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Course grading, part 4","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Word search","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"},{"id":"Dictionary stored in a file","type":"programming-exercise","parentPagePath":"/part-6/2-writing-files"}]}},{"node":{"id":"5e3d747d-aa24-5842-a67b-78b492d9b790","frontmatter":{"path":"/part-6/4-scope-of-variables","title":"Local and global variables"},"moocfiExercises":[{"id":"9662ad5e-7c21-52be-b107-44a017a27144","type":"quiz","parentPagePath":"/part-6/4-scope-of-variables"}]}},{"node":{"id":"4ed74d72-d2db-5e0d-9fa7-2e83368f15d0","frontmatter":{"path":"/part-6/3-errors","title":"Handling errors"},"moocfiExercises":[{"id":"Reading input","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Parameter validation","type":"programming-exercise","parentPagePath":"/part-6/3-errors"},{"id":"Incorrect lottery numbers","type":"programming-exercise","parentPagePath":"/part-6/3-errors"}]}},{"node":{"id":"9de63e55-70f0-56a7-9678-c3831fb2ec32","frontmatter":{"path":"/part-6","title":"Part 6"},"moocfiExercises":[]}},{"node":{"id":"9f85522c-13e2-508e-9db3-4db220faa4e1","frontmatter":{"path":"/part-7/1-modules","title":"Modules"},"moocfiExercises":[{"id":"Hypotenuse","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Special characters","type":"programming-exercise","parentPagePath":"/part-7/1-modules"},{"id":"Fractions","type":"programming-exercise","parentPagePath":"/part-7/1-modules"}]}},{"node":{"id":"ca42a301-e9ea-5aa7-8796-0b21cf4dcb09","frontmatter":{"path":"/part-7/2-randomness","title":"Randomness"},"moocfiExercises":[{"id":"Lottery numbers","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 1","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Password generator, part 2","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Dice roller","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"},{"id":"Random words","type":"programming-exercise","parentPagePath":"/part-7/2-randomness"}]}},{"node":{"id":"9c092e22-d71d-5a89-b52f-1c126b415fb6","frontmatter":{"path":"/part-7/3-times-and-dates","title":"Times and dates"},"moocfiExercises":[{"id":"How old","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Valid PIC?","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"},{"id":"Screen time","type":"programming-exercise","parentPagePath":"/part-7/3-times-and-dates"}]}},{"node":{"id":"d2ee86be-42be-5b6e-94e3-3dbd0a993cf6","frontmatter":{"path":"/part-7/4-data-processing","title":"Data processing"},"moocfiExercises":[{"id":"Handling JSON files","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Course statistics","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Who cheated, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"},{"id":"Spell checker, version 2","type":"programming-exercise","parentPagePath":"/part-7/4-data-processing"}]}},{"node":{"id":"66656754-3c31-5830-b339-9a60e081b2cb","frontmatter":{"path":"/part-7/5-creating-modules","title":"Creating your own modules"},"moocfiExercises":[{"id":"String helper","type":"programming-exercise","parentPagePath":"/part-7/5-creating-modules"},{"id":"c4d025fa-aeae-5eaa-bce2-f0246c9e64b7","type":"quiz","parentPagePath":"/part-7/5-creating-modules"}]}},{"node":{"id":"7da5c678-548b-5917-9c67-30ddd8f30dea","frontmatter":{"path":"/part-7/6-more-features","title":"More Python features"},"moocfiExercises":[{"id":"Your own programming language","type":"programming-exercise","parentPagePath":"/part-7/6-more-features"},{"id":"2515322a-c067-5343-9d5e-a897ffb89b2c","type":"quiz","parentPagePath":"/part-7/6-more-features"}]}},{"node":{"id":"ce286170-74db-5a72-9d90-a5bdae298203","frontmatter":{"path":"/part-7","title":"Part 7"},"moocfiExercises":[]}},{"node":{"id":"0273a4a6-1333-59da-a388-8f8230c1fbdb","frontmatter":{"path":"/part-8/1-objects-and-methods","title":"Objects and methods"},"moocfiExercises":[{"id":"The smallest average result","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"},{"id":"Row sums","type":"programming-exercise","parentPagePath":"/part-8/1-objects-and-methods"}]}},{"node":{"id":"5ddb9bac-1c10-5176-abfb-3559534c5814","frontmatter":{"path":"/part-8/2-classes-and-objects","title":"Classes and objects"},"moocfiExercises":[{"id":"List of years","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"},{"id":"Shopping list","type":"programming-exercise","parentPagePath":"/part-8/2-classes-and-objects"}]}},{"node":{"id":"8a2f364a-59eb-5bc8-8d72-d688481beaa2","frontmatter":{"path":"/part-8/3-defining-classes","title":"Defining classes"},"moocfiExercises":[{"id":"Book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Three classes","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Define class: Pet","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"The older book","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"},{"id":"Books of a genre","type":"programming-exercise","parentPagePath":"/part-8/3-defining-classes"}]}},{"node":{"id":"1cf0373f-e889-519a-8b08-b32b69d58c66","frontmatter":{"path":"/part-8/4-defining-methods","title":"Defining methods"},"moocfiExercises":[{"id":"Decreasing counter","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"First and last name","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"},{"id":"Statistics on numbers","type":"programming-exercise","parentPagePath":"/part-8/4-defining-methods"}]}},{"node":{"id":"f9b5518b-e9f6-56e3-a9dc-b9eb78cbad7a","frontmatter":{"path":"/part-8/5-more-examples-of-classes","title":"More examples of classes"},"moocfiExercises":[{"id":"Stopwatch","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Clock","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"LunchCard","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"Series","type":"programming-exercise","parentPagePath":"/part-8/5-more-examples-of-classes"},{"id":"65ee1264-957f-5547-b5a7-be4b3bd2be7e","type":"quiz","parentPagePath":"/part-8/5-more-examples-of-classes"}]}},{"node":{"id":"027ead7e-e579-5cbf-9f3e-518bcabc3aec","frontmatter":{"path":"/part-9/2-objects-as-attributes","title":"Objects as attributes"},"moocfiExercises":[{"id":"Pets","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"A box of presents","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"},{"id":"The shortest person in the room","type":"programming-exercise","parentPagePath":"/part-9/2-objects-as-attributes"}]}},{"node":{"id":"6687cba6-ac1f-5cbb-99ae-2f084b5765e0","frontmatter":{"path":"/part-8","title":"Part 8"},"moocfiExercises":[]}},{"node":{"id":"002e9b54-5ff8-5583-a571-e52a993e5052","frontmatter":{"path":"/part-9/1-objects-and-references","title":"Objects and references"},"moocfiExercises":[{"id":"The fastest car","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Passing submissions","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Baby Centre","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"LunchCard and PaymentTerminal","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"},{"id":"Comparing properties","type":"programming-exercise","parentPagePath":"/part-9/1-objects-and-references"}]}},{"node":{"id":"d47f4ca0-9a0a-5b35-b5a6-2e6123d2214a","frontmatter":{"path":"/part-9/4-scope-of-methods","title":"Scope of methods"},"moocfiExercises":[{"id":"Service charge","type":"programming-exercise","parentPagePath":"/part-9/4-scope-of-methods"}]}},{"node":{"id":"ca66f858-4041-54a1-8949-a606bf3fbbf8","frontmatter":{"path":"/part-9/3-encapsulation","title":"Encapsulation"},"moocfiExercises":[{"id":"Car","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Recording","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"},{"id":"Weather station","type":"programming-exercise","parentPagePath":"/part-9/3-encapsulation"}]}},{"node":{"id":"37494750-3226-5587-a1b0-c4735a59edf8","frontmatter":{"path":"/part-9/6-more-examples-with-classes","title":"More examples with classes"},"moocfiExercises":[{"id":"Item, Suitcase and Cargo hold","type":"programming-exercise","parentPagePath":"/part-9/6-more-examples-with-classes"},{"id":"7af6b87c-c7ce-50fe-acb6-bcb48a15f16b","type":"quiz","parentPagePath":"/part-9/6-more-examples-with-classes"}]}},{"node":{"id":"68b25bbc-1b43-53c7-85c2-31339b2e5ec2","frontmatter":{"path":"/part-9/5-class-attributes","title":"Class attributes"},"moocfiExercises":[{"id":"Postcodes","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"},{"id":"List helper","type":"programming-exercise","parentPagePath":"/part-9/5-class-attributes"}]}},{"node":{"id":"bde462c3-7c73-533d-a324-18481e825429","frontmatter":{"path":"/part-9","title":"Part 9"},"moocfiExercises":[]}},{"node":{"id":"e91bb465-0565-5764-8df9-bdfc4b10d4d1","frontmatter":{"path":"/part-10/4-application-development","title":"Developing a larger application"},"moocfiExercises":[{"id":"Phone book expansion, version 1","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"Phone book expansion, version 2","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"CourseRecords","type":"programming-exercise","parentPagePath":"/part-10/4-application-development"},{"id":"e1f692af-e830-58cf-b4cf-18a2fc76e434","type":"quiz","parentPagePath":"/part-10/4-application-development"}]}}]}},"pageContext":{}}, "staticQueryHashes": ["1357757885","994120085"]} \ No newline at end of file diff --git a/part-1/1-getting-started/index.html b/part-1/1-getting-started/index.html index c44e6fff3..4c19b02cb 100644 --- a/part-1/1-getting-started/index.html +++ b/part-1/1-getting-started/index.html @@ -210,4 +210,4 @@ # there are 365 days in a year and 24 hours in each day print(365*24)

    When the program is run, the comment will not be visible to the user:

    Sample output

    Hours in a year: 8760

    Short comments can also be added to the end of a line:

    print("Hours in a year:")
    -print(365*24) # 365 days, 24 hours in each day
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +print(365*24) # 365 days, 24 hours in each day
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-1/2-information-from-the-user/index.html b/part-1/2-information-from-the-user/index.html index ecc3e158a..a146ada6c 100644 --- a/part-1/2-information-from-the-user/index.html +++ b/part-1/2-information-from-the-user/index.html @@ -225,4 +225,4 @@ print("Your address is now " + address)

    An example of how the program's output might look like:

    Sample output

    What is your address? Python Path 10 Please type in a new address: Programmer's Walk 23 -Your address is now Programmer's Walk 23

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +Your address is now Programmer's Walk 23

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-1/3-more-about-variables/index.html b/part-1/3-more-about-variables/index.html index c804ae71e..724025fc4 100644 --- a/part-1/3-more-about-variables/index.html +++ b/part-1/3-more-about-variables/index.html @@ -210,7 +210,7 @@
  • You will be able to use variables in different contexts
  • You will know what kind of data can be stored in variables
  • You will understand the difference between strings, integers and floating point numbers
  • -

    Please fill in this questionnaire before getting started with this section. You will get one exercise point for answering.

    Loading...
    :
    Loading...

    Log in to view the quiz

    Variables are needed for various purposes in programming. You can use variables to store any information that will be needed later in the program's execution.

    In Python programming variables are created like so:

    variable_name = ...

    Here ... means the value stored in the variable.

    For example, when you used the input command to read a string from the user, you stored the string in a variable and then used the variable later in your program:

    name = input("What is your name? ")
    +

    Please fill in this questionnaire before getting started with this section. You will get one exercise point for answering.

    Loading...
    :
    Loading...

    Log in to view the quiz

    Variables are needed for various purposes in programming. You can use variables to store any information that will be needed later in the program's execution.

    In Python programming variables are created like so:

    variable_name = ...

    Here ... means the value stored in the variable.

    For example, when you used the input command to read a string from the user, you stored the string in a variable and then used the variable later in your program:

    name = input("What is your name? ")
     print("Hi, " + name)
    Sample output

    What is your name? Ghosty Hi, Ghosty

    The value stored in a variable can also be defined using other variables:

    given_name = "Paul"
     family_name = "Python"
    @@ -277,4 +277,4 @@
     number3 = 3.62
     
     mean = (number1 + number2 + number3) / 3
    -print(f"Mean: {mean}")
    Sample output

    Mean: 1.6233333333333333

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +print(f"Mean: {mean}")
    Sample output

    Mean: 1.6233333333333333

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-1/4-arithmetic-operations/index.html b/part-1/4-arithmetic-operations/index.html index cecde6400..bd7cf3296 100644 --- a/part-1/4-arithmetic-operations/index.html +++ b/part-1/4-arithmetic-operations/index.html @@ -328,4 +328,4 @@ print("Hi " + name + "!") age = int(input("What is your age? ")) -# program continues...
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +# program continues...
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-1/5-conditional-statements/index.html b/part-1/5-conditional-statements/index.html index 86df59086..f8798c5e4 100644 --- a/part-1/5-conditional-statements/index.html +++ b/part-1/5-conditional-statements/index.html @@ -316,4 +316,4 @@ print("a is less than 5")
    Sample output

    True a is less than 5

    The Python keywords True and False can also be used directly. In the following example the print command is executed every time, because the value of the condition is True:

    condition = True
     if condition:
    -    print("This is printed every time.")
    Sample output

    This is printed every time.

    A program like this is not very useful, but later on during the course you will see examples of Boolean variables coming in very handy.

    Please respond to a quick questionnaire on this week's materials. The questionnaire is worth one exercise point.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file + print("This is printed every time.")
    Sample output

    This is printed every time.

    A program like this is not very useful, but later on during the course you will see examples of Boolean variables coming in very handy.

    Please respond to a quick questionnaire on this week's materials. The questionnaire is worth one exercise point.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-1/index.html b/part-1/index.html index 72061da23..b302545de 100644 --- a/part-1/index.html +++ b/part-1/index.html @@ -152,4 +152,4 @@ } })
    \ No newline at end of file +
    Loading...
    \ No newline at end of file diff --git a/part-10/1-class-hierarchies/index.html b/part-10/1-class-hierarchies/index.html index 213920c47..210add617 100644 --- a/part-10/1-class-hierarchies/index.html +++ b/part-10/1-class-hierarchies/index.html @@ -387,4 +387,4 @@ print(bonus) print(bonus2)
    Sample output

    0.7225 -0.7586250000000001

    Loading
    Loading
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +0.7586250000000001

    Loading
    Loading
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-10/2-access-modifiers/index.html b/part-10/2-access-modifiers/index.html index 536b79d6d..3a5d441ff 100644 --- a/part-10/2-access-modifiers/index.html +++ b/part-10/2-access-modifiers/index.html @@ -311,4 +311,4 @@ if __name__ == "__main__": jp = Footballer("peter pythons", "pyper", "forward") print(jp) -
    Sample output

    Footballer - name: Peter Pythons, nickname: Pyper, position: forward

    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +
    Sample output

    Footballer - name: Peter Pythons, nickname: Pyper, position: forward

    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-10/3-oo-programming-techniques/index.html b/part-10/3-oo-programming-techniques/index.html index 5da86754d..0d7fb777d 100644 --- a/part-10/3-oo-programming-techniques/index.html +++ b/part-10/3-oo-programming-techniques/index.html @@ -442,4 +442,4 @@ for book in shelf: print(book.name)
    Sample output

    The Life of Python The Old Man and the C -A Good Cup of Java

    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +A Good Cup of Java

    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-10/4-application-development/index.html b/part-10/4-application-development/index.html index cae1a4499..5bf0157a0 100644 --- a/part-10/4-application-development/index.html +++ b/part-10/4-application-development/index.html @@ -501,4 +501,4 @@ storage_service = CloudHandler("amazon-cloud", "username", "passwrd") application = PhoneBookApplication(storage_service) -application.execute()

    As you have seen before, using techniques like this carries a price tag, as there is more code to write, so a programmer needs to consider whether that is an acceptable tradeoff.

    The technique outlined above is called dependency injection. As the name implies, the idea is to provide any dependency required by an object from outside the object. It is a very useful tool in a programmer's toolbox, as it makes it easier to implement new features in programs and facilitates automatic testing. This theme will be further explored on the aforementioned courses Software Development Methods and Software Engineering.

    Please respond to a quick questionnaire on this part of the course.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +application.execute()

    As you have seen before, using techniques like this carries a price tag, as there is more code to write, so a programmer needs to consider whether that is an acceptable tradeoff.

    The technique outlined above is called dependency injection. As the name implies, the idea is to provide any dependency required by an object from outside the object. It is a very useful tool in a programmer's toolbox, as it makes it easier to implement new features in programs and facilitates automatic testing. This theme will be further explored on the aforementioned courses Software Development Methods and Software Engineering.

    Please respond to a quick questionnaire on this part of the course.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-10/index.html b/part-10/index.html index 69e406aeb..8f4d6aa3e 100644 --- a/part-10/index.html +++ b/part-10/index.html @@ -152,4 +152,4 @@ } })
    \ No newline at end of file +
    Loading...
    \ No newline at end of file diff --git a/part-11/1-list-comprehensions/index.html b/part-11/1-list-comprehensions/index.html index b70f384b9..1c8ba2fc0 100644 --- a/part-11/1-list-comprehensions/index.html +++ b/part-11/1-list-comprehensions/index.html @@ -270,4 +270,4 @@ if __name__ == "__main__": test_list = ["hi", 3, True, "there", -123.344, "toodlepip", 2, False] lengths = string_lengths(test_list) - print(lengths)
    Sample output

    [2, -1, -1, 5, -1, 9, -1, -1]

    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file + print(lengths)
    Sample output

    [2, -1, -1, 5, -1, 9, -1, -1]

    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-11/2-more-comprehensions/index.html b/part-11/2-more-comprehensions/index.html index 051937a97..fd7fe2699 100644 --- a/part-11/2-more-comprehensions/index.html +++ b/part-11/2-more-comprehensions/index.html @@ -340,4 +340,4 @@ if __name__ == "__main__": numbers = [-2, 3, 2, 1, 4, -10, 5, 1, 6] factorials = {number : factorial(number) for number in numbers if number > 0} - print(factorials)
    Sample output

    {3: 6, 2: 2, 1: 1, 4: 24, 5: 120, 6: 720}

    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file + print(factorials)
    Sample output

    {3: 6, 2: 2, 1: 1, 4: 24, 5: 120, 6: 720}

    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-11/3-recursion/index.html b/part-11/3-recursion/index.html index 28d3085c5..69b80fb43 100644 --- a/part-11/3-recursion/index.html +++ b/part-11/3-recursion/index.html @@ -313,4 +313,4 @@ print(binary_search(target, 15, 0, len(target)-1))
    Sample output

    True True False -False

    The binary_search function takes four arguments: the target list, the item being searched for, and the left and right edges of the search area. When the function is first called, the search area covers the entire target list. The left edge is at index 0 and the right edge is at index len(target)-1. The function calculates the central index and checks that position on the list. Either the item was found, or the search continues to the smaller or greater half of the target list.

    Let's compare this to a simple linear search. In a linear search, the search area is traversed from the beginning onwards, until either the item is found, or we run out of search area. The number of steps needed to cover the entire search area grows linearly at the same pace as the size of the search area. Each search step covers only one search candidate from the beginning of the search area. Let's assume the item searched for is not found. If the search area is a million items long, we would have to take a million search steps to make sure the item is not in the search area.

    In a binary search, on the other hand, the number of steps needed grows logarithmically. Let's assume again that the item searched for is not found. The search area is cut in half with each step, as we know that the item is either smaller or greater than the current search candidate at the centre. 2 to the power of 20 (2^20) is already well over 1 million, so it will take at most 20 steps to cover the entire search area with a binary search. So, when we are dealing with sorted search areas, as we often are when dealing with computers and materials that are meant to be automatically processed, a binary search is much more efficient than a linear search.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +False

    The binary_search function takes four arguments: the target list, the item being searched for, and the left and right edges of the search area. When the function is first called, the search area covers the entire target list. The left edge is at index 0 and the right edge is at index len(target)-1. The function calculates the central index and checks that position on the list. Either the item was found, or the search continues to the smaller or greater half of the target list.

    Let's compare this to a simple linear search. In a linear search, the search area is traversed from the beginning onwards, until either the item is found, or we run out of search area. The number of steps needed to cover the entire search area grows linearly at the same pace as the size of the search area. Each search step covers only one search candidate from the beginning of the search area. Let's assume the item searched for is not found. If the search area is a million items long, we would have to take a million search steps to make sure the item is not in the search area.

    In a binary search, on the other hand, the number of steps needed grows logarithmically. Let's assume again that the item searched for is not found. The search area is cut in half with each step, as we know that the item is either smaller or greater than the current search candidate at the centre. 2 to the power of 20 (2^20) is already well over 1 million, so it will take at most 20 steps to cover the entire search area with a binary search. So, when we are dealing with sorted search areas, as we often are when dealing with computers and materials that are meant to be automatically processed, a binary search is much more efficient than a linear search.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-11/4-more-recursion-examples/index.html b/part-11/4-more-recursion-examples/index.html index 6fd539185..2ed6343de 100644 --- a/part-11/4-more-recursion-examples/index.html +++ b/part-11/4-more-recursion-examples/index.html @@ -286,4 +286,4 @@ if value > root.value: return find_node(root.right_child, value) - return find_node(root.left_child, value)
    Loading

    Revisiting the times before recursion

    Let's finish off this part of the material with a slightly larger exercise concentrating on object oriented programming principles. We do not recommend using recursion in this series of tasks, but list comprehension techniques will come in useful.

    Loading
    Loading

    Please respond to a quick questionnaire on this part of the course.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file + return find_node(root.left_child, value)
    Loading

    Revisiting the times before recursion

    Let's finish off this part of the material with a slightly larger exercise concentrating on object oriented programming principles. We do not recommend using recursion in this series of tasks, but list comprehension techniques will come in useful.

    Loading
    Loading

    Please respond to a quick questionnaire on this part of the course.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-11/index.html b/part-11/index.html index 9f292f749..a7d12eb88 100644 --- a/part-11/index.html +++ b/part-11/index.html @@ -152,4 +152,4 @@ } })
    \ No newline at end of file +
    Loading...
    \ No newline at end of file diff --git a/part-12/1-functions-as-arguments/index.html b/part-12/1-functions-as-arguments/index.html index e2a9b26ec..cc28fa9bc 100644 --- a/part-12/1-functions-as-arguments/index.html +++ b/part-12/1-functions-as-arguments/index.html @@ -378,4 +378,4 @@ copy_lines("first.txt", "second.txt", lambda line: "Python" in line) # Copy all lines which do not end in a full stop - copy_lines("first.txt", "second.txt", lambda line: line[-1] != ".")

    The function definition contains a default value for the keyword parameter criterion: lambda x: True. This anonymous function always returns True regardless of the input. So, the default behaviour is to copy all lines. As usual, if a value is given for a parameter with a default value, the new value replaces the default value.

    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file + copy_lines("first.txt", "second.txt", lambda line: line[-1] != ".")

    The function definition contains a default value for the keyword parameter criterion: lambda x: True. This anonymous function always returns True regardless of the input. So, the default behaviour is to copy all lines. As usual, if a value is given for a parameter with a default value, the new value replaces the default value.

    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-12/2-generators/index.html b/part-12/2-generators/index.html index 8b2c18b09..84b4b6054 100644 --- a/part-12/2-generators/index.html +++ b/part-12/2-generators/index.html @@ -252,4 +252,4 @@ ghi hij ijk -jkl

    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +jkl

    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-12/3-functional-programming/index.html b/part-12/3-functional-programming/index.html index f6685785c..c81176136 100644 --- a/part-12/3-functional-programming/index.html +++ b/part-12/3-functional-programming/index.html @@ -413,4 +413,4 @@ sum_of_numbers = reduce(lambda reduced_sum, item: reduced_sum + item, my_list) -print(sum_of_numbers)

    If the initial value is left out, reduce takes the first item in the list as the initial value and starts reducing from the second item onwards.

    NB: if the items in the series are of a different type than the intended reduced result, the thrd argument is mandatory. The example with the bank accounts would not work without the initial value. That is, trying this

    balances_total = reduce(balance_sum_helper, accounts)

    would produce an error:

    TypeError: unsupported operand type(s) for +: 'BankAccount' and 'int'

    In the above case, when reduce tries to execute the balance_sum_helper function for the first time, the arguments it uses are the two first items in the list, which are both of type BankAccount. Specifically, the value assigned to the parameter balance_sum is the first item in the list. The balance_sum_helper function tries to add an integer value to it, but adding an integer directly to a BankAccount object is not a supported operation.

    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +print(sum_of_numbers)

    If the initial value is left out, reduce takes the first item in the list as the initial value and starts reducing from the second item onwards.

    NB: if the items in the series are of a different type than the intended reduced result, the thrd argument is mandatory. The example with the bank accounts would not work without the initial value. That is, trying this

    balances_total = reduce(balance_sum_helper, accounts)

    would produce an error:

    TypeError: unsupported operand type(s) for +: 'BankAccount' and 'int'

    In the above case, when reduce tries to execute the balance_sum_helper function for the first time, the arguments it uses are the two first items in the list, which are both of type BankAccount. Specifically, the value assigned to the parameter balance_sum is the first item in the list. The balance_sum_helper function tries to add an integer value to it, but adding an integer directly to a BankAccount object is not a supported operation.

    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-12/4-regular-expressions/index.html b/part-12/4-regular-expressions/index.html index c94a00d51..3d86d4d85 100644 --- a/part-12/4-regular-expressions/index.html +++ b/part-12/4-regular-expressions/index.html @@ -288,4 +288,4 @@ Please type in a string: jarjar the hut Not found. Please type in a string: jabba the smut -Not found.

    Loading

    Grand finale

    To finish off this part of the material, let's work some more on objects and classes by building a slightly more extensive program. This exercise does not necessarily involve regular expressions, but the sections on functions as arguments and list comprehensions will likely be useful.

    You may also find the example set in part 10 helpful.

    Loading

    Please respond to a quick questionnaire on this part of the course.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +Not found.

    Loading

    Grand finale

    To finish off this part of the material, let's work some more on objects and classes by building a slightly more extensive program. This exercise does not necessarily involve regular expressions, but the sections on functions as arguments and list comprehensions will likely be useful.

    You may also find the example set in part 10 helpful.

    Loading

    Please respond to a quick questionnaire on this part of the course.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-12/index.html b/part-12/index.html index f0442a297..44038e9ce 100644 --- a/part-12/index.html +++ b/part-12/index.html @@ -152,4 +152,4 @@ } })
    \ No newline at end of file +
    Loading...
    \ No newline at end of file diff --git a/part-13/1-pygame/index.html b/part-13/1-pygame/index.html index 04b8e8887..0bdbf2e31 100644 --- a/part-13/1-pygame/index.html +++ b/part-13/1-pygame/index.html @@ -267,4 +267,4 @@ window.blit(robot, (300, 0)) window.blit(robot, (100, 200))

    The window should look like this as a result:

    Here we set the location of the image so that it lies at the centre of the window:

    width = robot.get_width()
     height = robot.get_height()
    -window.blit(robot, (320-width/2, 240-height/2))

    The window should now look like this:

    The method get_width returns the width of the image, and the method get_height returns its height, both in pixels. The centre of the window is at half its width and height, so at (320, 240), which we can use to calculate a suitable location for the top left corner of the image, so that it lies exactly at the centre.

    Loading
    Loading
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +window.blit(robot, (320-width/2, 240-height/2))

    The window should now look like this:

    The method get_width returns the width of the image, and the method get_height returns its height, both in pixels. The centre of the window is at half its width and height, so at (320, 240), which we can use to calculate a suitable location for the top left corner of the image, so that it lies exactly at the centre.

    Loading
    Loading
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-13/2-animation/index.html b/part-13/2-animation/index.html index 846fb79af..df0e09dd3 100644 --- a/part-13/2-animation/index.html +++ b/part-13/2-animation/index.html @@ -258,4 +258,4 @@ angle += 0.01 clock.tick(60)

    Running the above code should look like this:

    Rotation in a relatively precise circle is achieved with the help of some basic trigonometric functions. The varible angle contains the angle of the robots location in relation to the centre of the window and the horizontal line running through it. The sine and cosine functions from the Python math library are used to calculate the coordinates of the robot's location:

            x = 320+math.cos(angle)*100-robot.get_width()/2
    -        y = 240+math.sin(angle)*100-robot.get_height()/2

    The robot rotates around a circle of radius 100 around the centre of the window. The hypotenuse in this scenario is the radius of the circle. The cosine function gives the length of the adjacent side of a right triangle in relation to the hypotenuse, which means that it gives us the x coordinate of the location. The sine function gives the length of the opposite side, i.e. the y coordinate. The location is then adjusted for the size of the image, so that the centre of the circle is at the centre of the window.

    With each iteration the size of the angle is incremented by 0.01:llä. As we are using radians, a full circle is 2π, which equals about 6.28. It takes about 628 iterations for the robot to go a full circle, and at 60 iterations per second this takes just over 10 seconds.

    Loading
    Loading
    Loading
    Loading
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file + y = 240+math.sin(angle)*100-robot.get_height()/2

    The robot rotates around a circle of radius 100 around the centre of the window. The hypotenuse in this scenario is the radius of the circle. The cosine function gives the length of the adjacent side of a right triangle in relation to the hypotenuse, which means that it gives us the x coordinate of the location. The sine function gives the length of the opposite side, i.e. the y coordinate. The location is then adjusted for the size of the image, so that the centre of the circle is at the centre of the window.

    With each iteration the size of the angle is incremented by 0.01:llä. As we are using radians, a full circle is 2π, which equals about 6.28. It takes about 628 iterations for the robot to go a full circle, and at 60 iterations per second this takes just over 10 seconds.

    Loading
    Loading
    Loading
    Loading
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-13/3-events/index.html b/part-13/3-events/index.html index 9c9905985..b49cac1f6 100644 --- a/part-13/3-events/index.html +++ b/part-13/3-events/index.html @@ -347,4 +347,4 @@ window.blit(robot, (robot_x, robot_y)) pygame.display.flip() - clock.tick(60)

    The program's execution should look more or less like this:

    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file + clock.tick(60)

    The program's execution should look more or less like this:

    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-13/4-more-pygame-techniques/index.html b/part-13/4-more-pygame-techniques/index.html index 3a868b079..5db21f079 100644 --- a/part-13/4-more-pygame-techniques/index.html +++ b/part-13/4-more-pygame-techniques/index.html @@ -222,4 +222,4 @@ while True: for event in pygame.event.get(): if event.type == pygame.QUIT: - exit()

    Running the above code should look like this:

    Here the method pygame.font.SysFont creates a font object, which uses the system font Arial in size 24. The the method render creates an image of the specified text in the given colour. This image is drawn on the window with the blit method, just as before.

    NB: different systems will have different fonts available. If the system this program is exeuted on doesn't have the Arial font, even though Arial is a very common font available on most systems, the default system font is used instead. If you need to have a specific font available for your game, you can include the font file in the game directory and specify its location for the pygame.font.Font method.

    Exercises

    Here are some more advanced exercises for practicing what you have learned in this part of the course material.

    Loading
    Loading

    Please respond to a quick questionnaire on this part of the course.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file + exit()

    Running the above code should look like this:

    Here the method pygame.font.SysFont creates a font object, which uses the system font Arial in size 24. The the method render creates an image of the specified text in the given colour. This image is drawn on the window with the blit method, just as before.

    NB: different systems will have different fonts available. If the system this program is exeuted on doesn't have the Arial font, even though Arial is a very common font available on most systems, the default system font is used instead. If you need to have a specific font available for your game, you can include the font file in the game directory and specify its location for the pygame.font.Font method.

    Exercises

    Here are some more advanced exercises for practicing what you have learned in this part of the course material.

    Loading
    Loading

    Please respond to a quick questionnaire on this part of the course.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-13/index.html b/part-13/index.html index 3653737fb..da89a9305 100644 --- a/part-13/index.html +++ b/part-13/index.html @@ -152,4 +152,4 @@ } })
    \ No newline at end of file +
    Loading...
    \ No newline at end of file diff --git a/part-14/1-game-project/index.html b/part-14/1-game-project/index.html index c05b88602..c3623a590 100644 --- a/part-14/1-game-project/index.html +++ b/part-14/1-game-project/index.html @@ -408,4 +408,4 @@

    The main loop.display.flip()

    At this stage the only event actually handled by the game is closing the game window, e.g. from the exit button. The game then exits by calling the Python exit function.

    Each time draw_window method is called the entire game grid is matrix is traversed, and the image corresponding to each square in the grid is drawn in the correct location.

    -

    NB: the coordinates x and y are used in two different ways in the game. When dealing with the indexes of a two-dimensional list, it is logical to give the y coordinate first, as the y refers to the number of the row while x is the number of the column. On the other hand, when using pygame methods, x is usually passed first, as it quite often is when dealing with graphics, and also in mathematical contexts.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +

    NB: the coordinates x and y are used in two different ways in the game. When dealing with the indexes of a two-dimensional list, it is logical to give the y coordinate first, as the y refers to the number of the row while x is the number of the column. On the other hand, when using pygame methods, x is usually passed first, as it quite often is when dealing with graphics, and also in mathematical contexts.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-14/2-robot-and-boxes/index.html b/part-14/2-robot-and-boxes/index.html index fa3f43108..9e1c166ce 100644 --- a/part-14/2-robot-and-boxes/index.html +++ b/part-14/2-robot-and-boxes/index.html @@ -255,4 +255,4 @@

    Refactoring?The number 3 meant a box just previously, but now it is subtracted from the value of a square on the grid. This works in the context of our numbering scheme, as it changes a box (3) into a normal floor square (0), or a target square with a box (5) into an empty target square (2), but understanding this requiares a primer in the numbering scheme used.

    We could make it easier for anyone reading the code by refactoring our implementation. That means improving the structure and readability of the code. One way to achieve this would be to use the names of the squares instead of the numbers 0 to 6, even though this would still not explain how and why numbers can be added and subtracted while maintaining the integrity of the grid.

    Making the program code truly accessible would likely require much more fundamentally transformative refactoring. For example, we could keep the structure of the game map in one location, and store the locations of the robot and the boxes in some separate data structure. The downside of this would be that this would likely result in a lot more code, and the internal structure of the game would become much more complicated.

    -

    Refactoring and code quality is a subject for some subsequent courses, such as Software Development Methods and Software Engineering.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +

    Refactoring and code quality is a subject for some subsequent courses, such as Software Development Methods and Software Engineering.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-14/3-finishing-the-game/index.html b/part-14/3-finishing-the-game/index.html index 97df12cd9..ff58851dc 100644 --- a/part-14/3-finishing-the-game/index.html +++ b/part-14/3-finishing-the-game/index.html @@ -248,4 +248,4 @@

    Your game on GitHub?GitHub is a popular place for many kinds of programming projects. It can be used to store the source code and other materials of all your own programming projects as well, and your program will then be maintained through git version control, and it can be easily shared with others. You will become very familiar with git and GitHub if you continue on to other mooc.fi programming courses.

    How many moves are required?

    The grid in this game is quite small, but the game is not all that easy. The first challenge is simply passing the game, but the next stage is trying to do so with as few moves as possible. How short is the shortest path to a solution?

    -

    Looking for the shortest possible solution is not an easy task at all, but there are computational solutions to this as well. They are one of the subjects of the Data Structures and Algorithms course.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +

    Looking for the shortest possible solution is not an easy task at all, but there are computational solutions to this as well. They are one of the subjects of the Data Structures and Algorithms course.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-14/4-your-own-game/index.html b/part-14/4-your-own-game/index.html index 8caed4cf8..9ea236df3 100644 --- a/part-14/4-your-own-game/index.html +++ b/part-14/4-your-own-game/index.html @@ -255,9 +255,9 @@

    Peer reviewHow well is it programmed? Is the code readable, and does it make appropriate use of classes and functions?

    A good review usually points out both good features and some suggestions for improvements.

    -
    Loading...
    :
    Loading...

    Log in to view the quiz

    +
    Loading...
    :
    Loading...

    Log in to view the quiz

    Questionnaires to finish off

    First, please respond to a quick questionnaire on this part of the course.

    -
    Loading...
    :
    Loading...

    Log in to view the quiz

    +
    Loading...
    :
    Loading...

    Log in to view the quiz

    Please also respond to the course feedback questionnaire. The questionnaire results help us improve the course.

    -
    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +
    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-14/index.html b/part-14/index.html index 684abce46..597bcedc3 100644 --- a/part-14/index.html +++ b/part-14/index.html @@ -152,4 +152,4 @@ } })
    \ No newline at end of file +
    Loading...
    \ No newline at end of file diff --git a/part-2/1-programming-terminology/index.html b/part-2/1-programming-terminology/index.html index cc258043d..05c90e4f5 100644 --- a/part-2/1-programming-terminology/index.html +++ b/part-2/1-programming-terminology/index.html @@ -309,4 +309,4 @@ Daily wages: 120.0 euros

    It seems the value stored in daily_wages is correct at first: hourly_wage = 20.0 and hours = 6, and 20.0 * 6 = 120.0. The command which is supposed to double the figure doesn't do so, however, so there must be a problem with the command. And indeed the command

    daily_wages * 2

    does double the value, but it doesn't store the new value anywhere. Let's change it so it also stores the new value:

    daily_wages *= 2

    Running the program again reveals that the printout at the end is now also correct:

    Sample output

    condition: True wages before: 120 wages after doubling: 240 -Daily wages: 240.0 euros

    When the program has been fixed, remember to remove all debugging print statements and other code added for debugging purposes.

    This example was quite simple, and in such a short program one could probably figure out the bugs just by reading the code carefully. However, using debugging print statements is often a quick way to get a feeling for where the problem might lie. Print statements can be used to figure out which parts of the program seem to work correctly, so bug tracking efforts can be concentrated on the sections of code which are the most likely culprits.

    Debugging print statements are only one tool for debugging programs. We will come back to this subject later on during this course. You should now get into the habit of using debugging print statements to look for mistakes in your code. Programming professionals cannot get by without using them, so it is a very useful tool for beginners as well.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +Daily wages: 240.0 euros

    When the program has been fixed, remember to remove all debugging print statements and other code added for debugging purposes.

    This example was quite simple, and in such a short program one could probably figure out the bugs just by reading the code carefully. However, using debugging print statements is often a quick way to get a feeling for where the problem might lie. Print statements can be used to figure out which parts of the program seem to work correctly, so bug tracking efforts can be concentrated on the sections of code which are the most likely culprits.

    Debugging print statements are only one tool for debugging programs. We will come back to this subject later on during this course. You should now get into the habit of using debugging print statements to look for mistakes in your code. Programming professionals cannot get by without using them, so it is a very useful tool for beginners as well.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-2/2-else-elif/index.html b/part-2/2-else-elif/index.html index 40a19be69..7171c998e 100644 --- a/part-2/2-else-elif/index.html +++ b/part-2/2-else-elif/index.html @@ -261,4 +261,4 @@ It's Hogmanay Thanks and bye.

    Notice the previous example has no else branch. If the user inputs a date which is not mentioned in any of the if or elif branches, or inputs a date in a different format, none of the three branches of the conditional statement is executed.

    Sample output

    Holiday calendar What is the date today? Dec 25 -Thanks and bye.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +Thanks and bye.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-2/3-combining-conditions/index.html b/part-2/3-combining-conditions/index.html index 324cb1d9f..333e9f530 100644 --- a/part-2/3-combining-conditions/index.html +++ b/part-2/3-combining-conditions/index.html @@ -284,4 +284,4 @@ elif number > 0 and number % 2 != 0: print("The number is odd") else: - print("The number is negative or zero")

    Neither approach is intrinsically better than the other, but in different situations one or the other may seem more logical. In this particular example most people tend to find the first version with nesting to be more intuitive.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file + print("The number is negative or zero")

    Neither approach is intrinsically better than the other, but in different situations one or the other may seem more logical. In this particular example most people tend to find the first version with nesting to be more intuitive.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-2/4-simple-loops/index.html b/part-2/4-simple-loops/index.html index 730dfdb50..3230aef5c 100644 --- a/part-2/4-simple-loops/index.html +++ b/part-2/4-simple-loops/index.html @@ -357,4 +357,4 @@ attempts += 1 codes += code + ", " # ...

    The helper variable is initialized to an empty string, that is, a string with no characters in it:

    codes = ""

    With each iteration the string gets longer, as the code the user typed in is added, along with a comma:

        code = input("Please type in your PIN: ")
    -    codes += code + ", "

    If the user types in the codes 1111 2222 1234, at the end of the program's execution the value of codes would be

    Sample output

    1111, 2222, 1234,

    Please respond to a quick questionnaire on this week's materials.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file + codes += code + ", "

    If the user types in the codes 1111 2222 1234, at the end of the program's execution the value of codes would be

    Sample output

    1111, 2222, 1234,

    Please respond to a quick questionnaire on this week's materials.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-2/index.html b/part-2/index.html index 896c7ae32..2adf1cf1e 100644 --- a/part-2/index.html +++ b/part-2/index.html @@ -152,4 +152,4 @@ } })
    \ No newline at end of file +
    Loading...
    \ No newline at end of file diff --git a/part-3/1-loops-with-conditions/index.html b/part-3/1-loops-with-conditions/index.html index 71f6fad13..4a9f8f655 100644 --- a/part-3/1-loops-with-conditions/index.html +++ b/part-3/1-loops-with-conditions/index.html @@ -293,4 +293,4 @@ verdict += f"the grade {grade} " verdict += f"from the course {course}" -print(verdict)
    Sample output

    You have received the grade 4 from the course Introduction to Programming

    In the previous exercise you calculated the sum of consecutive numbers by always adding a new value inside a loop.

    The exact same idea applies to strings as well: you can add new parts to a string within a loop. This technique should be useful in the following exercise.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +print(verdict)
    Sample output

    You have received the grade 4 from the course Introduction to Programming

    In the previous exercise you calculated the sum of consecutive numbers by always adding a new value inside a loop.

    The exact same idea applies to strings as well: you can add new parts to a string within a loop. This technique should be useful in the following exercise.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-3/2-working-with-strings/index.html b/part-3/2-working-with-strings/index.html index 56524c0c1..430203113 100644 --- a/part-3/2-working-with-strings/index.html +++ b/part-3/2-working-with-strings/index.html @@ -337,4 +337,4 @@ Not found What are you looking for? pen Found it at the index 3 -...

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +...

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-3/3-more-loops/index.html b/part-3/3-more-loops/index.html index 8b7757093..abdf9c688 100644 --- a/part-3/3-more-loops/index.html +++ b/part-3/3-more-loops/index.html @@ -315,4 +315,4 @@ 0 1 2 3 0 1 2 0 1 -0

    In this program the outer loop uses the helper variable number, which decreases by 1 with each iteration until it reaches 0. The helper variable i is set to 0 just before the inner loop is entered, each time the outer loop repeats.

    The inner loop uses the helper variable i, which increases by 1 with each iteration of the inner loop. The inner loop repeats until i is equal to number, and prints out each value of i on the same line, separated by a space character. When the inner loop finishes, the print command in the outer loop starts a new line.

    Now remember that with each iteration of the outer loop the value of number decreases, so the amount of times the inner loop repeats also decreases. With each repetition the line of numbers gets shorter, and thus we get the pyramid shape.

    Nested loops can get confusing fast, but understanding the way they work is essential. You may well find the Python Tutor visualisation tool helpful in understanding how this example works. Copy the above code into the code window of the tool and follow the formation of the printout and the changing values of the helper variables as the execution progresses.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +0

    In this program the outer loop uses the helper variable number, which decreases by 1 with each iteration until it reaches 0. The helper variable i is set to 0 just before the inner loop is entered, each time the outer loop repeats.

    The inner loop uses the helper variable i, which increases by 1 with each iteration of the inner loop. The inner loop repeats until i is equal to number, and prints out each value of i on the same line, separated by a space character. When the inner loop finishes, the print command in the outer loop starts a new line.

    Now remember that with each iteration of the outer loop the value of number decreases, so the amount of times the inner loop repeats also decreases. With each repetition the line of numbers gets shorter, and thus we get the pyramid shape.

    Nested loops can get confusing fast, but understanding the way they work is essential. You may well find the Python Tutor visualisation tool helpful in understanding how this example works. Copy the above code into the code window of the tool and follow the formation of the printout and the changing values of the helper variables as the execution progresses.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-3/4-defining-functions/index.html b/part-3/4-defining-functions/index.html index 95361db70..ba6065ccb 100644 --- a/part-3/4-defining-functions/index.html +++ b/part-3/4-defining-functions/index.html @@ -275,4 +275,4 @@ hello("Steve") hello("Betty")
    Sample output

    Hello Betty -Hello Betty

    No matter how many different arguments we call the function with, it will always print out the value "Betty" stored in the global variable.

    Please respond to a quick questionnaire on this week's materials.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +Hello Betty

    No matter how many different arguments we call the function with, it will always print out the value "Betty" stored in the global variable.

    Please respond to a quick questionnaire on this week's materials.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-3/index.html b/part-3/index.html index 4a26b5c9f..c157ca2d3 100644 --- a/part-3/index.html +++ b/part-3/index.html @@ -152,4 +152,4 @@ } })
    \ No newline at end of file +
    Loading...
    \ No newline at end of file diff --git a/part-4/1-vscode/index.html b/part-4/1-vscode/index.html index 831d374a8..98042ad6d 100644 --- a/part-4/1-vscode/index.html +++ b/part-4/1-vscode/index.html @@ -313,4 +313,4 @@ 4 1 9 -

    You can include several breakpoints in your program code. When execution has halted, you can start it again by clicking on the blue triangle. Execution proceeds until it reaches the next breakpoint.

    The built-in visual debugger is a good alternative to debugging print statements. It is up to you which you choose to employ more in the future. Each programmer has their own preferences, but it's always a good idea to try different options before settling on any single solution.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +

    You can include several breakpoints in your program code. When execution has halted, you can start it again by clicking on the blue triangle. Execution proceeds until it reaches the next breakpoint.

    The built-in visual debugger is a good alternative to debugging print statements. It is up to you which you choose to employ more in the future. Each programmer has their own preferences, but it's always a good idea to try different options before settling on any single solution.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-4/2-more-functions/index.html b/part-4/2-more-functions/index.html index 40a927967..abe946894 100644 --- a/part-4/2-more-functions/index.html +++ b/part-4/2-more-functions/index.html @@ -322,4 +322,4 @@ print(message) times -= 1

    This tells anyone using the function that the argument stored in message is supposed to be a string, and the argument stored in times is supposed to be an integer.

    Similarly, the return value of a function can be hinted at in the function definition:

    def ask_for_name() -> str:
         name = input("Mikä on nimesi? ")
    -    return name

    This tells the user of the function that the function is supposed to return a string.

    NB: Type hinting is literally just hinting about the type of the argument or the return value. It is not a guarantee of type, and definitely not a safeguard against type errors. If a function receives an argument or returns a value of the wrong type, the function is still executed, but it might not work correctly.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file + return name

    This tells the user of the function that the function is supposed to return a string.

    NB: Type hinting is literally just hinting about the type of the argument or the return value. It is not a guarantee of type, and definitely not a safeguard against type errors. If a function receives an argument or returns a value of the wrong type, the function is still executed, but it might not work correctly.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-4/3-lists/index.html b/part-4/3-lists/index.html index be65b5bd7..61f6abf36 100644 --- a/part-4/3-lists/index.html +++ b/part-4/3-lists/index.html @@ -368,4 +368,4 @@ weights = input_numbers() print("Heights:") -heights = input_numbers()
    Loading
    Loading
    Loading

    There are many more ways to use lists in Python. The Python documentation is a good place to start if you want to know more.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +heights = input_numbers()
    Loading
    Loading
    Loading

    There are many more ways to use lists in Python. The Python documentation is a good place to start if you want to know more.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-4/4-definite-iteration/index.html b/part-4/4-definite-iteration/index.html index c2f139881..91ebdbd74 100644 --- a/part-4/4-definite-iteration/index.html +++ b/part-4/4-definite-iteration/index.html @@ -251,4 +251,4 @@ if item is better than best: best = item -# We now have the best one figured out!

    The details of the final program code depend on the type of the items in the list, and also on the criteria for choosing the best (or worst) item. Sometimes you may need more than one helper variable.

    Let's practice this method a little.

    Loading
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +# We now have the best one figured out!

    The details of the final program code depend on the type of the items in the list, and also on the criteria for choosing the best (or worst) item. Sometimes you may need more than one helper variable.

    Let's practice this method a little.

    Loading
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-4/5-print-statement-formatting/index.html b/part-4/5-print-statement-formatting/index.html index fd3517b3b..f3b4e5133 100644 --- a/part-4/5-print-statement-formatting/index.html +++ b/part-4/5-print-statement-formatting/index.html @@ -206,4 +206,4 @@ age = 48 city = "Palo Alto" greeting = f"Hi {name}, you are {age} years of age" -print(greeting + f", and you live in {city}")
    Sample output

    Hi Larry, you are 48 years of age, and you live in Palo Alto

    You can think of an f-string as a sort of function, which creates a normal string based on the "arguments" within the curly brackets.

    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +print(greeting + f", and you live in {city}")
    Sample output

    Hi Larry, you are 48 years of age, and you live in Palo Alto

    You can think of an f-string as a sort of function, which creates a normal string based on the "arguments" within the curly brackets.

    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-4/6-strings-and-lists/index.html b/part-4/6-strings-and-lists/index.html index e9da87f29..707c43927 100644 --- a/part-4/6-strings-and-lists/index.html +++ b/part-4/6-strings-and-lists/index.html @@ -338,4 +338,4 @@ print(analysis_result) # run the main function -main()
    Loading

    Please respond to a quick questionnaire on this week's materials.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +main()
    Loading

    Please respond to a quick questionnaire on this week's materials.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-4/index.html b/part-4/index.html index 06990d189..f7ff8fa5a 100644 --- a/part-4/index.html +++ b/part-4/index.html @@ -152,4 +152,4 @@ } })
    \ No newline at end of file +
    Loading...
    \ No newline at end of file diff --git a/part-5/1-more-lists/index.html b/part-5/1-more-lists/index.html index e471112b8..6bcead7b7 100644 --- a/part-5/1-more-lists/index.html +++ b/part-5/1-more-lists/index.html @@ -422,4 +422,4 @@ _ _ 7 8 _ 3 9 _ _ _ _ 1 _ _ _ _ _ 3 3 _ _ _ _ _ _ _ 2 -

    Any common game with a gameboard layout can be modelled in a similar fashion. Among others, chess, Minesweeper, Battleship or Mastermind are all based on a two-dimensional grid. For sudoku, it is natural to use numbers to represent the game state, but for other games, different methods may be better.

    Loading
    Loading
    Loading
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +

    Any common game with a gameboard layout can be modelled in a similar fashion. Among others, chess, Minesweeper, Battleship or Mastermind are all based on a two-dimensional grid. For sudoku, it is natural to use numbers to represent the game state, but for other games, different methods may be better.

    Loading
    Loading
    Loading
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-5/2-references/index.html b/part-5/2-references/index.html index 619fed1ef..f5395ea72 100644 --- a/part-5/2-references/index.html +++ b/part-5/2-references/index.html @@ -379,4 +379,4 @@ numbers = [1, 4, 2, 5, 3, 6, 4, 7] print(second_smallest(numbers)) print(numbers)
    Sample output

    2 -[1, 4, 2, 5, 3, 6, 4, 7]

    The function sorted returns a new, sorted copy of the list, so looking for the second smallest item no longer messes with the order of the original list.

    It is generally considered a good programming practice to avoid causing side effects with functions. Side effects can make it more difficult to verify that the program functions as intended in all situations.

    Functions free of side effects are also called pure functions. Especially when adhering to a functional programming style, this is a common ideal to follow. We will explore this topic further in Advanced Course in Programming, which is the course following this one.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +[1, 4, 2, 5, 3, 6, 4, 7]

    The function sorted returns a new, sorted copy of the list, so looking for the second smallest item no longer messes with the order of the original list.

    It is generally considered a good programming practice to avoid causing side effects with functions. Side effects can make it more difficult to verify that the program functions as intended in all situations.

    Functions free of side effects are also called pure functions. Especially when adhering to a functional programming style, this is a common ideal to follow. We will explore this topic further in Advanced Course in Programming, which is the course following this one.

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-5/3-dictionary/index.html b/part-5/3-dictionary/index.html index 10a714c97..baef6cd98 100644 --- a/part-5/3-dictionary/index.html +++ b/part-5/3-dictionary/index.html @@ -325,4 +325,4 @@ print("The average height is", combined_height / len(people))
    Sample output

    Pippa Python Peter Pythons Pedro Python -The average height is 173.0

    Loading
    Loading

    At this point in the course, you can choose to participate in a research study related to learning programming. Participation is voluntary and individual participants cannot be identified from the data gathered in the study. You can freely quit the experiment at any point. Click here to begin the study!

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +The average height is 173.0

    Loading
    Loading

    At this point in the course, you can choose to participate in a research study related to learning programming. Participation is voluntary and individual participants cannot be identified from the data gathered in the study. You can freely quit the experiment at any point. Click here to begin the study!

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-5/4-tuple/index.html b/part-5/4-tuple/index.html index 11946ea0e..fd6314c88 100644 --- a/part-5/4-tuple/index.html +++ b/part-5/4-tuple/index.html @@ -235,4 +235,4 @@ print("key:", key) print("value:", value)

    Tuples are at work here, too. The method my_dictionary.items() returns each key-value pair as a tuple, where the first item is the key and the second item is the value.

    Another common use case for tuples is swapping the values of two variables:

    number1, number2 = number2, number1

    The assignment statement above swaps the values stored in the variables number1 and number2. The result is identical to what is achieved with the following bit of code, using a helper variable:

    helper_var = number1
     number1 = number2
    -number2 = helper_var
    Loading
    Loading

    Please respond to a quick questionnaire on this week's materials.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +number2 = helper_var
    Loading
    Loading

    Please respond to a quick questionnaire on this week's materials.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-5/index.html b/part-5/index.html index 355fa9a92..ef643d6d1 100644 --- a/part-5/index.html +++ b/part-5/index.html @@ -152,4 +152,4 @@ } })
    \ No newline at end of file +
    Loading...
    \ No newline at end of file diff --git a/part-6/1-reading-files/index.html b/part-6/1-reading-files/index.html index 87fb6fe50..33cf71858 100644 --- a/part-6/1-reading-files/index.html +++ b/part-6/1-reading-files/index.html @@ -423,4 +423,4 @@ '080488-123X': 3300, '290274-044S': 4350, '010479-007Z': 2500 -}

    The for loop at the end of the program combines the names of the employees with their respective salaries.

    The program also takes care of situations where the employee's pic is not present in the salary file.

    Remember, the order in which items are stored in a dictionary does not matter, as the keys are processed based on hash values.

    Loading
    Loading
    Loading
    Loading
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +}

    The for loop at the end of the program combines the names of the employees with their respective salaries.

    The program also takes care of situations where the employee's pic is not present in the salary file.

    Remember, the order in which items are stored in a dictionary does not matter, as the keys are processed based on hash values.

    Loading
    Loading
    Loading
    Loading
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-6/2-writing-files/index.html b/part-6/2-writing-files/index.html index d25550db2..6c2639a6c 100644 --- a/part-6/2-writing-files/index.html +++ b/part-6/2-writing-files/index.html @@ -278,4 +278,4 @@ weekly_points = read_weekly_points("weekly_points.csv") print(get_grade("Paula", weekly_points)) -
    Sample data

    3

    If we determine a certain functionality in the program needs fixing, in a well designed program the change will affect only some select sections of code, and it will be easier to determine where the changes should be made. For example, if we wanted to change the grade boundaries, we'd only need to implement the change in the function for determining the grade, and it would work also in all the other functions utilizing this function. If the code for this single functionality was implemented in multiple places, there would be a definite risk that we would not remember to change all the instances when changing the functionality.

    Loading
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +
    Sample data

    3

    If we determine a certain functionality in the program needs fixing, in a well designed program the change will affect only some select sections of code, and it will be easier to determine where the changes should be made. For example, if we wanted to change the grade boundaries, we'd only need to implement the change in the function for determining the grade, and it would work also in all the other functions utilizing this function. If the code for this single functionality was implemented in multiple places, there would be a definite risk that we would not remember to change all the instances when changing the functionality.

    Loading
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-6/3-errors/index.html b/part-6/3-errors/index.html index 27541e050..5321c7cc9 100644 --- a/part-6/3-errors/index.html +++ b/part-6/3-errors/index.html @@ -293,4 +293,4 @@ print(factorial(-1)) File "test.py", line 3, in factorial raise ValueError("The input was negative: " + str(n)) -ValueError: The input was negative: -1
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +ValueError: The input was negative: -1
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-6/4-scope-of-variables/index.html b/part-6/4-scope-of-variables/index.html index 68a88a760..ffdb20bca 100644 --- a/part-6/4-scope-of-variables/index.html +++ b/part-6/4-scope-of-variables/index.html @@ -306,4 +306,4 @@ print(analysis_result) # run the main function -main()

    Please respond to a quick questionnaire on this week's materials.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +main()

    Please respond to a quick questionnaire on this week's materials.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-6/index.html b/part-6/index.html index 84dc84a2a..7f129d425 100644 --- a/part-6/index.html +++ b/part-6/index.html @@ -152,4 +152,4 @@ } })
    \ No newline at end of file +
    Loading...
    \ No newline at end of file diff --git a/part-7/1-modules/index.html b/part-7/1-modules/index.html index ce3cb6a43..a920919f2 100644 --- a/part-7/1-modules/index.html +++ b/part-7/1-modules/index.html @@ -230,4 +230,4 @@
  • https://docs.python.org/3/library/math.html
  • We can also have a look at the contents of the module with the function dir:

    import math
     
    -print(dir(math))

    The function returns a list of names defined by the module. These may be, for example, names of classes, constant values or functions:

    Sample output

    ['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +print(dir(math))

    The function returns a list of names defined by the module. These may be, for example, names of classes, constant values or functions:

    Sample output

    ['__doc__', '__name__', '__package__', 'acos', 'acosh', 'asin', 'asinh', 'atan', 'atan2', 'atanh', 'ceil', 'copysign', 'cos', 'cosh', 'degrees', 'e', 'erf', 'erfc', 'exp', 'expm1', 'fabs', 'factorial', 'floor', 'fmod', 'frexp', 'fsum', 'gamma', 'hypot', 'isinf', 'isnan', 'ldexp', 'lgamma', 'log', 'log10', 'log1p', 'modf', 'pi', 'pow', 'radians', 'sin', 'sinh', 'sqrt', 'tan', 'tanh', 'trunc']

    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-7/2-randomness/index.html b/part-7/2-randomness/index.html index b6c36a3aa..3d82109d8 100644 --- a/part-7/2-randomness/index.html +++ b/part-7/2-randomness/index.html @@ -231,4 +231,4 @@ seed(1337) # this will always produce the same "random" number -print(randint(1, 100))

    If we have functions which rely on randomization, and we set seed value, the function will produce the same result each time it is executed. The result may be different with different Python versions, but in essence randomness is lost by setting a seed value. This can be a useful feature when testing a program, for example.

    Loading
    Loading
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +print(randint(1, 100))

    If we have functions which rely on randomization, and we set seed value, the function will produce the same result each time it is executed. The result may be different with different Python versions, but in essence randomness is lost by setting a seed value. This can be a useful feature when testing a program, for example.

    Loading
    Loading
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-7/3-times-and-dates/index.html b/part-7/3-times-and-dates/index.html index a14d65bed..e1b0874a0 100644 --- a/part-7/3-times-and-dates/index.html +++ b/part-7/3-times-and-dates/index.html @@ -282,4 +282,4 @@ print("You were born in the previous millennium") else: print("You were born during this millennium")
    Sample output

    Please type in your birthday in the format dd.mm.yyyy: 5.11.1986 -You were born in the previous millennium

    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +You were born in the previous millennium

    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-7/4-data-processing/index.html b/part-7/4-data-processing/index.html index 31d51e5b7..d0dd1081b 100644 --- a/part-7/4-data-processing/index.html +++ b/part-7/4-data-processing/index.html @@ -227,4 +227,4 @@
  • https://docs.python.org/3/library/
  • In addition to the standard library, the internet is full of freely available Python modules for different purposes. Some commonly used modules are listed here:

    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-7/5-creating-modules/index.html b/part-7/5-creating-modules/index.html index c59c499d6..a74f37c42 100644 --- a/part-7/5-creating-modules/index.html +++ b/part-7/5-creating-modules/index.html @@ -292,4 +292,4 @@ print(words.last_word(my_string)) print(words.number_of_words(my_string))
    Sample output

    Sheila seashore -6

    In the exercises on this course, whenever you were asked to write functions, you were usually also expected to wrap test cases in an if __name__ == "__main__" block exactly like the one above. Now you know why.

    Loading

    Please respond to a quick questionnaire on this week's materials.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +6

    In the exercises on this course, whenever you were asked to write functions, you were usually also expected to wrap test cases in an if __name__ == "__main__" block exactly like the one above. Now you know why.

    Loading

    Please respond to a quick questionnaire on this week's materials.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-7/6-more-features/index.html b/part-7/6-more-features/index.html index bdf9c4bd0..aa9e317d9 100644 --- a/part-7/6-more-features/index.html +++ b/part-7/6-more-features/index.html @@ -234,4 +234,4 @@ print("The sum of the arguments is", sum(my_args)) testing(1, 2, 3, 4, 5)
    Sample output

    You passed 5 arguments -The sum of the arguments is 15

    Loading

    Please respond to the course feedback questionnaire. The questionnaire results help us improve the course.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +The sum of the arguments is 15

    Loading

    Please respond to the course feedback questionnaire. The questionnaire results help us improve the course.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-7/index.html b/part-7/index.html index 58b6a63f0..374a572e5 100644 --- a/part-7/index.html +++ b/part-7/index.html @@ -152,4 +152,4 @@ } })
    \ No newline at end of file +
    Loading...
    \ No newline at end of file diff --git a/part-8/1-objects-and-methods/index.html b/part-8/1-objects-and-methods/index.html index 71de3d311..e8bbf19fd 100644 --- a/part-8/1-objects-and-methods/index.html +++ b/part-8/1-objects-and-methods/index.html @@ -260,4 +260,4 @@ my_list.pop(0) print(my_list)
    Sample output

    [1, 2, 3, 5, 1] -[2, 3, 5, 1]

    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +[2, 3, 5, 1]

    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-8/2-classes-and-objects/index.html b/part-8/2-classes-and-objects/index.html index 30a6e8ea4..de2f27509 100644 --- a/part-8/2-classes-and-objects/index.html +++ b/part-8/2-classes-and-objects/index.html @@ -256,4 +256,4 @@ The month: 12

    The day of the week the date falls on is available through the method isoweekday:

    weekday = my_date.isoweekday()

    This is a method call, so there are parentheses after the name of the method. Leaving the parentheses out does not cause an error, but the results are weird:

    weekday =  my_date.isoweekday
     print("The day of the week:", weekday)
    Sample output

    The day of the week: <built-in method isoweekday of datetime.date object at 0x10ed66450>

    The month of a date object is a variable, so the value attached can be accessed with a reference.

    my_month = my_date.month

    Notice there are no parentheses here. Adding parentheses would cause an error:

    my_month = my_date.month()
    Sample output
    Traceback (most recent call last): File "", line 1, in -TypeError: 'int' object is not callable
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +TypeError: 'int' object is not callable
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-8/3-defining-classes/index.html b/part-8/3-defining-classes/index.html index 2b9fda883..0a005b834 100644 --- a/part-8/3-defining-classes/index.html +++ b/part-8/3-defining-classes/index.html @@ -305,4 +305,4 @@ deposit_money_on_account(peters_account, 500) print(peters_account.balance)
    Sample output

    0 -500

    Loading
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +500

    Loading
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-8/4-defining-methods/index.html b/part-8/4-defining-methods/index.html index 9cd22feb9..dc7ac164b 100644 --- a/part-8/4-defining-methods/index.html +++ b/part-8/4-defining-methods/index.html @@ -342,4 +342,4 @@ self.balance += superbonus def __str__(self): - return f"BonusCard(name={self.name}, balance={self.balance})"
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file + return f"BonusCard(name={self.name}, balance={self.balance})"
    Loading
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-8/5-more-examples-of-classes/index.html b/part-8/5-more-examples-of-classes/index.html index 6ce1e30a4..587e3a4c1 100644 --- a/part-8/5-more-examples-of-classes/index.html +++ b/part-8/5-more-examples-of-classes/index.html @@ -282,4 +282,4 @@ date studying 1 -0

    Loading

    Please respond to a quick questionnaire on this week's materials.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +0

    Loading

    Please respond to a quick questionnaire on this week's materials.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-8/index.html b/part-8/index.html index 7ec0425a7..76749e7dc 100644 --- a/part-8/index.html +++ b/part-8/index.html @@ -152,4 +152,4 @@ } })
    \ No newline at end of file +
    Loading...
    \ No newline at end of file diff --git a/part-9/1-objects-and-references/index.html b/part-9/1-objects-and-references/index.html index 7bac4fbeb..cfd7051e6 100644 --- a/part-9/1-objects-and-references/index.html +++ b/part-9/1-objects-and-references/index.html @@ -544,4 +544,4 @@ # this would cause an error, as Person must be enclosed in quotation marks def older_than(self, another: Person): - return self.year_of_birth < another.year_of_birth:
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file + return self.year_of_birth < another.year_of_birth:
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-9/2-objects-as-attributes/index.html b/part-9/2-objects-as-attributes/index.html index 7d07d736f..1d9fc064f 100644 --- a/part-9/2-objects-as-attributes/index.html +++ b/part-9/2-objects-as-attributes/index.html @@ -308,4 +308,4 @@ if player is not None: print(f"Goals by Charlie: {player.goals}") else: - print(f"Charlie doesn't play in Campus Allstars :(")
    Sample output

    Charlie doesn't play in Campus Allstars :(

    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file + print(f"Charlie doesn't play in Campus Allstars :(")
    Sample output

    Charlie doesn't play in Campus Allstars :(

    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-9/3-encapsulation/index.html b/part-9/3-encapsulation/index.html index 1ab9b92e0..2cfb25692 100644 --- a/part-9/3-encapsulation/index.html +++ b/part-9/3-encapsulation/index.html @@ -344,4 +344,4 @@
  • Today I ate porridge
  • Today I learned object oriented programming
  • Today I went to bed early
  • -
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +
    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-9/4-scope-of-methods/index.html b/part-9/4-scope-of-methods/index.html index 58156ea92..89872f103 100644 --- a/part-9/4-scope-of-methods/index.html +++ b/part-9/4-scope-of-methods/index.html @@ -246,4 +246,4 @@ print(hand1) hand2 = deck.deal(5) print(hand2)

    As the hands are randomly generated, the following is only an example of what could be printed out:

    Sample output

    [('spades', 7), ('spades', 11), ('hearts', 7), ('diamonds', 3), ('spades', 4)] -[('clubs', 8), ('spades', 12), ('diamonds', 13), ('clubs', 11), ('spades', 10)]

    Private methods are generally less common than private attributes. As a rule of thumb, a method should be hidden whenever the client has no need to directly access it. This is especially the case when it is possible that the client could adversely affect the integrity of the object by calling the method.

    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +[('clubs', 8), ('spades', 12), ('diamonds', 13), ('clubs', 11), ('spades', 10)]

    Private methods are generally less common than private attributes. As a rule of thumb, a method should be hidden whenever the client has no need to directly access it. This is especially the case when it is possible that the client could adversely affect the integrity of the object by calling the method.

    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-9/5-class-attributes/index.html b/part-9/5-class-attributes/index.html index 487782a0d..853ad6822 100644 --- a/part-9/5-class-attributes/index.html +++ b/part-9/5-class-attributes/index.html @@ -358,4 +358,4 @@ return True
    registration = Registration("Mary Motorist", "Volvo", "1992", "abc-123")
     
     if Registration.license_plate_valid("xyz-789"):
    -    print("This is a valid license plate!")
    Sample output

    This is a valid license plate!

    The validity of a license plate can be checked even without creating a single instance of the class, for example with Registration.license_plate_valid("xyz-789"). The same method is called within the constructor of the class. NB: even within the constructor this method is accessed through the name of the class, not self!

    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file + print("This is a valid license plate!")
    Sample output

    This is a valid license plate!

    The validity of a license plate can be checked even without creating a single instance of the class, for example with Registration.license_plate_valid("xyz-789"). The same method is called within the constructor of the class. NB: even within the constructor this method is accessed through the name of the class, not self!

    Loading
    You have reached the end of this section! Continue to the next section:

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-9/6-more-examples-with-classes/index.html b/part-9/6-more-examples-with-classes/index.html index 7378ebfd0..63cd38d15 100644 --- a/part-9/6-more-examples-with-classes/index.html +++ b/part-9/6-more-examples-with-classes/index.html @@ -385,4 +385,4 @@ print(student1.completed_courses) print(student2.completed_courses)
    Sample output

    ['ItP', 'ACiP'] -[]

    The Grand Finale

    Even though the following exercise finishes off this part of the material, the techniques required to solve it were all covered already in the section named objects as attributes. Specifically, you are not required to use the @property decorator or default values for parameters in this exercise. This exercise is very similar to the exercises a box of presents and the shortest person in the room.

    Important information regarding the next exercise

    Please note that there is an issue resulting from an update in Python, which conflicts with the inbuilt library and the original file name for this exercise. If you experience any issues, we recommend redownloading the exercise folder. Once you have obtained the new local test files, you can use either "code.py" or "code_1.py" as the file name. While using Visual Studio Code, you may receive notifications about problems in the test file. However, these notifications can be safely ignored, as they are caused by the test's inability to import from either the "code.py" or "code_1.py" files.

    Loading

    Please respond to a quick questionnaire on this week's materials.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file +[]

    The Grand Finale

    Even though the following exercise finishes off this part of the material, the techniques required to solve it were all covered already in the section named objects as attributes. Specifically, you are not required to use the @property decorator or default values for parameters in this exercise. This exercise is very similar to the exercises a box of presents and the shortest person in the room.

    Important information regarding the next exercise

    Please note that there is an issue resulting from an update in Python, which conflicts with the inbuilt library and the original file name for this exercise. If you experience any issues, we recommend redownloading the exercise folder. Once you have obtained the new local test files, you can use either "code.py" or "code_1.py" as the file name. While using Visual Studio Code, you may receive notifications about problems in the test file. However, these notifications can be safely ignored, as they are caused by the test's inability to import from either the "code.py" or "code_1.py" files.

    Loading

    Please respond to a quick questionnaire on this week's materials.

    Loading...
    :
    Loading...

    Log in to view the quiz

    You have reached the end of this section!

    You can check your current points from the blue blob in the bottom-right corner of the page.

    \ No newline at end of file diff --git a/part-9/index.html b/part-9/index.html index 292b73173..dfd125f13 100644 --- a/part-9/index.html +++ b/part-9/index.html @@ -152,4 +152,4 @@ } })
    \ No newline at end of file +
    Loading...
    \ No newline at end of file diff --git a/report-issue/index.html b/report-issue/index.html index 55567b3c3..016f2ceb7 100644 --- a/report-issue/index.html +++ b/report-issue/index.html @@ -134,4 +134,4 @@ } } }) -

    How to report an issue in the material


    You can report a mistake you found on the issues tab of the course's Github project. Github is a service where projects, software and source code can be managed and shared to others.

    Reporting an issue requires a Github account. If you don't have an account yet, you can create it here

    Before you open a new issue, please check here for an already opened issue regarding the mistake you found.


    How to compose the issue

    1. Press the button above.
    2. Please include a short and informative title in your issue report, for example "A typo in Part 1 in the description of exercise 30".
    3. Write a more detailed description of the issue in the comment section, for example "In Part 1 exercise 30, the sample output contains a typo. The previous code includes a line System.out.println("Enter a number");, but the sample output has the text "Exit a number"".
    4. Finally, press the green button "Submit new issue".

    We might ask some clarifying questions if we are unable to fix the issue without your help. Github should send an email notification regarding these questions to the email address linked to your Github account.

    Thank you for helping us improve our materials!

    \ No newline at end of file +

    How to report an issue in the material


    You can report a mistake you found on the issues tab of the course's Github project. Github is a service where projects, software and source code can be managed and shared to others.

    Reporting an issue requires a Github account. If you don't have an account yet, you can create it here

    Before you open a new issue, please check here for an already opened issue regarding the mistake you found.


    How to compose the issue

    1. Press the button above.
    2. Please include a short and informative title in your issue report, for example "A typo in Part 1 in the description of exercise 30".
    3. Write a more detailed description of the issue in the comment section, for example "In Part 1 exercise 30, the sample output contains a typo. The previous code includes a line System.out.println("Enter a number");, but the sample output has the text "Exit a number"".
    4. Finally, press the green button "Submit new issue".

    We might ask some clarifying questions if we are unable to fix the issue without your help. Github should send an email notification regarding these questions to the email address linked to your Github account.

    Thank you for helping us improve our materials!

    \ No newline at end of file diff --git a/sign-in/index.html b/sign-in/index.html index 402b2def3..8d7f09525 100644 --- a/sign-in/index.html +++ b/sign-in/index.html @@ -140,4 +140,4 @@ } } }) -
    \ No newline at end of file +
    \ No newline at end of file diff --git a/sign-up/index.html b/sign-up/index.html index 13f0c4055..230faab56 100644 --- a/sign-up/index.html +++ b/sign-up/index.html @@ -140,4 +140,4 @@ } } }) -

    Create new account

    This course uses mooc.fi accounts. If you have previously done mooc.fi -courses, you can log in with your existing account. On this page you can create a new account, which works on majority of mooc.fi courses and services.
    \ No newline at end of file +

    Create new account

    This course uses mooc.fi accounts. If you have previously done mooc.fi -courses, you can log in with your existing account. On this page you can create a new account, which works on majority of mooc.fi courses and services.
    \ No newline at end of file diff --git a/support-and-assistance/index.html b/support-and-assistance/index.html index c0630bddf..4894fc6fa 100644 --- a/support-and-assistance/index.html +++ b/support-and-assistance/index.html @@ -223,4 +223,4 @@

    Discord

    Discord is a communication platform which allows both text-based and voice/video chats. You can find out more about the platform on the Discord website.

    The course channels are available through this link. See especially the ohjelmoinnin_mooc_english and the ohjelmoinnin_mooc_voice channels for support in English.

    -

    Instructor will be on standby on the course channels in Discord at guidance time. Instructor will also reply to questions sent via private messages. Discord also has a discussion about the course outside of the guidance hours, so it’s worth joining!

    \ No newline at end of file +

    Instructor will be on standby on the course channels in Discord at guidance time. Instructor will also reply to questions sent via private messages. Discord also has a discussion about the course outside of the guidance hours, so it’s worth joining!

    \ No newline at end of file